diff --git a/app/models/concerns/local_import_support.rb b/app/models/concerns/local_import_support.rb index 8366aa9aa5..cd12728db0 100644 --- a/app/models/concerns/local_import_support.rb +++ b/app/models/concerns/local_import_support.rb @@ -21,7 +21,6 @@ def import_type def import Chouette::Benchmark.measure "import_#{import_type}", id: id do update status: 'running', started_at: Time.now - @progress = 0 ActiveRecord::Base.cache do import_without_status @@ -29,29 +28,20 @@ def import processor.after([referential]) - @progress = nil @status ||= 'successful' referential&.active! update status: @status, ended_at: Time.now end + rescue InvalidReferential + update status: 'failed', ended_at: Time.now + if overlapping_referential_ids.present? + create_message criticity: :error, message_key: 'referential_creation_overlapping_existing_referential_block' + end rescue StandardError => e update status: 'failed', ended_at: Time.now Chouette::Safe.capture "#{self.class.name} ##{id} failed", e - if (referential && overlapped_referential_ids = referential.overlapped_referential_ids).present? - overlapped = Referential.find overlapped_referential_ids.last - create_message( - criticity: :error, - message_attributes: { - referential_name: referential.name, - overlapped_name: overlapped.name, - overlapped_url: Rails.application.routes.url_helpers.workbench_referential_path(workbench, overlapped) - }, - message_key: 'referential_creation_overlapping_existing_referential' - ) - else - create_message criticity: :error, message_key: :full_text, message_attributes: { text: e.message } - end + create_message criticity: :error, message_key: :full_text, message_attributes: { text: e.message } referential&.failed! ensure main_resource&.save @@ -77,27 +67,68 @@ def import_resources(*resources) end end - def create_referential - Chouette::Benchmark.measure 'create_referential' do - self.referential ||= Referential.new( - name: referential_name, - organisation_id: workbench.organisation_id, - workbench_id: workbench.id, - metadatas: [referential_metadata], + def referential_builder + @referential_builder ||= ReferentialBuilder.new(workbench, name: referential_name, metadata: referential_metadata) + end + + # Create a Referential with given name and medata + class ReferentialBuilder + def initialize(workbench, name:, metadata:) + @workbench = workbench + @name = name + @metadata = metadata + end + attr_reader :workbench, :name, :metadata + + delegate :organisation, to: :workbench + + def create(&block) + if valid? + Rails.logger.debug "Create imported Referential: #{referential.inspect}" + block.call referential + else + Rails.logger.debug "Can't created imported Referential: #{referential.inspect}" + end + end + + def referential + @referential ||= workbench.referentials.create( + name: name, + organisation: organisation, + metadatas: [metadata], ready: false ) + end + + def valid? + @valid ||= referential.valid? + end + + def overlapping_referential_ids + @overlapping_referential_ids ||= referential.overlapped_referential_ids + end + end + + # DEPRECATED Use #within_referential + def create_referential + Chouette::Benchmark.measure 'create_referential' do + self.referential ||= referential_builder.referential - begin - self.referential.save! - rescue ActiveRecord::RecordInvalid - # No double capture for Chouette::Safe - Rails.logger.error "Unable to create referential: #{self.referential.errors.messages}" - raise - end main_resource.update referential: referential if main_resource + + unless referential_builder.valid? + self.overlapping_referential_ids = referential_builder.overlapping_referential_ids + raise InvalidReferential + end + + referential end end + # DEPRECATED Use #within_referential + class InvalidReferential < StandardError + end + def referential_name name.presence || File.basename(local_file.to_s) end diff --git a/app/models/import/netex_generic.rb b/app/models/import/netex_generic.rb index 1024cb7ac4..d461ef5434 100644 --- a/app/models/import/netex_generic.rb +++ b/app/models/import/netex_generic.rb @@ -85,14 +85,12 @@ def within_referential(&block) return if referential_builder.valid? - # Create a global error message - messages.create criticity: :error, message_key: 'referential_creation_overlapping_existing_referential' - # Save overlapping referentials for user display - # self.overlapping_referential_ids = referential_builder.overlapping_referential_ids - end - - def referential_builder - @referential_builder ||= ReferentialBuilder.new(workbench, name: name, metadata: referential_metadata) + # create_message has a strange behavior in this context + messages.build( + criticity: :error, + message_key: 'referential_creation_overlapping_existing_referential_block' + ) + self.overlapping_referential_ids = referential_builder.overlapping_referential_ids end def referential_metadata @@ -102,44 +100,6 @@ def referential_metadata ReferentialMetadata.new line_ids: imported_line_ids, periodes: [netex_source.validity_period] end - # Create a Referential with given name and medata - class ReferentialBuilder - def initialize(workbench, name:, metadata:) - @workbench = workbench - @name = name - @metadata = metadata - end - attr_reader :workbench, :name, :metadata - - delegate :organisation, to: :workbench - - def create(&block) - if valid? - Rails.logger.debug "Create imported Referential: #{referential.inspect}" - block.call referential - else - Rails.logger.debug "Can't created imported Referential: #{referential.inspect}" - end - end - - def referential - @referential ||= workbench.referentials.create( - name: name, - organisation: organisation, - metadatas: [metadata], - ready: false - ) - end - - def valid? - @valid ||= referential.valid? - end - - def overlapping_referential_ids - @overlapping_referential_ids ||= referential.overlapped_referential_ids - end - end - # TODO: why the resource statuses are not checked automaticaly ?? # See CHOUETTE-2747 def update_import_status diff --git a/config/locales/import_messages.en.yml b/config/locales/import_messages.en.yml index 193b833cc5..6aaa6de806 100644 --- a/config/locales/import_messages.en.yml +++ b/config/locales/import_messages.en.yml @@ -11,7 +11,7 @@ en: multiple_companies_in_file: "The file %{source_filename} contains multiple companies, imported lines have therefore not be associated." referential_creation_missing_lines_in_files: "Le référentiel %{referential_name} has not been created because the file contains no offer data" referential_creation_missing_lines: "The referential %{referential_name} has not been created because no line has been found in the organization" - # referential_creation_overlapping_existing_referential: "You are trying to import a file that overlaps DataSets in edition (see Overlapping section for details)." + referential_creation_overlapping_existing_referential_block: "You are trying to import a file that overlaps DataSets in edition (see Overlapping section for details)." referential_creation_overlapping_existing_referential: "The referential %{referential_name} has not been created because another referential already exists for the same periods and lines: %{overlapped_name}" referential_creation: "The referential %{referential_name} has not been created because another referential with the same lines and periods already exists" trip_with_inconsistent_stop_times: The trip %{trip_id} has inconsistent stop times diff --git a/config/locales/import_messages.fr.yml b/config/locales/import_messages.fr.yml index 7a5a08bed2..a76a19d603 100644 --- a/config/locales/import_messages.fr.yml +++ b/config/locales/import_messages.fr.yml @@ -11,7 +11,7 @@ fr: multiple_companies_in_file: "Le fichier %{source_filename} contient plusieurs transporteurs, les lignes importées n'ont donc pas étées associées." referential_creation_missing_lines_in_files: "Le référentiel %{referential_name} n'a pas pu être créé car aucune donnée d'offre n'est présente dans le fichier" referential_creation_missing_lines: "Le référentiel %{referential_name} n'a pas pu être créé car aucune ligne n'appartient à l'organisation" - # referential_creation_overlapping_existing_referential: "Vous essayez d’importer un fichier qui chevauche des Jeux de Données en édition (consulter la section Chevauchement)." + referential_creation_overlapping_existing_referential_block: "Vous essayez d’importer un fichier qui chevauche des Jeux de Données en édition (consulter la section Chevauchement)." referential_creation_overlapping_existing_referential: "Le référentiel %{referential_name} n'a pas pu être créé car un référentiel existe déjà sur les mêmes périodes et lignes: %{overlapped_name}" referential_creation: "Le référentiel %{referential_name} n'a pas pu être créé." trip_with_inconsistent_stop_times: La course %{trip_id} a des horaires incohérents diff --git a/config/locales/imports.en.yml b/config/locales/imports.en.yml index de33522f2a..0991291bbc 100644 --- a/config/locales/imports.en.yml +++ b/config/locales/imports.en.yml @@ -38,7 +38,7 @@ en: parent: Parent status: State overlapping: Overlapping - overlapping_tips: you can archive or delete these DataSets before restarting a new import + overlapping_tips: You can archive or delete these DataSets before restarting a new import netex: table_title: "Status of analyzed files" table_explanation: "When calendriers.xml and/or commun.xml are not imported, then all lines file are not processed." diff --git a/config/locales/imports.fr.yml b/config/locales/imports.fr.yml index c12a29ff69..78d6d56b70 100644 --- a/config/locales/imports.fr.yml +++ b/config/locales/imports.fr.yml @@ -40,7 +40,7 @@ fr: status: État referential: Jeu de données overlapping: Chevauchement - overlapping_tips: vous pouvez archiver ou supprimer ces Jeux de Données avant de relancer le même import + overlapping_tips: Vous pouvez archiver ou supprimer ces Jeux de Données avant de relancer le même import netex: table_title: "État des fichiers analysés" table_explanation: "Dans le cas ou le(s) fichiers calendriers.xml et/ou commun.xml sont dans un état non importé, alors tous les fichiers lignes sont automatiquement dans un état non traité."