private bool ChangeManufacturerModel(MobilePlantComponentDataAdapter adapter, MobilePlantComponent newComponent) { //CHANGING Manufacturer if (!string.IsNullOrEmpty(adapter.Manufacturer)) { if (adapter.Manufacturer.ToLower() != "null") { Manufacturer manufactuer = (from x in mExistingManufacturers where x.Name.ToLower() == adapter.Manufacturer.ToLower() select x).FirstOrDefault(); if (manufactuer == null) { RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage("Manufacturer", adapter.Manufacturer, adapter.RowNumber))); return false; } newComponent.Manufacturer = manufactuer; newComponent.ManufacturerId = manufactuer.Id; if (adapter.Model.ToLower() != "null") { //CHANGING Model if (!string.IsNullOrEmpty(adapter.Model)) { Model model = (from x in manufactuer.Models where x.Name.ToLower() == adapter.Model.ToLower() select x).FirstOrDefault(); if (model == null) { RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage("Model", adapter.Model, adapter.RowNumber))); return false; } newComponent.Model = model; newComponent.ModelId = model.Id; } } else { newComponent.Model = null; newComponent.ModelId = null; } } else { newComponent.Manufacturer = null; newComponent.ManufacturerId = null; newComponent.Model = null; newComponent.ModelId = null; } } return false; }
public override DbImportResult Import(bool canCreateProperties = false) { DbImportResult = new DbImportResult(); if (MetaData.ImportType != CommonUtils.ImportType.CreateMobilePlantComponent && MetaData.ImportType != CommonUtils.ImportType.UpdateMobilePlantComponent) { DbImportResult.ErrorMessages.Add(IMPORT_TYPE_NOT_COMPATIBLE); return DbImportResult; } mExistingEquipments = Cee.MobilePlants.ToList(); mExistingComponents = (from x in Cee.MobilePlantComponents .Include("MobilePlantComponentType") select x).ToList(); mExistingEquipmentProperties = Cee.MobilePlantProperties.ToList(); mExistingEquipmentComponentTypes = Cee.MobilePlantComponentTypes.ToList(); mExistingEquipmentComponentTypeProperty = (from x in Cee.MobilePlantComponentTypeProperties.Include("MobilePlantComponentType") select x).ToList(); mExistingPropertyValues = Cee.MobilePlantPropertyValues.ToList(); mExistingManufacturers = (from x in Cee.Manufacturers.Include("Models") where x.EquipmentTypeId == (int)CommonUtils.EquipmentTypeCode.MOB select x).ToList(); CanCreateProperties = canCreateProperties; //Include Headers so we can match up the component properties (HDR=YES). string connString = BuildConnectionString(MetaData.FullFileName); using (var excelConn = new OleDbConnection(connString)) { IList<MobilePlantComponentDataAdapter> importData = new List<MobilePlantComponentDataAdapter>(); try { using (var cmd = new OleDbCommand()) { cmd.CommandTimeout = 600; cmd.Connection = excelConn; cmd.CommandText = ComponentCommandText; excelConn.Open(); List<string> spreadSheetPropertyNames = GetPropertyColumnHeadersFromDataSet(cmd); if (spreadSheetPropertyNames.Count != spreadSheetPropertyNames.Distinct().Count()) { DbImportResult.ErrorMessages.Add(DuplicatePropertyNamesExistMessage()); return DbImportResult; } if (!WorkSheetCheckColumnNamesAreValid<BaseComponentColumn>(GetColumnHeadersFromDataSet(cmd, (int)BaseComponentColumn.Model))) { DbImportResult.ErrorMessages.Add(ExcelWorkSheetColumnsNotValidMessage()); return DbImportResult; } using (OleDbDataReader dr = cmd.ExecuteReader()) { int rowNumber = 1; while (dr.Read()) { rowNumber++; var adapter = new MobilePlantComponentDataAdapter(dr, mExistingEquipmentProperties, spreadSheetPropertyNames, rowNumber, canCreateProperties, MetaData.ImportType); if (!adapter.IsValid()) { foreach (string errorMessage in adapter.ErrorMessages) { RaiseMessage(CommonUtils.MessageType.Error, errorMessage); } } else { importData.Add(adapter); } } excelConn.Close(); } } if (importData.Count == 0) { RaiseMessage(CommonUtils.MessageType.Warning, NoDataFoundForWorkSheetMessage()); } string operation = "Addded"; if (MetaData.ImportType == CommonUtils.ImportType.CreateMobilePlantComponent) { InsertData(importData); } else if (MetaData.ImportType == CommonUtils.ImportType.UpdateMobilePlantComponent) { UpdateData(importData); operation = "Updated"; } foreach (MobilePlantComponent result in mSavedResults) { RaiseMessage(CommonUtils.MessageType.Added, string.Format("{0} Component '{1}' from worksheet {2}.", operation, result.Name, WorkSheetName)); } ImportedCount = mSavedResults.Count; DbImportResult.ImportedCount = ImportedCount; return DbImportResult; } catch (OleDbException ex) { DbImportResult.ErrorMessages.Add(ex.ToString()); return DbImportResult; } finally { if (excelConn.State == ConnectionState.Open) { excelConn.Close(); } } } }
private void BuildPropertyValues(MobilePlantComponent componentIn, MobilePlantComponentDataAdapter adapter) { foreach (var pair in adapter.PropertyValues) { //DOES PROPERTY EXIST? MobilePlantProperty property = (from x in mExistingEquipmentProperties where x.Name.ToLower() == pair.Key.ToLower() select x).FirstOrDefault(); if (property == null) { if (CanCreateProperties) { property = new MobilePlantProperty { Name = pair.Key, DefaultValue = pair.Value, Description = " (created by importer)." }; mExistingEquipmentProperties.Add(property); //update cache } else { //ERROR! RaiseMessage(CommonUtils.MessageType.Error, string.Format("WorkSheet '{0}' Row '{1} Tag {2} Component Name {3}' : The property does not exist.", WorkSheetName, adapter.RowNumber, adapter.Tag, adapter.ComponentName)); continue; } } //CHECK MobilePlantComponentTypeProperty Exists MobilePlantComponentTypeProperty equipmentComponentTypeProperty = null; if (mExistingEquipmentComponentTypeProperty.Any()) { equipmentComponentTypeProperty = (from x in mExistingEquipmentComponentTypeProperty where x.MobilePlantComponentType.Name.ToLower() == componentIn.MobilePlantComponentType.Name.ToLower() && x.MobilePlantProperty.Name.ToLower() == property.Name.ToLower() select x).FirstOrDefault(); } if (equipmentComponentTypeProperty == null) { if (CanCreateProperties) { //CREATE JOIN ROW equipmentComponentTypeProperty = new MobilePlantComponentTypeProperty(); equipmentComponentTypeProperty.MobilePlantComponentType = componentIn.MobilePlantComponentType; //note: set the object! equipmentComponentTypeProperty.MobilePlantProperty = property; //not set the object! mExistingEquipmentComponentTypeProperty.Add(equipmentComponentTypeProperty); //update cache } else { //ERROR! RaiseMessage(CommonUtils.MessageType.Warning, string.Format("WorkSheet '{0}' Row '{1} Tag {2} Component Type {3}' : The property {4} does not belong to the Component Type.", WorkSheetName, adapter.RowNumber, adapter.Tag, componentIn.MobilePlantComponentType.Name, property.Name)); continue; } } property.MobilePlantComponentTypeProperties.Add(equipmentComponentTypeProperty); //CHECK PROPERTYVALUE EXISTS MobilePlantPropertyValue propertyValue = null; if (mExistingPropertyValues.Any()) { propertyValue = (from x in mExistingPropertyValues where x.MobilePlantComponent.Name.ToLower() == componentIn.Name.ToLower() && x.MobilePlantProperty.Name.ToLower() == property.Name.ToLower() select x).FirstOrDefault(); } if (propertyValue == null) { propertyValue = new MobilePlantPropertyValue(); propertyValue.MobilePlantComponent = componentIn; propertyValue.MobilePlantProperty = property; mExistingPropertyValues.Add(propertyValue); //update cache } //set value if (!string.IsNullOrEmpty(pair.Value)) { propertyValue.Value = pair.Value.ChangeNullToEmptyString(); } componentIn.MobilePlantPropertyValues.Add(propertyValue); } }