]> git.openstreetmap.org Git - rails.git/blob - app/models/old_way.rb
Merge branch 'master' into openid
[rails.git] / app / models / old_way.rb
1 class OldWay < ActiveRecord::Base
2   include ConsistencyValidations
3   
4   set_table_name 'ways'
5
6   belongs_to :changeset
7
8   validates_associated :changeset
9   
10   def self.from_way(way)
11     old_way = OldWay.new
12     old_way.visible = way.visible
13     old_way.changeset_id = way.changeset_id
14     old_way.timestamp = way.timestamp
15     old_way.id = way.id
16     old_way.version = way.version
17     old_way.nds = way.nds
18     old_way.tags = way.tags
19     return old_way
20   end
21
22   def save_with_dependencies!
23
24     # dont touch this unless you really have figured out why it's called
25     # (Rails doesn't deal well with the old ways table (called 'ways') because
26     # it doesn't have a unique key. It knows how to insert and auto_increment
27     # id and get it back but we have that and we want to get the 'version' back
28     # we could add another column but thats a lot of data. No, set_primary_key
29     # doesn't work either.
30     save!
31     clear_aggregation_cache
32     clear_association_cache
33     @attributes.update(OldWay.find(:first, :conditions => ['id = ? AND timestamp = ?', self.id, self.timestamp], :order => "version desc").instance_variable_get('@attributes'))
34
35     # ok, you can touch from here on
36
37     self.tags.each do |k,v|
38       tag = OldWayTag.new
39       tag.k = k
40       tag.v = v
41       tag.id = self.id
42       tag.version = self.version
43       tag.save!
44     end
45
46     sequence = 1
47     self.nds.each do |n|
48       nd = OldWayNode.new
49       nd.id = [self.id, self.version, sequence]
50       nd.node_id = n
51       nd.save!
52       sequence += 1
53     end
54   end
55
56   def nds
57     unless @nds
58         @nds = Array.new
59         OldWayNode.find(:all, :conditions => ["id = ? AND version = ?", self.id, self.version], :order => "sequence_id").each do |nd|
60             @nds += [nd.node_id]
61         end
62     end
63     @nds
64   end
65
66   def tags
67     unless @tags
68         @tags = Hash.new
69         OldWayTag.find(:all, :conditions => ["id = ? AND version = ?", self.id, self.version]).each do |tag|
70             @tags[tag.k] = tag.v
71         end
72     end
73     @tags = Hash.new unless @tags
74     @tags
75   end
76
77   def nds=(s)
78     @nds = s
79   end
80
81   def tags=(t)
82     @tags = t
83   end
84
85 #  has_many :way_nodes, :class_name => 'OldWayNode', :foreign_key => 'id'
86 #  has_many :way_tags, :class_name => 'OldWayTag', :foreign_key => 'id'
87
88   def old_nodes
89     OldWayNode.find(:all, :conditions => ['id = ? AND version = ?', self.id, self.version])    
90   end
91
92   def old_tags
93     OldWayTag.find(:all, :conditions => ['id = ? AND version = ?', self.id, self.version])    
94   end
95
96   def to_xml_node
97     el1 = XML::Node.new 'way'
98     el1['id'] = self.id.to_s
99     el1['visible'] = self.visible.to_s
100     el1['timestamp'] = self.timestamp.xmlschema
101     if self.changeset.user.data_public?
102       el1['user'] = self.changeset.user.display_name
103       el1['uid'] = self.changeset.user.id.to_s
104     end
105     el1['version'] = self.version.to_s
106     el1['changeset'] = self.changeset.id.to_s
107     
108     self.old_nodes.each do |nd| # FIXME need to make sure they come back in the right order
109       e = XML::Node.new 'nd'
110       e['ref'] = nd.node_id.to_s
111       el1 << e
112     end
113  
114     self.old_tags.each do |tag|
115       e = XML::Node.new 'tag'
116       e['k'] = tag.k
117       e['v'] = tag.v
118       el1 << e
119     end
120     return el1
121   end
122
123   # Read full version of old way
124   # For get_nodes_undelete, uses same nodes, even if they've moved since
125   # For get_nodes_revert,   allocates new ids 
126   # Currently returns Potlatch-style array
127   # where [5] indicates whether latest version is usable as is (boolean)
128   # (i.e. is it visible? are we actually reverting to an earlier version?)
129
130   def get_nodes_undelete
131         points = []
132         self.nds.each do |n|
133           node=Node.find(n)
134           points << [node.lon, node.lat, n, node.version, node.tags_as_hash, node.visible]
135     end
136         points
137   end
138   
139   def get_nodes_revert(timestamp)
140     points=[]
141     self.nds.each do |n|
142       oldnode=OldNode.find(:first, :conditions=>['id=? AND timestamp<=?',n,timestamp], :order=>"timestamp DESC")
143       curnode=Node.find(n)
144       id=n; reuse=curnode.visible
145       if oldnode.lat!=curnode.lat or oldnode.lon!=curnode.lon or oldnode.tags!=curnode.tags then
146         # node has changed: if it's in other ways, give it a new id
147         if curnode.ways-[self.id] then id=-1; reuse=false end
148       end
149       points << [oldnode.lon, oldnode.lat, id, curnode.version, oldnode.tags_as_hash, reuse]
150     end
151     points
152   end
153
154   # Temporary method to match interface to nodes
155   def tags_as_hash
156     return self.tags
157   end
158
159   # Temporary method to match interface to ways
160   def way_nodes
161     return self.old_nodes
162   end
163
164   # Pretend we're not in any relations
165   def containing_relation_members
166     return []
167   end
168 end