more rails updates
[rails.git] / app / controllers / node_controller.rb
1 class NodeController < ApplicationController
2   require 'xml/libxml'
3   
4   before_filter :authorize
5
6   def create
7     @node = Node.new
8     @node.id = 1
9     @node.latitude = 1
10     @node.save
11   end
12
13   def dummy
14     if request.post?
15       userid = dao.useridfromcreds(r.user, r.get_basic_auth_pw)
16       doc = Document.new $stdin.read
17
18       doc.elements.each('osm/node') do |pt|
19         lat = pt.attributes['lat'].to_f
20         lon = pt.attributes['lon'].to_f
21         xmlnodeid = pt.attributes['id'].to_i
22
23         tags = []
24         pt.elements.each('tag') do |tag|
25           tags << [tag.attributes['k'],tag.attributes['v']]
26         end
27
28         tags = tags.collect { |k,v| "#{k}=#{v}" }.join(';')
29
30         tags = '' unless tags
31         if xmlnodeid == nodeid && userid != 0
32           if nodeid == 0
33             new_node_id = dao.create_node(lat, lon, userid, tags)
34             if new_node_id
35               puts new_node_id
36               exit
37             else
38               exit HTTP_INTERNAL_SERVER_ERROR
39             end
40           else
41             node = dao.getnode(nodeid)
42             if node
43               #FIXME: need to check the node hasn't moved too much
44               if dao.update_node?(nodeid, userid, lat, lon, tags)
45                 exit
46               else
47                 exit HTTP_INTERNAL_SERVER_ERROR
48               end
49             else
50               exit HTTP_NOT_FOUND
51             end
52           end
53
54         else
55           exit BAD_REQUEST
56         end
57       end
58       exit HTTP_INTERNAL_SERVER_ERROR
59
60
61     end
62   end
63
64
65   def rest
66
67     #
68     # POST ???
69     #
70
71     if request.post?
72       nodeid = r.args.match(/nodeid=([0-9]+)/).captures.first.to_i
73       userid = dao.useridfromcreds(r.user, r.get_basic_auth_pw)
74       doc = Document.new $stdin.read
75
76       doc.elements.each('osm/node') do |pt|
77         lat = pt.attributes['lat'].to_f
78         lon = pt.attributes['lon'].to_f
79         xmlnodeid = pt.attributes['id'].to_i
80
81         tags = []
82         pt.elements.each('tag') do |tag|
83           tags << [tag.attributes['k'],tag.attributes['v']]
84         end
85
86         tags = tags.collect { |k,v| "#{k}=#{v}" }.join(';')
87
88         tags = '' unless tags
89         if xmlnodeid == nodeid && userid != 0
90           if nodeid == 0
91             new_node_id = dao.create_node(lat, lon, userid, tags)
92             if new_node_id
93               puts new_node_id
94               exit
95             else
96               exit HTTP_INTERNAL_SERVER_ERROR
97             end
98           else
99             node = dao.getnode(nodeid)
100             if node
101               #FIXME: need to check the node hasn't moved too much
102               if dao.update_node?(nodeid, userid, lat, lon, tags)
103                 exit
104               else
105                 exit HTTP_INTERNAL_SERVER_ERROR
106               end
107             else
108               exit HTTP_NOT_FOUND
109             end
110           end
111
112         else
113           exit BAD_REQUEST
114         end
115       end
116       exit HTTP_INTERNAL_SERVER_ERROR
117
118     end
119
120     #
121     # GET ???
122     #
123
124     if request.get?
125       node = node.find(params[:id])
126       doc = Document.new
127       doc.encoding = "UTF-8"
128       root = Node.new 'osm'
129       root['version'] = '0.4'
130       root['generator'] = 'OpenStreetMap server'
131       doc.root = root
132       el1 = Node.new 'node'
133       el1['id'] = node.id.to_s
134       el1['lat'] = node.latitude.to_s
135       el1['lon'] = node.longitude.to_s
136       split_tags(el1, node.tags)
137       el1['visible'] = node.visible.to_s
138       el1['timestamp'] = node.timestamp
139       root << el1
140
141       render :text => doc.to_s, :template => false
142     end
143
144     #
145     # DELETE????
146     # 
147
148     if request.delete?
149       userid = dao.useridfromcreds(r.user, r.get_basic_auth_pw)
150       #cgi doesnt work with DELETE so extract manually:
151       nodeid = r.args.match(/nodeid=([0-9]+)/).captures.first.to_i
152
153       if userid > 0 && nodeid != 0
154         node = dao.getnode(nodeid)
155         if node
156           if node.visible  
157             if dao.delete_node?(nodeid, userid)
158               exit
159             else
160               exit HTTP_INTERNAL_SERVER_ERROR
161             end
162           else
163             exit HTTP_GONE
164           end
165         else
166           exit HTTP_NOT_FOUND
167         end
168       else
169         exit BAD_REQUEST
170
171       end
172
173
174     end
175
176
177   end
178
179   private
180   def split_tags(el, tags)
181     tags.split(';').each do |tag|
182       parts = tag.split('=')
183       key = ''
184       val = ''
185       key = parts[0].strip unless parts[0].nil?
186       val = parts[1].strip unless parts[1].nil?
187       if key != '' && val != ''
188         el2 = Node.new('tag')
189         el2['k'] = key.to_s
190         el2['v'] = val.to_s
191         el << el2
192       end
193     end
194   end
195
196 end