public AddEditExistingControlSystemComponentAlarmPropertyViewModel(ControlSystemComponentType controlSystemEquipmentComponentType)
        {
            //new
            mControlSystemComponentTypeAlarmProperty = new ControlSystemComponentTypeAlarmProperty { ComponentTypeId = controlSystemEquipmentComponentType.Id };

            OkButtonCommand = new DelegateCommand<object>(OkButtonHander, CanModify);
            CancelButtonCommand = new DelegateCommand<object>(CancelButtonHander, x => true);

            CmsWebServiceClient cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);
            Properties = new List<ControlSystemAlarmProperty>();

            //Load Component types
            EventHandler<GetAllControlSystemAlarmPropertiesCompletedEventArgs> getAllControlSystemAlarmPropertiesCompleted = (s, allEventArgs) =>
                                              {
                                                  cmsWebServiceClient.GetControlSystemComponentTypeAlarmPropertiesCompleted +=
                                                      (s1, e1)
                                                      =>
                                                      {
                                                          List<int> listOfAssignedPropertyIds = new List<int>();
                                                          e1.Result.ForEach(x => listOfAssignedPropertyIds.Add(x.AlarmPropertyId));

                                                          foreach (var componentProperty in allEventArgs.Result)
                                                          {
                                                              if (!listOfAssignedPropertyIds.Contains(componentProperty.Id))
                                                              {
                                                                  Properties.Add(componentProperty);
                                                              }
                                                          }

                                                          if (Properties.Count > 0)
                                                          {
                                                              mControlSystemComponentTypeAlarmProperty.AlarmPropertyId = Properties[0].Id;
                                                              mControlSystemComponentTypeAlarmProperty.ControlSystemAlarmProperty = Properties[0];
                                                          }
                                                          Loaded();
                                                      };

                                                  cmsWebServiceClient.GetControlSystemComponentTypeAlarmPropertiesAsync(controlSystemEquipmentComponentType.Id);
                                              };
            cmsWebServiceClient.GetAllControlSystemAlarmPropertiesCompleted += getAllControlSystemAlarmPropertiesCompleted;
            cmsWebServiceClient.GetAllControlSystemAlarmPropertiesAsync();
        }
        public AddEditExistingControlSystemComponentAlarmPropertyViewModel(int componentTypePropertyId)
        {
            //edit

            OkButtonCommand = new DelegateCommand<object>(OkButtonHander, CanModify);
            CancelButtonCommand = new DelegateCommand<object>(CancelButtonHander, x => true);

            CmsWebServiceClient cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);
            Properties = new List<ControlSystemAlarmProperty>();

            //Load ControlSystem Properties types
            EventHandler<GetAllControlSystemAlarmPropertiesCompletedEventArgs>
                fetchCompleted = (s, e) =>
                                     {
                                         Properties = e.Result;
                                         cmsWebServiceClient.GetControlSystemComponentTypeAlarmPropertyCompleted +=
                                             (s2, e2) =>
                                             {
                                                 if (Properties.Count > 0)
                                                 {
                                                     SelectedProperty = (from x in Properties
                                                                         where x.Id == e2.Result.ComponentTypeId
                                                                         select x).FirstOrDefault();
                                                 }

                                                 mControlSystemComponentTypeAlarmProperty = e2.Result;
                                                 Loaded();
                                             };
                                         cmsWebServiceClient.GetControlSystemComponentTypeAlarmPropertyAsync(componentTypePropertyId);
                                     };

            cmsWebServiceClient.GetAllControlSystemAlarmPropertiesCompleted += fetchCompleted;
            cmsWebServiceClient.GetAllControlSystemAlarmPropertiesAsync();
        }
        private void AddExistingControlSystemComponentTypeAlarmProperty(NodeView nodeView)
        {
            var controlSystemEquipmentComponentTypeId = nodeView.Parent.Id;
            var cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);

            cmsWebServiceClient.GetControlSystemComponentTypeCompleted +=
                (s, e) =>
                {
                    var dialog = new AddEditExistingControlSystemComponentAlarmPropertyDialog(e.Result);
                    dialog.Show();

                    dialog.Closed += (s1, e1) =>
                    {
                        if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
                        {
                            EventHandler<SaveControlSystemComponentTypeAlarmPropertyCompletedEventArgs> addCompleted = null;
                            addCompleted = (s2, eventArgs) =>
                            {
                                var entityResult = eventArgs.Result.EntityResult;

                                if (eventArgs.Result.HasErrors)
                                {
                                    var popup = new PopupDialog(PopupDialogType.Error, Utils.DisplayErrorMessages(eventArgs.Result.ServerErrorMessages));
                                    popup.Show();
                                    return;
                                }

                                if (entityResult != null)
                                {
                                    var child = new NodeView(nodeView)
                                    {
                                        Id = entityResult.Id,
                                        Name = dialog.ControlSystemComponentTypeAlarmProperty.ControlSystemAlarmProperty.Name,
                                        Description = dialog.ControlSystemComponentTypeAlarmProperty.ControlSystemAlarmProperty.Description,
                                        Icon = "/CmsEquipmentDatabase;component/Images/Configuration.png",
                                        Type = NodeType.ControlSystemComponentTypeAlarmProperty,
                                        HasChildren = false,
                                        SortField = entityResult.Ordinal.ToString()
                                    };
                                    if (nodeView.ChildrenLoaded)
                                    {
                                        nodeView.Children.Add(child);
                                        nodeView.Sort();
                                    }
                                }

                                cmsWebServiceClient.SaveControlSystemComponentTypeAlarmPropertyCompleted -= addCompleted;
                            };
                            cmsWebServiceClient.SaveControlSystemComponentTypeAlarmPropertyCompleted += addCompleted;

                            var systemComponentTypeTuningProperty = new ControlSystemComponentTypeAlarmProperty
                            {
                                ComponentTypeId = controlSystemEquipmentComponentTypeId,
                                AlarmPropertyId = dialog.ControlSystemComponentTypeAlarmProperty.AlarmPropertyId,
                                Ordinal = dialog.ControlSystemComponentTypeAlarmProperty.Ordinal
                            };

                            cmsWebServiceClient.SaveControlSystemComponentTypeAlarmPropertyAsync(systemComponentTypeTuningProperty);
                        }
                    };
                };
            cmsWebServiceClient.GetControlSystemComponentTypeAsync(controlSystemEquipmentComponentTypeId);
        }
