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

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

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

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

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

                ControlSystemPropertyValue matchSystemPropertyValue = (from x in Cee.ControlSystemPropertyValues
                                                                       where (x.ControlSystemPropertyId == matchProperty.Id) && (x.ControlSystemComponentId == matchingComponent.Id)
                                                                       select x).FirstOrDefault();

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

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

                    if (matchSystemPropertyValue == null)
                    {
                        //create
                        matchSystemPropertyValue = new ControlSystemPropertyValue
                        {
                            ControlSystemPropertyId = 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.ControlSystemPropertyValues.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);
                    }
                }

                var 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.CreateEngineeringProperties && MetaData.ImportType != CommonUtils.ImportType.UpdateEngineeringProperties)
            {
                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(CultureInfo.CurrentCulture))));

                return DbImportResult;
            }

            IList<EngineeringPropertyDataAdapter> importData = new List<EngineeringPropertyDataAdapter>();

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

                        excelConn.Open();

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

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

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

                                try
                                {
                                    var adapter = new EngineeringPropertyDataAdapter(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.CreateEngineeringProperties)
                    {
                        InsertData(importData);
                    }
                    else if (MetaData.ImportType == CommonUtils.ImportType.UpdateEngineeringProperties)
                    {
                        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(EngineeringPropertyDataAdapter adapter, ControlSystemComponent matchingComponent, int i)
        {
            foreach (DynamicProperty dynamicProperty in adapter.DynamicProperties)
            {
                ControlSystemProperty matchProperty = (from x in Cee.ControlSystemProperties where string.Compare(x.Name, dynamicProperty.PropertyName, true, CultureInfo.CurrentCulture) == 0 select x).FirstOrDefault();

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

                ControlSystemComponentTypeProperty matchCompTypeProperty = (from x in Cee.ControlSystemComponentTypeProperties
                                                                            where x.ControlSystemPropertyId == matchProperty.Id && x.ControlSystemComponentTypeId == matchingComponent.ControlSystemComponentTypeId
                                                                            select x).FirstOrDefault();

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

                    string message = string.Format("Missing a entry in Database for the Table 'ControlSystemComponentTypeProperty' Where ControlSystemComponentTyped = '{0}'({1}) And ControlSystemPropertyId = '{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).
                int duplicateCount = (from x in Cee.ControlSystemPropertyValues where x.ControlSystemComponentId == matchingComponent.ControlSystemComponentTypeId && x.ControlSystemPropertyId == matchProperty.Id select x.Id).Count();

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

                    string message = string.Format("An entry already exists in Database for the Table 'ControlSystemPropertyValue' Where ControlSystemComponentId = '{0}'({1}) And ControlSystemPropertyId = '{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;
                }

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

                matchingComponent.ControlSystemPropertyValues.Add(propertyValue);

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

                mSavedResults.Add(pair);
            }
        }