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);
            }
        }
        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 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);
            }
        }
        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;
        }