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"
23 render :nothing => true, :status => :internal_server_error
27 render :nothing => true, :status => :bad_request
30 render :nothing => true, :status => :method_not_allowed
36 relation = Relation.find(params[:id])
39 render :text => relation.to_xml.to_s, :content_type => "text/xml"
41 render :nothing => true, :status => :gone
43 rescue ActiveRecord::RecordNotFound
44 render :nothing => true, :status => :not_found
46 render :nothing => true, :status => :internal_server_error
52 relation = Relation.find(params[:id])
55 new_relation = Relation.from_xml(request.raw_post)
57 if new_relation and new_relation.id == relation.id
58 if !new_relation.preconditions_ok?
59 render :nothing => true, :status => :precondition_failed
61 relation.user_id = @user.id
62 relation.tags = new_relation.tags
63 relation.members = new_relation.members
64 relation.visible = true
66 if relation.save_with_history
67 render :nothing => true
69 render :nothing => true, :status => :internal_server_error
73 render :nothing => true, :status => :bad_request
76 render :nothing => true, :status => :gone
78 rescue ActiveRecord::RecordNotFound
79 render :nothing => true, :status => :not_found
81 render :nothing => true, :status => :internal_server_error
86 #XXX check if member somewhere!
88 relation = Relation.find(params[:id])
91 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]])
92 render :nothing => true, :status => :precondition_failed
94 relation.user_id = @user.id
97 relation.visible = false
99 if relation.save_with_history
100 render :nothing => true
102 render :nothing => true, :status => :internal_server_error
106 render :nothing => true, :status => :gone
108 rescue ActiveRecord::RecordNotFound
109 render :nothing => true, :status => :not_found
111 render :nothing => true, :status => :internal_server_error
117 relation = Relation.find(params[:id])
120 # In future, we might want to do all the data fetch in one step
121 seg_ids = relation.segs + [-1]
122 segments = Segment.find_by_sql "select * from current_segments where visible = 1 and id IN (#{seg_ids.join(',')})"
124 node_ids = segments.collect {|segment| segment.node_a }
125 node_ids += segments.collect {|segment| segment.node_b }
127 nodes = Node.find(:all, :conditions => "visible = 1 AND id IN (#{node_ids.join(',')})")
130 doc = OSM::API.new.get_xml_doc
132 doc.root << node.to_xml_node()
134 segments.each do |segment|
135 doc.root << segment.to_xml_node()
137 doc.root << relation.to_xml_node()
139 render :text => doc.to_s, :content_type => "text/xml"
141 render :nothing => true, :status => :gone
143 rescue ActiveRecord::RecordNotFound
144 render :nothing => true, :status => :not_found
146 render :nothing => true, :status => :internal_server_error
151 ids = params['relations'].split(',').collect { |w| w.to_i }
154 doc = OSM::API.new.get_xml_doc
156 Relation.find(ids).each do |relation|
157 doc.root << relation.to_xml_node
160 render :text => doc.to_s, :content_type => "text/xml"
162 render :nothing => true, :status => :bad_request
166 def relations_for_object(objtype)
167 relationids = RelationMember.find(:all, :conditions => ['member_type=? and member_id=?', objtype, params[:id]]).collect { |ws| ws.id }.uniq
169 if relationids.length > 0
170 doc = OSM::API.new.get_xml_doc
172 Relation.find(relationids).each do |relation|
173 doc.root << relation.to_xml_node
176 render :text => doc.to_s, :content_type => "text/xml"
178 render :nothing => true, :status => :bad_request