Esempio n. 1
0
        public bool AllocationEditing_PreTransitionCRUD(string transition)
        {
            switch (transition.ToUpper())
            {
            case "SAVE":
                if (ASPxEdit.ValidateEditorsInContainer(formlayoutAllocationEditingForm))
                {
                    using (UnitOfWork uow = XpoHelper.GetNewUnitOfWork())
                    {
                        Guid             selectedAllocationTypeId;
                        Guid             selectedAccountActorTypeId;
                        AllocationType   selectedAllocationType   = null;
                        AccountActorType selectedAccountActorType = null;
                        Allocation       allocation = null;
                        bool             isParseSuccess;

                        //Get selected allocation type
                        isParseSuccess = Guid.TryParse(cboAllocationType.Value.ToString(), out selectedAllocationTypeId);
                        if (!isParseSuccess)
                        {
                            throw new Exception("The string is invalid for parsing to GUID");
                        }
                        selectedAllocationType = uow.GetObjectByKey <AllocationType>(selectedAllocationTypeId);

                        //Update allocation
                        allocation                  = uow.GetObjectByKey <Allocation>(AllocationId);
                        allocation.Code             = txtCode.Text;
                        allocation.Name             = txtName.Text;
                        allocation.Description      = txtDescription.Text;
                        allocation.AllocationTypeId = selectedAllocationType;

                        //Get selected IsMaster AccountActorType
                        isParseSuccess =
                            Guid.TryParse(cbIsMasterAccountActorType.Value.ToString()
                                          , out selectedAccountActorTypeId);
                        if (!isParseSuccess)
                        {
                            throw new Exception("The string is invalid for parsing to GUID");
                        }
                        selectedAccountActorType = uow.GetObjectByKey <AccountActorType>(selectedAccountActorTypeId);

                        //Update isMaster AllocationAccountActorType
                        AllocationAccountActorType isMasterAllocationAccountActorType =
                            allocation.AllocationAccountActorTypes.FirstOrDefault(r => r.IsMaster);
                        if (isMasterAllocationAccountActorType != null)
                        {
                            isMasterAllocationAccountActorType.AccountActorTypeId = selectedAccountActorType;
                        }
                        else
                        {
                            //Create new IsMaster AllocationAccountActorType
                            AllocationAccountActorType allocationAccountActorType
                                = new AllocationAccountActorType(uow)
                                {
                                AccountActorTypeId = selectedAccountActorType,
                                AllocationId       = allocation,
                                IsMaster           = true
                                };
                        }

                        //Get selected ids in gridlookupAccountActorType
                        var relatedAccountActorTypeIds = gridlookupAccountActorType.GridView
                                                         .GetSelectedFieldValues("AccountActorTypeId")
                                                         .Select(r => Guid.Parse(r.ToString()));
                        List <AllocationAccountActorType> relatedAccountActorTypes =
                            allocation.AllocationAccountActorTypes.Where(r => !r.IsMaster).ToList();
                        uow.Delete(relatedAccountActorTypes);

                        //Create new related AllocationAccountActorTypes
                        foreach (var relatedAccountActorTypeId in relatedAccountActorTypeIds)
                        {
                            AllocationAccountActorType relatedAllocationAccountActorType
                                = new AllocationAccountActorType(uow)
                                {
                                AccountActorTypeId = uow.GetObjectByKey <AccountActorType>(relatedAccountActorTypeId),
                                AllocationId       = allocation,
                                IsMaster           = false
                                };
                            relatedAllocationAccountActorType.Save();
                        }

                        //Set new Id to session variable
                        AllocationId = allocation.AllocationId;
                        uow.CommitChanges();
                    }
                }
                else
                {
                    return(false);
                }
                break;

            default:
                break;
            }
            return(true);
        }
Esempio n. 2
0
        public bool AllocationCreating_PreTransitionCRUD(string transition)
        {
            switch (transition.ToUpper())
            {
            case "SAVE":
                if (ASPxEdit.ValidateEditorsInContainer(formlayoutAllocationEditingForm))
                {
                    using (UnitOfWork uow = XpoHelper.GetNewUnitOfWork())
                    {
                        Guid             selectedAllocationTypeId;
                        Guid             selectedAccountActorTypeId;
                        AllocationType   selectedAllocationType   = null;
                        AccountActorType selectedAccountActorType = null;
                        Allocation       allocation = null;
                        bool             isParseSuccess;

                        //Get selected allocation type
                        isParseSuccess = Guid.TryParse(cboAllocationType.Value.ToString(), out selectedAllocationTypeId);
                        if (!isParseSuccess)
                        {
                            throw new Exception("The string is invalid for parsing to GUID");
                        }
                        selectedAllocationType = uow.GetObjectByKey <AllocationType>(selectedAllocationTypeId);

                        //Create new allocation
                        allocation = new Allocation(uow)
                        {
                            AllocationId     = Guid.NewGuid(),
                            AllocationTypeId = selectedAllocationType,
                            Code             = txtCode.Text,
                            Description      = txtDescription.Text,
                            Name             = txtName.Text,
                            RowStatus        = Utility.Constant.ROWSTATUS_ACTIVE
                                               //OwnerOrgId
                        };

                        //Get selected IsMaster AccountActorType
                        isParseSuccess =
                            Guid.TryParse(cbIsMasterAccountActorType.Value.ToString()
                                          , out selectedAccountActorTypeId);
                        if (!isParseSuccess)
                        {
                            throw new Exception("The string is invalid for parsing to GUID");
                        }
                        selectedAccountActorType = uow.GetObjectByKey <AccountActorType>(selectedAccountActorTypeId);

                        //Create new IsMaster AllocationAccountActorType
                        AllocationAccountActorType allocationAccountActorType
                            = new AllocationAccountActorType(uow)
                            {
                            AccountActorTypeId = selectedAccountActorType,
                            AllocationId       = allocation,
                            IsMaster           = true
                            };

                        //Get selected ids in gridlookupAccountActorType
                        var relatedAccountActorTypeIds = gridlookupAccountActorType.GridView
                                                         .GetSelectedFieldValues("AccountActorTypeId")
                                                         .Select(r => Guid.Parse(r.ToString()));
                        //Create new related AllocationAccountActorTypes
                        foreach (var relatedAccountActorTypeId in relatedAccountActorTypeIds)
                        {
                            AllocationAccountActorType relatedAllocationAccountActorType
                                = new AllocationAccountActorType(uow)
                                {
                                AccountActorTypeId = uow.GetObjectByKey <AccountActorType>(relatedAccountActorTypeId),
                                AllocationId       = allocation,
                                IsMaster           = false
                                };
                            relatedAllocationAccountActorType.Save();
                        }

                        //Set new Id to session variable
                        AllocationId = allocation.AllocationId;
                        uow.CommitChanges();
                    }
                }
                else
                {
                    return(false);
                }
                break;

            default:
                break;
            }
            return(true);
        }