1 class Relation < ActiveRecord::Base
4 set_table_name 'current_relations'
8 has_many :old_relations, :foreign_key => 'id', :order => 'version'
10 has_many :relation_members, :foreign_key => 'id'
11 has_many :relation_tags, :foreign_key => 'id'
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
16 def self.from_xml(xml, create=false)
22 doc.find('//osm/relation').each do |pt|
23 return Relation.from_xml_node(pt, create)
30 def self.from_xml_node(pt, create=false)
31 relation = Relation.new
33 if !create and pt['id'] != '0'
34 relation.id = pt['id'].to_i
37 relation.version = pt['version']
38 relation.changeset_id = pt['changeset']
41 relation.timestamp = Time.now
42 relation.visible = true
45 relation.timestamp = Time.parse(pt['timestamp'])
49 pt.find('tag').each do |tag|
50 relation.add_tag_keyval(tag['k'], tag['v'])
53 pt.find('member').each do |member|
54 relation.add_member(member['type'], member['ref'], member['role'])
61 doc = OSM::API.new.get_xml_doc
62 doc.root << to_xml_node()
66 def to_xml_node(user_display_name_cache = nil)
67 el1 = XML::Node.new 'relation'
68 el1['id'] = self.id.to_s
69 el1['visible'] = self.visible.to_s
70 el1['timestamp'] = self.timestamp.xmlschema
71 el1['version'] = self.version.to_s
72 el1['changeset'] = self.changeset_id.to_s
74 user_display_name_cache = {} if user_display_name_cache.nil?
76 if user_display_name_cache and user_display_name_cache.key?(self.changeset.user_id)
77 # use the cache if available
78 elsif self.user.data_public?
79 user_display_name_cache[self.changeset.user_id] = self.changeset.user.display_name
81 user_display_name_cache[self.changeset.user_id] = nil
84 el1['user'] = user_display_name_cache[self.changeset.user_id] unless user_display_name_cache[self.changeset.user_id].nil?
86 self.relation_members.each do |member|
89 # # if there is a list of visible members then use that to weed out deleted segments
90 # if visible_members[member.member_type][member.member_id]
94 # otherwise, manually go to the db to check things
95 if member.member.visible?
100 e = XML::Node.new 'member'
101 e['type'] = member.member_type
102 e['ref'] = member.member_id.to_s
103 e['role'] = member.member_role
108 self.relation_tags.each do |tag|
109 e = XML::Node.new 'tag'
117 def self.find_for_nodes(ids, options = {})
121 self.with_scope(:find => { :joins => "INNER JOIN current_relation_members ON current_relation_members.id = current_relations.id", :conditions => "current_relation_members.member_type = 'node' AND current_relation_members.member_id IN (#{ids.join(',')})" }) do
122 return self.find(:all, options)
127 def self.find_for_ways(ids, options = {})
131 self.with_scope(:find => { :joins => "INNER JOIN current_relation_members ON current_relation_members.id = current_relations.id", :conditions => "current_relation_members.member_type = 'way' AND current_relation_members.member_id IN (#{ids.join(',')})" }) do
132 return self.find(:all, options)
137 def self.find_for_relations(ids, options = {})
141 self.with_scope(:find => { :joins => "INNER JOIN current_relation_members ON current_relation_members.id = current_relations.id", :conditions => "current_relation_members.member_type = 'relation' AND current_relation_members.member_id IN (#{ids.join(',')})" }) do
142 return self.find(:all, options)
147 # FIXME is this really needed?
151 self.relation_members.each do |member|
152 @members += [[member.member_type,member.member_id,member.member_role]]
161 self.relation_tags.each do |tag|
176 def add_member(type,id,role)
177 @members = Array.new unless @members
178 @members += [[type,id,role]]
181 def add_tag_keyval(k, v)
182 @tags = Hash.new unless @tags
186 def save_with_history!
187 Relation.transaction do
194 RelationTag.delete_all(['id = ?', self.id])
196 tag = RelationTag.new
203 members = self.members
204 RelationMember.delete_all(['id = ?', self.id])
206 mem = RelationMember.new
208 mem.member_type = n[0];
209 mem.member_id = n[1];
210 mem.member_role = n[2];
214 old_relation = OldRelation.from_relation(self)
215 old_relation.timestamp = t
216 old_relation.save_with_dependencies!
220 def delete_with_history(new_relation, user)
222 check_consistency(self, new_relation, user)
223 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 ])
224 raise OSM::APIPreconditionFailedError.new
226 #self.user_id = user.id
227 # FIXME we need to deal with changeset here, which is probably already dealt with
228 self.changeset_id = new_relation.changeset_id
235 raise OSM::APIAlreadyDeletedError.new
239 def update_from(new_relation, user)
240 check_consistency(self, new_relation, user)
241 if !new_relation.preconditions_ok?
242 raise OSM::APIPreconditionFailedError.new
244 # FIXME need to deal with changeset etc
245 #self.user_id = user.id
246 self.changeset_id = new_relation.changeset_id
247 self.tags = new_relation.tags
248 self.members = new_relation.members
253 def preconditions_ok?
254 # These are hastables that store an id in the index of all
255 # the nodes/way/relations that have already been added.
256 # Once we know the id of the node/way/relation exists
257 # we check to see if it is already existing in the hashtable
258 # if it does, then we return false. Otherwise
259 # we add it to the relevant hash table, with the value true..
260 # Thus if you have nodes with the ids of 50 and 1 already in the
261 # relation, then the hash table nodes would contain:
262 # => {50=>true, 1=>true}
266 self.members.each do |m|
268 n = Node.find(:first, :conditions => ["id = ?", m[1]])
269 unless n and n.visible
277 elsif (m[0] == "way")
278 w = Way.find(:first, :conditions => ["id = ?", m[1]])
279 unless w and w.visible and w.preconditions_ok?
287 elsif (m[0] == "relation")
288 e = Relation.find(:first, :conditions => ["id = ?", m[1]])
289 unless e and e.visible and e.preconditions_ok?
295 relations[m[1]] = true
306 # Temporary method to match interface to nodes