]> git.openstreetmap.org Git - rails.git/blob - app/controllers/relation_controller.rb
c67b4ddf1c0f159d6ba86d10c58dc35dc3df814b
[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     if request.put?
12       relation = Relation.from_xml(request.raw_post, true)
13
14       if relation
15         if !relation.preconditions_ok?
16           render :nothing => true, :status => :precondition_failed
17         else
18           relation.user_id = @user.id
19           relation.save_with_history!
20
21           render :text => relation.id.to_s, :content_type => "text/plain"
22         end
23       else
24         render :nothing => true, :status => :bad_request
25       end
26     else
27       render :nothing => true, :status => :method_not_allowed
28     end
29   end
30
31   def read
32     begin
33       relation = Relation.find(params[:id])
34
35       if relation.visible
36         render :text => relation.to_xml.to_s, :content_type => "text/xml"
37       else
38         render :nothing => true, :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     begin
49       relation = Relation.find(params[:id])
50
51       if relation.visible
52         new_relation = Relation.from_xml(request.raw_post)
53
54         if new_relation and new_relation.id == relation.id
55           if !new_relation.preconditions_ok?
56             render :nothing => true, :status => :precondition_failed
57           else
58             relation.user_id = @user.id
59             relation.tags = new_relation.tags
60             relation.members = new_relation.members
61             relation.visible = true
62             relation.save_with_history!
63
64             render :nothing => true
65           end
66         else
67           render :nothing => true, :status => :bad_request
68         end
69       else
70         render :nothing => true, :status => :gone
71       end
72     rescue ActiveRecord::RecordNotFound
73       render :nothing => true, :status => :not_found
74     rescue
75       render :nothing => true, :status => :internal_server_error
76     end
77   end
78
79   def delete
80 #XXX check if member somewhere!
81     begin
82       relation = Relation.find(params[:id])
83
84       if relation.visible
85         if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = 1 AND member_type='relation' and member_id=?", params[:id]])
86           render :nothing => true, :status => :precondition_failed
87         else
88           relation.user_id = @user.id
89           relation.tags = []
90           relation.members = []
91           relation.visible = false
92           relation.save_with_history!
93
94           render :nothing => true
95         end
96       else
97         render :nothing => true, :status => :gone
98       end
99     rescue ActiveRecord::RecordNotFound
100       render :nothing => true, :status => :not_found
101     rescue
102       render :nothing => true, :status => :internal_server_error
103     end
104   end
105
106   # -----------------------------------------------------------------
107   # full
108   # 
109   # input parameters: id
110   #
111   # returns XML representation of one relation object plus all its
112   # members, plus all nodes part of member ways
113   # -----------------------------------------------------------------
114   def full
115     begin
116       relation = Relation.find(params[:id])
117
118       if relation.visible
119
120         # first collect nodes, ways, and relations referenced by this relation.
121         
122         ways = Way.find_by_sql("select w.* from current_ways w,current_relation_members rm where "+
123             "rm.member_type='way' and rm.member_id=w.id and rm.id=#{relation.id}");
124         nodes = Node.find_by_sql("select n.* from current_nodes n,current_relation_members rm where "+
125             "rm.member_type='node' and rm.member_id=n.id and rm.id=#{relation.id}");
126         # note query is built to exclude self just in case.
127         relations = Relation.find_by_sql("select r.* from current_relations r,current_relation_members rm where "+
128             "rm.member_type='relation' and rm.member_id=r.id and rm.id=#{relation.id} and r.id<>rm.id");
129
130         # now additionally collect nodes referenced by ways. Note how we recursively 
131         # evaluate ways but NOT relations.
132
133         node_ids = nodes.collect {|node| node.id }
134         way_node_ids = ways.collect { |way|
135            way.way_nodes.collect { |way_node| way_node.node_id }
136         }
137         way_node_ids.flatten!
138         way_node_ids.uniq
139         way_node_ids -= node_ids
140         nodes += Node.find(way_node_ids)
141     
142         # create XML.
143         doc = OSM::API.new.get_xml_doc
144         user_display_name_cache = {}
145
146         nodes.each do |node|
147           if node.visible? # should be unnecessary if data is consistent.
148             doc.root << node.to_xml_node(user_display_name_cache)
149           end
150         end
151         ways.each do |way|
152           if way.visible? # should be unnecessary if data is consistent.
153             doc.root << way.to_xml_node(user_display_name_cache)
154           end
155         end
156         relations.each do |rel|
157           if rel.visible? # should be unnecessary if data is consistent.
158             doc.root << rel.to_xml_node(user_display_name_cache)
159           end
160         end
161         # finally add self and output
162         doc.root << relation.to_xml_node(user_display_name_cache)
163         render :text => doc.to_s, :content_type => "text/xml"
164
165       else
166
167         render :nothing => true, :status => :gone
168       end
169
170     rescue ActiveRecord::RecordNotFound
171       render :nothing => true, :status => :not_found
172
173     rescue
174       render :nothing => true, :status => :internal_server_error
175     end
176   end
177
178   def relations
179     ids = params['relations'].split(',').collect { |w| w.to_i }
180
181     if ids.length > 0
182       doc = OSM::API.new.get_xml_doc
183
184       Relation.find(ids).each do |relation|
185         doc.root << relation.to_xml_node
186       end
187
188       render :text => doc.to_s, :content_type => "text/xml"
189     else
190       render :nothing => true, :status => :bad_request
191     end
192   end
193
194   def relations_for_way
195     relations_for_object("way")
196   end
197   def relations_for_node
198     relations_for_object("node")
199   end
200   def relations_for_relation
201     relations_for_object("relation")
202   end
203
204   def relations_for_object(objtype)
205     relationids = RelationMember.find(:all, :conditions => ['member_type=? and member_id=?', objtype, params[:id]]).collect { |ws| ws.id }.uniq
206
207     if relationids.length > 0
208       doc = OSM::API.new.get_xml_doc
209
210       Relation.find(relationids).each do |relation|
211         doc.root << relation.to_xml_node
212       end
213
214       render :text => doc.to_s, :content_type => "text/xml"
215     else
216       render :nothing => true, :status => :not_found
217     end
218   end
219 end