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