class BudgetDetailService {

    public List<BudgetDetail> fetchRelated(final List<BudgetDetail> budgetdetails,RequestInfo requestInfo) {
        if (budgetdetails != null)
            for (final BudgetDetail budgetDetail : budgetdetails) {

                // fetch related items
                if (budgetDetail.getBudget() != null && budgetDetail.getBudget().getId() != null
                        && budgetDetail.getBudget().getTenantId() != null) {
                    final Budget budget = budgetRepository.findById(budgetDetail.getBudget());
                    if (budget == null)
                        throw new InvalidDataException("budget", "budget.invalid", " Invalid budget");
                    budgetDetail.setBudget(budget);
                }

                if (budgetDetail.getFund() != null && budgetDetail.getFund().getId() != null
                        && budgetDetail.getFund().getTenantId() != null) {
                    final FundSearchContract contract = new FundSearchContract();
                    contract.setId(budgetDetail.getFund().getId());
                    contract.setTenantId(budgetDetail.getFund().getTenantId());
                    final FundContract fund = fundContractRepository.findById(contract,requestInfo);
                    if (fund == null)
                        throw new InvalidDataException("fund", "fund.invalid", " Invalid fund");
                    budgetDetail.setFund(fund);
                }

                if (budgetDetail.getBudgetGroup() != null && budgetDetail.getBudgetGroup().getId() != null
                        && budgetDetail.getBudgetGroup().getTenantId() != null) {
                    final BudgetGroupSearchContract contract = new BudgetGroupSearchContract();
                    contract.setId(budgetDetail.getBudgetGroup().getId());
                    contract.setTenantId(budgetDetail.getBudgetGroup().getTenantId());
                    final BudgetGroupContract budgetGroup = budgetGroupContractRepository.findById(contract,requestInfo);
                    if (budgetGroup == null)
                        throw new InvalidDataException("budgetGroup", "budgetGroup.invalid", " Invalid budgetGroup");
                    budgetDetail.setBudgetGroup(budgetGroup);
                }

                if (budgetDetail.getUsingDepartment() != null && budgetDetail.getUsingDepartment().getId() != null
                        && budgetDetail.getUsingDepartment().getTenantId() != null) {
                    final DepartmentRes usingDepartment = departmentRepository.getDepartmentById(
                            budgetDetail.getUsingDepartment().getId().toString(), budgetDetail.getTenantId());
                    if (usingDepartment == null || usingDepartment.getDepartment() == null
                            || usingDepartment.getDepartment().isEmpty())
                        throw new InvalidDataException("usingDepartment", "usingDepartment.invalid",
                                " Invalid usingDepartment");
                    budgetDetail.setUsingDepartment(usingDepartment.getDepartment().get(0));
                }

                if (budgetDetail.getExecutingDepartment() != null
                        && budgetDetail.getExecutingDepartment().getId() != null
                        && budgetDetail.getExecutingDepartment().getTenantId() != null) {
                    final DepartmentRes executingDepartment = departmentRepository.getDepartmentById(
                            budgetDetail.getExecutingDepartment().getId().toString(), budgetDetail.getTenantId());
                    if (executingDepartment == null)
                        throw new InvalidDataException("executingDepartment", "executingDepartment.invalid",
                                " Invalid executingDepartment");
                    budgetDetail.setExecutingDepartment(executingDepartment.getDepartment().get(0));
                }

                if (budgetDetail.getFunction() != null && budgetDetail.getFunction().getId() != null
                        && budgetDetail.getFunction().getTenantId() != null) {
                    final FunctionSearchContract contract = new FunctionSearchContract();
                    contract.setId(budgetDetail.getFunction().getId());
                    contract.setTenantId(budgetDetail.getFunction().getTenantId());
                    final FunctionContract function = functionContractRepository.findById(contract,requestInfo);
                    if (function == null)
                        throw new InvalidDataException("function", "function.invalid", " Invalid function");
                    budgetDetail.setFunction(function);
                }

                if (budgetDetail.getScheme() != null && budgetDetail.getScheme().getId() != null
                        && budgetDetail.getScheme().getTenantId() != null) {
                    final SchemeSearchContract contract = new SchemeSearchContract();
                    contract.setId(budgetDetail.getScheme().getId());
                    contract.setTenantId(budgetDetail.getScheme().getTenantId());
                    final SchemeContract scheme = schemeContractRepository.findById(contract,requestInfo);
                    if (scheme == null)
                        throw new InvalidDataException("scheme", "scheme.invalid", " Invalid scheme");
                    budgetDetail.setScheme(scheme);
                }

                if (budgetDetail.getSubScheme() != null && budgetDetail.getSubScheme().getId() != null
                        && budgetDetail.getSubScheme().getTenantId() != null) {
                    final SubSchemeSearchContract contract = new SubSchemeSearchContract();
                    contract.setId(budgetDetail.getSubScheme().getId());
                    contract.setTenantId(budgetDetail.getSubScheme().getTenantId());
                    final SubSchemeContract subScheme = subSchemeContractRepository.findById(contract,requestInfo);
                    if (subScheme == null)
                        throw new InvalidDataException("subScheme", "subScheme.invalid", " Invalid subScheme");
                    budgetDetail.setSubScheme(subScheme);
                }

                if (budgetDetail.getFunctionary() != null && budgetDetail.getFunctionary().getId() != null
                        && budgetDetail.getFunctionary().getTenantId() != null) {
                    final FunctionarySearchContract contract = new FunctionarySearchContract();
                    contract.setId(budgetDetail.getFunctionary().getId());
                    contract.setTenantId(budgetDetail.getFunctionary().getTenantId());
                    final FunctionaryContract functionary = functionaryContractRepository.findById(contract,requestInfo);
                    if (functionary == null)
                        throw new InvalidDataException("functionary", "functionary.invalid", " Invalid functionary");
                    budgetDetail.setFunctionary(functionary);
                }

                if (budgetDetail.getBoundary() != null && budgetDetail.getBoundary().getId() != null
                        && budgetDetail.getBoundary().getTenantId() != null) {
                    final Boundary boundary = boundaryRepository.getBoundaryById(budgetDetail.getBoundary().getId(),
                            budgetDetail.getBoundary().getTenantId());
                    if (boundary == null)
                        throw new InvalidDataException("boundary", "boundary.invalid", " Invalid boundary");
                    budgetDetail.setBoundary(boundary);
                }

            }

        return budgetdetails;

    }

}