Example #4
0
        private void ImportComponentTypeAlarmProperties(CmsEntities cee, OldCmsEntities old)
        {
            var sw = new Stopwatch();
            sw.Start();
            Logger.Out("ImportControlSystemComponentTypeAlarmProperties - start");

            var alarmProperties = (from x in cee.ControlSystemAlarmProperties select x).ToList();

            var oldAlarms = (from x in old.Alarms
                .Include("AlarmType")
                .Include("AlarmConsequence")
                .Include("AlarmPurpose")
                .Include("AlarmResponse")
                .Include("Element")
                .Include("Element.ElementTypical")
                             select x).ToList();

            List<KeyValuePair<int, int>> dicKeyValuePairs = LoadExistingPropertyComponentTypes(cee);

            int index = 0;
            int dup = 0;
            foreach (var oldAlarm in oldAlarms.OrderBy(x => x.Id))
            {

                //AlarmProperty
                ControlSystemAlarmProperty matchedAlarmProperty;
                if (MatchAlarmPropertyFailed(alarmProperties, oldAlarm, out matchedAlarmProperty)) continue;

                ControlSystemComponentType matchedComponentType;
                if (MatchComponentTypeFailed(cee, old, oldAlarm, out matchedComponentType)) continue;

                var kvp = new KeyValuePair<int, int>(matchedAlarmProperty.Id, matchedComponentType.Id);
                if (!dicKeyValuePairs.Contains(kvp))
                {
                    dicKeyValuePairs.Add(kvp);

                    var ctap = new ControlSystemComponentTypeAlarmProperty
                    {
                        ComponentTypeId = matchedComponentType.Id,
                        AlarmPropertyId = matchedAlarmProperty.Id,
                        Ordinal = 0
                    };

                    cee.ControlSystemComponentTypeAlarmProperties.Add(ctap);
                    index++;
                }
                else
                {
                    dup++;
                    //Logger.Out(string.Format("Skipping Alarm ID {0} - duplicate on ComponentTypeId '{1}' and AlarmPropertyId '{2}'.", oldAlarm.Id, matchedComponentType.Id, matchedAlarmProperty.Id));
                    continue;
                }

                UpdateStatus(string.Format("ComponentType AlarmProperties: Mapped Alarm ID {0} ({1}).", oldAlarm.Id, index));
            }

            Logger.Out("Saving ComponentTypeAlarmProperties...");
            cee.SaveChanges();
            sw.Stop();
            Logger.Out(string.Format("Imported {0} out of {1} ComponentTypeAlarmProperties in {2} min, {3} sec. ", index, (oldAlarms.Count - dup), sw.Elapsed.Minutes, sw.Elapsed.Seconds));
            Logger.Out("");
            Logger.Out("");
        }
