1 class Relation < ActiveRecord::Base
6 has_many :relation_members, :foreign_key => 'id'
7 has_many :relation_tags, :foreign_key => 'id'
9 has_many :old_relations, :foreign_key => 'id', :order => 'version'
11 set_table_name 'current_relations'
13 def self.from_xml(xml, create=false)
19 doc.find('//osm/relation').each do |pt|
20 return Relation.from_xml_node(pt, create)
27 def self.from_xml_node(pt, create=false)
28 relation = Relation.new
30 if !create and pt['id'] != '0'
31 relation.id = pt['id'].to_i
35 relation.timestamp = Time.now
36 relation.visible = true
39 relation.timestamp = Time.parse(pt['timestamp'])
43 pt.find('tag').each do |tag|
44 relation.add_tag_keyval(tag['k'], tag['v'])
47 pt.find('member').each do |member|
48 relation.add_member(member['type'], member['ref'], member['role'])
55 doc = OSM::API.new.get_xml_doc
56 doc.root << to_xml_node()
60 def to_xml_node(user_display_name_cache = nil)
61 el1 = XML::Node.new 'relation'
62 el1['id'] = self.id.to_s
63 el1['visible'] = self.visible.to_s
64 el1['timestamp'] = self.timestamp.xmlschema
65 el1['version'] = self.version.to_s
67 user_display_name_cache = {} if user_display_name_cache.nil?
69 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
70 # use the cache if available
71 elsif self.user.data_public?
72 user_display_name_cache[self.user_id] = self.user.display_name
74 user_display_name_cache[self.user_id] = nil
77 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
79 self.relation_members.each do |member|
82 # # if there is a list of visible members then use that to weed out deleted segments
83 # if visible_members[member.member_type][member.member_id]
87 # otherwise, manually go to the db to check things
88 if member.member.visible?
93 e = XML::Node.new 'member'
94 e['type'] = member.member_type
95 e['ref'] = member.member_id.to_s
96 e['role'] = member.member_role
101 self.relation_tags.each do |tag|
102 e = XML::Node.new 'tag'
111 # collect relationships. currently done in one big block at the end;
112 # may need to move this upwards if people want automatic completion of
113 # relationships, i.e. deliver referenced objects like we do with ways...
114 # FIXME: rip out the fucking SQL
115 def self.find_for_nodes_and_ways(node_ids, way_ids)
118 if node_ids.length > 0
119 relations += Relation.find_by_sql("select e.* from current_relations e,current_relation_members em where " +
121 "em.id = e.id and em.member_type='node' and em.member_id in (#{node_ids.join(',')})")
123 if way_ids.length > 0
124 relations += Relation.find_by_sql("select e.* from current_relations e,current_relation_members em where " +
126 "em.id = e.id and em.member_type='way' and em.member_id in (#{way_ids.join(',')})")
129 relations # if you don't do this then it returns nil and not []
133 # FIXME is this really needed?
137 self.relation_members.each do |member|
138 @members += [[member.member_type,member.member_id,member.member_role]]
147 self.relation_tags.each do |tag|
162 def add_member(type,id,role)
163 @members = Array.new unless @members
164 @members += [[type,id,role]]
167 def add_tag_keyval(k, v)
168 @tags = Hash.new unless @tags
172 def save_with_history!
173 Relation.transaction do
180 RelationTag.delete_all(['id = ?', self.id])
182 tag = RelationTag.new
189 members = self.members
190 RelationMember.delete_all(['id = ?', self.id])
192 mem = RelationMember.new
194 mem.member_type = n[0];
195 mem.member_id = n[1];
196 mem.member_role = n[2];
200 old_relation = OldRelation.from_relation(self)
201 old_relation.timestamp = t
202 old_relation.save_with_dependencies!
206 def delete_with_history(user)
208 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 ])
209 raise OSM::APIPreconditionFailedError.new
211 self.user_id = user.id
218 raise OSM::APIAlreadyDeletedError.new
222 def update_from(new_relation, user)
223 if !new_relation.preconditions_ok?
224 raise OSM::APIPreconditionFailedError.new
226 self.user_id = user.id
227 self.tags = new_relation.tags
228 self.members = new_relation.members
234 def preconditions_ok?
235 self.members.each do |m|
237 n = Node.find(:first, :conditions => ["id = ?", m[1]])
238 unless n and n.visible
241 elsif (m[0] == "way")
242 w = Way.find(:first, :conditions => ["id = ?", m[1]])
243 unless w and w.visible and w.preconditions_ok?
246 elsif (m[0] == "relation")
247 e = Relation.find(:first, :conditions => ["id = ?", m[1]])
248 unless e and e.visible and e.preconditions_ok?