Ejemplo n.º 1
0
        private OrganisationHierarchyLookupListsCacheObject GetOrganisationHierarchyAndLookups()
        {
            UcbServiceClient          sc           = new UcbServiceClient();
            OrganisationHierarchyVMDC returnObject = sc.GetOrganisationHierarchy(HttpContext.Current.User.Identity.Name, HttpContext.Current.User.Identity.Name, "FrameworkAdmin", "", null);

            OrganisationHierarchyLookupListsCacheObject CachedLists = new OrganisationHierarchyLookupListsCacheObject();

            CachedLists.AncestorOrganisationList = Mapper.Map <IEnumerable <OrganisationDC>, List <OrganisationModel> >(returnObject.AncestorOrganisationList);
            CachedLists.OrganisationList         = Mapper.Map <IEnumerable <OrganisationDC>, List <OrganisationModel> >(returnObject.OrganisationList);
            return(CachedLists);
        }
        public ActionResult Edit()
        {
            // Retrieve ID from session
            string code = SessionManager.OrganisationHierarchyCode;

            OrganisationHierarchyVM model = new OrganisationHierarchyVM();

            // Not from staff or error
            if (String.IsNullOrEmpty(code))
            {
                //If session has lists then use them
                RepopulateListsFromCacheSession(model);

                //Assume we are in create mode as no code passed
                model.OrganisationHierarchyItem = new OrganisationHierarchyModel();
            }
            //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 OrganisationHierarchy item and any associated lookups
                    OrganisationHierarchyVMDC returnedObject = sc.GetOrganisationHierarchy(CurrentUser, CurrentUser, appID, "", code);

                    // Close service communication
                    sc.Close();

                    //Get view model from service
                    model = ConvertOrganisationHierarchyDC(returnedObject);

                    ResolveFieldCodesToFieldNamesUsingLists(model);

                    //Store the service version
                    SessionManager.OrganisationHierarchyServiceVersion = model.OrganisationHierarchyItem;
                }
                catch (Exception e)
                {
                    // Handle the exception
                    string message = ExceptionManager.HandleException(e, sc);
                    model.Message = message;

                    return(View(model));
                }
            }

            //Adds current retrieved OrganisationHierarchy to session
            SessionManager.CurrentOrganisationHierarchy = model.OrganisationHierarchyItem;
            SetAccessContext(model);

            return(View(model));
        }
        private OrganisationHierarchyVM ConvertOrganisationHierarchyDC(OrganisationHierarchyVMDC returnedObject)
        {
            OrganisationHierarchyVM model = new OrganisationHierarchyVM();

            // Map OrganisationHierarchy Item
            model.OrganisationHierarchyItem = Mapper.Map <OrganisationHierarchyDC, OrganisationHierarchyModel>(returnedObject.OrganisationHierarchyItem);

            // Map lookup data lists
            model.AncestorOrganisationList = Mapper.Map <IEnumerable <OrganisationDC>, List <OrganisationModel> >(returnedObject.AncestorOrganisationList);
            model.OrganisationList         = Mapper.Map <IEnumerable <OrganisationDC>, List <OrganisationModel> >(returnedObject.OrganisationList);

            return(model);
        }
        /// <summary>
        ///  Create a OrganisationHierarchy
        /// </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 OrganisationHierarchyVMDC CreateOrganisationHierarchy(string currentUser, string user, string appID, string overrideID, OrganisationHierarchyDC dc, IRepository <OrganisationHierarchy> dataRepository, IUnitOfWork uow, IExceptionManager exceptionManager, IMappingService mappingService)
        {
            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 == uow)
                {
                    throw new ArgumentOutOfRangeException("uow");
                }
                if (null == exceptionManager)
                {
                    throw new ArgumentOutOfRangeException("exceptionManager");
                }
                if (null == mappingService)
                {
                    throw new ArgumentOutOfRangeException("mappingService");
                }

                #endregion

                using (uow)
                {
                    // Create a new ID for the OrganisationHierarchy item
                    dc.Code = Guid.NewGuid();

                    // Map data contract to model
                    OrganisationHierarchy destination = mappingService.Map <OrganisationHierarchyDC, OrganisationHierarchy>(dc);

                    // Add the new item
                    dataRepository.Add(destination);

                    // Commit unit of work
                    uow.Commit();

                    // Map model back to data contract to return new row id.
                    dc = mappingService.Map <OrganisationHierarchy, OrganisationHierarchyDC>(destination);
                }

                // Create aggregate data contract
                OrganisationHierarchyVMDC returnObject = new OrganisationHierarchyVMDC();

                // Add new item to aggregate
                returnObject.OrganisationHierarchyItem = dc;

                return(returnObject);
            }
            catch (Exception e)
            {
                //Prevent exception from propogating across the service interface
                exceptionManager.ShieldException(e);

                return(null);
            }
        }
        /// <summary>
        /// Retrieve a OrganisationHierarchy 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 OrganisationHierarchyVMDC GetOrganisationHierarchy(string currentUser, string user, string appID, string overrideID, string code, IUnitOfWork uow, IRepository <OrganisationHierarchy> dataRepository
                                                                  , IRepository <Organisation> ancestorOrganisationRepository
                                                                  , IRepository <Organisation> organisationRepository
                                                                  , IExceptionManager exceptionManager, IMappingService mappingService)

        {
            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 (null == uow)
                {
                    throw new ArgumentOutOfRangeException("uow");
                }
                if (null == exceptionManager)
                {
                    throw new ArgumentOutOfRangeException("exceptionManager");
                }
                if (null == mappingService)
                {
                    throw new ArgumentOutOfRangeException("mappingService");
                }

                #endregion

                using (uow)
                {
                    OrganisationHierarchyDC destination = null;

                    // If code is null then just return supporting lists
                    if (!string.IsNullOrEmpty(code))
                    {
                        // Convert code to Guid
                        Guid codeGuid = Guid.Parse(code);

                        // Retrieve specific OrganisationHierarchy
                        OrganisationHierarchy dataEntity = dataRepository.Single(x => x.Code == codeGuid);

                        // Convert to data contract for passing through service interface
                        destination = mappingService.Map <OrganisationHierarchy, OrganisationHierarchyDC>(dataEntity);
                    }

                    IEnumerable <Organisation> ancestorOrganisationList = ancestorOrganisationRepository.GetAll(x => x.Name);
                    IEnumerable <Organisation> organisationList         = organisationRepository.GetAll(x => x.Name);

                    List <OrganisationDC> ancestorOrganisationDestinationList = mappingService.Map <List <OrganisationDC> >(ancestorOrganisationList);
                    List <OrganisationDC> organisationDestinationList         = mappingService.Map <List <OrganisationDC> >(organisationList);

                    // Create aggregate contract
                    OrganisationHierarchyVMDC returnObject = new OrganisationHierarchyVMDC();

                    returnObject.OrganisationHierarchyItem = destination;
                    returnObject.AncestorOrganisationList  = ancestorOrganisationDestinationList;
                    returnObject.OrganisationList          = organisationDestinationList;

                    return(returnObject);
                }
            }
            catch (Exception e)
            {
                //Prevent exception from propogating across the service interface
                exceptionManager.ShieldException(e);

                return(null);
            }
        }
        //This method is shared between create and save
        private ActionResult UpdateOrganisationHierarchy()
        {
            // Get the updated model
            var model = GetUpdatedModel();

            // 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
                {
                    // Map model to data contract
                    OrganisationHierarchyDC OrganisationHierarchyItem = Mapper.Map <OrganisationHierarchyDC>(model.OrganisationHierarchyItem);

                    OrganisationHierarchyVMDC returnedObject = null;

                    if (null == model.OrganisationHierarchyItem.Code || model.OrganisationHierarchyItem.Code == Guid.Empty)
                    {
                        // Call service to create new OrganisationHierarchy item
                        returnedObject = sc.CreateOrganisationHierarchy(CurrentUser, CurrentUser, appID, "", OrganisationHierarchyItem);
                    }
                    else
                    {
                        // Call service to update OrganisationHierarchy item
                        returnedObject = sc.UpdateOrganisationHierarchy(CurrentUser, CurrentUser, appID, "", OrganisationHierarchyItem);
                    }

                    // Close service communication
                    sc.Close();

                    // Retrieve item returned by service
                    var createdOrganisationHierarchy = returnedObject.OrganisationHierarchyItem;

                    // Map data contract to model
                    model.OrganisationHierarchyItem = Mapper.Map <OrganisationHierarchyModel>(createdOrganisationHierarchy);

                    //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 = OrganisationHierarchyAccessContext.Edit;

                    // Save version of item returned by service into session
                    SessionManager.OrganisationHierarchyServiceVersion = model.OrganisationHierarchyItem;
                    SessionManager.CurrentOrganisationHierarchy        = model.OrganisationHierarchyItem;

                    // 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));
        }