| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
 | class FileValidation < ActiveRecord::Base
  validates_presence_of :resources
  validates_inclusion_of :status, :in => %w{ pending completed failed }
  attr_accessor :resources,:uncheck_count,:ok_count,:warning_count,:error_count,:fatal_count,:log_message_tree
  attr_accessor :validator
  belongs_to :organisation
  validates_presence_of :organisation
  has_many :log_messages, :class_name => "FileValidationLogMessage", :order => :position, :dependent => :destroy
  serialize :options
  def self.option(name)
    name = name.to_s
    define_method(name) do
      self.options and self.options[name]
    end
    define_method("#{name}=") do |prefix|
      (self.options ||= {})[name] = prefix
    end
  end
  option :test3_1_minimal_distance
  option :test3_2_minimal_distance
  option :test3_2_polygon_points
  option :test3_7_minimal_distance
  option :test3_7_maximal_distance
  option :test3_8a_minimal_speed
  option :test3_8a_maximal_speed
  option :test3_8b_minimal_speed
  option :test3_8b_maximal_speed
  option :test3_8c_minimal_speed
  option :test3_8c_maximal_speed
  option :test3_8d_minimal_speed
  option :test3_8d_maximal_speed
  option :test3_9_minimal_speed
  option :test3_9_maximal_speed
  option :test3_10_minimal_distance
  option :test3_15_minimal_time
  option :test3_16_1_maximal_time
  option :test3_16_3a_maximal_time
  option :test3_16_3b_maximal_time
  option :test3_21a_minimal_speed
  option :test3_21a_maximal_speed
  option :test3_21b_minimal_speed
  option :test3_21b_maximal_speed
  option :test3_21c_minimal_speed
  option :test3_21c_maximal_speed
  option :test3_21d_minimal_speed
  option :test3_21d_maximal_speed
  option :projection_reference
  def validator
    @validator ||= ::Chouette::FileValidator.new("public")
  end
  def with_original_filename
    Dir.mktmpdir do |tmp_dir|
      tmp_link = File.join(tmp_dir, resources.original_filename)
      FileUtils.ln_s resources.path, tmp_link
      yield tmp_link
    end
  end
  before_validation :define_default_attributes, :on => :create
  def define_default_attributes
    self.status ||= "pending"
  end
  after_validation :extract_file_type, :on => :create
  def extract_file_type
    if ! resources.nil? 
      if !resources.original_filename.nil?
        self.file_type = resources.original_filename.rpartition(".").last
        self.file_name = resources.original_filename
      end
    end
  end
  after_create :delayed_validate
  def delayed_validate
    save_resources
    delay.validate
  end
  @@root = "#{Rails.root}/tmp/validations"
  cattr_accessor :root
  def save_resources
    FileUtils.mkdir_p root
    FileUtils.cp resources.path, saved_resources 
  end
  after_destroy :destroy_resources
  def destroy_resources
    FileUtils.rm saved_resources if File.exists? saved_resources
  end
  def saved_resources
    "#{root}/#{id}.#{file_type}"
  end
  def name
    "#{FileValidation.model_name.humanize} #{id}"
  end
  def validation_options
    hash = { :validation_id => self.id ,
      :file_format => self.file_type}
    options.keys.each do |opt|
      hash.merge! opt.to_sym => self.send(opt.to_sym)
    end
    hash
  end
  def validate
    begin
      # log_messages.create :key => :started
      if resources
        with_original_filename do |file|
          # chouette-command checks the file extension (and requires .zip) :(
          validator.validate file, validation_options
        end
      else
        validator.validate saved_resources, validation_options
      end
      update_attribute :status, "completed"
    rescue => e
      Rails.logger.error "Validation #{id} failed : #{e}, #{e.backtrace}"
      update_attribute :status, "failed"
    end
    # log_messages.create :key => status
  end
  after_find :compute_tests
  def compute_tests
    if status == 'completed'
      self.uncheck_count = 0
      self.ok_count = 0
      self.warning_count = 0
      self.error_count = 0
      self.fatal_count = 0
      self.log_message_tree = Array.new
      father1=nil
      father2=nil
      father3=nil
      log_messages.each do |message| 
        if message.level == 1
          self.log_message_tree << message
          father1=message
        elsif message.level == 2
          father1.add_child message
          father2=message
        elsif message.level == 3
          father2.add_child message
          father3=message
          if message.severity == 'uncheck'
            self.uncheck_count += 1
          end
          if message.severity == 'ok'
            self.ok_count += 1
          end
          if message.severity == 'warning'
            self.warning_count += 1
          end
          if message.severity == 'error'
            self.error_count += 1
          end
          if message.severity == 'fatal'
            self.fatal_count += 1
          end
        elsif message.level == 4
          father3.add_child message
        end
      end
    end
    
  end
  
end
 |