private void ClearControls()
        {
            mKeyword = "";
            RaisePropertyChanged("Keyword");

            mSelectedEquipmentType = EquipmentTypes[0];
            RaisePropertyChanged("SelectedEquipmentType");

            if (SelectedArea != null)
            {
                mSelectedArea = Areas[0];
                RaisePropertyChanged("SelectedArea");
            }

            if (SelectedSubArea != null)
            {
                mSelectedSubArea = SubAreas[0];
                RaisePropertyChanged("SelectedSubArea");
            }

            if (mSelectedUpperEquipment != null)
            {
                mUpperEquipments.AddRange(mMasterListUpperEquipments);
                mUpperEquipments.Insert(0, new UpperEquipment { Name = All, });
                mSelectedUpperEquipment = UpperEquipments[0];
                RaisePropertyChanged("SelectedUpperEquipment");
            }

            if (mSelectedGraphic != null)
            {
                mSelectedGraphic = Graphics[0];
                RaisePropertyChanged("SelectedGraphic");
            }

            if (mSelectedPAndIdDocument != null)
            {
                mSelectedPAndIdDocument = PandIDDocuments[0];
                RaisePropertyChanged("SelectedPAndIDDocument");
            }

            if (mSelectedComponentType != null)
            {
                mSelectedComponentType = ComponentTypes[0];
                RaisePropertyChanged("SelectedComponentType");
            }

            if (mSelectedSpecificationDocument != null)
            {
                mSelectedSpecificationDocument = SpecificationDocuments[0];
                RaisePropertyChanged("SelectedSpecificationDocument");
            }

            if (mSelectedClassified != null)
            {
                mSelectedClassified = Classifieds[0];
                RaisePropertyChanged("SelectedClassified");
            }
        }
        private void LoadData()
        {
            //AREAS
            mAreas = new List<Area>(from x in CMS.Cache.Areas
                                    where x.IsActive && x.SiteId == CMS.AppSetting.DefaultSiteId
                                    select x);
            mAreas.Insert(0, new Area { Id = -1, Name = All });
            mSelectedArea = Areas[0];

            //SubAreas
            LoadSubAreasFromCache(true);

            var specificationDocumentsTask = DatabaseLoader.GetQuickDocuments(CommonUtils.DoctypeFuncspecCode);
            var getQuickControlSystemInterlocksTask = DatabaseLoader.GetQuickControlSystemInterlocks();
            var getQuickControlSystemTypesTask = DatabaseLoader.GetQuickControlSystemTypes();
            var getUpperEquipmentsTask = DatabaseLoader.GetUpperEquipments();

            List<Task> tasks = new List<Task>();

            tasks.Add(specificationDocumentsTask);
            tasks.Add(getQuickControlSystemTypesTask);
            tasks.Add(getUpperEquipmentsTask);
            tasks.Add(getQuickControlSystemInterlocksTask);

            Task.Factory.ContinueWhenAll(tasks.ToArray(), x =>
            {
                CMS.UiFactory.StartNew(() =>
                {

                    SpecificationDocuments = specificationDocumentsTask.Result;
                    SpecificationDocuments.Insert(0, new QuickDocument { Id = -1, Name = All });
                    mSelectedSpecificationDocument = SpecificationDocuments[0];
                    RaisePropertyChanged("SpecificationDocuments");
                    RaisePropertyChanged("SelectedSpecificationDocument");

                    mEquipmentTypes = getQuickControlSystemTypesTask.Result;
                    mEquipmentTypes.Insert(0, new QuickControlSystemType { Id = -1, Name = All });
                    mSelectedEquipmentType = mEquipmentTypes[0];
                    RaisePropertyChanged("EquipmentTypes");
                    RaisePropertyChanged("SelectedEquipmentType");

                    mUpperEquipments = new List<UpperEquipment>();
                    mUpperEquipments.AddRange(getUpperEquipmentsTask.Result);
                    mUpperEquipments.Insert(0, new UpperEquipment { Name = All, });
                    mSelectedUpperEquipment = mUpperEquipments[0];
                    RaisePropertyChanged("UpperEquipments");
                    RaisePropertyChanged("SelectedUpperEquipment");

                    mQuickInterlocks = getQuickControlSystemInterlocksTask.Result;

                    ProcessSearchFilter();

                });
            });
        }
        private void SetControlValuesFromSearchFilterList(SearchFilterList searchFilterList)
        {
            ClearControls();

            // BECAUSE ORDER OF SETTING THESE 3 ( Area & SubArea & UpperEquipment) IS IMPORTANT - we will no rely on a 'loop order' SearchFilters - too risky.
            var areaFilter = (from x in searchFilterList.SearchFilters where x.Name.Equals(CommonUtils.ControlSearchFilterNames.Area.ToString(), StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault();
            if (areaFilter != null && !string.IsNullOrEmpty(areaFilter.Value))
            {
                int result;
                if (int.TryParse(areaFilter.Value, out result))
                {
                    var match = (from x in mAreas where x.Id == result select x).FirstOrDefault();
                    mSelectedArea = match;
                    RaisePropertyChanged("SelectedArea");

                    FilterSubAreasByArea(false); //might change the list of SubAreas ie 'mSubAreas'
                }
            }

            var subAreaFilter = (from x in searchFilterList.SearchFilters where x.Name.Equals(CommonUtils.ControlSearchFilterNames.SubArea.ToString(), StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault();
            if (subAreaFilter != null && !string.IsNullOrEmpty(subAreaFilter.Value))
            {
                int result;
                if (int.TryParse(subAreaFilter.Value, out result))
                {
                    var match = (from x in mSubAreas where x.Id == result select x).FirstOrDefault();
                    mSelectedSubArea = match;
                    RaisePropertyChanged("SelectedSubArea");

                }
            }

            RaisePropertyChanged("UpperEquipments");

            var upperEquipmentFilter = (from x in searchFilterList.SearchFilters where x.Name.Equals(CommonUtils.ControlSearchFilterNames.UpperEquipment.ToString(), StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault();
            if (upperEquipmentFilter != null && !string.IsNullOrEmpty(upperEquipmentFilter.Value))
            {
                //the UpperEquipments_get does the filtering.
                mSelectedUpperEquipment = (from x in UpperEquipments where x.Name == upperEquipmentFilter.Value select x).FirstOrDefault();
                if (mSelectedUpperEquipment == null)
                {
                    mSelectedUpperEquipment = UpperEquipments[0];
                }
                RaisePropertyChanged("SelectedUpperEquipment");
            }

            foreach (SearchFilter filter in searchFilterList.SearchFilters)
            {
                if (!string.IsNullOrEmpty(filter.Value))
                {
                    //KeyWords
                    if (filter.Name.Equals(CommonUtils.ControlSearchFilterNames.KeyWord.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        mKeyword = filter.Value;
                        RaisePropertyChanged("Keyword");
                    }

                    //ControlSystemType
                    if (filter.Name.Equals(CommonUtils.ControlSearchFilterNames.ControlSystemType.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        int result;
                        if (int.TryParse(filter.Value, out result))
                        {
                            mSelectedEquipmentType = (from x in EquipmentTypes where x.Id == result select x).FirstOrDefault();
                            RaisePropertyChanged("SelectedEquipmentType");
                        }
                    }

                    //Graphic
                    if (filter.Name.Equals(CommonUtils.ControlSearchFilterNames.Graphic.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        int result;
                        if (int.TryParse(filter.Value, out result))
                        {
                            mSelectedGraphic = (from x in Graphics where x.Id == result select x).FirstOrDefault();
                            RaisePropertyChanged("SelectedGraphic");
                        }
                    }

                    //PidDocument
                    if (filter.Name.Equals(CommonUtils.ControlSearchFilterNames.PidDocument.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        int result;
                        if (int.TryParse(filter.Value, out result))
                        {
                            mSelectedPAndIdDocument = (from x in PandIDDocuments where x.Id == result select x).FirstOrDefault();
                            RaisePropertyChanged("SelectedPAndIdDocument");
                        }
                    }

                    //ControlSystemComponentType
                    if (filter.Name.Equals(CommonUtils.ControlSearchFilterNames.ComponentType.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        int result;
                        if (int.TryParse(filter.Value, out result))
                        {
                            mSelectedComponentType = (from x in ComponentTypes where x.Id == result select x).FirstOrDefault();
                            RaisePropertyChanged("SelectedComponentType");
                        }
                    }

                    //Specification
                    if (filter.Name.Equals(CommonUtils.ControlSearchFilterNames.Specification.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        int result;
                        if (int.TryParse(filter.Value, out result))
                        {
                            mSelectedSpecificationDocument = (from x in SpecificationDocuments where x.Id == result select x).FirstOrDefault();
                            RaisePropertyChanged("SelectedSpecificationDocument");
                        }
                    }

                    //Classified
                    if (filter.Name.Equals(CommonUtils.ControlSearchFilterNames.Classified.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        mSelectedClassified = filter.Value; //Yes or No
                        RaisePropertyChanged("SelectedClassified");
                    }

                    //IsActive
                    if (filter.Name.Equals(CommonUtils.ControlSearchFilterNames.IsActive.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        mSelectedIsActive = filter.Value; //Yes or No
                        RaisePropertyChanged("SelectedIsActive");
                    }
                }
            }
            ProcessSearchFilter();
        }
        private void LoadData()
        {
            //PID & Specs
            var pidDocumentsTask = DatabaseLoader.GetQuickDocuments(CommonUtils.DoctypePidCode);
            var specificationDocumentsTask = DatabaseLoader.GetQuickDocuments(CommonUtils.DoctypeFuncspecCode);
            var getQuickControlSystemTypesTask = DatabaseLoader.GetQuickControlSystemTypes();
            var getUpperEquipmentsTask = DatabaseLoader.GetUpperEquipments();
            var getGraphicsTask = DatabaseLoader.GetGraphics();
            var getControlSystemComponentTypesTask = DatabaseLoader.GetControlSystemComponentTypes();

            List<Task> tasks = new List<Task>();
            tasks.Add(pidDocumentsTask);
            tasks.Add(specificationDocumentsTask);
            tasks.Add(getQuickControlSystemTypesTask);
            tasks.Add(getUpperEquipmentsTask);
            tasks.Add(getGraphicsTask);
            tasks.Add(getControlSystemComponentTypesTask);

            Task.Factory.ContinueWhenAll(tasks.ToArray(), x =>
            {
                CMS.UiFactory.StartNew(() =>
                {
                    //Type
                    mEquipmentTypes = getQuickControlSystemTypesTask.Result;
                    mEquipmentTypes.Insert(0, new QuickControlSystemType { Id = -1, Name = All });
                    mSelectedEquipmentType = mEquipmentTypes[0];
                    RaisePropertyChanged("EquipmentTypes");
                    RaisePropertyChanged("SelectedEquipmentType");

                    //UpperEquipment
                    mUpperEquipments = new List<UpperEquipment>();
                    mMasterListUpperEquipments = new List<UpperEquipment>();

                    mUpperEquipments.AddRange(getUpperEquipmentsTask.Result);
                    mMasterListUpperEquipments.AddRange(getUpperEquipmentsTask.Result);
                    mUpperEquipments.Insert(0, new UpperEquipment { Name = All, });
                    mSelectedUpperEquipment = mUpperEquipments[0];
                    RaisePropertyChanged("UpperEquipments");
                    RaisePropertyChanged("SelectedUpperEquipment");

                    //GRAPHICS
                    Graphics = getGraphicsTask.Result;
                    Graphics.Insert(0, new Graphic { Id = -1, Name = All });
                    mSelectedGraphic = Graphics[0];
                    RaisePropertyChanged("Graphics");
                    RaisePropertyChanged("SelectedGraphic");

                    //P&ID
                    PandIDDocuments = pidDocumentsTask.Result;
                    PandIDDocuments.Insert(0, new QuickDocument { Id = -1, Name = All });
                    mSelectedPAndIdDocument = PandIDDocuments[0];
                    RaisePropertyChanged("PandIDDocuments");
                    RaisePropertyChanged("SelectedPAndIDDocument");

                    //ControlSystemComponentType
                    ComponentTypes = getControlSystemComponentTypesTask.Result;
                    ComponentTypes.Insert(0, new ControlSystemComponentType { Id = -1, Name = All });
                    mSelectedComponentType = ComponentTypes[0];
                    RaisePropertyChanged("ComponentTypes");
                    RaisePropertyChanged("SelectedComponentType");

                    //Specification
                    SpecificationDocuments = specificationDocumentsTask.Result;
                    SpecificationDocuments.Insert(0, new QuickDocument { Id = -1, Name = All });
                    mSelectedSpecificationDocument = SpecificationDocuments[0];
                    RaisePropertyChanged("SpecificationDocuments");
                    RaisePropertyChanged("SelectedSpecificationDocument");

                    //Classified
                    Classifieds = new List<string> { All, Yes, "No" };
                    mSelectedClassified = Classifieds[0];
                    RaisePropertyChanged("Classifieds");
                    RaisePropertyChanged("SelectedClassified");

                    //IsActive
                    IsActiveChoices = new List<string> { All, Yes, "No" };
                    mSelectedIsActive = IsActiveChoices[1];
                    RaisePropertyChanged("IsActiveChoices");
                    RaisePropertyChanged("SelectedIsActive");

                    //AREAS
                    mAreas = new List<Area>(from a in CMS.Cache.Areas
                                            where a.IsActive && a.SiteId == CMS.AppSetting.DefaultSiteId
                                            select a);
                    mAreas.Insert(0, new Area { Id = -1, Name = All });
                    mSelectedArea = Areas[0];
                    RaisePropertyChanged("Areas");
                    RaisePropertyChanged("SelectedArea");

                    //SubAreas
                    LoadSubAreasFromCache(true);

                    SetUpFilteredLinkControl();
                    //safe to initialise the filter.
                    InitialiseSearchFilterControl(CommonUtils.TabId.Control);
                });
            });
        }
        private void LoadUppersFilteredByCell()
        {
            if (mSelectedCell == null)
            {
                return;
            }

            mUpperEquipments.Clear();
            var allEqip = new UpperEquipment { Name = ALL };

            var f = (from x in mMasterListUpperEquipments where x.CellId == mSelectedCell.Id select x).ToList();
            if (f.Any())
            {
                mUpperEquipments.AddRange(f);
                mUpperEquipments.Insert(0, allEqip);
                RaisePropertyChanged("UpperEquipments");
            }
        }
        private void LoadData()
        {
            var getQuickControlSystemsTask = DatabaseLoader.GetQuickControlSystems(new EquipmentFilterObject());
            var getQuickControlSystemInterlocksTask = DatabaseLoader.GetQuickControlSystemInterlocks();
            var specificationDocumentsTask = DatabaseLoader.GetQuickDocuments(CommonUtils.DoctypeFuncspecCode);
            var getUpperEquipmentsTask = DatabaseLoader.GetUpperEquipments();
            var getQuickControlSystemTypesTask = DatabaseLoader.GetQuickControlSystemTypes();

            var tasks = new List<Task> {
                getQuickControlSystemsTask,
                getQuickControlSystemInterlocksTask,
                specificationDocumentsTask,
                getUpperEquipmentsTask,
                getQuickControlSystemTypesTask,
                getQuickControlSystemInterlocksTask
            };

            Task.Factory.ContinueWhenAll(tasks.ToArray(), x =>
            {
                CMS.UiFactory.StartNew(() =>
                {
                    SpecificationDocuments = specificationDocumentsTask.Result;

                    var alllDocs = new QuickDocument { Id = -1, Name = ALL };
                    SpecificationDocuments.Insert(0, alllDocs);
                    mSelectedFunctionalSpec = alllDocs;
                    RaisePropertyChanged("SpecificationDocuments");
                    RaisePropertyChanged("SelectedFunctionalSpecification");

                    ControlSystemTypes = getQuickControlSystemTypesTask.Result;

                    var allControlSys = new QuickControlSystemType { Id = -1, Name = ALL };
                    ControlSystemTypes.Insert(0, allControlSys);
                    mSelectedControlSystemType = allControlSys;
                    RaisePropertyChanged("ControlSystemTypes");
                    RaisePropertyChanged("SelectedControlSystemType");

                    var allUpper = new UpperEquipment { Name = ALL };
                    mUpperEquipments = new List<UpperEquipment>();
                    mMasterListUpperEquipments = new List<UpperEquipment>();
                    mUpperEquipments.AddRange(getUpperEquipmentsTask.Result);
                    mMasterListUpperEquipments.AddRange(getUpperEquipmentsTask.Result);

                    mUpperEquipments.Insert(0, allUpper);
                    //mMasterListUpperEquipments.Insert(0, allUpper);

                    mSelectedUpperEquipment = allUpper;
                    RaisePropertyChanged("UpperEquipments");
                    RaisePropertyChanged("SelectedUpperEquipment");

                    ControlSystems = getQuickControlSystemsTask.Result;
                    RaisePropertyChanged("ControlSystems");

                    mQuickInterlocks = getQuickControlSystemInterlocksTask.Result;

                    //Area
                    Areas = new List<Area>();
                    Areas.AddRange(CMS.Cache.Areas);
                    var allArea = new Area { Id = -1, Name = ALL };
                    Areas.Insert(0, allArea);

                    mSelectedArea = allArea;
                    RaisePropertyChanged("Areas");
                    RaisePropertyChanged("SelectedArea");

                    //Cells
                    var allCells = new Cell { Id = -1, Name = ALL };
                    mCells = new List<Cell>();
                    mMasterListCells = new List<Cell>();

                    mCells.AddRange(CMS.Cache.SubAreas);
                    mMasterListCells.AddRange(CMS.Cache.SubAreas);

                    mCells.Insert(0, allCells);
                    //mMasterListCells.Insert(0, allCells);

                    mSelectedCell = allCells;
                    RaisePropertyChanged("Cells");
                    RaisePropertyChanged("SelectedCell");

                    Utils.ResetOriginalValues(View);

                    IsGridBusy = false;
                    RaisePropertyChanged("IsGridBusy");
                });
            });
        }
        private void LoadData()
        {
            var getQuickControlSystemsTask = DatabaseLoader.GetQuickControlSystems(new EquipmentFilterObject());
            var specificationDocumentsTask = DatabaseLoader.GetQuickDocuments(CommonUtils.DoctypeFuncspecCode);
            var getUpperEquipmentsTask = DatabaseLoader.GetUpperEquipments();
            var getQuickControlSystemTypesTask = DatabaseLoader.GetQuickControlSystemTypes();

            List<Task> tasks = new List<Task>
            {
                getQuickControlSystemsTask,
                specificationDocumentsTask,
                getUpperEquipmentsTask,
                getQuickControlSystemTypesTask
            };

            Task.Factory.ContinueWhenAll(tasks.ToArray(), x =>
            {
                CMS.UiFactory.StartNew(() =>
                {
                    SpecificationDocuments = specificationDocumentsTask.Result;
                    SpecificationDocuments.Insert(0, new QuickDocument {Id = -1, Name = "All"});
                    mSelectedFunctionalSpec = SpecificationDocuments[0];
                    RaisePropertyChanged("SpecificationDocuments");
                    RaisePropertyChanged("SelectedFunctionalSpecification");

                    ControlSystemTypes = getQuickControlSystemTypesTask.Result;
                    ControlSystemTypes.Insert(0, new QuickControlSystemType {Id = -1, Name = "All"});
                    mSelectedControlSystemType = ControlSystemTypes[0];
                    RaisePropertyChanged("ControlSystemTypes");
                    RaisePropertyChanged("SelectedControlSystemType");

                    UpperEquipments = new List<UpperEquipment>();
                    UpperEquipments.AddRange(getUpperEquipmentsTask.Result);
                    UpperEquipments.Insert(0, new UpperEquipment {Name = "All"});
                    RaisePropertyChanged("UpperEquipments");
                    mSelectedUpperEquipment = UpperEquipments[0];
                    RaisePropertyChanged("SelectedUpperEquipment");

                    ControlSystems = (from c in getQuickControlSystemsTask.Result where c.IsActive select c).ToList();
                    RaisePropertyChanged("ControlSystems");

                    //Area
                    Areas = new List<Area>();
                    Areas.AddRange(CMS.Cache.Areas);
                    Areas.Insert(0, new Area {Id = -1, Name = "All"});
                    mSelectedArea = Areas[0];
                    RaisePropertyChanged("Areas");
                    RaisePropertyChanged("SelectedArea");

                    //Cells
                    LoadCellsFromCache();
                    RaisePropertyChanged("SelectedCell");

                    Utils.ResetOriginalValues(View);
                });
            });
        }