Example #1
0
        /// <summary>
        /// Save the program unit.
        /// </summary>
        /// <param name="programUnit">The program unit.</param>
        /// <returns></returns>
        public Response <ProgramUnitDetailsModel> SaveProgramUnit(ProgramUnitDetailsModel programUnit)
        {
            var programUnitResponse = new Response <ProgramUnitDetailsModel>();

            using (var transactionScope = _unitOfWork.BeginTransactionScope())
            {
                var programUnitResult = new Response <OrganizationDetailsModel>();
                if (programUnit.ProgramUnit.DetailID > 0)
                {
                    programUnitResult = _organizationStructureDataProvider.UpdateOrganizationStructure(programUnit.ProgramUnit);
                }
                else
                {
                    programUnit.ProgramUnit.DataKey = OrganizationType.ProgramUnit.ToString();
                    programUnitResult = _organizationStructureDataProvider.AddOrganizationStructure(programUnit.ProgramUnit);
                    programUnit.ProgramUnit.DetailID = programUnitResult.ID;
                }

                programUnit.ReportingUnit.DetailID = programUnit.ProgramUnit.DetailID;

                programUnit.Addresses.ForEach(item =>
                {
                    item.DetailID = programUnit.ProgramUnit.DetailID;
                });
                programUnit.ProgramUnitHierarchies.ForEach(item =>
                {
                    item.ProgramUnitID = programUnit.ProgramUnit.DetailID;
                });
                programUnit.ProgramUnitServices.ForEach(item =>
                {
                    item.DetailID = programUnit.ProgramUnit.DetailID;
                });
                programUnit.ProgramUnitServiceWorkflows.ForEach(item =>
                {
                    item.DetailID = programUnit.ProgramUnit.DetailID;
                });

                // if program unit is failed to save
                if (programUnitResult.ResultCode != 0)
                {
                    programUnitResponse.ResultCode    = programUnitResult.ResultCode;
                    programUnitResponse.ResultMessage = programUnitResult.ResultMessage;
                    return(programUnitResponse);
                }

                var reportingUnitResult = _organizationIdentifiersDataProvider.SaveOrganizationIdentifiers(new List <OrganizationIdentifiersModel>()
                {
                    programUnit.ReportingUnit
                });
                // if reporting unit is failed to save
                if (reportingUnitResult.ResultCode != 0)
                {
                    programUnitResponse.ResultCode    = reportingUnitResult.ResultCode;
                    programUnitResponse.ResultMessage = reportingUnitResult.ResultMessage;
                    return(programUnitResponse);
                }

                var programUnitAddressResult = _organizationStructureDataProvider.SaveOrganizationAddress(programUnit.Addresses);
                // if program unit address is failed to save
                if (programUnitAddressResult.ResultCode != 0)
                {
                    programUnitResponse.ResultCode    = programUnitAddressResult.ResultCode;
                    programUnitResponse.ResultMessage = programUnitAddressResult.ResultMessage;
                    return(programUnitResponse);
                }

                var programUnitHierarchyResult = _organizationStructureDataProvider.SaveOrganizationHierarchy(programUnit.ProgramUnitHierarchies, OrganizationType.ProgramUnit.ToString());
                // if program unit hierarchy is failed to save
                if (programUnitHierarchyResult.ResultCode != 0)
                {
                    programUnitResponse.ResultCode    = programUnitHierarchyResult.ResultCode;
                    programUnitResponse.ResultMessage = programUnitHierarchyResult.ResultMessage;
                    return(programUnitResponse);
                }

                var programUnitServicesResult = _organizationStructureDataProvider.SaveServiceOrganization(programUnit.ProgramUnitServices);
                // if program unit services is failed to save
                if (programUnitServicesResult.ResultCode != 0)
                {
                    programUnitResponse.ResultCode    = programUnitServicesResult.ResultCode;
                    programUnitResponse.ResultMessage = programUnitServicesResult.ResultMessage;
                    return(programUnitResponse);
                }

                var programUnitServiceWorkflowsResult = _organizationStructureDataProvider.SaveOrganizationDetailsModuleComponent(programUnit.ProgramUnitServiceWorkflows);
                // if program unit service workflows is failed to save
                if (programUnitServiceWorkflowsResult.ResultCode != 0)
                {
                    programUnitResponse.ResultCode    = programUnitServiceWorkflowsResult.ResultCode;
                    programUnitResponse.ResultMessage = programUnitServiceWorkflowsResult.ResultMessage;
                    return(programUnitResponse);
                }

                if (!programUnit.ForceRollback.GetValueOrDefault(false))
                {
                    _unitOfWork.TransactionScopeComplete(transactionScope);
                }
            }

            return(programUnitResponse);
        }