Example #5
0
        //private void InsertData(IList<AlarmDataAdapter> importData)
        //{
        //    if (importData.Count == 0)
        //    {
        //        RaiseMessage(CommonUtils.MessageType.Warning, NoDataFoundForWorkSheetMessage());
        //        return;
        //    }
        //    for (int i = 0; i < importData.Count; i++)
        //    {
        //        AlarmDataAdapter adapter = importData[i];
        //        ControlSystemComponent parentComponent = (from x in Cee.ControlSystemComponents.Include("ControlSystemComponentType")
        //                                                  where x.Name.Equals(adapter.ComponentName, StringComparison.InvariantCultureIgnoreCase)
        //                                                  select x).FirstOrDefault();
        //        if (parentComponent == null)
        //        {
        //            RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage(adapter.ComponentName, adapter.RowNumber.ToString())));
        //            continue;
        //        }
        //        ControlSystemAlarmPropertyValue dbDuplicateCheck = (from x in Cee.ControlSystemAlarmPropertyValues.Include("ControlSystemAlarmProperty")
        //                                                            where x.ControlSystemAlarmProperty.Name.Equals(adapter.AlarmPropertyName, StringComparison.InvariantCultureIgnoreCase)
        //                                                            && x.ControlSystemComponentId == parentComponent.Id
        //                                                            select x).FirstOrDefault();
        //        if (dbDuplicateCheck != null)
        //        {
        //            RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildDuplicateNameExistsInDbMessage(string.Format("{0}-{1}", adapter.ComponentName, adapter.AlarmPropertyName), adapter.RowNumber)));
        //            continue;
        //        }
        //        ControlSystemAlarmPropertyValue memoryDuplicateCheck = (from x in mSavedResults
        //                                                                where x.ControlSystemAlarmProperty.Name.Equals(adapter.AlarmPropertyName, StringComparison.InvariantCultureIgnoreCase)
        //                                           && x.ControlSystemComponentId == parentComponent.Id
        //                                                                select x).FirstOrDefault();
        //        if (memoryDuplicateCheck != null)
        //        {
        //            RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildDuplicateNameExistsInExcelMessage(string.Format("{0}-{1}", adapter.ComponentName, adapter.AlarmPropertyName), adapter.RowNumber)));
        //            continue;
        //        }
        //        if (!adapter.Enabled.HasValue)
        //        {
        //            RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildPropertyEmpty(AlarmColumn.Enabled.ToString(), adapter.RowNumber)));
        //            continue;
        //        }
        //        if (!adapter.Tested.HasValue)
        //        {
        //            RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildPropertyEmpty(AlarmColumn.Tested.ToString(), adapter.RowNumber)));
        //            continue;
        //        }
        //        if (!adapter.AlarmCalcExprEnabled.HasValue)
        //        {
        //            RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildPropertyEmpty(AlarmColumn.AlarmCalcExprEnabled.ToString(), adapter.RowNumber)));
        //            continue;
        //        }
        //        ControlSystemAlarmProperty alarmProperty = new ControlSystemAlarmProperty
        //        {
        //            Name = adapter.AlarmPropertyName,
        //        };
        //        ControlSystemAlarmPropertyValue apv = new ControlSystemAlarmPropertyValue
        //        {
        //            ControlSystemComponentId = parentComponent.Id,
        //            ControlSystemAlarmProperty = alarmProperty,
        //            Enabled = adapter.Enabled.Value,
        //            Tested = adapter.Tested.Value,
        //            Reportable = adapter.Reportable.Value,
        //            ActivationExpr = adapter.ActivationExpr,
        //            AlarmCalcExpr = adapter.AlarmCalcExpr,
        //            AlarmCalcExprEnabled = adapter.AlarmCalcExprEnabled.Value,
        //            Guidance = adapter.Guidance,
        //            MaskingExpr = adapter.MaskingExpr,
        //            Notes = adapter.Notes,
        //            OffDelay = adapter.OffDelay,
        //            OnDelay = adapter.OnDelay,
        //            ModifiedDate = DateTime.Now,
        //            ModifiedUserId = MetaData.UserId,
        //        };
        //        if (!string.IsNullOrEmpty(adapter.TuningPropertyName))
        //        {
        //            var tuningProperty = (from x in Cee.ControlSystemTuningProperties where x.Name.Equals("", StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault();
        //            if (tuningProperty == null)
        //            {
        //                RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage(AlarmColumn.TuningPropertyName.ToString(), adapter.RowNumber.ToString())));
        //                continue;
        //            }
        //            apv.ControlSystemAlarmProperty.TuningPropertyId = tuningProperty.Id;
        //        }
        //        //Tested
        //        if (adapter.Tested.Value || !string.IsNullOrEmpty(adapter.TestedUser))
        //        {
        //            //ensure we have values for both.
        //            if (!adapter.TestedDate.HasValue)
        //            {
        //                RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildPropertyEmpty(AlarmColumn.TestedDate.ToString(), adapter.RowNumber)));
        //                continue;
        //            }
        //            if (string.IsNullOrEmpty(adapter.TestedUser))
        //            {
        //                RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildPropertyEmpty(AlarmColumn.TestedUser.ToString(), adapter.RowNumber)));
        //                continue;
        //            }
        //            apv.TestedDate = adapter.TestedDate.Value;
        //            apv.TestedUserId = GetUserFromText(adapter.TestedUser).Id;
        //        }
        //        //Consequence
        //        if (!string.IsNullOrEmpty(adapter.Consequence))
        //        {
        //            var match = (from x in mExistingConsequences where x.Name.Equals(adapter.Consequence, StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault();
        //            if (match == null)
        //            {
        //                RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage(AlarmColumn.Consequence.ToString(), adapter.RowNumber.ToString())));
        //                continue;
        //            }
        //            apv.ConsequenceId = match.Id;
        //        }
        //        //Purpose
        //        if (!string.IsNullOrEmpty(adapter.Purpose))
        //        {
        //            var match = (from x in mExistingPurposes where x.Name.Equals(adapter.Purpose, StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault();
        //            if (match == null)
        //            {
        //                RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage(AlarmColumn.Purpose.ToString(), adapter.RowNumber.ToString())));
        //                continue;
        //            }
        //            apv.PurposeId = match.Id;
        //        }
        //        //Priority
        //        if (!string.IsNullOrEmpty(adapter.Priority))
        //        {
        //            var match = (from x in mExistingPriorities where x.Name.Equals(adapter.Priority, StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault();
        //            if (match == null)
        //            {
        //                RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage(AlarmColumn.Priority.ToString(), adapter.RowNumber.ToString())));
        //                continue;
        //            }
        //            apv.PriorityId = match.Id;
        //        }
        //        //Response
        //        if (!string.IsNullOrEmpty(adapter.Response))
        //        {
        //            var match = (from x in mExistingResponses where x.Name.Equals(adapter.Response, StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault();
        //            if (match == null)
        //            {
        //                RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage(AlarmColumn.ResponseTime.ToString(), adapter.RowNumber.ToString())));
        //                continue;
        //            }
        //            apv.ResponseTimeId = match.Id;
        //        }
        //        var dbCompoentTypeConfigured = (from x in Cee.ControlSystemComponentTypeAlarmProperties
        //                                        where x.ComponentTypeId == parentComponent.ControlSystemComponentTypeId && x.AlarmPropertyId == apv.ControlSystemAlarmPropertyId
        //                                        select x).FirstOrDefault();
        //        if (dbCompoentTypeConfigured == null)
        //        {
        //            ControlSystemComponentTypeAlarmProperty cap = new ControlSystemComponentTypeAlarmProperty();
        //            cap.ComponentTypeId = parentComponent.ControlSystemComponentTypeId;
        //            cap.AlarmPropertyId = apv.ControlSystemAlarmPropertyId;
        //            Cee.ControlSystemComponentTypeAlarmProperties.Add(cap);
        //            string msg = string.Format("The Configuration of ComponentType '{0}' with the Alarm Property '{1}' does not exist.  The Configuration has been created. (Row Number: {2}).", parentComponent.ControlSystemComponentType.Name, adapter.AlarmPropertyName, adapter.RowNumber);
        //            RaiseMessage(CommonUtils.MessageType.Info, msg);
        //        }
        //        Cee.ControlSystemAlarmPropertyValues.Add(apv);
        //        mSavedResults.Add(apv);
        //    }
        //    if (mSavedResults.Count == 0)
        //    {
        //        RaiseMessage(CommonUtils.MessageType.Warning, string.Format("No Alarms were added from from worksheet {0}.", WorkSheetName));
        //    }
        //    else
        //    {
        //        //SAVE
        //        Cee.SaveChanges();
        //    }
        //}
        private void UpdateData(IList<AlarmDataAdapter> importData)
        {
            if (importData.Count == 0)
            {
                RaiseMessage(CommonUtils.MessageType.Warning, NoDataFoundForWorkSheetMessage());
                return;
            }

            for (int i = 0; i < importData.Count; i++)
            {
                AlarmDataAdapter adapter = importData[i];

                CmsEquipmentDatabase.CmsWebService.ControlSystem controlSystem = (from x in Cee.ControlSystems where x.Name.Equals(adapter.ControlSystemName, StringComparison.InvariantCultureIgnoreCase) select x).FirstOrDefault();

                if (controlSystem == null)
                {
                    RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage(adapter.ControlSystemName, adapter.RowNumber.ToString())));
                    continue;
                }

                ControlSystemComponent parentComponent = (from x in Cee.ControlSystemComponents
                                                          .Include("ControlSystemComponentType")
                                                          where x.ControlSystem.Id == controlSystem.Id
                                                          && x.Name.Equals(adapter.ComponentName, StringComparison.InvariantCultureIgnoreCase)
                                                          select x).FirstOrDefault();

                if (parentComponent == null)
                {
                    RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage(adapter.ComponentName, adapter.RowNumber.ToString())));
                    continue;
                }

                ControlSystemAlarmPropertyValue dbExisting = (from x in Cee.ControlSystemAlarmPropertyValues.Include("ControlSystemAlarmProperty")
                                                              where x.ControlSystemAlarmProperty.Name.Equals(adapter.AlarmPropertyName, StringComparison.InvariantCultureIgnoreCase)
                                                              && x.ControlSystemComponentId == parentComponent.Id
                                                              select x).FirstOrDefault();

                if (dbExisting == null)
                {
                    RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage(string.Format("{0}-{1}", adapter.ComponentName, adapter.AlarmPropertyName), adapter.RowNumber.ToString())));
                    continue;
                }

                //Enabled
                if (!adapter.Enabled.HasValue)
                {
                    if (dbExisting.Enabled != adapter.Enabled.Value)
                    {
                        if (adapter.Enabled.Value == false)
                        {
                            dbExisting.ModifiedDate = DateTime.Now;
                            dbExisting.ModifiedUserId = MetaData.UserId;

                            RaiseMessage(CommonUtils.MessageType.Warning, string.Format("Attempted to Disable Alarm '{0}' at Rows {1}.", adapter.AlarmPropertyName, adapter.RowNumber));
                            continue;

                        }

                    }

                    dbExisting.Enabled = adapter.Enabled.Value;
                }

                //Tested - only touch if we have a Tested Value
                if (adapter.Tested.HasValue)
                {
                    dbExisting.Tested = adapter.Tested.Value;

                    if (!adapter.Tested.Value)
                    {//FALSE
                        adapter.TestedDate = null;
                        adapter.TestedUser = null;
                    }
                    else
                    {
                        //TRUE
                        if (adapter.TestedDate.HasValue)
                        {
                            dbExisting.TestedDate = adapter.TestedDate.Value;
                        }
                        else
                        {
                            RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildPropertyEmpty(AlarmColumn.TestedDate.ToString(), adapter.RowNumber)));
                            continue;
                        }
                        if (!string.IsNullOrEmpty(adapter.TestedUser))
                        {
                            dbExisting.TestedUserId = GetUserFromText(adapter.TestedUser).Id;
                        }
                        else
                        {
                            RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildPropertyEmpty(AlarmColumn.TestedUser.ToString(), adapter.RowNumber)));
                            continue;
                        }
                    }
                }

                //Reportable
                if (adapter.Reportable.HasValue)
                {
                    dbExisting.Reportable = adapter.Reportable.Value;

                    if (!adapter.Reportable.Value)
                    {//FALSE
                        adapter.ReportableDate = null;
                        adapter.ReportableUser = null;
                    }
                    else
                    {
                        //TRUE
                        if (adapter.ReportableDate.HasValue)
                        {
                            dbExisting.ReportableSetDate = adapter.ReportableDate.Value;
                        }
                        else
                        {
                            RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildPropertyEmpty(AlarmColumn.ReportableDate.ToString(), adapter.RowNumber)));
                            continue;
                        }
                        if (!string.IsNullOrEmpty(adapter.ReportableUser))
                        {
                            dbExisting.ReportableSetById = GetUserFromText(adapter.ReportableUser).Id;
                        }
                        else
                        {
                            RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildPropertyEmpty(AlarmColumn.ReportableUser.ToString(), adapter.RowNumber)));
                            continue;
                        }
                    }
                }

                //Reviewed
                if (adapter.Reviewed.HasValue)
                {
                    dbExisting.ReviewCompleted = adapter.Reviewed.Value;

                    if (!adapter.Reviewed.Value)
                    {//FALSE
                        adapter.ReviewedDate = null;
                        adapter.ReviewedUser = null;
                    }
                    else
                    {
                        //TRUE
                        if (adapter.ReviewedDate.HasValue)
                        {
                            dbExisting.ReviewedCompletedDate = adapter.ReviewedDate.Value;
                        }
                        else
                        {
                            RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildPropertyEmpty(AlarmColumn.ReviewedDate.ToString(), adapter.RowNumber)));
                            continue;
                        }
                        if (!string.IsNullOrEmpty(adapter.ReviewedUser))
                        {
                            dbExisting.ReviewedCompletedById = GetUserFromText(adapter.ReviewedUser).Id;
                        }
                        else
                        {
                            RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildPropertyEmpty(AlarmColumn.ReviewedUser.ToString(), adapter.RowNumber)));
                            continue;
                        }
                    }
                }

                //AlarmCalcExprEnabled
                if (adapter.AlarmCalcExprEnabled.HasValue)
                {
                    dbExisting.AlarmCalcExprEnabled = adapter.AlarmCalcExprEnabled.Value;
                }

                //AlarmPropertyName
                if (!string.IsNullOrEmpty(adapter.AlarmPropertyName) && !dbExisting.ControlSystemAlarmProperty.Name.Equals(adapter.AlarmPropertyName, StringComparison.CurrentCultureIgnoreCase))
                {

                    ControlSystemAlarmProperty dbExistingPropertyMatch = (from x in Cee.ControlSystemAlarmProperties where x.Name.Equals(adapter.AlarmPropertyName, StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault();

                    if (dbExistingPropertyMatch == null)
                    {
                        RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage(AlarmColumn.Consequence.ToString(), adapter.RowNumber.ToString())));
                        continue;
                    }

                    //todo - check duplicates!
                    //create new??

                    dbExisting.ControlSystemAlarmProperty.Name = adapter.AlarmPropertyName;
                }

                //ActivationExpr
                if (adapter.ActivationExprIsIntendedNull)
                {
                    dbExisting.ActivationExpr = string.Empty;
                }
                else
                {
                    if (!string.IsNullOrEmpty(adapter.ActivationExpr))
                    {
                        dbExisting.ActivationExpr = adapter.ActivationExpr;
                    }
                }

                //AlarmCalcExpr
                if (!string.IsNullOrEmpty(adapter.AlarmCalcExpr))
                {
                    dbExisting.AlarmCalcExpr = adapter.AlarmCalcExpr;
                }

                //MaskingExpr
                if (adapter.MaskingExprIsIntendedNull)
                {
                    dbExisting.MaskingExpr = string.Empty;
                }
                else
                {
                    if (!string.IsNullOrEmpty(adapter.MaskingExpr))
                    {
                        dbExisting.MaskingExpr = adapter.MaskingExpr;
                    }
                }

                //AlarmCalcExpr
                if (adapter.AlarmCalcExprIsIntendedNull)
                {
                    dbExisting.AlarmCalcExpr = string.Empty;
                }
                else
                {
                    if (!string.IsNullOrEmpty(adapter.AlarmCalcExpr))
                    {
                        dbExisting.AlarmCalcExpr = adapter.AlarmCalcExpr;
                    }
                }

                //Guidance
                if (adapter.GuidanceIsIntendedNull)
                {
                    dbExisting.Guidance = string.Empty;
                }
                else
                {
                    if (!string.IsNullOrEmpty(adapter.Guidance))
                    {
                        dbExisting.Guidance = adapter.Guidance;
                    }
                }

                //Notes
                if (adapter.NotesIsIntendedNull)
                {
                    dbExisting.Notes = string.Empty;
                }

                else
                {
                    if (!string.IsNullOrEmpty(adapter.Notes))
                    {
                        dbExisting.Notes = adapter.Notes;
                    }
                }

                //OnDelay
                if (adapter.OnDelayIsIntendedNull)
                {
                    dbExisting.OnDelay = null;
                }
                else
                {
                    if (adapter.OnDelay.HasValue)
                    {
                        dbExisting.OnDelay = adapter.OnDelay.Value;
                    }
                }

                //OffDelay
                if (adapter.OffDelayIsIntendedNull)
                {
                    dbExisting.OffDelay = null;
                }
                else
                {
                    if (adapter.OffDelay.HasValue)
                    {
                        dbExisting.OffDelay = adapter.OffDelay.Value;
                    }
                }

                //Consequence
                if (adapter.ConsequenceIsIntendedNull)
                {
                    dbExisting.ConsequenceId = null;
                }
                else
                {
                    if (!string.IsNullOrEmpty(adapter.Consequence))
                    {
                        var match = (from x in mExistingConsequences where x.Name.Equals(adapter.Consequence, StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault();

                        if (match == null)
                        {
                            RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage(AlarmColumn.Consequence.ToString(), adapter.RowNumber.ToString())));
                            continue;
                        }
                        dbExisting.ConsequenceId = match.Id;
                    }
                }

                //Purpose
                if (adapter.PurposeIsIndendedNull)
                {
                    dbExisting.PurposeId = null;
                }
                else
                {

                    if (!string.IsNullOrEmpty(adapter.Purpose))
                    {
                        var match = (from x in mExistingPurposes where x.Name.Equals(adapter.Purpose, StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault();

                        if (match == null)
                        {
                            RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage(AlarmColumn.Purpose.ToString(), adapter.RowNumber.ToString())));
                            continue;
                        }
                        dbExisting.PurposeId = match.Id;
                    }
                }

                //Response
                if (adapter.ResponseTimeIsIndendedNull)
                {
                    dbExisting.ResponseTimeId = null;
                }
                else
                {

                    if (!string.IsNullOrEmpty(adapter.Response))
                    {
                        var match = (from x in mExistingResponses where x.Name.Equals(adapter.Response, StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault();

                        if (match == null)
                        {
                            RaiseMessage(CommonUtils.MessageType.Error, string.Format(BuildItemNotFoundInDatabaseMessage(AlarmColumn.Response.ToString(), adapter.RowNumber.ToString())));
                            continue;
                        }
                        dbExisting.ResponseTimeId = match.Id;
                    }
                }

                var dbCompoentTypeConfigured = (from x in Cee.ControlSystemComponentTypeAlarmProperties
                                                where x.ComponentTypeId == parentComponent.ControlSystemComponentTypeId && x.AlarmPropertyId == dbExisting.ControlSystemAlarmPropertyId
                                                select x).FirstOrDefault();

                if (dbCompoentTypeConfigured == null)
                {
                    ControlSystemComponentTypeAlarmProperty cap = new ControlSystemComponentTypeAlarmProperty
                    {
                        ComponentTypeId = parentComponent.ControlSystemComponentTypeId,
                        AlarmPropertyId = dbExisting.ControlSystemAlarmPropertyId
                    };
                    Cee.ControlSystemComponentTypeAlarmProperties.Add(cap);

                    string msg = string.Format("The Configuration of ComponentType '{0}' with the Alarm Property '{1}' does not exist.  The Configuration has been created. (Row Number: {2}).", parentComponent.ControlSystemComponentType.Name, adapter.AlarmPropertyName, adapter.RowNumber);
                    RaiseMessage(CommonUtils.MessageType.Info, msg);
                }

                dbExisting.ModifiedDate = DateTime.Now;
                dbExisting.ModifiedUserId = MetaData.UserId;

                mSavedResults.Add(dbExisting);
            }

            if (mSavedResults.Count == 0)
            {
                RaiseMessage(CommonUtils.MessageType.Warning, string.Format("No Alarms were updated from from worksheet {0}.", WorkSheetName));
            }
            else
            {
                //SAVE
                Cee.SaveChanges();
            }
        }
        public DbOperationResult<ControlSystemComponentTypeAlarmProperty> SaveControlSystemComponentTypeAlarmProperty(ControlSystemComponentTypeAlarmProperty controlSystemComponentTypeAlarmProperty)
        {
            var result = new DbOperationResult<ControlSystemComponentTypeAlarmProperty>();

            try
            {
                using (var cee = new CmsEntities())
                {
                    var idMatch = (from x in cee.ControlSystemComponentTypeAlarmProperties
                                   where x.Id == controlSystemComponentTypeAlarmProperty.Id
                                   select x).FirstOrDefault();

                    if (idMatch != null)
                    {
                        //we have a ID set.

                        //see if we already have this comptype <-> property combo already - excluding this ID!.

                        //Check if this component property already exist
                        var dbDuplicate = (from x in cee.ControlSystemComponentTypeAlarmProperties
                                           where x.AlarmPropertyId == controlSystemComponentTypeAlarmProperty.AlarmPropertyId
                                                 && x.ComponentTypeId == controlSystemComponentTypeAlarmProperty.ComponentTypeId
                                                 && x.Id != controlSystemComponentTypeAlarmProperty.Id
                                           select x).FirstOrDefault();

                        if (dbDuplicate != null)
                        {
                            result.ServerErrorMessages.Add("The combination of Component Type and Property already exists.  Save cancelled.");
                            return result;
                        }

                        idMatch.AlarmPropertyId = controlSystemComponentTypeAlarmProperty.AlarmPropertyId;
                        idMatch.ComponentTypeId = controlSystemComponentTypeAlarmProperty.ComponentTypeId;
                        idMatch.Ordinal = controlSystemComponentTypeAlarmProperty.Ordinal;
                        cee.SaveChanges();
                        result.EntityResult = (from x in cee.ControlSystemComponentTypeAlarmProperties.Include("ControlSystemAlarmProperty")
                                               where x.Id == idMatch.Id
                                               select x).FirstOrDefault();
                        return result;
                    }

                    //MUST BE A NEW RECORD
                    //see if we already have this comptype <-> property combo already.
                    var dbDuplicate2 = (from x in cee.ControlSystemComponentTypeAlarmProperties
                                        where x.AlarmPropertyId == controlSystemComponentTypeAlarmProperty.AlarmPropertyId
                                              && x.ComponentTypeId == controlSystemComponentTypeAlarmProperty.ComponentTypeId
                                        select x).FirstOrDefault();

                    if (dbDuplicate2 != null)
                    {
                        result.ServerErrorMessages.Add("The combination of Component Type and Property already exists.  Save cancelled.");
                        return result;
                    }

                    cee.ControlSystemComponentTypeAlarmProperties.Add(controlSystemComponentTypeAlarmProperty);
                    cee.SaveChanges();

                    result.EntityResult = (from x in cee.ControlSystemComponentTypeAlarmProperties.Include("ControlSystemAlarmProperty")
                                           where x.Id == controlSystemComponentTypeAlarmProperty.Id
                                           select x).FirstOrDefault();
                    return result;
                }
            }
            catch (Exception ex)
            {
                log.Error("", ex, ex.ToString());
                result.ServerErrorMessages.Add(string.Format("Could not Save Control System Component Type Alarm Property.{0}{1}", Environment.NewLine, ex.Message));
            }

            return result;
        }