private ApplicationOrganisationAdminVM ConvertApplicationOrganisationDC(ApplicationOrganisationAdminVMDC returnedObject) { ApplicationOrganisationAdminVM model = new ApplicationOrganisationAdminVM(); // Map Organisation Item model.OrganisationItem = Mapper.Map <OrganisationDC, OrganisationModel>(returnedObject.OrganisationItem); model.ParentOrganisationCode = returnedObject.ParentOrganisation == null ? Guid.Empty : returnedObject.ParentOrganisation.Code; // Map lookup data lists model.MaximumHopsToChildOrganisation = returnedObject.MaximumHopsToChildOrganisation; model.AllTypesForApplication = Mapper.Map <IEnumerable <OrganisationTypeDC>, List <OrganisationTypeModel> >(returnedObject.AllTypesForApplication); model.OrganisationsByTypesList = new List <OrganisationByTypeVM>(); MergeReturnedObjectToModel(model, returnedObject); return(model); }
private static void MergeReturnedObjectToModel(ApplicationOrganisationAdminVM model, ApplicationOrganisationAdminVMDC returnedObject) { //Clear the organisations list and repopulate from the returned object model.OrganisationsByTypesList.Clear(); List <OrganisationByTypeVM> AllOrganisationsForApplicationByTypesList = new List <OrganisationByTypeVM>(); foreach (var item in returnedObject.OrganisationsByTypesList) { AllOrganisationsForApplicationByTypesList.Add(new OrganisationByTypeVM() { OrganisationList = Mapper.Map <IEnumerable <OrganisationDC>, IEnumerable <OrganisationModel> >(item.OrganisationList).ToList(), OrganisationTypeItem = Mapper.Map <OrganisationTypeModel>(item.OrganisationTypeItem) }); } SessionManager.AllOrganisationsForApplicationByTypesList = AllOrganisationsForApplicationByTypesList; //initialise the available orgs, the first drop down should have THE ROOT ORGANISATION selected if (AllOrganisationsForApplicationByTypesList.Count > 1) { foreach (var item in AllOrganisationsForApplicationByTypesList) { model.OrganisationsByTypesList.Add(new OrganisationByTypeVM() { OrganisationList = item.OrganisationList, OrganisationTypeItem = item.OrganisationTypeItem, SelectedOrganisationCode = item.OrganisationList.SingleOrDefault(x => x.Code == model.ParentOrganisationCode) != null ? model.ParentOrganisationCode.ToString() : "" }); } } model.RootNodeOrganisation = Mapper.Map <OrganisationModel>(returnedObject.RootNodeOrganisation); //model.SelectedApplicationCode = returnedObject.SelectedApplicationCode; }
//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)); }
public ActionResult Edit() { // Retrieve ID from session string OrganisationCode = SessionManager.OrganisationCode; string ApplicationCode = SessionManager.ApplicationCode; ApplicationOrganisationAdminVM model = new ApplicationOrganisationAdminVM(); // Not from staff or error if (String.IsNullOrEmpty(OrganisationCode)) { //If session has lists then use them //RepopulateListsFromCacheSession(model); // Create service instance AdminServiceClient sc = new AdminServiceClient(); try { // Call service to get Organisation item and any associated lookups ApplicationOrganisationAdminVMDC returnedObject = sc.GetOrganisationWithParent(CurrentUser, CurrentUser, appID, "", Guid.Empty, Guid.Parse(ApplicationCode)); // Close service communication sc.Close(); //Get view model from service model = ConvertApplicationOrganisationDC(returnedObject); ResolveFieldCodesToFieldNamesUsingLists(model); AddListsToSession(model); SessionManager.RootOrganisation = model.RootNodeOrganisation; SessionManager.OrganisationServiceVersion = model.OrganisationItem.Clone(); SessionManager.MaximumHopsToChildOrganisation = model.MaximumHopsToChildOrganisation; } catch (Exception e) { // Handle the exception string message = ExceptionManager.HandleException(e, sc); model.Message = message; return(View(model)); } //Assume we are in create mode as no code passed model.OrganisationItem = new OrganisationModel() { IsActive = true }; } //if we have been passed a code then assume we are in edit situation and we need to retrieve from the database. else { // Create service instance AdminServiceClient sc = new AdminServiceClient(); try { // Call service to get Organisation item and any associated lookups ApplicationOrganisationAdminVMDC returnedObject = sc.GetOrganisationWithParent(CurrentUser, CurrentUser, appID, "", Guid.Parse(OrganisationCode), Guid.Parse(ApplicationCode)); // Close service communication sc.Close(); //Get view model from service model = ConvertApplicationOrganisationDC(returnedObject); ResolveFieldCodesToFieldNamesUsingLists(model); AddListsToSession(model); //Store the service version SessionManager.RootOrganisation = model.RootNodeOrganisation; SessionManager.OrganisationServiceVersion = model.OrganisationItem.Clone(); SessionManager.MaximumHopsToChildOrganisation = model.MaximumHopsToChildOrganisation; } catch (Exception e) { // Handle the exception string message = ExceptionManager.HandleException(e, sc); model.Message = message; return(View(model)); } } //Adds current retrieved Organisation to session SessionManager.CurrentOrganisation = model.OrganisationItem; SetAccessContext(model); return(View(model)); }