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