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