fix problem with blank areas throwing a 500
[rails.git] / app / models / relation.rb
1 class Relation < ActiveRecord::Base
2   require 'xml/libxml'
3   
4   belongs_to :user
5
6   has_many :relation_members, :foreign_key => 'id'
7   has_many :relation_tags, :foreign_key => 'id'
8
9   has_many :old_relations, :foreign_key => 'id', :order => 'version'
10
11   set_table_name 'current_relations'
12
13   def self.from_xml(xml, create=false)
14     begin
15       p = XML::Parser.new
16       p.string = xml
17       doc = p.parse
18
19       relation = Relation.new
20
21       doc.find('//osm/relation').each do |pt|
22         if !create and pt['id'] != '0'
23           relation.id = pt['id'].to_i
24         end
25
26         if create
27           relation.timestamp = Time.now
28           relation.visible = true
29         else
30           if pt['timestamp']
31             relation.timestamp = Time.parse(pt['timestamp'])
32           end
33         end
34
35         pt.find('tag').each do |tag|
36           relation.add_tag_keyval(tag['k'], tag['v'])
37         end
38
39         pt.find('member').each do |member|
40           relation.add_member(member['type'], member['ref'], member['role'])
41         end
42       end
43     rescue
44       relation = nil
45     end
46
47     return relation
48   end
49
50   def to_xml
51     doc = OSM::API.new.get_xml_doc
52     doc.root << to_xml_node()
53     return doc
54   end
55
56   def to_xml_node(user_display_name_cache = nil)
57     el1 = XML::Node.new 'relation'
58     el1['id'] = self.id.to_s
59     el1['visible'] = self.visible.to_s
60     el1['timestamp'] = self.timestamp.xmlschema
61
62     user_display_name_cache = {} if user_display_name_cache.nil?
63     
64     if user_display_name_cache and user_display_name_cache.key?(self.user_id)
65       # use the cache if available
66     elsif self.user.data_public?
67       user_display_name_cache[self.user_id] = self.user.display_name
68     else
69       user_display_name_cache[self.user_id] = nil
70     end
71
72     el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
73
74     self.relation_members.each do |member|
75       p=0
76       #if visible_members
77       #  # if there is a list of visible members then use that to weed out deleted segments
78       #  if visible_members[member.member_type][member.member_id]
79       #    p=1
80       #  end
81       #else
82         # otherwise, manually go to the db to check things
83         if member.member.visible?
84           p=1
85         end
86       #end
87       if p
88         e = XML::Node.new 'member'
89         e['type'] = member.member_type
90         e['ref'] = member.member_id.to_s 
91         e['role'] = member.member_role
92         el1 << e
93        end
94     end
95
96     self.relation_tags.each do |tag|
97       e = XML::Node.new 'tag'
98       e['k'] = tag.k
99       e['v'] = tag.v
100       el1 << e
101     end
102     return el1
103   end 
104
105   def self.find_for_nodes_and_ways(node_ids, way_ids)
106     # collect relationships. currently done in one big block at the end;
107     # may need to move this upwards if people want automatic completion of
108     # relationships, i.e. deliver referenced objects like we do with ways...
109         return [] if node_ids.empty? and node_ids.empty?
110     relations = Array.new
111     if node_ids.length > 0
112         relations += Relation.find_by_sql("select e.* from current_relations e,current_relation_members em where " +
113             "e.visible=1 and " +
114             "em.id = e.id and em.member_type='node' and em.member_id in (#{node_ids.join(',')})")
115     end
116     if way_ids.length > 0
117         relations += Relation.find_by_sql("select e.* from current_relations e,current_relation_members em where " +
118             "e.visible=1 and " +
119             "em.id = e.id and em.member_type='way' and em.member_id in (#{way_ids.join(',')})")
120     end
121   end
122
123
124   # FIXME is this really needed?
125   def members
126     unless @members
127         @members = Array.new
128         self.relation_members.each do |member|
129             @members += [[member.member_type,member.member_id,member.member_role]]
130         end
131     end
132     @members
133   end
134
135   def tags
136     unless @tags
137         @tags = Hash.new
138         self.relation_tags.each do |tag|
139             @tags[tag.k] = tag.v
140         end
141     end
142     @tags
143   end
144
145   def members=(m)
146     @members = m
147   end
148
149   def tags=(t)
150     @tags = t
151   end
152
153   def add_member(type,id,role)
154     @members = Array.new unless @members
155     @members += [[type,id,role]]
156   end
157
158   def add_tag_keyval(k, v)
159     @tags = Hash.new unless @tags
160     @tags[k] = v
161   end
162
163   def save_with_history!
164     Relation.transaction do
165       t = Time.now
166       self.timestamp = t
167       self.save!
168
169       tags = self.tags
170
171       RelationTag.delete_all(['id = ?', self.id])
172
173       tags.each do |k,v|
174         tag = RelationTag.new
175         tag.k = k
176         tag.v = v
177         tag.id = self.id
178         tag.save!
179       end
180
181       members = self.members
182
183       RelationMember.delete_all(['id = ?', self.id])
184
185       members.each do |n|
186         mem = RelationMember.new
187         mem.id = self.id
188         mem.member_type = n[0];
189         mem.member_id = n[1];
190         mem.member_role = n[2];
191         mem.save!
192       end
193
194       old_relation = OldRelation.from_relation(self)
195       old_relation.timestamp = t
196       old_relation.save_with_dependencies!
197     end
198   end
199
200   def preconditions_ok?
201     self.members.each do |m|
202       if (m[0] == "node")
203         n = Node.find(:first, :conditions => ["id = ?", m[1]])
204         unless n and n.visible 
205           return false
206         end
207       elsif (m[0] == "way")
208         w = Way.find(:first, :conditions => ["id = ?", m[1]])
209         unless w and w.visible and w.preconditions_ok?
210           return false
211         end
212       elsif (m[0] == "relation")
213         e = Relation.find(:first, :conditions => ["id = ?", m[1]])
214         unless e and e.visible and e.preconditions_ok?
215           return false
216         end
217       else
218         return false
219       end
220     end
221     return true
222   rescue
223     return false
224   end
225
226 end