More changes from the live server.
[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     if ids.empty?
110       return []
111     else
112       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
113         return self.find(:all, options)
114       end
115     end
116   end
117
118   def self.find_for_ways(ids, options = {})
119     if ids.empty?
120       return []
121     else
122       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
123         return self.find(:all, options)
124       end
125     end
126   end
127
128   def self.find_for_relations(ids, options = {})
129     if ids.empty?
130       return []
131     else
132       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
133         return self.find(:all, options)
134       end
135     end
136   end
137
138   # FIXME is this really needed?
139   def members
140     unless @members
141       @members = Array.new
142       self.relation_members.each do |member|
143         @members += [[member.member_type,member.member_id,member.member_role]]
144       end
145     end
146     @members
147   end
148
149   def tags
150     unless @tags
151       @tags = Hash.new
152       self.relation_tags.each do |tag|
153         @tags[tag.k] = tag.v
154       end
155     end
156     @tags
157   end
158
159   def members=(m)
160     @members = m
161   end
162
163   def tags=(t)
164     @tags = t
165   end
166
167   def add_member(type,id,role)
168     @members = Array.new unless @members
169     @members += [[type,id,role]]
170   end
171
172   def add_tag_keyval(k, v)
173     @tags = Hash.new unless @tags
174     @tags[k] = v
175   end
176
177   def save_with_history!
178     Relation.transaction do
179       t = Time.now
180       self.timestamp = t
181       self.save!
182
183       tags = self.tags
184
185       RelationTag.delete_all(['id = ?', self.id])
186
187       tags.each do |k,v|
188         tag = RelationTag.new
189         tag.k = k
190         tag.v = v
191         tag.id = self.id
192         tag.save!
193       end
194
195       members = self.members
196
197       RelationMember.delete_all(['id = ?', self.id])
198
199       members.each do |n|
200         mem = RelationMember.new
201         mem.id = self.id
202         mem.member_type = n[0];
203         mem.member_id = n[1];
204         mem.member_role = n[2];
205         mem.save!
206       end
207
208       old_relation = OldRelation.from_relation(self)
209       old_relation.timestamp = t
210       old_relation.save_with_dependencies!
211     end
212   end
213
214   def preconditions_ok?
215     # These are hastables that store an id in the index of all 
216     # the nodes/way/relations that have already been added.
217     # Once we know the id of the node/way/relation exists
218     # we check to see if it is already existing in the hashtable
219     # if it does, then we return false. Otherwise
220     # we add it to the relevant hash table, with the value true..
221     # Thus if you have nodes with the ids of 50 and 1 already in the
222     # relation, then the hash table nodes would contain:
223     # => {50=>true, 1=>true}
224     nodes = Hash.new
225     ways = Hash.new
226     relations = Hash.new
227     self.members.each do |m|
228       if (m[0] == "node")
229         n = Node.find(:first, :conditions => ["id = ?", m[1]])
230         unless n and n.visible 
231           return false
232         end
233         if nodes[m[1]]
234           return false
235         else
236           nodes[m[1]] = true
237         end
238       elsif (m[0] == "way")
239         w = Way.find(:first, :conditions => ["id = ?", m[1]])
240         unless w and w.visible and w.preconditions_ok?
241           return false
242         end
243         if ways[m[1]]
244           return false
245         else
246           ways[m[1]] = true
247         end
248       elsif (m[0] == "relation")
249         e = Relation.find(:first, :conditions => ["id = ?", m[1]])
250         unless e and e.visible and e.preconditions_ok?
251           return false
252         end
253         if relations[m[1]]
254           return false
255         else
256           relations[m[1]] = true
257         end
258       else
259         return false
260       end
261     end
262     return true
263   rescue
264     return false
265   end
266
267   # Temporary method to match interface to nodes
268   def tags_as_hash
269     return self.tags
270   end
271 end