public AddEditMobilePlantComponentTypeViewModel(MobilePlantComponentType mct)
        {
            mMobilePlantComponentType = mct;

            OkButtonCommand = new DelegateCommand<object>(OkButtonHander, CanModifyConfig);
            CancelButtonCommand = new DelegateCommand<object>(CancelButtonHander, x => true);
        }
 public AddEditMobilePlantComponentTypeDialog()
 {
     InitializeComponent();
     mMobilePlantComponentType = new MobilePlantComponentType();
     model = new AddEditMobilePlantComponentTypeViewModel(mMobilePlantComponentType);
     model.View = this;
     DataContext = model;
     Utils.ResetOriginalValues(this);
 }
        public AddEditExistingMobilePlantComponentPropertyDialog(MobilePlantComponentType electricalEquipmentComponentType)
        {
            InitializeComponent();

            AddEditExistingMobilePlantComponentPropertyViewModel model =
                new AddEditExistingMobilePlantComponentPropertyViewModel(electricalEquipmentComponentType) {Ordinal = "0"};
            model.DataLoaded +=
                () =>
                {
                    model.View = this;
                    DataContext = model;
                };
        }
        public AddEditExistingMobilePlantComponentPropertyViewModel(MobilePlantComponentType electricalEquipmentComponentType)
        {
            mComponentTypeId = electricalEquipmentComponentType.Id;
            mOrdinal = electricalEquipmentComponentType.Ordinal;
            OkButtonCommand = new DelegateCommand<object>(OkButtonHander, CanModify);
            CancelButtonCommand = new DelegateCommand<object>(CancelButtonHander, x => true);

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

            //Load Component types
            EventHandler<GetAllMobilePlantPropertiesCompletedEventArgs> fetchCompleted = (s, eventArgs) =>
                                              {
                                                  cmsWebServiceClient.GetMobilePlantPropertiesCompleted +=
                                                      (s1, e1)
                                                      =>
                                                          {
                                                              List<int> listOfAssignedPropertyIds = new List<int>();
                                                              e1.Result.ForEach(x => listOfAssignedPropertyIds.Add(x.Id));

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

                                                              if (Properties.Count > 0)
                                                              {
                                                                  SelectedProperty = Properties[0];
                                                              }
                                                              DataLoaded();
                                                          };

                                                  cmsWebServiceClient.GetMobilePlantPropertiesAsync(electricalEquipmentComponentType.Id);
                                              };
            cmsWebServiceClient.GetAllMobilePlantPropertiesCompleted += fetchCompleted;
            cmsWebServiceClient.GetAllMobilePlantPropertiesAsync();
        }
        public MobilePlantComponentType AddMobilePlantComponentType(MobilePlantComponentType mobilePlantComponentType)
        {
            MobilePlantComponentType newMect = new MobilePlantComponentType();

            using (CmsEntities cee = new CmsEntities())
            {
                //Check if this component type already exist
                MobilePlantComponentType originalMect = (from x in cee.MobilePlantComponentTypes
                                                                 where x.Id == mobilePlantComponentType.Id
                                                                 select x).FirstOrDefault();

                if (originalMect != null)
                {
                    //Edit the Component Type
                    originalMect.Name = mobilePlantComponentType.Name;
                    originalMect.Description = mobilePlantComponentType.Description;
                    //pct.Code = mect.Code;
                    cee.SaveChanges();
                }
                else
                {
                    //Add new Component Type
                    originalMect = new MobilePlantComponentType();
                    originalMect.Name = mobilePlantComponentType.Name;
                    originalMect.Description = mobilePlantComponentType.Description;
                    originalMect.Code = mobilePlantComponentType.Name.Replace(" ", "");
                    originalMect.IsActive = true;

                    cee.MobilePlantComponentTypes.Add(originalMect);
                    //cee.AddToPipeComponentTypes(pct);
                    cee.SaveChanges();
                }

                newMect.Id = originalMect.Id;
                newMect.Name = originalMect.Name;
                newMect.Description = originalMect.Description;

                return newMect;
            }
        }
        private void SetControlValuesFromSearchFilterList(SearchFilterList searchFilterList)
        {
            mSearchFilterControl.SetFilterIsOn(true);
            ClearControls();

            //1. Manufacturer
            SearchFilter manufactuerFilter = (from x in searchFilterList.SearchFilters where x.Name.Equals(CommonUtils.MobilePlantSearchFilterNames.Manufacturer.ToString(), StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault();
            if (manufactuerFilter != null)
            {
                int result;
                if (int.TryParse(manufactuerFilter.Value, out result))
                {
                    Manufacturer match = (from x in Manufacturers where x.Id == result select x).FirstOrDefault();
                    mSelectedManufacturer = match;
                    RaisePropertyChanged("SelectedManufacturer");
                }
            }

            //2. Model
            SearchFilter modelFilter = (from x in searchFilterList.SearchFilters where x.Name.Equals(CommonUtils.MobilePlantSearchFilterNames.Model.ToString(), StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault();
            if (modelFilter != null)
            {
                int result;
                if (int.TryParse(modelFilter.Value, out result))
                {
                    Model match = (from x in mModelsCache where x.Id == result select x).FirstOrDefault();
                    mSelectedModel = match;
                    RaisePropertyChanged("SelectedModel");
                }
            }

            //1. Area
            SearchFilter areaFilter = (from x in searchFilterList.SearchFilters where x.Name.Equals(CommonUtils.MobilePlantSearchFilterNames.Area.ToString(), StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault();
            if (areaFilter != null)
            {
                int result;
                if (int.TryParse(areaFilter.Value, out result))
                {
                    Area match = (from x in Areas where x.Id == result select x).FirstOrDefault();
                    mSelectedArea = match;
                    RaisePropertyChanged("SelectedArea");
                }
            }

            //2. SubArea
            SearchFilter subareaFilter = (from x in searchFilterList.SearchFilters where x.Name.Equals(CommonUtils.MobilePlantSearchFilterNames.SubArea.ToString(), StringComparison.CurrentCultureIgnoreCase) select x).FirstOrDefault();
            if (subareaFilter != null)
            {
                int result;
                if (int.TryParse(subareaFilter.Value, out result))
                {
                    Cell match = (from x in SubAreas where x.Id == result select x).FirstOrDefault();
                    mSelectedSubArea = match;
                    RaisePropertyChanged("SelectedSubArea");
                }
            }

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

                    //MainSystId
                    if (filter.Name.Equals(CommonUtils.MobilePlantSearchFilterNames.MaintSystemId.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        mMainSystId = filter.Value;
                        RaisePropertyChanged("MainSystId");
                    }

                    //Owner
                    if (filter.Name.Equals(CommonUtils.MobilePlantSearchFilterNames.Owner.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        int result;
                        if (int.TryParse(filter.Value, out result))
                        {
                            mSelectedMobileOwner = (from x in MobileOwners where x.Id == result select x).FirstOrDefault();
                            RaisePropertyChanged("SelectedMobileOwner");
                        }
                    }

                    //Hirer
                    if (filter.Name.Equals(CommonUtils.MobilePlantSearchFilterNames.Hirer.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        int result;
                        if (int.TryParse(filter.Value, out result))
                        {
                            mSelectedMobileHirer = (from x in MobileHirers where x.Id == result select x).FirstOrDefault();
                            RaisePropertyChanged("SelectedMobileHirer");
                        }
                    }

                    //Classified
                    if (filter.Name.Equals(CommonUtils.MobilePlantSearchFilterNames.Classified.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        //For compatibility as previously it was saved as Id
                        if (filter.Value == "-1") filter.Value = All;
                        if (filter.Value == "-2") filter.Value = Yes;
                        if (filter.Value == "-3") filter.Value = No;

                        var match = (from x in Classifieds where x == filter.Value select x).FirstOrDefault();
                        mSelectedClassified = match;
                        RaisePropertyChanged("Classified");
                    }

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

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

                    //IsActive
                    if (filter.Name.Equals(CommonUtils.MobilePlantSearchFilterNames.IsActive.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        //For compatibility as previously it was saved as Id
                        if (filter.Value == "-1") filter.Value = All;
                        if (filter.Value == "-2") filter.Value = Yes;
                        if (filter.Value == "-3") filter.Value = No;

                        var match = (from x in IsActiveChoices where x == filter.Value select x).FirstOrDefault();
                        mSelectedIsActive = match;
                        RaisePropertyChanged("SelectedIsActive");

                    }

                }
            }

            ProcessSearchFilter();
        }
        private void LoadData()
        {
            var getMobilePlantTypesTask = DatabaseLoader.GetMobilePlantTypes();
            var getMobileOwnersTask = DatabaseLoader.GetMobileOwners();
            var getMobileHirersTask = DatabaseLoader.GetMobileHirers();
            var getMobilePlantComponentTypesTask = DatabaseLoader.GetMobilePlantComponentTypes();
            var getManufacturersTask = DatabaseLoader.GetManufacturers(CommonUtils.EquipmentTypeCode.MOB);
            var getModelsTask = DatabaseLoader.GetModels(CommonUtils.EquipmentTypeCode.MOB);

            List<Task> tasks = new List<Task>();
            tasks.Add(getMobilePlantTypesTask);
            tasks.Add(getMobileOwnersTask);
            tasks.Add(getMobileHirersTask);
            tasks.Add(getMobilePlantComponentTypesTask);
            tasks.Add(getManufacturersTask);

            Task.Factory.ContinueWhenAll(tasks.ToArray(), x =>
            {
                CMS.UiFactory.StartNew(() =>
                {
                    //EquipmentType
                    var allItem = new MobilePlantType { Id = -1, Name = All };
                    EquipmentTypes = getMobilePlantTypesTask.Result;
                    EquipmentTypes.Insert(0, allItem);
                    mSelectedEquipmentType = EquipmentTypes[0];
                    RaisePropertyChanged("EquipmentTypes");
                    RaisePropertyChanged("SelectedEquipmentType");

                    //Owners
                    MobileOwners = getMobileOwnersTask.Result;
                    MobileOwners.Insert(0, new MobileOwner { Id = -1, Name = All });
                    mSelectedMobileOwner = MobileOwners[0];
                    RaisePropertyChanged("MobileOwners");
                    RaisePropertyChanged("SelectedMobileOwner");

                    //MobileHirers
                    MobileHirers = getMobileHirersTask.Result;
                    MobileHirers.Insert(0, new MobileHirer { Id = -1, Name = All });
                    mSelectedMobileHirer = MobileHirers[0];
                    RaisePropertyChanged("MobileHirers");
                    RaisePropertyChanged("SelectedMobileHirer");

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

                    //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[0];
                    RaisePropertyChanged("IsActiveChoices");
                    RaisePropertyChanged("SelectedIsActive");

                    //Model
                    mModelsCache = getModelsTask.Result;

                    //Manufacturer
                    Manufacturers = getManufacturersTask.Result;
                    Manufacturers.Insert(0, new Manufacturer { Id = -1, Name = All });
                    SelectedManufacturer = Manufacturers[0];
                    RaisePropertyChanged("Manufacturers");
                    RaisePropertyChanged("SelectedManufacturer");

                    SetUpFilteredLinkControl();
                    InitialiseSearchFilterControl(CommonUtils.TabId.MobilePlant);
                });
            });

            LoadAreas();
            LoadSubAreas();
        }
        private void ClearControls()
        {
            mKeyword = string.Empty;
            RaisePropertyChanged("Keyword");

            mSelectedArea = Areas[0];
            RaisePropertyChanged("SelectedArea");

            mMainSystId = string.Empty;
            RaisePropertyChanged("MainSystId");

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

            mSelectedSubArea = SubAreas[0];
            RaisePropertyChanged("SelectedSubArea");

            mSelectedMobileOwner = MobileOwners[0];
            RaisePropertyChanged("SelectedMobileOwner");

            mSelectedMobileHirer = MobileHirers[0];
            RaisePropertyChanged("SelectedMobileHirer");

            mSelectedComponentType = ComponentTypes[0];
            RaisePropertyChanged("SelectedComponentType");

            mSelectedClassified = Classifieds[0];
            RaisePropertyChanged("Classified");

            mSelectedIsActive = IsActiveChoices[0];
            RaisePropertyChanged("SelectedIsActive");

            mSelectedManufacturer = Manufacturers[0];
            RaisePropertyChanged("SelectedManufacturer");

            mSelectedModel = Models[0];
            RaisePropertyChanged("SelectedModel");

            mSelectedClassified = Classifieds[0];
            RaisePropertyChanged("SelectedClassified");
        }
        private bool GetMobilePlantComponentType(string typeName, MobilePlantComponent component, int rowIndex)
        {
            MobilePlantComponentType componentType = (from x in mExistingEquipmentComponentTypes
                                                      where string.Compare(typeName, x.Name, true, CultureInfo.CurrentCulture) == 0
                                                            && !string.IsNullOrEmpty(x.Name)
                                                      select x).FirstOrDefault();

            if (componentType != null)
            {
                component.MobilePlantComponentType = componentType;
                component.MobilePlantComponentTypeId = componentType.Id;
            }
            else
            {
                if (CanCreateProperties)
                {
                    string code = typeName.Replace(" ", "_").ToUpper();

                    componentType = new MobilePlantComponentType { Name = typeName, Code = code, Description = typeName + " (created by importer)", IsActive = true };
                    mExistingEquipmentComponentTypes.Add(componentType);
                    component.MobilePlantComponentType = componentType;
                }
                else
                {
                    RaiseMessage(CommonUtils.MessageType.Error, string.Format("WorkSheet '{0}' Row '{1}': Could not find MobilePlantComponentType '{2}' in database. Skipping this row.", WorkSheetName, rowIndex, typeName));
                    return true;
                }
            }
            return false;
        }
        private void LoadData()
        {
            var getMobilePlantTypesTask = DatabaseLoader.GetMobilePlantTypes();
            var getMobileOwnersTask = DatabaseLoader.GetMobileOwners();
            var getMobileHirersTask = DatabaseLoader.GetMobileHirers();
            var getMobilePlantComponentTypesTask = DatabaseLoader.GetMobilePlantComponentTypes();
            var getManufacturersTask = DatabaseLoader.GetManufacturers(CommonUtils.EquipmentTypeCode.MOB);
            var getModelsTask = DatabaseLoader.GetModels(CommonUtils.EquipmentTypeCode.MOB);

            List<Task> tasks = new List<Task>();
            tasks.Add(getMobilePlantTypesTask);
            tasks.Add(getMobileOwnersTask);
            tasks.Add(getMobileHirersTask);
            tasks.Add(getMobilePlantComponentTypesTask);
            tasks.Add(getManufacturersTask);

            Task.Factory.ContinueWhenAll(tasks.ToArray(), x =>
            {
                CMS.UiFactory.StartNew(() =>
                {
                    //EquipmentType
                    var allItem = new MobilePlantType { Id = -1, Name = All };
                    EquipmentTypes = getMobilePlantTypesTask.Result;
                    EquipmentTypes.Insert(0, allItem);
                    mSelectedEquipmentType = EquipmentTypes[0];

                    //Owners
                    MobileOwners = getMobileOwnersTask.Result;
                    MobileOwners.Insert(0, new MobileOwner { Id = -1, Name = All });
                    mSelectedMobileOwner = MobileOwners[0];

                    //MobileHirers
                    MobileHirers = getMobileHirersTask.Result;
                    MobileHirers.Insert(0, new MobileHirer { Id = -1, Name = All });
                    mSelectedMobileHirer = MobileHirers[0];

                    //Component Types
                    ComponentTypes = getMobilePlantComponentTypesTask.Result;
                    ComponentTypes.Insert(0, new MobilePlantComponentType { Id = -1, Name = All });
                    mSelectedComponentType = ComponentTypes[0];

                    //Classified
                    Classifieds = new List<String> { All, Yes, No };
                    mSelectedClassified = Classifieds[0];

                    //Classified
                    IsActiveChoices = new List<String> { All, Yes, No };
                    mSelectedIsActive = IsActiveChoices[0];

                    //Model
                    mModelsCache = getModelsTask.Result;

                    //Manufacturer
                    Manufacturers = getManufacturersTask.Result;
                    Manufacturers.Insert(0, new Manufacturer { Id = -1, Name = All });
                    SelectedManufacturer = Manufacturers[0];

                    ProcessSearchFilter();
                    AllDataLoadedAction();

                });
            });

            LoadAreas();
            LoadSubAreas();
        }