public override DbImportResult Import(bool canCreateProperties = false) { DbImportResult = new DbImportResult(); if (MetaData.ImportType != CommonUtils.ImportType.CreateInterlocks && MetaData.ImportType != CommonUtils.ImportType.UpdateInterlocks) { DbImportResult.ErrorMessages.Add(IMPORT_TYPE_NOT_COMPATIBLE); return DbImportResult; } CanCreateProperties = false; mUser = (from x in Cee.Users where x.Id == MetaData.UserId select x).FirstOrDefault(); if (mUser == null) { DbImportResult.ErrorMessages.Add(string.Format(BuildItemNotFoundInDatabaseMessage("UserId", MetaData.UserId.ToString()))); return DbImportResult; } IList<InterlockDataAdapter> importData = new List<InterlockDataAdapter>(); string connString = BuildConnectionString(MetaData.FullFileName); using (var excelConn = new OleDbConnection(connString)) { try { using (var cmd = new OleDbCommand()) { cmd.CommandTimeout = 600; cmd.Connection = excelConn; cmd.CommandText = string.Format(@"SELECT * FROM [{0}$] WHERE [{1}] IS NOT NULL", WorkSheetName, InterlockColumn.ControlSystemName); excelConn.Open(); if (!WorkSheetCheckColumnNamesAreValid<InterlockColumn>(GetColumnHeadersFromDataSet(cmd, (int) InterlockColumn.Description))) { DbImportResult.ErrorMessages.Add(ExcelWorkSheetColumnsNotValidMessage()); return DbImportResult; } const int STARTCOLUMN = (int) InterlockColumn.Description; List<string> dynamicProperyNames = GetDynamicProperties(cmd, STARTCOLUMN); int k = 1; using (OleDbDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { k++; try { var adapter = new InterlockDataAdapter(dr, dynamicProperyNames, STARTCOLUMN + 1); importData.Add(adapter); } catch (Exception ex) { DbImportResult.ErrorMessages.Add(string.Format("InterlockDataAdapter row {0} - {1}", k, ex.Message)); } } excelConn.Close(); } } if (MetaData.ImportType == CommonUtils.ImportType.CreateInterlocks) { InsertData(importData); } else if (MetaData.ImportType == CommonUtils.ImportType.UpdateInterlocks) { UpdateData(importData); } DbImportResult.ImportedCount = mSavedResults.Count; return DbImportResult; } catch (OleDbException ex) { DbImportResult.ErrorMessages.Add(ex.ToString()); return DbImportResult; } finally { if (excelConn.State == ConnectionState.Open) { excelConn.Close(); } } } }
private List<PropertyNameComponentNamePair> UpdateDynamicProperties(InterlockDataAdapter adapter, ControlSystem matchingControl, Interlock matchingInterlock, int i, out bool failed) { failed = false; var results = new List<PropertyNameComponentNamePair>(); foreach (DynamicProperty dynamicProperty in adapter.DynamicProperties) { //MATCH THE PROPERTY InterlockProperty matchProperty = (from x in Cee.InterlockProperties where string.Compare(x.Name, dynamicProperty.PropertyName, true, CultureInfo.CurrentCulture) == 0 select x).FirstOrDefault(); if (matchProperty == null) { RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage("InterlockProperty", dynamicProperty.PropertyName, i + 1))); failed = true; continue; } //CHECK ASSOCIATEION TO INTERLOCK TYPE InterlockTypeProperty matchCompTypeProperty = (from x in Cee.InterlockTypeProperties where x.InterlockPropertyId == matchProperty.Id && x.InterlockTypeId == matchingInterlock.InterlockTypeId select x).FirstOrDefault(); if (matchCompTypeProperty == null) { InterlockType matchingInterlockType = (from x in Cee.InterlockTypes where x.Id == matchingInterlock.InterlockTypeId select x).FirstOrDefault(); string message = string.Format("Missing a entry in Database for the Table 'InterlockTypeProperty' Where InterlockPropertyId = '{0}'({1}) And InterlockTypeId = '{2}' ({3}). Row {4}.", matchProperty.Id, matchProperty.Name, matchingInterlockType.Id, matchingInterlockType.Name, i); RaiseMessage(CommonUtils.MessageType.Error, message); failed = true; continue; } //CHECK IF THE PROP VALUE ALREADY EXISTS InterlockPropertyValue propertyValue = (from x in Cee.InterlockPropertyValues where (x.InterlockId == matchingInterlock.Id) && (x.InterlockPropertyId == matchProperty.Id) select x).FirstOrDefault(); if (dynamicProperty.PropertyValue.ToLower().Trim() == NULLTEXT) { if (propertyValue == null) { //do nothing continue; } //delete Cee.InterlockPropertyValues.Remove(propertyValue); } else { if (PropertyValueToPropertyTypeMismatched(matchProperty.PropertyListId, matchProperty.Type, dynamicProperty, i + 1)) { continue; } if (propertyValue == null) { //create propertyValue = new InterlockPropertyValue { InterlockPropertyId = matchProperty.Id, InterlockId = matchingInterlock.Id, Value = dynamicProperty.PropertyValue, VerifiedUserDate = string.Format("{0} by {1}", DateTime.Now.ToString(@"dd/MM/yyyy hh:mm"), mUser.FirstLastName) }; Cee.InterlockPropertyValues.Add(propertyValue); } else { //update propertyValue.Value = dynamicProperty.PropertyValue; propertyValue.VerifiedUserDate = string.Format("{0} by {1}", DateTime.Now.ToString(@"dd/MM/yyyy hh:mm"), mUser.FirstLastName); } } var pair = new PropertyNameComponentNamePair { ComponentName = matchingControl.Name, PropertyName = matchProperty.Name, Value = dynamicProperty.PropertyValue }; results.Add(pair); } return results; }
private List<PropertyNameComponentNamePair> AddDynamicProperties(InterlockDataAdapter adapter, InterlockType interlockType, Interlock interlock, string controlSystemName, int i, out bool failed) { failed = false; var results = new List<PropertyNameComponentNamePair>(); foreach (DynamicProperty dynamicProperty in adapter.DynamicProperties) { //InterlockProperty InterlockProperty matchProperty = (from x in Cee.InterlockProperties where string.Compare(x.Name, dynamicProperty.PropertyName, true, CultureInfo.CurrentCulture) == 0 select x).FirstOrDefault(); if (matchProperty == null) { RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage("InterlockProperty", dynamicProperty.PropertyName, i + 1))); failed = true; continue; } //InterlockTypeProperty InterlockTypeProperty matchTypeProperty = (from x in Cee.InterlockTypeProperties where x.InterlockPropertyId == matchProperty.Id && x.InterlockTypeId == interlockType.Id select x).FirstOrDefault(); if (matchTypeProperty == null) { string message = string.Format("Missing a entry in Database for the Table 'InterlockTypeProperty' Where InterlockTypeId = '{0}'({1}) And InterlockPropertyId = '{2}' ({3}). Row {4}.", interlockType.Id, interlockType.Name, matchProperty.Id, matchProperty.Name, i); RaiseMessage(CommonUtils.MessageType.Error, message); failed = true; continue; } if (PropertyValueToPropertyTypeMismatched(matchProperty.PropertyListId, matchProperty.Type, dynamicProperty, i + 1)) { continue; } //NEW var propertyValue = new InterlockPropertyValue { InterlockPropertyId = matchProperty.Id, Value = dynamicProperty.PropertyValue, VerifiedUserDate = string.Format("{0} by {1}", DateTime.Now.ToString(@"dd/MM/yyyy hh:mm"), mUser.FirstLastName) }; //add to interlock interlock.InterlockPropertyValues.Add(propertyValue); var pair = new PropertyNameComponentNamePair { ComponentName = string.Format("Control:{0}- Interlock:{1}", controlSystemName, interlockType.Name), PropertyName = matchProperty.Name, Value = dynamicProperty.PropertyValue }; results.Add(pair); } return results; }