source: subversion/sites/rails_port/app/models/segment.rb @ 3148

Last change on this file since 3148 was 2852, checked in by steve, 13 years ago

various fixes

File size: 2.8 KB
Line 
1class Segment < ActiveRecord::Base
2  require 'xml/libxml'
3  set_table_name 'current_segments'
4
5  validates_numericality_of :node_a
6  validates_numericality_of :node_b
7
8  has_many :old_segments, :foreign_key => :id
9  belongs_to :user
10
11  # using belongs_to :foreign_key = 'node_*', since if use has_one :foreign_key = 'id', segment preconditions? fails checking for segment id in node table
12  belongs_to :from_node, :class_name => 'Node', :foreign_key => 'node_a'
13  belongs_to :to_node, :class_name => 'Node', :foreign_key => 'node_b'
14
15  def self.from_xml(xml, create=false)
16    p = XML::Parser.new
17    p.string = xml
18    doc = p.parse
19
20    segment = Segment.new
21
22    doc.find('//osm/segment').each do |pt|
23
24      segment.node_a = pt['from'].to_i
25      segment.node_b = pt['to'].to_i
26
27      if pt['id'] != '0'
28        segment.id = pt['id'].to_i
29      end
30
31      segment.visible = true
32
33      if create
34        segment.timestamp = Time.now
35      else
36        if pt['timestamp']
37          segment.timestamp = Time.parse(pt['timestamp'])
38        end
39      end
40
41      tags = []
42
43      pt.find('tag').each do |tag|
44        tags << [tag['k'],tag['v']]
45      end
46
47      tags = tags.collect { |k,v| "#{k}=#{v}" }.join(';')
48      tags = '' if tags.nil?
49
50      segment.tags = tags
51
52    end
53    return segment
54  end
55
56  def save_with_history
57    begin
58      Segment.transaction do
59        self.timestamp = Time.now
60        self.save
61        old_segment = OldSegment.from_segment(self)
62        old_segment.save
63      end
64      return true
65    rescue Exception => ex
66      return nil
67    end
68  end
69
70  def to_xml
71    doc = OSM::API.new.get_xml_doc
72    doc.root << to_xml_node()
73    return doc
74  end
75
76  def to_xml_node(user_display_name_cache  = nil)
77    el1 = XML::Node.new 'segment'
78    el1['id'] = self.id.to_s
79    el1['from'] = self.node_a.to_s
80    el1['to'] = self.node_b.to_s
81
82    user_display_name_cache = {} if user_display_name_cache.nil?
83    if user_display_name_cache and user_display_name_cache[self.user_id]
84      # use the cache if available
85    else
86      user_display_name_cache[self.user_id] = self.user.display_name
87    end
88   
89    #el1['user'] = self.user.display_name if self.user.data_public?
90    el1['user'] = user_display_name_cache[self.user_id]
91
92    Segment.split_tags(el1, self.tags)
93    el1['visible'] = self.visible.to_s
94    el1['timestamp'] = self.timestamp.xmlschema
95    return el1
96  end
97
98  def self.split_tags(el, tags)
99    tags.split(';').each do |tag|
100      parts = tag.split('=')
101      key = ''
102      val = ''
103      key = parts[0].strip unless parts[0].nil?
104      val = parts[1].strip unless parts[1].nil?
105      if key != '' && val != ''
106        el2 = XML::Node.new('tag')
107        el2['k'] = key.to_s
108        el2['v'] = val.to_s
109        el << el2
110      end
111    end
112  end
113
114  def preconditions_ok?
115    from_node and from_node.visible and to_node and to_node.visible
116  end
117
118end
Note: See TracBrowser for help on using the repository browser.