]> git.openstreetmap.org Git - rails.git/blob - app/controllers/relation_controller.rb
moving the conistency checks for updates and deletes to library, hopefully got the...
[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     logger.debug request.raw_post
50     begin
51       relation = Relation.find(params[:id])
52       new_relation = Relation.from_xml(request.raw_post)
53
54       if new_relation and new_relation.id == relation.id
55         relation.update_from new_relation, @user
56         render :text => relation.version.to_s, :content_type => "text/plain"
57       else
58         render :nothing => true, :status => :bad_request
59       end
60     rescue ActiveRecord::RecordNotFound
61       render :nothing => true, :status => :not_found
62     rescue OSM::APIError => ex
63       render ex.render_opts
64     end
65   end
66
67   def delete
68 #XXX check if member somewhere!
69     begin
70       relation = Relation.find(params[:id])
71       new_relation = Relation.from_xml(request.raw_post)
72       if new_relation and new_relation.id == relation.id
73         relation.delete_with_history(new_relation, @user)
74         render :nothing => true, :status => :success
75       else
76         render :nothing => true, :status => :bad_request
77       end
78     rescue OSM::APIError => ex
79       render ex.render_opts
80     rescue ActiveRecord::RecordNotFound
81       render :nothing => true, :status => :not_found
82     end
83   end
84
85   # -----------------------------------------------------------------
86   # full
87   # 
88   # input parameters: id
89   #
90   # returns XML representation of one relation object plus all its
91   # members, plus all nodes part of member ways
92   # -----------------------------------------------------------------
93   def full
94     begin
95       relation = Relation.find(params[:id])
96
97       if relation.visible
98
99         # first collect nodes, ways, and relations referenced by this relation.
100         
101         ways = Way.find_by_sql("select w.* from current_ways w,current_relation_members rm where "+
102             "rm.member_type='way' and rm.member_id=w.id and rm.id=#{relation.id}");
103         nodes = Node.find_by_sql("select n.* from current_nodes n,current_relation_members rm where "+
104             "rm.member_type='node' and rm.member_id=n.id and rm.id=#{relation.id}");
105         # note query is built to exclude self just in case.
106         relations = Relation.find_by_sql("select r.* from current_relations r,current_relation_members rm where "+
107             "rm.member_type='relation' and rm.member_id=r.id and rm.id=#{relation.id} and r.id<>rm.id");
108
109         # now additionally collect nodes referenced by ways. Note how we recursively 
110         # evaluate ways but NOT relations.
111
112         node_ids = nodes.collect {|node| node.id }
113         way_node_ids = ways.collect { |way|
114            way.way_nodes.collect { |way_node| way_node.node_id }
115         }
116         way_node_ids.flatten!
117         way_node_ids.uniq
118         way_node_ids -= node_ids
119         nodes += Node.find(way_node_ids)
120     
121         # create XML.
122         doc = OSM::API.new.get_xml_doc
123         visible_nodes = {}
124         user_display_name_cache = {}
125
126         nodes.each do |node|
127           if node.visible? # should be unnecessary if data is consistent.
128             doc.root << node.to_xml_node(user_display_name_cache)
129             visible_nodes[node.id] = node
130           end
131         end
132         ways.each do |way|
133           if way.visible? # should be unnecessary if data is consistent.
134             doc.root << way.to_xml_node(visible_nodes, user_display_name_cache)
135           end
136         end
137         relations.each do |rel|
138           if rel.visible? # should be unnecessary if data is consistent.
139             doc.root << rel.to_xml_node(user_display_name_cache)
140           end
141         end
142         # finally add self and output
143         doc.root << relation.to_xml_node(user_display_name_cache)
144         render :text => doc.to_s, :content_type => "text/xml"
145
146       else
147
148         render :text => "", :status => :gone
149       end
150
151     rescue ActiveRecord::RecordNotFound
152       render :nothing => true, :status => :not_found
153
154     rescue
155       render :nothing => true, :status => :internal_server_error
156     end
157   end
158
159   def relations
160     ids = params['relations'].split(',').collect { |w| w.to_i }
161
162     if ids.length > 0
163       doc = OSM::API.new.get_xml_doc
164
165       Relation.find(ids).each do |relation|
166         doc.root << relation.to_xml_node
167       end
168
169       render :text => doc.to_s, :content_type => "text/xml"
170     else
171       render :nothing => true, :status => :bad_request
172     end
173   end
174
175   def relations_for_way
176     relations_for_object("way")
177   end
178   def relations_for_node
179     relations_for_object("node")
180   end
181   def relations_for_relation
182     relations_for_object("relation")
183   end
184
185   def relations_for_object(objtype)
186     relationids = RelationMember.find(:all, :conditions => ['member_type=? and member_id=?', objtype, params[:id]]).collect { |ws| ws.id }.uniq
187
188     doc = OSM::API.new.get_xml_doc
189
190     Relation.find(relationids).each do |relation|
191       doc.root << relation.to_xml_node
192     end
193
194     render :text => doc.to_s, :content_type => "text/xml"
195   end
196 end