aboutsummaryrefslogtreecommitdiffstats
path: root/lib/tasks/imports.rake
blob: cd9217e5afb43a709c063f3500eddda9670c642a (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
require 'csv'
require 'tasks/helpers/simple_interfaces'

namespace :import do
  desc "Notify parent imports when children finish"
  task notify_parent: :environment do
    ParentNotifier.new(Import).notify_when_finished
  end

  desc "Mark old unfinished Netex imports as 'aborted'"
  task netex_abort_old: :environment do
    NetexImport.abort_old
  end

  desc "import the given file with the corresponding importer"
  task :import, [:configuration_name, :filepath, :referential_id, :logs_output_dir] => :environment do |t, args|
    args.with_defaults(logs_output_dir: "./log/importers/")
    FileUtils.mkdir_p args[:logs_output_dir]

    importer = SimpleImporter.create configuration_name: args[:configuration_name], filepath: args[:filepath]

    if args[:referential_id].present?
      referential = Referential.find args[:referential_id]
      importer.configure do |config|
        config.add_value :referential, referential
        config.context = {referential: referential, logs_output_dir: args[:logs_output_dir]}
      end
    end

    SimpleInterfacesHelper.run_interface_controlling_interruption importer, :import, args
  end

  desc "import the given file with the corresponding importer in the given StopAreaReferential"
  task :import_in_stop_area_referential, [:referential_id, :configuration_name, :filepath] => :environment do |t, args|
    args.with_defaults(logs_output_dir: "./log/importers/")
    FileUtils.mkdir_p args[:logs_output_dir]

    referential = StopAreaReferential.find args[:referential_id]
    importer = SimpleImporter.create configuration_name: args[:configuration_name], filepath: args[:filepath]
    importer.configure do |config|
      config.add_value :stop_area_referential, referential
      config.context = {stop_area_referential: referential, logs_output_dir: args[:logs_output_dir]}
    end

    SimpleInterfacesHelper.run_interface_controlling_interruption importer, :import, args
  end

  desc "import the given routes files"
  task :import_routes, [:referential_id, :configuration_name, :mapping_filepath, :filepath] => :environment do |t, args|
    args.with_defaults(logs_output_dir: "./log/importers/")
    FileUtils.mkdir_p args[:logs_output_dir]

    referential = Referential.find args[:referential_id]
    referential.switch
    stop_area_referential = referential.stop_area_referential
    importer = SimpleImporter.create configuration_name: args[:configuration_name], filepath: args[:filepath]
    importer.configure do |config|
      config.add_value :stop_area_referential, referential
      config.context = {stop_area_referential: stop_area_referential, mapping_filepath: args[:mapping_filepath], logs_output_dir: args[:logs_output_dir]}
    end

    SimpleInterfacesHelper.run_interface_controlling_interruption importer, :import, args
  end

  desc "import the given file with the corresponding importer in the given LineReferential"
  task :import_in_line_referential, [:referential_id, :configuration_name, :filepath, :logs_output_dir] => :environment do |t, args|
    args.with_defaults(logs_output_dir: "./log/importers/")
    FileUtils.mkdir_p args[:logs_output_dir]

    referential = LineReferential.find args[:referential_id]
    importer = SimpleImporter.create configuration_name: args[:configuration_name], filepath: args[:filepath]
    importer.configure do |config|
      config.add_value :line_referential, referential
      config.context = {line_referential: referential, logs_output_dir: args[:logs_output_dir]}
    end

    SimpleInterfacesHelper.run_interface_controlling_interruption importer, :import, args
  end
end