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