]> git.openstreetmap.org Git - rails.git/blob - app/controllers/way_controller.rb
fixed null value bug in rake task
[rails.git] / app / controllers / way_controller.rb
1 class WayController < 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       way = Way.from_xml(request.raw_post, true)
13
14       if way
15         if !way.preconditions_ok?
16           render :text => "", :status => :precondition_failed
17         else
18           way.user_id = @user.id
19           way.save_with_history!
20
21           render :text => way.id.to_s, :content_type => "text/plain"
22         end
23       else
24         render :nothing => true, :status => :bad_request
25       end
26     else
27       render :nothing => true, :status => :method_not_allowed
28     end
29   end
30
31   def read
32     begin
33       way = Way.find(params[:id])
34
35       response.headers['Last-Modified'] = way.timestamp.rfc822
36
37       if way.visible
38         render :text => way.to_xml.to_s, :content_type => "text/xml"
39       else
40         render :text => "", :status => :gone
41       end
42     rescue ActiveRecord::RecordNotFound
43       render :nothing => true, :status => :not_found
44     end
45   end
46
47   def update
48     begin
49       way = Way.find(params[:id])
50       new_way = Way.from_xml(request.raw_post)
51
52       if new_way and new_way.id == way.id
53         if !new_way.preconditions_ok?
54           render :text => "", :status => :precondition_failed
55         else
56           way.user_id = @user.id
57           way.tags = new_way.tags
58           way.nds = new_way.nds
59           way.visible = true
60           way.save_with_history!
61
62           render :nothing => true
63         end
64       else
65         render :nothing => true, :status => :bad_request
66       end
67     rescue ActiveRecord::RecordNotFound
68       render :nothing => true, :status => :not_found
69     end
70   end
71
72   def delete
73     begin
74       way = Way.find(params[:id])
75
76       if way.visible
77         # omg FIXME
78         if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = 1 AND member_type='way' and member_id=?", params[:id]])
79           render :text => "", :status => :precondition_failed
80         else
81           way.user_id = @user.id
82           way.tags = []
83           way.nds = []
84           way.visible = false
85           way.save_with_history!
86
87           render :nothing => true
88         end
89       else
90         render :text => "", :status => :gone
91       end
92     rescue ActiveRecord::RecordNotFound
93       render :nothing => true, :status => :not_found
94     rescue => ex
95       puts ex
96     end
97   end
98
99   def full
100     begin
101       way = Way.find(params[:id])
102
103       if way.visible
104         nd_ids = way.nds + [-1]
105         nodes = Node.find(:all, :conditions => "visible = 1 AND id IN (#{nd_ids.join(',')})")
106
107         # Render
108         doc = OSM::API.new.get_xml_doc
109         nodes.each do |node|
110           doc.root << node.to_xml_node()
111         end
112         doc.root << way.to_xml_node()
113
114         render :text => doc.to_s, :content_type => "text/xml"
115       else
116         render :text => "", :status => :gone
117       end
118     rescue ActiveRecord::RecordNotFound
119       render :nothing => true, :status => :not_found
120     end
121   end
122
123   def ways
124     begin
125       ids = params['ways'].split(',').collect { |w| w.to_i }
126     rescue
127       ids = []
128     end
129
130     if ids.length > 0
131       doc = OSM::API.new.get_xml_doc
132
133       Way.find(ids).each do |way|
134         doc.root << way.to_xml_node
135       end
136
137       render :text => doc.to_s, :content_type => "text/xml"
138     else
139       render :nothing => true, :status => :bad_request
140     end
141   end
142
143   def ways_for_node
144     wayids = WayNode.find(:all, :conditions => ['node_id = ?', params[:id]]).collect { |ws| ws.id }.uniq
145
146     doc = OSM::API.new.get_xml_doc
147
148     Way.find(wayids).each do |way|
149       doc.root << way.to_xml_node
150     end
151
152     render :text => doc.to_s, :content_type => "text/xml"
153   end
154 end