using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Drawing; using System.IO; using Core.Common.Base; using log4net; using Ringtoets.Piping.Data; using Ringtoets.Piping.IO; using Ringtoets.Piping.IO.Exceptions; using WtiFormsResources = Ringtoets.Piping.Forms.Properties.Resources; using ApplicationResources = Ringtoets.Piping.Plugin.Properties.Resources; namespace Ringtoets.Piping.Plugin.FileImporter { /// /// Imports .soil files (SqlLite database files) created with the DSoilModel application. /// public class PipingSoilProfilesImporter : IFileImporter { private readonly ILog log = LogManager.GetLogger(typeof(PipingSoilProfilesImporter)); public string Name { get { return WtiFormsResources.PipingSoilProfilesCollection_DisplayName; } } public string Category { get { return ApplicationResources.Wti_application_name; } } public Bitmap Image { get { return WtiFormsResources.PipingSoilProfileIcon; } } public IEnumerable SupportedItemTypes { get { return new[] { typeof(ObservableList) }; } } public bool CanImportOnRootLevel { get { return false; } } public string FileFilter { get { return String.Format("{0} {1} (*.soil)|*.soil", WtiFormsResources.PipingSoilProfilesCollection_DisplayName, ApplicationResources.Soil_file_name); } } public string TargetDataDirectory { get; set; } public bool ShouldCancel { get; set; } public ImportProgressChangedDelegate ProgressChanged { get; set; } public bool OpenViewAfterImport { get { return false; } } public bool CanImportOn(object targetObject) { return targetObject is ObservableList; } public object ImportItem(string path, object target = null) { var importResult = ReadSoilProfiles(path); if (!importResult.CriticalErrorOccurred) { if (!ShouldCancel) { AddImportedDataToModel(target, importResult); } else { HandleUserCancellingImport(); } } return target; } private PipingReadResult ReadSoilProfiles(string path) { NotifyProgress(ApplicationResources.PipingSoilProfilesImporter_Reading_database, 1, 1); try { using (var soilProfileReader = new PipingSoilProfileReader(path)) { return GetProfileReadResult(path, soilProfileReader); } } catch (PipingSoilProfileReadException e) { HandleException(path, e); } catch (FileNotFoundException e) { HandleException(path, e); } catch (ArgumentException e) { HandleException(path, e); } return new PipingReadResult(true); } private void HandleException(string path, Exception e) { var message = string.Format(ApplicationResources.PipingSoilProfilesImporter_Critical_error_reading_File_0_Cause_1_, path, e.Message); log.Error(message); } private PipingReadResult GetProfileReadResult(string path, PipingSoilProfileReader soilProfileReader) { var totalNumberOfSteps = soilProfileReader.Count; var currentStep = 1; var profiles = new Collection(); while (soilProfileReader.HasNext) { if (ShouldCancel) { return new PipingReadResult(false); } try { NotifyProgress(ApplicationResources.PipingSoilProfilesImporter_ReadingSoilProfiles, currentStep++, totalNumberOfSteps); profiles.Add(soilProfileReader.ReadProfile()); } catch (PipingSoilProfileReadException e) { var message = string.Format(ApplicationResources.PipingSoilProfilesImporter_ReadSoilProfiles_File_0_Message_1_, path, e.Message); log.Error(message); } catch (CriticalFileReadException e) { var message = string.Format(ApplicationResources.PipingSoilProfilesImporter_ReadSoilProfiles_File_0_Message_1_, path, e.Message); log.Error(message); return new PipingReadResult(true); } } return new PipingReadResult(false) { ImportedItems = profiles }; } private void AddImportedDataToModel(object target, PipingReadResult imported) { var targetCollection = (ObservableList)target; int totalProfileCount = imported.ImportedItems.Count; NotifyProgress(ApplicationResources.PipingSoilProfilesImporter_Adding_imported_data_to_model, totalProfileCount, totalProfileCount); targetCollection.AddRange(imported.ImportedItems); targetCollection.NotifyObservers(); } private void NotifyProgress(string currentStepName, int currentStep, int totalNumberOfSteps) { if (ProgressChanged != null) { ProgressChanged(currentStepName, currentStep, totalNumberOfSteps); } } private void HandleUserCancellingImport() { log.Info(ApplicationResources.PipingSoilProfilesImporter_ImportItem_Import_cancelled); } } }