Merge potlatch_010 branch to head.
[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       relation = Relation.new
23
24       doc.find('//osm/relation').each do |pt|
25         if !create and pt['id'] != '0'
26           relation.id = pt['id'].to_i
27         end
28
29         if create
30           relation.timestamp = Time.now
31           relation.visible = true
32         else
33           if pt['timestamp']
34             relation.timestamp = Time.parse(pt['timestamp'])
35           end
36         end
37
38         pt.find('tag').each do |tag|
39           relation.add_tag_keyval(tag['k'], tag['v'])
40         end
41
42         pt.find('member').each do |member|
43           relation.add_member(member['type'], member['ref'], member['role'])
44         end
45       end
46     rescue
47       relation = nil
48     end
49
50     return relation
51   end
52
53   def to_xml
54     doc = OSM::API.new.get_xml_doc
55     doc.root << to_xml_node()
56     return doc
57   end
58
59   def to_xml_node(user_display_name_cache = nil)
60     el1 = XML::Node.new 'relation'
61     el1['id'] = self.id.to_s
62     el1['visible'] = self.visible.to_s
63     el1['timestamp'] = self.timestamp.xmlschema
64
65     user_display_name_cache = {} if user_display_name_cache.nil?
66     
67     if user_display_name_cache and user_display_name_cache.key?(self.user_id)
68       # use the cache if available
69     elsif self.user.data_public?
70       user_display_name_cache[self.user_id] = self.user.display_name
71     else
72       user_display_name_cache[self.user_id] = nil
73     end
74
75     el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
76
77     self.relation_members.each do |member|
78       p=0
79       #if visible_members
80       #  # if there is a list of visible members then use that to weed out deleted segments
81       #  if visible_members[member.member_type][member.member_id]
82       #    p=1
83       #  end
84       #else
85         # otherwise, manually go to the db to check things
86         if member.member.visible?
87           p=1
88         end
89       #end
90       if p
91         e = XML::Node.new 'member'
92         e['type'] = member.member_type
93         e['ref'] = member.member_id.to_s 
94         e['role'] = member.member_role
95         el1 << e
96        end
97     end
98
99     self.relation_tags.each do |tag|
100       e = XML::Node.new 'tag'
101       e['k'] = tag.k
102       e['v'] = tag.v
103       el1 << e
104     end
105     return el1
106   end 
107
108   def self.find_for_nodes(ids, options = {})
109     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
110       return self.find(:all, options)
111     end
112   end
113
114   def self.find_for_ways(ids, options = {})
115     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
116       return self.find(:all, options)
117     end
118   end
119
120   def self.find_for_relations(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 = 'relation' AND current_relation_members.member_id IN (#{ids.join(',')})" }) do
122       return self.find(:all, options)
123     end
124   end
125
126   # FIXME is this really needed?
127   def members
128     unless @members
129       @members = Array.new
130       self.relation_members.each do |member|
131         @members += [[member.member_type,member.member_id,member.member_role]]
132       end
133     end
134     @members
135   end
136
137   def tags
138     unless @tags
139       @tags = Hash.new
140       self.relation_tags.each do |tag|
141         @tags[tag.k] = tag.v
142       end
143     end
144     @tags
145   end
146
147   def members=(m)
148     @members = m
149   end
150
151   def tags=(t)
152     @tags = t
153   end
154
155   def add_member(type,id,role)
156     @members = Array.new unless @members
157     @members += [[type,id,role]]
158   end
159
160   def add_tag_keyval(k, v)
161     @tags = Hash.new unless @tags
162     @tags[k] = v
163   end
164
165   def save_with_history!
166     Relation.transaction do
167       t = Time.now
168       self.timestamp = t
169       self.save!
170
171       tags = self.tags
172
173       RelationTag.delete_all(['id = ?', self.id])
174
175       tags.each do |k,v|
176         tag = RelationTag.new
177         tag.k = k
178         tag.v = v
179         tag.id = self.id
180         tag.save!
181       end
182
183       members = self.members
184
185       RelationMember.delete_all(['id = ?', self.id])
186
187       members.each do |n|
188         mem = RelationMember.new
189         mem.id = self.id
190         mem.member_type = n[0];
191         mem.member_id = n[1];
192         mem.member_role = n[2];
193         mem.save!
194       end
195
196       old_relation = OldRelation.from_relation(self)
197       old_relation.timestamp = t
198       old_relation.save_with_dependencies!
199     end
200   end
201
202   def preconditions_ok?
203     # These are hastables that store an id in the index of all 
204     # the nodes/way/relations that have already been added.
205     # Once we know the id of the node/way/relation exists
206     # we check to see if it is already existing in the hashtable
207     # if it does, then we return false. Otherwise
208     # we add it to the relevant hash table, with the value true..
209     # Thus if you have nodes with the ids of 50 and 1 already in the
210     # relation, then the hash table nodes would contain:
211     # => {50=>true, 1=>true}
212     nodes = Hash.new
213     ways = Hash.new
214     relations = Hash.new
215     self.members.each do |m|
216       if (m[0] == "node")
217         n = Node.find(:first, :conditions => ["id = ?", m[1]])
218         unless n and n.visible 
219           return false
220         end
221         if nodes[m[1]]
222           return false
223         else
224           nodes[m[1]] = true
225         end
226       elsif (m[0] == "way")
227         w = Way.find(:first, :conditions => ["id = ?", m[1]])
228         unless w and w.visible and w.preconditions_ok?
229           return false
230         end
231         if ways[m[1]]
232           return false
233         else
234           ways[m[1]] = true
235         end
236       elsif (m[0] == "relation")
237         e = Relation.find(:first, :conditions => ["id = ?", m[1]])
238         unless e and e.visible and e.preconditions_ok?
239           return false
240         end
241         if relations[m[1]]
242           return false
243         else
244           relations[m[1]] = true
245         end
246       else
247         return false
248       end
249     end
250     return true
251   rescue
252     return false
253   end
254
255   # Temporary method to match interface to nodes
256   def tags_as_hash
257     return self.tags
258   end
259 end