private CustomerControlMeasureLookupListsCacheObject GetCustomerControlMeasureAndLookups()
        {
            UcbServiceClient           sc           = new UcbServiceClient();
            CustomerControlMeasureVMDC returnObject = sc.GetCustomerControlMeasure(HttpContext.Current.User.Identity.Name, HttpContext.Current.User.Identity.Name, "FrameworkAdmin", "", null);

            CustomerControlMeasureLookupListsCacheObject CachedLists = new CustomerControlMeasureLookupListsCacheObject();

            CachedLists.CustomerList       = Mapper.Map <IEnumerable <CustomerDC>, List <CustomerModel> >(returnObject.CustomerList);
            CachedLists.ControlMeasureList = Mapper.Map <IEnumerable <ControlMeasureDC>, List <ControlMeasureModel> >(returnObject.ControlMeasureList);
            return(CachedLists);
        }
        public ActionResult Edit()
        {
            // Retrieve ID from session
            string code = sessionManager.CustomerControlMeasureCode;

            CustomerControlMeasureVM model = new CustomerControlMeasureVM();

            // 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.CustomerControlMeasureItem = new CustomerControlMeasureModel();
            }
            //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
                IUcbService sc = UcbService;

                try
                {
                    // Call service to get CustomerControlMeasure item and any associated lookups
                    CustomerControlMeasureVMDC returnedObject = sc.GetCustomerControlMeasure(CurrentUser, CurrentUser, appID, "", code);

                    // Close service communication
                    ((ICommunicationObject)sc).Close();

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

                    ResolveFieldCodesToFieldNamesUsingLists(model);

                    //Store the service version
                    sessionManager.CustomerControlMeasureServiceVersion = model.CustomerControlMeasureItem;
                }
                catch (Exception e)
                {
                    // Handle the exception
                    string message = ExceptionManager.HandleException(e, (ICommunicationObject)sc);
                    model.Message = message;

                    return(View(model));
                }
            }

            //Adds current retrieved CustomerControlMeasure to session
            sessionManager.CurrentCustomerControlMeasure = model.CustomerControlMeasureItem;
            SetAccessContext(model);

            return(View(model));
        }
        private CustomerControlMeasureVM ConvertCustomerControlMeasureDC(CustomerControlMeasureVMDC returnedObject)
        {
            CustomerControlMeasureVM model = new CustomerControlMeasureVM();

            // Map CustomerControlMeasure Item
            model.CustomerControlMeasureItem = Mapper.Map <CustomerControlMeasureDC, CustomerControlMeasureModel>(returnedObject.CustomerControlMeasureItem);

            // Map lookup data lists
            model.CustomerList       = Mapper.Map <IEnumerable <CustomerDC>, List <CustomerModel> >(returnedObject.CustomerList);
            model.ControlMeasureList = Mapper.Map <IEnumerable <ControlMeasureDC>, List <ControlMeasureModel> >(returnedObject.ControlMeasureList);

            return(model);
        }
        //This method is shared between create and save
        private ActionResult UpdateCustomerControlMeasure()
        {
            // 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
                IUcbService sc = UcbService;

                //Attempt update
                try
                {
                    // Map model to data contract
                    CustomerControlMeasureDC CustomerControlMeasureItem = Mapper.Map <CustomerControlMeasureDC>(model.CustomerControlMeasureItem);

                    CustomerControlMeasureVMDC returnedObject = null;

                    if (null == model.CustomerControlMeasureItem.Code || model.CustomerControlMeasureItem.Code == Guid.Empty)
                    {
                        // Call service to create new CustomerControlMeasure item
                        returnedObject = sc.CreateCustomerControlMeasure(CurrentUser, CurrentUser, appID, "", CustomerControlMeasureItem);
                    }
                    else
                    {
                        // Call service to update CustomerControlMeasure item
                        returnedObject = sc.UpdateCustomerControlMeasure(CurrentUser, CurrentUser, appID, "", CustomerControlMeasureItem);
                    }

                    // Close service communication
                    ((ICommunicationObject)sc).Close();

                    // Retrieve item returned by service
                    var createdCustomerControlMeasure = returnedObject.CustomerControlMeasureItem;

                    // Map data contract to model
                    model.CustomerControlMeasureItem = Mapper.Map <CustomerControlMeasureModel>(createdCustomerControlMeasure);

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

                    // Save version of item returned by service into session
                    sessionManager.CustomerControlMeasureServiceVersion = model.CustomerControlMeasureItem;
                    sessionManager.CurrentCustomerControlMeasure        = model.CustomerControlMeasureItem;

                    // 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 CustomerControlMeasure
        /// </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 CustomerControlMeasureVMDC CreateCustomerControlMeasure(string currentUser, string user, string appID, string overrideID, CustomerControlMeasureDC dc, IRepository <CustomerControlMeasure> 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 CustomerControlMeasure item
                    dc.Code = Guid.NewGuid();

                    // Map data contract to model
                    CustomerControlMeasure destination = mappingService.Map <CustomerControlMeasureDC, CustomerControlMeasure>(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 <CustomerControlMeasure, CustomerControlMeasureDC>(destination);
                }

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

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

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

                return(null);
            }
        }
        /// <summary>
        /// Retrieve a CustomerControlMeasure 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 CustomerControlMeasureVMDC GetCustomerControlMeasure(string currentUser, string user, string appID, string overrideID, string code, IUnitOfWork uow, IRepository <CustomerControlMeasure> dataRepository
                                                                    , IRepository <Customer> customerRepository
                                                                    , IRepository <ControlMeasure> controlMeasureRepository
                                                                    , 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)
                {
                    CustomerControlMeasureDC 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 CustomerControlMeasure
                        CustomerControlMeasure dataEntity = dataRepository.Single(x => x.Code == codeGuid);

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

                    IEnumerable <Customer>       customerList       = customerRepository.GetAll(x => new { x.Title });
                    IEnumerable <ControlMeasure> controlMeasureList = controlMeasureRepository.GetAll(x => new { x.ControlMeasureDescription });

                    List <CustomerDC>       customerDestinationList       = mappingService.Map <List <CustomerDC> >(customerList);
                    List <ControlMeasureDC> controlMeasureDestinationList = mappingService.Map <List <ControlMeasureDC> >(controlMeasureList);

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

                    returnObject.CustomerControlMeasureItem = destination;
                    returnObject.CustomerList       = customerDestinationList;
                    returnObject.ControlMeasureList = controlMeasureDestinationList;

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

                return(null);
            }
        }