private void UpdateDynamicProperties(TuningPropertiesDataAdapter adapter, ControlSystemComponent matchingComponent, int i)
        {
            foreach (DynamicProperty dynamicProperty in adapter.DynamicProperties.Where(x => !string.IsNullOrEmpty(x.PropertyValue)))
            {
                ControlSystemTuningProperty matchProperty = (from x in Cee.ControlSystemTuningProperties where string.Compare(x.Name, dynamicProperty.PropertyName, true, CultureInfo.CurrentCulture) == 0 select x).FirstOrDefault();

                if (matchProperty == null)
                {
                    RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage("ControlSystemTuningProperty", dynamicProperty.PropertyName, i + 1)));
                    continue;
                }

                //is this property assocaited with the comp type?
                ControlSystemComponentTypeTuningProperty matchCompTypeProperty = (from x in Cee.ControlSystemComponentTypeTuningProperties
                                                                                  where x.ControlSystemTuningPropertyId == matchProperty.Id
                                                                                  && x.ControlSystemComponentTypeId == matchingComponent.ControlSystemComponentTypeId
                                                                                  select x).FirstOrDefault();

                if (matchCompTypeProperty == null)
                {
                    string controlSystemTuningPropertyName = (from x in Cee.ControlSystemTuningProperties where x.Id == matchProperty.Id select x.Name).FirstOrDefault();
                    var controlSystemTypeName = (from x in Cee.ControlSystemComponentTypes where x.Id == matchingComponent.ControlSystemComponentTypeId select x.Name).FirstOrDefault();
                    string message = string.Format("Missing a entry in Database for the Table 'ControlSystemComponentTypeTuningProperty' Where ControlSystemComponentTyped = '{0}'({1}) And ControlSystemTuningPropertyId = '{2}' ({3}). Row {4}.", matchingComponent.ControlSystemComponentTypeId, controlSystemTypeName, matchProperty.Id, controlSystemTuningPropertyName, i);
                    RaiseMessage(CommonUtils.MessageType.Error, message);
                    continue;
                }

                ControlSystemTuningPropertyValue matchSystemPropertyValue = (from x in Cee.ControlSystemTuningPropertyValues
                                                                             where (x.ControlSystemTuningPropertyId == matchProperty.Id) && (x.ControlSystemComponentId == matchingComponent.Id)
                                                                             select x).FirstOrDefault();

                if (dynamicProperty.PropertyValue.ToLower().Trim() == NULLTEXT)
                {
                    if (matchSystemPropertyValue == null)
                    {
                        //do nothing
                        continue;
                    }

                    //delete
                    Cee.ControlSystemTuningPropertyValues.Remove(matchSystemPropertyValue);
                }
                else
                {
                    if (PropertyValueToPropertyTypeMismatched(matchProperty.PropertyListId, matchProperty.Type, dynamicProperty, i + 1)) { continue; }

                    if (matchSystemPropertyValue == null)
                    {
                        //create
                        matchSystemPropertyValue = new ControlSystemTuningPropertyValue
                        {
                            ControlSystemTuningPropertyId = matchProperty.Id,
                            ControlSystemComponentId = matchingComponent.Id,
                            Value = dynamicProperty.PropertyValue,
                            VerifiedUserDate = string.Format("{0} by {1}", DateTime.Now.ToString(@"dd/MM/yyyy hh:mm"), mUser.FirstLastName)
                        };
                        Cee.ControlSystemTuningPropertyValues.Add(matchSystemPropertyValue);
                    }
                    else
                    {
                        //update
                        matchSystemPropertyValue.Value = dynamicProperty.PropertyValue;
                        matchSystemPropertyValue.VerifiedUserDate = string.Format("{0} by {1}", DateTime.Now.ToString(@"dd/MM/yyyy hh:mm"), mUser.FirstLastName);
                    }
                }

                PropertyNameComponentNamePair pair = new PropertyNameComponentNamePair
                {
                    ComponentName = matchingComponent.Name,
                    PropertyName = matchProperty.Name,
                    Value = dynamicProperty.PropertyValue
                };

                mSavedResults.Add(pair);
            }
        }
        public override DbImportResult Import(bool canCreateProperties = false)
        {
            DbImportResult = new DbImportResult();

            if (MetaData.ImportType != CommonUtils.ImportType.CreateTuningProperties && MetaData.ImportType != CommonUtils.ImportType.UpdateTuningProperties)
            {
                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<TuningPropertiesDataAdapter> importData = new List<TuningPropertiesDataAdapter>();

            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, TuningPropertyColumn.ControlSystemName);

                        excelConn.Open();

                        if (!WorkSheetCheckColumnNamesAreValid<TuningPropertyColumn>(GetColumnHeadersFromDataSet(cmd, (int)TuningPropertyColumn.ComponentName)))
                        {
                            DbImportResult.ErrorMessages.Add(ExcelWorkSheetColumnsNotValidMessage());
                            return DbImportResult;
                        }

                        const int STARTCOLUMN = (int)TuningPropertyColumn.ComponentName;
                        List<string> dynamicProperyNames = GetDynamicProperties(cmd, STARTCOLUMN);

                        int k = 1;
                        using (OleDbDataReader dr = cmd.ExecuteReader())
                        {
                            while (dr.Read())
                            {
                                k++;

                                try
                                {
                                    var adapter = new TuningPropertiesDataAdapter(dr, dynamicProperyNames, STARTCOLUMN + 1);
                                    importData.Add(adapter);
                                }
                                catch (Exception ex)
                                {
                                    DbImportResult.ErrorMessages.Add(string.Format("EngineeringPropertyDataAdapter row {0} - {1}", k, ex.Message));
                                }
                            }

                            excelConn.Close();
                        }
                    }

                    if (MetaData.ImportType == CommonUtils.ImportType.CreateTuningProperties)
                    {
                        InsertData(importData);
                    }
                    else if (MetaData.ImportType == CommonUtils.ImportType.UpdateTuningProperties)
                    {
                        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 void AddDynamicProperties(TuningPropertiesDataAdapter adapter, ControlSystemComponent matchingComponent, int i)
        {
            foreach (DynamicProperty dynamicProperty in adapter.DynamicProperties)
            {
                ControlSystemTuningProperty matchProperty = (from x in Cee.ControlSystemTuningProperties where string.Compare(x.Name, dynamicProperty.PropertyName, true, CultureInfo.CurrentCulture) == 0 select x).FirstOrDefault();

                if (matchProperty == null)
                {
                    RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage("ControlSystemTuningProperty", dynamicProperty.PropertyName, i + 1)));
                    continue;
                }

                ControlSystemComponentTypeTuningProperty matchCompTypeProperty = (from x in Cee.ControlSystemComponentTypeTuningProperties
                                                                                  where x.ControlSystemTuningPropertyId == matchProperty.Id
                                                                                  && x.ControlSystemComponentTypeId == matchingComponent.ControlSystemComponentTypeId
                                                                                  select x).FirstOrDefault();

                if (matchCompTypeProperty == null)
                {
                    var controlSystemPropertyName = (from x in Cee.ControlSystemTuningProperties where x.Id == matchProperty.Id select x.Name).FirstOrDefault();
                    var controlSystemTypeName = (from x in Cee.ControlSystemComponentTypes where x.Id == matchingComponent.ControlSystemComponentTypeId select x.Name).FirstOrDefault();

                    string message = string.Format("Missing a entry in Database for the Table 'ControlSystemComponentTypeTuningProperty' Where ControlSystemComponentTyped = '{0}'({1}) And ControlSystemTuningPropertyId = '{2}' ({3}). Row {4}.", matchingComponent.ControlSystemComponentTypeId, controlSystemTypeName, matchProperty.Id, controlSystemPropertyName, i);
                    RaiseMessage(CommonUtils.MessageType.Error, message);
                    continue;
                }

                //check for duplicates properties on this component (not allowed).
                var duplicateCount = (from x in  Cee.ControlSystemTuningPropertyValues
                                      where x.ControlSystemComponentId == matchingComponent.ControlSystemComponentTypeId
                                         && x.ControlSystemTuningPropertyId == matchProperty.Id
                                      select x.Id).Count();

                if (duplicateCount > 0)
                {
                    var propertyName = matchProperty.Name;
                    var componentName = matchingComponent.Name;

                    string message = string.Format("An entry already exists in Database for the Table 'ControlSystemComponentTypeTuningProperty' Where ControlSystemComponentId = '{0}'({1}) And ControlSystemTuningPropertyId = '{2}' ({3}). Row {4}.", matchingComponent.ControlSystemComponentTypeId, componentName, matchProperty.Id, propertyName, i);
                    RaiseMessage(CommonUtils.MessageType.Error, message);
                    continue;
                }

                if (PropertyValueToPropertyTypeMismatched(matchProperty.PropertyListId, matchProperty.Type, dynamicProperty, i + 1)) { continue; }

                ControlSystemTuningPropertyValue propertyValue = new ControlSystemTuningPropertyValue
                {
                    ControlSystemTuningPropertyId = matchProperty.Id,
                    ControlSystemComponentId = matchingComponent.Id,
                    Value = dynamicProperty.PropertyValue,
                    VerifiedUserDate = string.Format("{0} by {1}", DateTime.Now.ToString(@"dd/MM/yyyy hh:mm"), mUser.FirstLastName)
                };

                matchingComponent.ControlSystemTuningPropertyValues.Add(propertyValue);

                PropertyNameComponentNamePair pair = new PropertyNameComponentNamePair
                {
                    ComponentName = matchingComponent.Name,
                    PropertyName = matchProperty.Name,
                    Value = dynamicProperty.PropertyValue
                };

                mSavedResults.Add(pair);
            }
        }