Exemplo n.º 1
0
        //This method is shared between create and save
        private ActionResult UpdateOrganisation()
        {
            // Get the updated model
            var model = GetUpdatedModelOrg();

            // Test to see if there are any errors
            var errors = ModelState
                         .Where(x => x.Value.Errors.Count > 0)
                         .Select(x => new { x.Key, x.Value.Errors[0].ErrorMessage })
                         .ToArray();

            //Set flags false
            SetFlagsFalse(model);

            // Test to see if the model has validated correctly
            if (ModelState.IsValid)
            {
                // Create service instance
                AdminServiceClient sc = new AdminServiceClient();

                //Attempt update
                try
                {
                    //FindParentOrganisation
                    var SelectedOrganisationTypeCode = model.OrganisationItem.OrganisationTypeCode;
                    var SelectedOrganisationTypeParentLevelNumber = model.AllTypesForApplication.Single(x => x.Code == SelectedOrganisationTypeCode).LevelNumber - 2;
                    //OrganisationByTypeVM ParentOrgsList = SessionManager.AllOrganisationsForApplicationByTypesList[SelectedOrganisationTypeParentLevelNumber];
                    //var ParentOrganisation = ParentOrgsList.OrganisationList.Single(x => x.Code == model.ParentOrganisationCode);


                    // Map model to data contract
                    OrganisationDC OrganisationItem = Mapper.Map <OrganisationDC>(model.OrganisationItem);

                    ApplicationOrganisationAdminVMDC returnedObject = null;

                    if (null == model.OrganisationItem.Code || model.OrganisationItem.Code == Guid.Empty)
                    {
                        // Call service to create new Organisation item
                        returnedObject = sc.CreateOrganisationForApplication(CurrentUser, CurrentUser, appID, "", OrganisationItem, model.ParentOrganisationCode, Guid.Parse(SessionManager.ApplicationCode));
                    }
                    else
                    {
                        // Call service to update Organisation item
                        returnedObject = sc.UpdateOrganisationForApplication(CurrentUser, CurrentUser, appID, "", OrganisationItem, model.ParentOrganisationCode, Guid.Parse(SessionManager.ApplicationCode));
                    }

                    // Close service communication
                    sc.Close();

                    //not saved because organisation already existed
                    if (!string.IsNullOrEmpty(returnedObject.Message))
                    {
                        if (returnedObject.Message.Contains("Update failed"))
                        {
                            // Set access context to Edit mode
                            model.AccessContext = ApplicationOrganisationAccessContext.Edit;
                            model.Message       = FixedResources.MESSAGE_UPDATEFAILED_DUE_TO_ORGANISATIONALREADY_EXISTS;
                        }
                        else
                        {
                            // Set access context to Create mode
                            model.AccessContext = ApplicationOrganisationAccessContext.Create;
                            model.Message       = FixedResources.MESSAGE_CREATEFAILED_DUE_TO_ORGANISATIONALREADY_EXISTS;
                        }
                    }

                    else
                    {
                        //Get view model from service
                        model = ConvertApplicationOrganisationDC(returnedObject);

                        SessionManager.RootOrganisation               = model.RootNodeOrganisation;
                        SessionManager.OrganisationServiceVersion     = model.OrganisationItem.Clone();
                        SessionManager.MaximumHopsToChildOrganisation = model.MaximumHopsToChildOrganisation;
                        // Save version of item returned by service into session
                        SessionManager.OrganisationServiceVersion = model.OrganisationItem.Clone();
                        SessionManager.CurrentOrganisation        = model.OrganisationItem;


                        // Set access context to Edit mode
                        model.AccessContext = ApplicationOrganisationAccessContext.Edit;

                        ResolveFieldCodesToFieldNamesUsingLists(model);
                        AddListsToSession(model);
                        //Store the service version
                        SessionManager.OrganisationServiceVersion = model.OrganisationItem;
                        // Remove the state from the model as these are being populated by the controller and the HTML helpers are being populated with
                        // the POSTED values and not the changed ones.
                        ModelState.Clear();
                        model.Message = FixedResources.MESSAGE_UPDATE_SUCCEEDED;
                    }
                }
                catch (Exception e)
                {
                    // Handle the exception
                    string message = ExceptionManager.HandleException(e, sc);
                    model.Message = message;

                    return(View(model));
                }
            }

            return(View(model));
        }