1 class RelationController < ApplicationController
4 before_filter :authorize, :only => [:create, :update, :delete]
5 before_filter :check_availability, :only => [:create, :update, :delete]
7 after_filter :compress_output
11 relation = Relation.from_xml(request.raw_post, true)
14 if !relation.preconditions_ok?
15 render :nothing => true, :status => :precondition_failed
17 relation.user_id = @user.id
19 if relation.save_with_history
20 render :text => relation.id.to_s, :content_type => "text/plain"
22 render :text => "save error", :status => :internal_server_error
26 render :nothing => true, :status => :bad_request
29 render :nothing => true, :status => :method_not_allowed
35 relation = Relation.find(params[:id])
38 render :text => relation.to_xml.to_s, :content_type => "text/xml"
40 render :nothing => true, :status => :gone
42 rescue ActiveRecord::RecordNotFound
43 render :nothing => true, :status => :not_found
45 render :nothing => true, :status => :internal_server_error
51 relation = Relation.find(params[:id])
54 new_relation = Relation.from_xml(request.raw_post)
56 if new_relation and new_relation.id == relation.id
57 if !new_relation.preconditions_ok?
58 render :nothing => true, :status => :precondition_failed
60 relation.user_id = @user.id
61 relation.tags = new_relation.tags
62 relation.members = new_relation.members
63 relation.visible = true
65 if relation.save_with_history
66 render :nothing => true
68 render :nothing => true, :status => :internal_server_error
72 render :nothing => true, :status => :bad_request
75 render :nothing => true, :status => :gone
77 rescue ActiveRecord::RecordNotFound
78 render :nothing => true, :status => :not_found
80 render :nothing => true, :status => :internal_server_error
85 #XXX check if member somewhere!
87 relation = Relation.find(params[:id])
90 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]])
91 render :nothing => true, :status => :precondition_failed
93 relation.user_id = @user.id
96 relation.visible = false
98 if relation.save_with_history
99 render :nothing => true
101 render :nothing => true, :status => :internal_server_error
105 render :nothing => true, :status => :gone
107 rescue ActiveRecord::RecordNotFound
108 render :nothing => true, :status => :not_found
110 render :nothing => true, :status => :internal_server_error
116 relation = Relation.find(params[:id])
119 # In future, we might want to do all the data fetch in one step
120 seg_ids = relation.segs + [-1]
121 segments = Segment.find_by_sql "select * from current_segments where visible = 1 and id IN (#{seg_ids.join(',')})"
123 node_ids = segments.collect {|segment| segment.node_a }
124 node_ids += segments.collect {|segment| segment.node_b }
126 nodes = Node.find(node_ids, :conditions => "visible = TRUE")
129 doc = OSM::API.new.get_xml_doc
131 doc.root << node.to_xml_node()
133 segments.each do |segment|
134 doc.root << segment.to_xml_node()
136 doc.root << relation.to_xml_node()
138 render :text => doc.to_s, :content_type => "text/xml"
140 render :nothing => true, :status => :gone
142 rescue ActiveRecord::RecordNotFound
143 render :nothing => true, :status => :not_found
145 render :nothing => true, :status => :internal_server_error
150 ids = params['relations'].split(',').collect { |w| w.to_i }
153 doc = OSM::API.new.get_xml_doc
155 Relation.find(ids).each do |relation|
156 doc.root << relation.to_xml_node
159 render :text => doc.to_s, :content_type => "text/xml"
161 render :nothing => true, :status => :bad_request
165 def relations_for_object(objtype)
166 relationids = RelationMember.find(:all, :conditions => ['member_type=? and member_id=?', objtype, params[:id]]).collect { |ws| ws.id }.uniq
168 if relationids.length > 0
169 doc = OSM::API.new.get_xml_doc
171 Relation.find(relationids).each do |relation|
172 doc.root << relation.to_xml_node
175 render :text => doc.to_s, :content_type => "text/xml"
177 render :nothing => true, :status => :bad_request