/// <summary> /// SaveSite /// </summary> /// <param name="siteModel"></param> /// <returns></returns> public void SaveSite(SiteModel siteModel) { SiteDC site = Mapper.Map <SiteModel, SiteDC>(siteModel); ServiceResponse <int> saveSiteResponse = _securityProxy.Execute(opt => opt.SaveSite(site)); if (saveSiteResponse.Status != ResponseStatus.Success) { HandleError(saveSiteResponse.Status, saveSiteResponse.ResponseMessage); } else { siteModel.SiteId = saveSiteResponse.Result; } }
/// <summary> /// SaveSite /// </summary> /// <returns></returns> public ServiceResponse <int> SaveSite(SiteDC site) { ServiceResponse <int> saveSiteResponse = new ServiceResponse <int>(); try { SetContext(); Site siteModel = Mapper.Map <SiteDC, Site>(site); _securityManager.SaveSite(siteModel); saveSiteResponse.Result = site.SiteId; } catch (Exception ex) { HandleError(ex, saveSiteResponse); } return(saveSiteResponse); }
//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> /// 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); } }