1 class RelationController < ApplicationController
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
12 relation = Relation.from_xml(request.raw_post, true)
15 if !relation.preconditions_ok?
16 render :text => "", :status => :precondition_failed
19 relation.user_id = @user.id
20 relation.save_with_history!
22 render :text => relation.id.to_s, :content_type => "text/plain"
25 render :nothing => true, :status => :bad_request
28 render :nothing => true, :status => :method_not_allowed
34 relation = Relation.find(params[:id])
35 response.headers['Last-Modified'] = relation.timestamp.rfc822
37 render :text => relation.to_xml.to_s, :content_type => "text/xml"
39 render :text => "", :status => :gone
41 rescue ActiveRecord::RecordNotFound
42 render :nothing => true, :status => :not_found
44 render :nothing => true, :status => :internal_server_error
50 relation = Relation.find(params[:id])
51 new_relation = Relation.from_xml(request.raw_post)
52 if new_relation.version != relation.version
53 render :text => "Version mismatch: Provided " + new_relation.version.to_s + ", server had: " + relation.version.to_s, :status => :bad_request
57 if new_relation and new_relation.id == relation.id
58 relation.update_from new_relation, user
59 render :text => relation.version.to_s, :content_type => "text/plain"
61 render :nothing => true, :status => :bad_request
63 rescue ActiveRecord::RecordNotFound
64 render :nothing => true, :status => :not_found
65 rescue OSM::APIPreconditionFailedError
66 render :text => "", :status => :precondition_failed
68 render :nothing => true, :status => :internal_server_error
73 #XXX check if member somewhere!
75 relation = Relation.find(params[:id])
76 relation.delete_with_history(@user)
77 rescue OSM::APIAlreadyDeletedError
78 render :text => "", :status => :gone
79 rescue OSM::APIPreconditionFailedError
80 render :text => "", :status => :precondition_failed
81 rescue ActiveRecord::RecordNotFound
82 render :nothing => true, :status => :not_found
84 render :nothing => true, :status => :internal_server_error
88 # -----------------------------------------------------------------
91 # input parameters: id
93 # returns XML representation of one relation object plus all its
94 # members, plus all nodes part of member ways
95 # -----------------------------------------------------------------
98 relation = Relation.find(params[:id])
102 # first collect nodes, ways, and relations referenced by this relation.
104 ways = Way.find_by_sql("select w.* from current_ways w,current_relation_members rm where "+
105 "rm.member_type='way' and rm.member_id=w.id and rm.id=#{relation.id}");
106 nodes = Node.find_by_sql("select n.* from current_nodes n,current_relation_members rm where "+
107 "rm.member_type='node' and rm.member_id=n.id and rm.id=#{relation.id}");
108 # note query is built to exclude self just in case.
109 relations = Relation.find_by_sql("select r.* from current_relations r,current_relation_members rm where "+
110 "rm.member_type='relation' and rm.member_id=r.id and rm.id=#{relation.id} and r.id<>rm.id");
112 # now additionally collect nodes referenced by ways. Note how we recursively
113 # evaluate ways but NOT relations.
115 node_ids = nodes.collect {|node| node.id }
116 way_node_ids = ways.collect { |way|
117 way.way_nodes.collect { |way_node| way_node.node_id }
119 way_node_ids.flatten!
121 way_node_ids -= node_ids
122 nodes += Node.find(way_node_ids)
125 doc = OSM::API.new.get_xml_doc
127 user_display_name_cache = {}
130 if node.visible? # should be unnecessary if data is consistent.
131 doc.root << node.to_xml_node(user_display_name_cache)
132 visible_nodes[node.id] = node
136 if way.visible? # should be unnecessary if data is consistent.
137 doc.root << way.to_xml_node(visible_nodes, user_display_name_cache)
140 relations.each do |rel|
141 if rel.visible? # should be unnecessary if data is consistent.
142 doc.root << rel.to_xml_node(user_display_name_cache)
145 # finally add self and output
146 doc.root << relation.to_xml_node(user_display_name_cache)
147 render :text => doc.to_s, :content_type => "text/xml"
151 render :text => "", :status => :gone
154 rescue ActiveRecord::RecordNotFound
155 render :nothing => true, :status => :not_found
158 render :nothing => true, :status => :internal_server_error
163 ids = params['relations'].split(',').collect { |w| w.to_i }
166 doc = OSM::API.new.get_xml_doc
168 Relation.find(ids).each do |relation|
169 doc.root << relation.to_xml_node
172 render :text => doc.to_s, :content_type => "text/xml"
174 render :nothing => true, :status => :bad_request
178 def relations_for_way
179 relations_for_object("way")
181 def relations_for_node
182 relations_for_object("node")
184 def relations_for_relation
185 relations_for_object("relation")
188 def relations_for_object(objtype)
189 relationids = RelationMember.find(:all, :conditions => ['member_type=? and member_id=?', objtype, params[:id]]).collect { |ws| ws.id }.uniq
191 doc = OSM::API.new.get_xml_doc
193 Relation.find(relationids).each do |relation|
194 doc.root << relation.to_xml_node
197 render :text => doc.to_s, :content_type => "text/xml"