]> git.openstreetmap.org Git - rails.git/blob - app/models/way.rb
Include a reply link in email notifications of messages.
[rails.git] / app / models / way.rb
1 class Way < ActiveRecord::Base
2   require 'xml/libxml'
3   
4   belongs_to :user
5
6   has_many :way_segments, :foreign_key => 'id', :order => 'sequence_id'
7   has_many :way_tags, :foreign_key => 'id'
8
9   has_many :old_ways, :foreign_key => 'id', :order => 'version'
10
11   set_table_name 'current_ways'
12
13   def self.from_xml(xml, create=false)
14     begin
15       p = XML::Parser.new
16       p.string = xml
17       doc = p.parse
18
19       way = Way.new
20
21       doc.find('//osm/way').each do |pt|
22         if !create and pt['id'] != '0'
23           way.id = pt['id'].to_i
24         end
25
26         if create
27           way.timestamp = Time.now
28           way.visible = true
29         else
30           if pt['timestamp']
31             way.timestamp = Time.parse(pt['timestamp'])
32           end
33         end
34
35         pt.find('tag').each do |tag|
36           way.add_tag_keyval(tag['k'], tag['v'])
37         end
38
39         pt.find('seg').each do |seg|
40           way.add_seg_num(seg['id'])
41         end
42       end
43     rescue
44       way = nil
45     end
46
47     return way
48   end
49
50   def to_xml
51     doc = OSM::API.new.get_xml_doc
52     doc.root << to_xml_node()
53     return doc
54   end
55
56   def to_xml_node(visible_segments = nil, user_display_name_cache = nil)
57     el1 = XML::Node.new 'way'
58     el1['id'] = self.id.to_s
59     el1['visible'] = self.visible.to_s
60     el1['timestamp'] = self.timestamp.xmlschema
61
62     user_display_name_cache = {} if user_display_name_cache.nil?
63     
64     if user_display_name_cache and user_display_name_cache.key?(self.user_id)
65       # use the cache if available
66     elsif self.user.data_public?
67       user_display_name_cache[self.user_id] = self.user.display_name
68     else
69       user_display_name_cache[self.user_id] = nil
70     end
71
72     el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
73
74     # make sure segments are output in sequence_id order
75     ordered_segments = []
76     self.way_segments.each do |seg|
77       if visible_segments
78         # if there is a list of visible segments then use that to weed out deleted segments
79         if visible_segments[seg.segment_id]
80           ordered_segments[seg.sequence_id] = seg.segment_id.to_s
81         end
82       else
83         # otherwise, manually go to the db to check things
84         if seg.segment.visible? and seg.segment.from_node.visible? and seg.segment.to_node.visible?
85           ordered_segments[seg.sequence_id] = seg.segment_id.to_s
86         end
87       end
88     end
89
90     ordered_segments.each do |seg_id|
91       if seg_id and seg_id != '0'
92         e = XML::Node.new 'seg'
93         e['id'] = seg_id
94         el1 << e
95       end
96     end
97  
98     self.way_tags.each do |tag|
99       e = XML::Node.new 'tag'
100       e['k'] = tag.k
101       e['v'] = tag.v
102       el1 << e
103     end
104     return el1
105   end 
106
107   def segs
108     unless @segs
109         @segs = Array.new
110         self.way_segments.each do |seg|
111             @segs += [seg.segment_id]
112         end
113     end
114     @segs
115   end
116
117   def tags
118     unless @tags
119         @tags = Hash.new
120         self.way_tags.each do |tag|
121             @tags[tag.k] = tag.v
122         end
123     end
124     @tags
125   end
126
127   def segs=(s)
128     @segs = s
129   end
130
131   def tags=(t)
132     @tags = t
133   end
134
135   def add_seg_num(n)
136     @segs = Array.new unless @segs
137     @segs << n.to_i
138   end
139
140   def add_tag_keyval(k, v)
141     @tags = Hash.new unless @tags
142     @tags[k] = v
143   end
144
145   def save_with_history!
146     t = Time.now
147
148     Way.transaction do
149       self.timestamp = t
150       self.save!
151     end
152
153     WayTag.transaction do
154       tags = self.tags
155
156       WayTag.delete_all(['id = ?', self.id])
157
158       tags.each do |k,v|
159         tag = WayTag.new
160         tag.k = k
161         tag.v = v
162         tag.id = self.id
163         tag.save!
164       end
165     end
166
167     WaySegment.transaction do
168       segs = self.segs
169
170       WaySegment.delete_all(['id = ?', self.id])
171
172       i = 1
173       segs.each do |n|
174         seg = WaySegment.new
175         seg.id = self.id
176         seg.segment_id = n
177         seg.sequence_id = i
178         seg.save!
179         i += 1
180       end
181     end
182
183     old_way = OldWay.from_way(self)
184     old_way.timestamp = t
185     old_way.save_with_dependencies!
186   end
187
188   def preconditions_ok?
189     return false if self.segs.empty?
190     self.segs.each do |n|
191       segment = Segment.find(:first, :conditions => ["id = ?", n])
192       unless segment and segment.visible and segment.preconditions_ok?
193         return false
194       end
195     end
196     return true
197   end
198
199 end