]> git.openstreetmap.org Git - rails.git/blob - app/controllers/relation_controller.rb
Railsify the relation member model, type attribute, by putting it into class case...
[rails.git] / app / controllers / relation_controller.rb
1 class RelationController < ApplicationController
2   require 'xml/libxml'
3
4   session :off
5   before_filter :authorize, :only => [:create, :update, :delete]
6   before_filter :check_write_availability, :only => [:create, :update, :delete]
7   before_filter :check_read_availability, :except => [:create, :update, :delete]
8   after_filter :compress_output
9
10   def create
11     begin
12       if request.put?
13         relation = Relation.from_xml(request.raw_post, true)
14
15         # We assume that an exception has been thrown if there was an error 
16         # generating the relation
17         #if relation
18           relation.create_with_history @user
19           render :text => relation.id.to_s, :content_type => "text/plain"
20         #else
21          # render :text => "Couldn't get turn the input into a relation.", :status => :bad_request
22         #end
23       else
24         render :nothing => true, :status => :method_not_allowed
25       end
26     rescue OSM::APIError => ex
27       render ex.render_opts
28     end
29   end
30
31   def read
32     begin
33       relation = Relation.find(params[:id])
34       response.headers['Last-Modified'] = relation.timestamp.rfc822
35       if relation.visible
36         render :text => relation.to_xml.to_s, :content_type => "text/xml"
37       else
38         render :text => "", :status => :gone
39       end
40     rescue ActiveRecord::RecordNotFound
41       render :nothing => true, :status => :not_found
42     rescue
43       render :nothing => true, :status => :internal_server_error
44     end
45   end
46
47   def update
48     logger.debug request.raw_post
49     begin
50       relation = Relation.find(params[:id])
51       new_relation = Relation.from_xml(request.raw_post)
52
53       if new_relation and new_relation.id == relation.id
54         relation.update_from new_relation, @user
55         render :text => relation.version.to_s, :content_type => "text/plain"
56       else
57         render :nothing => true, :status => :bad_request
58       end
59     rescue ActiveRecord::RecordNotFound
60       render :nothing => true, :status => :not_found
61     rescue OSM::APIError => ex
62       render ex.render_opts
63     end
64   end
65
66   def delete
67     begin
68       relation = Relation.find(params[:id])
69       new_relation = Relation.from_xml(request.raw_post)
70       if new_relation and new_relation.id == relation.id
71         relation.delete_with_history!(new_relation, @user)
72         render :text => relation.version.to_s, :content_type => "text/plain"
73       else
74         render :nothing => true, :status => :bad_request
75       end
76     rescue OSM::APIError => ex
77       render ex.render_opts
78     rescue ActiveRecord::RecordNotFound
79       render :nothing => true, :status => :not_found
80     end
81   end
82
83   # -----------------------------------------------------------------
84   # full
85   # 
86   # input parameters: id
87   #
88   # returns XML representation of one relation object plus all its
89   # members, plus all nodes part of member ways
90   # -----------------------------------------------------------------
91   def full
92     begin
93       relation = Relation.find(params[:id])
94
95       if relation.visible
96
97         # first collect nodes, ways, and relations referenced by this relation.
98         
99         ways = Way.find_by_sql("select w.* from current_ways w,current_relation_members rm where "+
100             "rm.member_type='Way' and rm.member_id=w.id and rm.id=#{relation.id}");
101         nodes = Node.find_by_sql("select n.* from current_nodes n,current_relation_members rm where "+
102             "rm.member_type='Node' and rm.member_id=n.id and rm.id=#{relation.id}");
103         # note query is built to exclude self just in case.
104         relations = Relation.find_by_sql("select r.* from current_relations r,current_relation_members rm where "+
105             "rm.member_type='Relation' and rm.member_id=r.id and rm.id=#{relation.id} and r.id<>rm.id");
106
107         # now additionally collect nodes referenced by ways. Note how we recursively 
108         # evaluate ways but NOT relations.
109
110         node_ids = nodes.collect {|node| node.id }
111         way_node_ids = ways.collect { |way|
112            way.way_nodes.collect { |way_node| way_node.node_id }
113         }
114         way_node_ids.flatten!
115         way_node_ids.uniq
116         way_node_ids -= node_ids
117         nodes += Node.find(way_node_ids)
118     
119         # create XML.
120         doc = OSM::API.new.get_xml_doc
121         visible_nodes = {}
122         user_display_name_cache = {}
123
124         nodes.each do |node|
125           if node.visible? # should be unnecessary if data is consistent.
126             doc.root << node.to_xml_node(user_display_name_cache)
127             visible_nodes[node.id] = node
128           end
129         end
130         ways.each do |way|
131           if way.visible? # should be unnecessary if data is consistent.
132             doc.root << way.to_xml_node(visible_nodes, user_display_name_cache)
133           end
134         end
135         relations.each do |rel|
136           if rel.visible? # should be unnecessary if data is consistent.
137             doc.root << rel.to_xml_node(user_display_name_cache)
138           end
139         end
140         # finally add self and output
141         doc.root << relation.to_xml_node(user_display_name_cache)
142         render :text => doc.to_s, :content_type => "text/xml"
143
144       else
145         render :nothing => true, :status => :gone
146       end
147
148     rescue ActiveRecord::RecordNotFound
149       render :nothing => true, :status => :not_found
150
151     rescue
152       render :nothing => true, :status => :internal_server_error
153     end
154   end
155
156   def relations
157     ids = params['relations'].split(',').collect { |w| w.to_i }
158
159     if ids.length > 0
160       doc = OSM::API.new.get_xml_doc
161
162       Relation.find(ids).each do |relation|
163         doc.root << relation.to_xml_node
164       end
165
166       render :text => doc.to_s, :content_type => "text/xml"
167     else
168       render :text => "You need to supply a comma separated list of ids.", :status => :bad_request
169     end
170   rescue ActiveRecord::RecordNotFound
171     render :text => "Could not find one of the relations", :status => :not_found
172   end
173
174   def relations_for_way
175     relations_for_object("Way")
176   end
177   def relations_for_node
178     relations_for_object("Node")
179   end
180   def relations_for_relation
181     relations_for_object("Relation")
182   end
183
184   def relations_for_object(objtype)
185     relationids = RelationMember.find(:all, :conditions => ['member_type=? and member_id=?', objtype, params[:id]]).collect { |ws| ws.id[0] }.uniq
186
187     doc = OSM::API.new.get_xml_doc
188
189     Relation.find(relationids).each do |relation|
190       doc.root << relation.to_xml_node if relation.visible
191     end
192
193     render :text => doc.to_s, :content_type => "text/xml"
194   end
195 end