diff options
| author | Zog | 2018-04-09 12:54:07 +0200 |
|---|---|---|
| committer | Zog | 2018-04-09 12:54:07 +0200 |
| commit | 543e3d051731ef50f7420d03dd03849054925f32 (patch) | |
| tree | c9a9224f125730ebd2daabd7d44441a21c24c232 | |
| parent | cb3a2fecf94c658448a6886529efc417f8d8dd39 (diff) | |
| download | chouette-core-543e3d051731ef50f7420d03dd03849054925f32.tar.bz2 | |
Refactor NetexImport creation6360-workbenchimport-display
And associates error message to the parent importer
| -rw-r--r-- | app/controllers/api/v1/netex_imports_controller.rb | 39 | ||||
| -rw-r--r-- | app/models/import/netex.rb | 37 | ||||
| -rw-r--r-- | config/locales/import_messages.en.yml | 3 | ||||
| -rw-r--r-- | config/locales/import_messages.fr.yml | 3 |
4 files changed, 42 insertions, 40 deletions
diff --git a/app/controllers/api/v1/netex_imports_controller.rb b/app/controllers/api/v1/netex_imports_controller.rb index 2654fa088..94600e93b 100644 --- a/app/controllers/api/v1/netex_imports_controller.rb +++ b/app/controllers/api/v1/netex_imports_controller.rb @@ -25,56 +25,19 @@ module Api def create_models find_workbench - create_referential create_netex_import end def create_netex_import attributes = netex_import_params.merge creator: "Webservice" - - attributes = attributes.merge referential_id: @new_referential.id - @netex_import = Import::Netex.new attributes @netex_import.save! - - unless @netex_import.referential - Rails.logger.info "Can't create referential for import #{@netex_import.id}: #{@new_referential.inspect} #{@new_referential.metadatas.inspect} #{@new_referential.errors.full_messages}" - @netex_import.messages.create criticity: :error, message_key: "referential_creation" - end + @netex_import.create_referential rescue ActiveRecord::RecordInvalid render json: {errors: @netex_import.errors}, status: 406 finish_action! end - def create_referential - @new_referential = - Referential.new( - name: netex_import_params['name'], - organisation_id: @workbench.organisation_id, - workbench_id: @workbench.id, - metadatas: [metadata] - ) - @new_referential.save - end - - def metadata - metadata = ReferentialMetadata.new - - if netex_import_params['file'] - netex_file = STIF::NetexFile.new(netex_import_params['file'].to_io) - frame = netex_file.frames.first - - if frame - metadata.periodes = frame.periods - - line_objectids = frame.line_refs.map { |ref| "STIF:CODIFLIGNE:Line:#{ref}" } - metadata.line_ids = @workbench.lines.where(objectid: line_objectids).pluck(:id) - end - end - - metadata - end - def netex_import_params params .require('netex_import') diff --git a/app/models/import/netex.rb b/app/models/import/netex.rb index 2b0982229..83b654f81 100644 --- a/app/models/import/netex.rb +++ b/app/models/import/netex.rb @@ -10,6 +10,25 @@ class Import::Netex < Import::Base validates_presence_of :parent + def create_referential + self.referential = + Referential.new( + name: self.name, + organisation_id: workbench.organisation_id, + workbench_id: workbench.id, + metadatas: [referential_metadata] + ) + self.referential.save + unless self.referential.valid? + Rails.logger.info "Can't create referential for import #{self.id}: #{referential.inspect} #{referential.metadatas.inspect} #{referential.errors.messages}" + if referential.metadatas.all?{|m| m.line_ids.empty?} + parent.messages.create criticity: :error, message_key: "referential_creation_missing_lines", message_attributes: {referential_name: referential.name} + else + parent.messages.create criticity: :error, message_key: "referential_creation", message_attributes: {referential_name: referential.name} + end + end + end + private def iev_callback_url @@ -21,4 +40,22 @@ class Import::Netex < Import::Base referential.destroy end end + + def referential_metadata + metadata = ReferentialMetadata.new + + if self.file + netex_file = STIF::NetexFile.new(self.file.path) + frame = netex_file.frames.first + + if frame + metadata.periodes = frame.periods + + line_objectids = frame.line_refs.map { |ref| "STIF:CODIFLIGNE:Line:#{ref}" } + metadata.line_ids = workbench.lines.where(objectid: line_objectids).pluck(:id) + end + end + + metadata + end end diff --git a/config/locales/import_messages.en.yml b/config/locales/import_messages.en.yml index 184d024ff..9f370d319 100644 --- a/config/locales/import_messages.en.yml +++ b/config/locales/import_messages.en.yml @@ -4,7 +4,8 @@ en: inconsistent_zip_file: "The zip file %{source_filename} contains unexpected directories: %{spurious_dirs}, which are ignored" missing_calendar_in_zip_file: "The folder %{source_filename} lacks a calendar file" wrong_calendar_in_zip_file: "The calendar file %{source_filename} cannot be parsed, or contains inconsistant data" - referential_creation: "Le référentiel n'a pas pu être créé car un référentiel existe déjà sur les mêmes périodes et lignes" + referential_creation: "The referential %{referential_name} has not been created because another referential with the same lines and periods already exists" + referential_creation_missing_lines: "The referential %{referential_name} has not been created because no matching line has been found" 1_netexstif_2: "Le fichier %{source_filename} ne respecte pas la syntaxe XML ou la XSD NeTEx : erreur '%{error_value}' rencontré" 1_netexstif_5: "%{source_filename}-Ligne %{source_line_number}-Colonne %{source_column_number} : l'objet %{source_label} d'identifiant %{source_objectid} a une date de mise à jour dans le futur" 2_netexstif_1_1: "Le fichier commun.xml ne contient pas de frame nommée NETEX_COMMUN" diff --git a/config/locales/import_messages.fr.yml b/config/locales/import_messages.fr.yml index 32dbb9fc5..5ecef68d1 100644 --- a/config/locales/import_messages.fr.yml +++ b/config/locales/import_messages.fr.yml @@ -4,7 +4,8 @@ fr: inconsistent_zip_file: "Le fichier zip contient des repertoires non prévus : %{spurious_dirs} qui seront ignorés" missing_calendar_in_zip_file: "Le dossier %{source_filename} ne contient pas de calendrier" wrong_calendar_in_zip_file: "Le calendrier contenu dans %{source_filename} contient des données incorrectes ou incohérentes" - referential_creation: "Le référentiel n'a pas pu être créé car un référentiel existe déjà sur les mêmes périodes et lignes" + referential_creation: "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" + referential_creation_missing_lines: "Le référentiel %{referential_name} n'a pas pu être créé car aucune ligne ne correspond" 1_netexstif_2: "Le fichier %{source_filename} ne respecte pas la syntaxe XML ou la XSD NeTEx : erreur '%{error_value}' rencontré" 1_netexstif_5: "%{source_filename}-Ligne %{source_line_number}-Colonne %{source_column_number} : l'objet %{source_label} d'identifiant %{source_objectid} a une date de mise à jour dans le futur" 2_netexstif_1_1: "Le fichier commun.xml ne contient pas de frame nommée NTEX_COMMUN" |
