public ActionResult Edit() { // Retrieve ID from session string organisationCode = sessionManager.IsExpected(sessionManager.OrganisationCode); SiteVM model = null; // Create service instance IUcbService sc = UcbService; try { // Call service to get Site item and any associated lookups via Organisation code SiteVMDC returnedObject = sc.GetSiteByOrganisationCode(CurrentUser, CurrentUser, appID, "", organisationCode); // Close service communication ((ICommunicationObject)sc).Close(); //Get view model from service model = ConvertSiteDC(returnedObject); ResolveFieldCodesToFieldNamesUsingLists(model); // Store the service version sessionManager.SiteServiceVersion = model.SiteItem; } catch (Exception e) { // Handle the exception string message = ExceptionManager.HandleException(e, (ICommunicationObject)sc); model.Message = message; return(View(model)); } //Adds current retrieved Site to session sessionManager.CurrentSite = model.SiteItem; sessionManager.SiteNominatedManagersSearch = model.NominatedManagerSearchList; sessionManager.SiteDeputyNominatedManagersSearch = model.DeputyNominatedManagerSearchList; sessionManager.SiteDeputyNominatedManagers = model.DeputyNominatedManagerList; SetAccessContext(model); return(View(model)); }
private SiteVM ConvertSiteDC(SiteVMDC returnedObject) { SiteVM model = new SiteVM(); // Map Site Item model.SiteItem = Mapper.Map <SiteDC, SiteModel>(returnedObject.SiteItem); // Set Nominated Manager model.NominatedManagerCode = returnedObject.NominatedManagerCode; // Map lookup data lists if (null != returnedObject.NominatedManagerSearchList) { model.NominatedManagerSearchList = Mapper.Map <List <StaffModel> >(returnedObject.NominatedManagerSearchList); } else { model.NominatedManagerSearchList = new List <StaffModel>(); } if (null != returnedObject.DeputyNominatedManagerList) { model.DeputyNominatedManagerList = Mapper.Map <List <StaffModel> >(returnedObject.DeputyNominatedManagerList); } else { model.DeputyNominatedManagerList = new List <StaffModel>(); } if (null != returnedObject.DeputyNominatedManagerSearchList) { model.DeputyNominatedManagerSearchList = Mapper.Map <List <StaffModel> >(returnedObject.DeputyNominatedManagerSearchList); } else { model.DeputyNominatedManagerSearchList = new List <StaffModel>(); } return(model); }
//This method is shared between create and save private ActionResult UpdateSite() { // Get the updated model var model = GetUpdatedModel(); #if (debug) // 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(); #endif //Set flags false SetFlagsFalse(model); // Test to see if the model has validated correctly if (ModelState.IsValid) { // Create service instance IUcbService sc = UcbService; //Attempt update try { // Map model to data contract SiteDC SiteItem = Mapper.Map <SiteDC>(model.SiteItem); SiteVMDC SiteAggregate = new SiteVMDC(); SiteAggregate.SiteItem = SiteItem; SiteAggregate.NominatedManagerCode = model.NominatedManagerCode; SiteAggregate.DeputyNominatedManagerList = Mapper.Map <IEnumerable <StaffDC> >(model.DeputyNominatedManagerList).ToArray(); SiteVMDC returnedObject = null; if (null == model.SiteItem.Code || model.SiteItem.Code == Guid.Empty) { // Call service to create new Site item returnedObject = sc.CreateSite(CurrentUser, CurrentUser, appID, "", SiteAggregate); } else { // Call service to update Site item returnedObject = sc.UpdateSite(CurrentUser, CurrentUser, appID, "", SiteAggregate); } // Close service communication ((ICommunicationObject)sc).Close(); // Map data contract to model model.SiteItem = Mapper.Map <SiteModel>(returnedObject.SiteItem); //After creation some of the fields are display only so we need the resolved look up nmames ResolveFieldCodesToFieldNamesUsingLists(model); // Set access context to Edit mode model.AccessContext = SiteAccessContext.Edit; // Save version of item returned by service into session sessionManager.SiteServiceVersion = model.SiteItem; sessionManager.CurrentSite = model.SiteItem; sessionManager.SiteNominatedManagersSearch = model.NominatedManagerSearchList; sessionManager.SiteDeputyNominatedManagersSearch = model.DeputyNominatedManagerSearchList; sessionManager.SiteDeputyNominatedManagers = model.DeputyNominatedManagerList; // 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 = Resources.MESSAGE_UPDATE_SUCCEEDED; } catch (Exception e) { // Handle the exception string message = ExceptionManager.HandleException(e, (ICommunicationObject)sc); model.Message = message; return(View(model)); } } return(View(model)); }
/// <summary> /// Create a Site /// </summary> /// <param name="currentUser"></param> /// <param name="user"></param> /// <param name="appID"></param> /// <param name="overrideID"></param> /// <param name="dc"></param> /// <param name="dataRepository"></param> /// <param name="uow"></param> public SiteVMDC CreateSite(string currentUser, string user, string appID, string overrideID, SiteVMDC dc, IRepository <Site> dataRepository, IRepository <SiteStaff> siteStaffRepository, IUnitOfWork uow, IExceptionManager exceptionManager) { try { #region Parameter validation // Validate parameters if (string.IsNullOrEmpty(currentUser)) { throw new ArgumentOutOfRangeException("currentUser"); } if (string.IsNullOrEmpty(user)) { throw new ArgumentOutOfRangeException("user"); } if (string.IsNullOrEmpty(appID)) { throw new ArgumentOutOfRangeException("appID"); } if (null == dc) { throw new ArgumentOutOfRangeException("dc"); } if (null == dataRepository) { throw new ArgumentOutOfRangeException("dataRepository"); } if (null == siteStaffRepository) { throw new ArgumentOutOfRangeException("siteStaffRepository"); } if (null == uow) { throw new ArgumentOutOfRangeException("uow"); } #endregion using (uow) { // Create a new ID for the Site item dc.SiteItem.Code = Guid.NewGuid(); // Map data contract to model Site siteDestination = Mapper.Map <SiteDC, Site>(dc.SiteItem); // Add the new item dataRepository.Add(siteDestination); SiteStaff siteStaffItem = null; // Add Deputy Nominated Managers foreach (StaffDC staffItem in dc.DeputyNominatedManagerList) { siteStaffItem = new SiteStaff(); siteStaffItem.Code = Guid.NewGuid(); siteStaffItem.Responsibility = ServiceConstants.SITE_STAFF_RESPONSIBILITY_DEPUTY_NOMINATED_MANAGER; siteStaffItem.SiteCode = siteDestination.Code; siteStaffItem.StaffCode = staffItem.Code; siteStaffRepository.Add(siteStaffItem); } // Add Nominated Manager siteStaffItem = new SiteStaff(); siteStaffItem.Code = Guid.NewGuid(); siteStaffItem.Responsibility = ServiceConstants.SITE_STAFF_RESPONSIBILITY_NOMINATED_MANAGER; siteStaffItem.SiteCode = siteDestination.Code; siteStaffItem.StaffCode = Guid.Parse(dc.NominatedManagerCode); siteStaffRepository.Add(siteStaffItem); // Commit unit of work uow.Commit(); // Map model back to data contract to return new row id. dc.SiteItem = Mapper.Map <Site, SiteDC>(siteDestination); } return(dc); } catch (Exception e) { //Prevent exception from propogating across the service interface exceptionManager.ShieldException(e); return(null); } }
/// <summary> /// Retrieve a Site with associated lookups /// </summary> /// <param name="currentUser"></param> /// <param name="user"></param> /// <param name="appID"></param> /// <param name="overrideID"></param> /// <param name="code"></param> /// <param name="dataRepository"></param> /// <param name="uow"></param> /// <returns></returns> public SiteVMDC GetSiteByOrganisationCode(string currentUser, string user, string appID, string overrideID, string organisationCode, IUnitOfWork uow, IRepository <Site> dataRepository , IRepository <Organisation> organisationRepository, IRepository <Staff> staffRepository, IExceptionManager exceptionManager) { try { #region Parameter validation // Validate parameters if (string.IsNullOrEmpty(currentUser)) { throw new ArgumentOutOfRangeException("currentUser"); } if (string.IsNullOrEmpty(user)) { throw new ArgumentOutOfRangeException("user"); } if (string.IsNullOrEmpty(appID)) { throw new ArgumentOutOfRangeException("appID"); } if (null == dataRepository) { throw new ArgumentOutOfRangeException("dataRepository"); } if (string.IsNullOrEmpty(organisationCode)) { throw new ArgumentOutOfRangeException("organisationCode"); } if (null == dataRepository) { throw new ArgumentOutOfRangeException("organisationRepository"); } if (null == dataRepository) { throw new ArgumentOutOfRangeException("staffRepository"); } if (null == uow) { throw new ArgumentOutOfRangeException("uow"); } #endregion using (uow) { SiteDC destination = null; StaffDC currentNominatedManagerDestination = null; List <StaffDC> currentDeputyNominatedManagersDestination = null; // Convert code to Guid Guid organisationCodeGuid = Guid.Parse(organisationCode); // Retrieve specific Site Site dataEntity = dataRepository.Find(x => x.OrganisationCode == organisationCodeGuid).SingleOrDefault(); // See if Site has already been created if (null != dataEntity) { // Convert to data contract for passing through service interface destination = Mapper.Map <Site, SiteDC>(dataEntity); Guid siteCodeGuid = dataEntity.Code; // Get nominated manager for this Site ISpecification <Staff> currentNominatedManagerSpecification = new Specification <Staff>( x => x.SiteStaff.Any( y => y.SiteCode == siteCodeGuid && y.Responsibility.ToLower() == ServiceConstants.SITE_STAFF_RESPONSIBILITY_NOMINATED_MANAGER.ToLower() ) ); Staff currentNominatedManager = staffRepository.Single(currentNominatedManagerSpecification); currentNominatedManagerDestination = Mapper.Map <StaffDC>(currentNominatedManager); // Get deputy nominated managers for this Site ISpecification <Staff> currentDeputyNominatedManagerSpecification = new Specification <Staff>( x => x.SiteStaff.Any( y => y.SiteCode == siteCodeGuid && y.Responsibility.ToLower() == ServiceConstants.SITE_STAFF_RESPONSIBILITY_DEPUTY_NOMINATED_MANAGER.ToLower() ) ); IEnumerable <Staff> currentDeputyNominatedManagerList = staffRepository.Find(currentDeputyNominatedManagerSpecification); currentDeputyNominatedManagersDestination = Mapper.Map <List <StaffDC> >(currentDeputyNominatedManagerList); } else // If site does not exist then create Site object and provide default Site name same a Organisation name { // Get site organisations ISpecification <Organisation> siteOrgansiationSpecification = new Specification <Organisation>(x => x.Code == organisationCodeGuid); Organisation siteOrganisation = organisationRepository.Single(siteOrgansiationSpecification); destination = new SiteDC(); destination.OrganisationCode = siteOrganisation.Code; destination.IsActive = true; // Provide default Site name, same as Organisation Name destination.SiteName = siteOrganisation.Name; } // Create aggregate contract SiteVMDC returnObject = new SiteVMDC(); returnObject.SiteItem = destination; // Set Nominated Manager if (null != currentNominatedManagerDestination) { returnObject.NominatedManagerCode = currentNominatedManagerDestination.Code.ToString(); returnObject.NominatedManagerSearchList = new List <StaffDC>(); returnObject.NominatedManagerSearchList.Add(currentNominatedManagerDestination); } // Set Deputy Nominated Managers returnObject.DeputyNominatedManagerList = currentDeputyNominatedManagersDestination; return(returnObject); } } catch (Exception e) { //Prevent exception from propogating across the service interface exceptionManager.ShieldException(e); return(null); } }
/// <summary> /// Create a Site /// </summary> /// <param name="currentUser"></param> /// <param name="user"></param> /// <param name="appID"></param> /// <param name="overrideID"></param> /// <param name="dc"></param> public SiteVMDC CreateSite(string currentUser, string user, string appID, string overrideID, SiteVMDC dc) { // Create unit of work IUnitOfWork uow = new UnitOfWork(currentUser); // Create repository Repository <Site> dataRepository = new Repository <Site>(uow.ObjectContext, currentUser, user, appID, overrideID); // Create SiteStaff repository Repository <SiteStaff> siteStaffRepository = new Repository <SiteStaff>(uow.ObjectContext, currentUser, user, appID, overrideID); //Create ExceptionManager IExceptionManager exceptionManager = new ExceptionManager(); // Call overload with injected objects return(CreateSite(currentUser, user, appID, overrideID, dc, dataRepository, siteStaffRepository, uow, exceptionManager)); }