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