]> git.openstreetmap.org Git - rails.git/blob - app/models/relation.rb
Fixing the resync that I had done wrong at the end of last week on the nodes. All...
[rails.git] / app / models / relation.rb
1 class Relation < ActiveRecord::Base
2   require 'xml/libxml'
3   
4   set_table_name 'current_relations'
5
6   belongs_to :user
7
8   has_many :old_relations, :foreign_key => 'id', :order => 'version'
9
10   has_many :relation_members, :foreign_key => 'id'
11   has_many :relation_tags, :foreign_key => 'id'
12
13   has_many :containing_relation_members, :class_name => "RelationMember", :as => :member
14   has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation, :extend => ObjectFinder
15
16   def self.from_xml(xml, create=false)
17     begin
18       p = XML::Parser.new
19       p.string = xml
20       doc = p.parse
21
22       doc.find('//osm/relation').each do |pt|
23         return Relation.from_xml_node(pt, create)
24       end
25     rescue
26       return nil
27     end
28   end
29
30   def self.from_xml_node(pt, create=false)
31     relation = Relation.new
32
33     if !create and pt['id'] != '0'
34       relation.id = pt['id'].to_i
35     end
36
37     relation.version = pt['version']
38
39     if create
40       relation.timestamp = Time.now
41       relation.visible = true
42     else
43       if pt['timestamp']
44         relation.timestamp = Time.parse(pt['timestamp'])
45       end
46     end
47
48     pt.find('tag').each do |tag|
49       relation.add_tag_keyval(tag['k'], tag['v'])
50     end
51
52     pt.find('member').each do |member|
53       relation.add_member(member['type'], member['ref'], member['role'])
54     end
55
56     return relation
57   end
58
59   def to_xml
60     doc = OSM::API.new.get_xml_doc
61     doc.root << to_xml_node()
62     return doc
63   end
64
65   def to_xml_node(user_display_name_cache = nil)
66     el1 = XML::Node.new 'relation'
67     el1['id'] = self.id.to_s
68     el1['visible'] = self.visible.to_s
69     el1['timestamp'] = self.timestamp.xmlschema
70     el1['version'] = self.version.to_s
71
72     user_display_name_cache = {} if user_display_name_cache.nil?
73     
74     if user_display_name_cache and user_display_name_cache.key?(self.user_id)
75       # use the cache if available
76     elsif self.user.data_public?
77       user_display_name_cache[self.user_id] = self.user.display_name
78     else
79       user_display_name_cache[self.user_id] = nil
80     end
81
82     el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
83
84     self.relation_members.each do |member|
85       p=0
86       #if visible_members
87       #  # if there is a list of visible members then use that to weed out deleted segments
88       #  if visible_members[member.member_type][member.member_id]
89       #    p=1
90       #  end
91       #else
92         # otherwise, manually go to the db to check things
93         if member.member.visible?
94           p=1
95         end
96       #end
97       if p
98         e = XML::Node.new 'member'
99         e['type'] = member.member_type
100         e['ref'] = member.member_id.to_s 
101         e['role'] = member.member_role
102         el1 << e
103        end
104     end
105
106     self.relation_tags.each do |tag|
107       e = XML::Node.new 'tag'
108       e['k'] = tag.k
109       e['v'] = tag.v
110       el1 << e
111     end
112     return el1
113   end 
114
115   # FIXME is this really needed?
116   def members
117     unless @members
118       @members = Array.new
119       self.relation_members.each do |member|
120         @members += [[member.member_type,member.member_id,member.member_role]]
121       end
122     end
123     @members
124   end
125
126   def tags
127     unless @tags
128       @tags = Hash.new
129       self.relation_tags.each do |tag|
130         @tags[tag.k] = tag.v
131       end
132     end
133     @tags
134   end
135
136   def members=(m)
137     @members = m
138   end
139
140   def tags=(t)
141     @tags = t
142   end
143
144   def add_member(type,id,role)
145     @members = Array.new unless @members
146     @members += [[type,id,role]]
147   end
148
149   def add_tag_keyval(k, v)
150     @tags = Hash.new unless @tags
151     @tags[k] = v
152   end
153
154   def save_with_history!
155     Relation.transaction do
156       t = Time.now
157       self.version += 1
158       self.timestamp = t
159       self.save!
160
161       tags = self.tags
162       RelationTag.delete_all(['id = ?', self.id])
163       tags.each do |k,v|
164         tag = RelationTag.new
165         tag.k = k
166         tag.v = v
167         tag.id = self.id
168         tag.save!
169       end
170
171       members = self.members
172       RelationMember.delete_all(['id = ?', self.id])
173       members.each do |n|
174         mem = RelationMember.new
175         mem.id = self.id
176         mem.member_type = n[0];
177         mem.member_id = n[1];
178         mem.member_role = n[2];
179         mem.save!
180       end
181
182       old_relation = OldRelation.from_relation(self)
183       old_relation.timestamp = t
184       old_relation.save_with_dependencies!
185     end
186   end
187
188   def delete_with_history(user)
189     if self.visible
190       if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = 1 AND member_type='relation' and member_id=?", self.id ])
191         raise OSM::APIPreconditionFailedError.new
192       else
193         self.user_id = user.id
194         self.tags = []
195         self.members = []
196         self.visible = false
197         save_with_history!
198       end
199     else
200       raise OSM::APIAlreadyDeletedError.new
201     end
202   end
203
204   def update_from(new_relation, user)
205     if !new_relation.preconditions_ok?
206       raise OSM::APIPreconditionFailedError.new
207     elsif new_relation.version != version
208       raise OSM::APIVersionMismatchError.new(new_relation.version, version)
209     else
210       self.user_id = user.id
211       self.tags = new_relation.tags
212       self.members = new_relation.members
213       self.visible = true
214       save_with_history!
215     end
216   end
217
218   def preconditions_ok?
219     # These are hastables that store an id in the index of all 
220     # the nodes/way/relations that have already been added.
221     # Once we know the id of the node/way/relation exists
222     # we check to see if it is already existing in the hashtable
223     # if it does, then we return false. Otherwise
224     # we add it to the relevant hash table, with the value true..
225     # Thus if you have nodes with the ids of 50 and 1 already in the
226     # relation, then the hash table nodes would contain:
227     # => {50=>true, 1=>true}
228     nodes = Hash.new
229     ways = Hash.new
230     relations = Hash.new
231     self.members.each do |m|
232       if (m[0] == "node")
233         n = Node.find(:first, :conditions => ["id = ?", m[1]])
234         unless n and n.visible 
235           return false
236         end
237         if nodes[m[1]]
238           return false
239         else
240           nodes[m[1]] = true
241         end
242       elsif (m[0] == "way")
243         w = Way.find(:first, :conditions => ["id = ?", m[1]])
244         unless w and w.visible and w.preconditions_ok?
245           return false
246         end
247         if ways[m[1]]
248           return false
249         else
250           ways[m[1]] = true
251         end
252       elsif (m[0] == "relation")
253         e = Relation.find(:first, :conditions => ["id = ?", m[1]])
254         unless e and e.visible and e.preconditions_ok?
255           return false
256         end
257         if relations[m[1]]
258           return false
259         else
260           relations[m[1]] = true
261         end
262       else
263         return false
264       end
265     end
266     return true
267   rescue
268     return false
269   end
270
271   # Temporary method to match interface to nodes
272   def tags_as_hash
273     return self.tags
274   end
275 end