fixed "full" and "relationships_for_object" modes
[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   # FIXME is this really needed?
106   def members
107     unless @members
108         @members = Array.new
109         self.relation_members.each do |member|
110             @members += [[member.member_type,member.member_id,member.member_role]]
111         end
112     end
113     @members
114   end
115
116   def tags
117     unless @tags
118         @tags = Hash.new
119         self.relation_tags.each do |tag|
120             @tags[tag.k] = tag.v
121         end
122     end
123     @tags
124   end
125
126   def members=(m)
127     @members = m
128   end
129
130   def tags=(t)
131     @tags = t
132   end
133
134   def add_member(type,id,role)
135     @members = Array.new unless @members
136     @members += [[type,id,role]]
137   end
138
139   def add_tag_keyval(k, v)
140     @tags = Hash.new unless @tags
141     @tags[k] = v
142   end
143
144   def save_with_history
145     begin
146       Relation.transaction do
147         t = Time.now
148         self.timestamp = t
149         self.save!
150
151         tags = self.tags
152
153         RelationTag.delete_all(['id = ?', self.id])
154
155         tags.each do |k,v|
156           tag = RelationTag.new
157           tag.k = k
158           tag.v = v
159           tag.id = self.id
160           tag.save!
161         end
162
163         members = self.members
164
165         RelationMember.delete_all(['id = ?', self.id])
166
167         members.each do |n|
168           mem = RelationMember.new
169           mem.id = self.id
170           mem.member_type = n[0];
171           mem.member_id = n[1];
172           mem.member_role = n[2];
173           mem.save!
174         end
175
176         old_relation = OldRelation.from_relation(self)
177         old_relation.timestamp = t
178         old_relation.save_with_dependencies!
179       end
180
181       return true
182     rescue Exception => ex
183       return nil
184     end
185   end
186
187   def preconditions_ok?
188     self.members.each do |m|
189       if (m[0] == "node")
190         n = Node.find(:first, :conditions => ["id = ?", m[1]])
191         unless n and n.visible 
192           return false
193         end
194       elsif (m[0] == "way")
195         w = Way.find(:first, :conditions => ["id = ?", m[1]])
196         unless w and w.visible and w.preconditions_ok?
197           return false
198         end
199       elsif (m[0] == "relation")
200         e = Relation.find(:first, :conditions => ["id = ?", m[1]])
201         unless e and e.visible and e.preconditions_ok?
202           return false
203         end
204       else
205         return false
206       end
207     end
208     return true
209   rescue
210     return false
211   end
212
213 end