]> git.openstreetmap.org Git - rails.git/blob - app/controllers/way_controller.rb
3f5f661970edaad4a6d5ae56118abb565a839de1
[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.version = 0
19           way.user_id = @user.id
20           way.save_with_history!
21
22           render :text => way.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       way = Way.find(params[:id])
35
36       response.headers['Last-Modified'] = way.timestamp.rfc822
37
38       if way.visible
39         render :text => way.to_xml.to_s, :content_type => "text/xml"
40       else
41         render :text => "", :status => :gone
42       end
43     rescue ActiveRecord::RecordNotFound
44       render :nothing => true, :status => :not_found
45     end
46   end
47
48   def update
49     begin
50       way = Way.find(params[:id])
51       new_way = Way.from_xml(request.raw_post)
52
53       if new_way and new_way.id == way.id
54         way.update_from(new_way, @user)
55         render :nothing => true
56       else
57         render :nothing => true, :status => :bad_request
58       end
59     rescue OSM::APIPreconditionFailedError
60       render :text => "", :status => :precondition_failed
61     rescue ActiveRecord::RecordNotFound
62       render :nothing => true, :status => :not_found
63     end
64   end
65
66   # This is the API call to delete a way
67   def delete
68     begin
69       way = Way.find(params[:id])
70       way.delete_with_relations_and_history(@user)
71
72       # if we get here, all is fine, otherwise something will catch below.  
73       render :nothing => true
74     rescue OSM::APIAlreadyDeletedError
75       render :text => "", :status => :gone
76     rescue OSM::APIPreconditionFailedError
77       render :text => "", :status => :precondition_failed
78     rescue ActiveRecord::RecordNotFound
79       render :nothing => true, :status => :not_found
80     end
81   end
82
83   def full
84     begin
85       way = Way.find(params[:id])
86
87       if way.visible
88         nd_ids = way.nds + [-1]
89         nodes = Node.find(:all, :conditions => "visible = 1 AND id IN (#{nd_ids.join(',')})")
90
91         # Render
92         doc = OSM::API.new.get_xml_doc
93         nodes.each do |node|
94           doc.root << node.to_xml_node()
95         end
96         doc.root << way.to_xml_node()
97
98         render :text => doc.to_s, :content_type => "text/xml"
99       else
100         render :text => "", :status => :gone
101       end
102     rescue ActiveRecord::RecordNotFound
103       render :nothing => true, :status => :not_found
104     end
105   end
106
107   def ways
108     begin
109       ids = params['ways'].split(',').collect { |w| w.to_i }
110     rescue
111       ids = []
112     end
113
114     if ids.length > 0
115       doc = OSM::API.new.get_xml_doc
116
117       Way.find(ids).each do |way|
118         doc.root << way.to_xml_node
119       end
120
121       render :text => doc.to_s, :content_type => "text/xml"
122     else
123       render :nothing => true, :status => :bad_request
124     end
125   end
126
127   def ways_for_node
128     wayids = WayNode.find(:all, :conditions => ['node_id = ?', params[:id]]).collect { |ws| ws.id[0] }.uniq
129
130     doc = OSM::API.new.get_xml_doc
131
132     Way.find(wayids).each do |way|
133       doc.root << way.to_xml_node
134     end
135
136     render :text => doc.to_s, :content_type => "text/xml"
137   end
138 end