]> git.openstreetmap.org Git - rails.git/blob - app/models/way.rb
api06: Return version ids in XML responses.
[rails.git] / app / models / way.rb
1 class Way < ActiveRecord::Base
2   require 'xml/libxml'
3
4   belongs_to :user
5
6   has_many :nodes, :through => :way_nodes, :order => 'sequence_id'
7   has_many :way_nodes, :foreign_key => 'id', :order => 'sequence_id'
8   has_many :way_tags, :foreign_key => 'id'
9
10   has_many :old_ways, :foreign_key => 'id', :order => 'version'
11
12   set_table_name 'current_ways'
13
14   def self.from_xml(xml, create=false)
15     begin
16       p = XML::Parser.new
17       p.string = xml
18       doc = p.parse
19
20       way = Way.new
21
22       doc.find('//osm/way').each do |pt|
23         if !create and pt['id'] != '0'
24           way.id = pt['id'].to_i
25         end
26
27         if create
28           way.timestamp = Time.now
29           way.visible = true
30         else
31           if pt['timestamp']
32             way.timestamp = Time.parse(pt['timestamp'])
33           end
34         end
35
36         pt.find('tag').each do |tag|
37           way.add_tag_keyval(tag['k'], tag['v'])
38         end
39
40         pt.find('nd').each do |nd|
41           way.add_nd_num(nd['ref'])
42         end
43       end
44     rescue
45       way = nil
46     end
47
48     return way
49   end
50
51   # Find a way given it's ID, and in a single SQL call also grab its nodes
52   #
53   
54   # You can't pull in all the tags too unless we put a sequence_id on the way_tags table and have a multipart key
55   def self.find_eager(id)
56     way = Way.find(id, :include => {:way_nodes => :node})
57     #If waytag had a multipart key that was real, you could do this:
58     #way = Way.find(id, :include => [:way_tags, {:way_nodes => :node}])
59   end
60
61   # Find a way given it's ID, and in a single SQL call also grab its nodes and tags
62   def to_xml
63     doc = OSM::API.new.get_xml_doc
64     doc.root << to_xml_node()
65     return doc
66   end
67
68   def to_xml_node(visible_nodes = nil, user_display_name_cache = nil)
69     el1 = XML::Node.new 'way'
70     el1['id'] = self.id.to_s
71     el1['visible'] = self.visible.to_s
72     el1['timestamp'] = self.timestamp.xmlschema
73     el1['version'] = self.version.to_s
74
75     user_display_name_cache = {} if user_display_name_cache.nil?
76
77     if user_display_name_cache and user_display_name_cache.key?(self.user_id)
78       # use the cache if available
79     elsif self.user.data_public?
80       user_display_name_cache[self.user_id] = self.user.display_name
81     else
82       user_display_name_cache[self.user_id] = nil
83     end
84
85     el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
86
87     # make sure nodes are output in sequence_id order
88     ordered_nodes = []
89     self.way_nodes.each do |nd|
90       if visible_nodes
91         # if there is a list of visible nodes then use that to weed out deleted nodes
92         if visible_nodes[nd.node_id]
93           ordered_nodes[nd.sequence_id] = nd.node_id.to_s
94         end
95       else
96         # otherwise, manually go to the db to check things
97         if nd.node.visible? and nd.node.visible?
98           ordered_nodes[nd.sequence_id] = nd.node_id.to_s
99         end
100       end
101     end
102
103     ordered_nodes.each do |nd_id|
104       if nd_id and nd_id != '0'
105         e = XML::Node.new 'nd'
106         e['ref'] = nd_id
107         el1 << e
108       end
109     end
110
111     self.way_tags.each do |tag|
112       e = XML::Node.new 'tag'
113       e['k'] = tag.k
114       e['v'] = tag.v
115       el1 << e
116     end
117     return el1
118   end 
119
120   def nds
121     unless @nds
122       @nds = Array.new
123       self.way_nodes.each do |nd|
124         @nds += [nd.node_id]
125       end
126     end
127     @nds
128   end
129
130   def tags
131     unless @tags
132       @tags = {}
133       self.way_tags.each do |tag|
134         @tags[tag.k] = tag.v
135       end
136     end
137     @tags
138   end
139
140   def nds=(s)
141     @nds = s
142   end
143
144   def tags=(t)
145     @tags = t
146   end
147
148   def add_nd_num(n)
149     @nds = Array.new unless @nds
150     @nds << n.to_i
151   end
152
153   def add_tag_keyval(k, v)
154     @tags = Hash.new unless @tags
155     @tags[k] = v
156   end
157
158   def save_with_history!
159     t = Time.now
160
161     Way.transaction do
162       self.version += 1
163       self.timestamp = t
164       self.save!
165
166       tags = self.tags
167       WayTag.delete_all(['id = ?', self.id])
168       tags.each do |k,v|
169         tag = WayTag.new
170         tag.k = k
171         tag.v = v
172         tag.id = self.id
173         tag.save!
174       end
175
176       nds = self.nds
177       WayNode.delete_all(['id = ?', self.id])
178       sequence = 1
179       nds.each do |n|
180         nd = WayNode.new
181         nd.id = [self.id, sequence]
182         nd.node_id = n
183         nd.save!
184         sequence += 1
185       end
186
187       old_way = OldWay.from_way(self)
188       old_way.timestamp = t
189       old_way.save_with_dependencies!
190     end
191   end
192
193   def preconditions_ok?
194     return false if self.nds.empty?
195     self.nds.each do |n|
196       node = Node.find(:first, :conditions => ["id = ?", n])
197       unless node and node.visible
198         return false
199       end
200     end
201     return true
202   end
203
204   # Delete the way and it's relations, but don't really delete it - set its visibility to false and update the history etc to maintain wiki-like functionality.
205   def delete_with_relations_and_history(user)
206     if self.visible
207           # FIXME
208           # this should actually delete the relations,
209           # not just throw a PreconditionFailed if it's a member of a relation!!
210       if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id",
211                              :conditions => [ "visible = 1 AND member_type='way' and member_id=?", self.id])
212         raise OSM::APIPreconditionFailedError
213       # end FIXME
214       else
215         self.user_id = user.id
216         self.tags = []
217         self.nds = []
218         self.visible = false
219         self.save_with_history!
220       end
221     else
222       raise OSM::APIAlreadyDeletedError
223     end
224   end
225
226   # delete a way and it's nodes that aren't part of other ways, with history
227   def delete_with_relations_and_nodes_and_history(user)
228     
229     node_ids = self.nodes.collect {|node| node.id }
230     node_ids_not_to_delete = []
231     way_nodes = WayNode.find(:all, :conditions => "node_id in (#{node_ids.join(',')}) and id != #{self.id}")
232     
233     node_ids_not_to_delete = way_nodes.collect {|way_node| way_node.node_id}
234
235     node_ids_to_delete = node_ids - node_ids_not_to_delete
236
237     # delete the nodes not used by other ways
238     node_ids_to_delete.each do |node_id|
239       n = Node.find(node_id)
240       n.user_id = user.id
241       n.visible = false
242       n.save_with_history!
243     end
244     
245     self.user_id = user.id
246
247     self.delete_with_relations_and_history(user)
248
249   end
250 end