aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/export.rb
blob: 0c2f24cde65f4990fae9c66ce19c7699446a6f33 (plain)
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
class Export < ActiveRecord::Base

  belongs_to :referential
  validates_presence_of :referential_id

  validates_inclusion_of :status, :in => %w{ pending completed failed }

  has_many :log_messages, :class_name => "ExportLogMessage", :order => :position, :dependent => :delete_all

  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


  def exporter
    exporter ||= ::Chouette::Exporter.new(referential.slug)
  end

  @@root = "#{Rails.root}/tmp/exports"
  cattr_accessor :root

  after_destroy :destroy_file
  def destroy_file
    FileUtils.rm file if File.exists? file
  end

  def file
    "#{root}/#{id}.zip"
  end

  def name
    "#{self.class.model_name.human} #{id}"
  end

  def export_options
    { :export_id => self.id, :o => export_object_type }.tap do |options|
      options[:id] = reference_ids.join(',') if reference_ids.present?
    end
  end

  def export_object_type
#    case references_type
#    when "Chouette::Network"
#      "ptnetwork"
#    else
      references_relation ? references_relation.singularize : "line"
#    end
  end

  before_validation :define_default_attributes, :on => :create
  def define_default_attributes
    self.status ||= "pending"
    self.options ||= {}
  end

  after_create :delayed_export
  def delayed_export
    delay.export
  end

  def export
    FileUtils.mkdir_p root

    begin
      # delayed job may repeat call
      ExportLogMessage.where(:export_id => self.id).delete_all 
      log_messages.create :key => :started

      exporter.export file, export_options

      update_attribute :status, "completed"
    rescue => e
      Rails.logger.error "Export #{id} failed : #{e}, #{e.backtrace}"
      update_attribute :status, "failed"
    end

    log_messages.create :key => status
  end

  @@references_types = [ Chouette::Line, Chouette::Network, Chouette::Company ]
  cattr_reader :references_types

  validates_inclusion_of :references_type, :in => references_types.map(&:to_s), :allow_blank => true, :allow_nil => true

  def references
    if references_relation.present? and reference_ids.present?
      referential.send(references_relation).find(reference_ids)
    else
      []
    end
  end

  def references=(references)
    unless references.blank?
      self.references_type = references.first.class.name
      self.reference_ids = references.map(&:id)
    else
      self.references_type = nil
      self.reference_ids = []
    end
  end

  def references_relation
    if references_type.present?
      relation = self.class.references_relation(references_type)
      relation if referential.respond_to?(relation)
    end
  end

  def self.references_relation(type)
    type.to_s.demodulize.underscore.pluralize 
  end

  def reference_ids
    if raw_reference_ids
      raw_reference_ids.split(',').map(&:to_i) 
    else
      []
    end
  end

  def reference_ids=(records)
    records = Array(records)
    write_attribute :reference_ids, (records.present? ? records.join(',') : nil)
  end

  def raw_reference_ids
    read_attribute :reference_ids
  end

  def self.format_name(format)
    name_by_format = { 
      "NeptuneExport" => "Neptune",
      "CsvExport" => "CSV",
      "GtfsExport" => "GTFS",
      "NetexExport" => "NeTEx",
      "KmlExport" => "KML"
    }
    name_by_format[format]
  end

  def self.types
    unless Rails.env.development?
      subclasses.map(&:to_s)
    else
      # FIXME
      %w{NeptuneExport CsvExport GtfsExport NetexExport KmlExport}
    end
  end

  def self.new(attributes = {}, options = {}, &block)
    if self == Export
      Object.const_get(attributes.delete(:type) || "NeptuneExport").new(attributes, options)
    else
      super
    end
  end

end