com.ugam.collage.plus.service.people_count.impl.TeamStructureServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.ugam.collage.plus.service.people_count.impl.TeamStructureServiceImpl.java

Source

/**
 * Class Name:         TeamStructureServiceImpl
 *
 * Created By:         soumya.b2
 * Created On:         7:18:21 PM, Sep 24, 2014
 *
 * Modified By:         soumya.b2
 * Modified On:         7:18:21 PM, Sep 24, 2014
 *
 * Description:
 *
 * Copyright (c) 2014 Ugam Solutions. All rights reserved.
 *
 * Use is subject to license terms.
 */

package com.ugam.collage.plus.service.people_count.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.ugam.collage.plus.common.utils.Constants;
import com.ugam.collage.plus.dao.EmpClientProjectTeamStructDao;
import com.ugam.collage.plus.dao.EmpCntPcApportionApproachDao;
import com.ugam.collage.plus.dao.EmpFsAccessDao;
import com.ugam.collage.plus.dao.EmployeeMasterDao;
import com.ugam.collage.plus.dao.EmployeeMonthlyAssignmentDao;
import com.ugam.collage.plus.dao.EmployeePcTagsTeamStructDao;
import com.ugam.collage.plus.dao.ProjectMasterDao;
import com.ugam.collage.plus.dao.TsCommentsDao;
import com.ugam.collage.plus.model.CompanyMaster;
import com.ugam.collage.plus.model.CountClassification;
import com.ugam.collage.plus.model.EmpClientProjectTeamStruct;
import com.ugam.collage.plus.model.EmpCntPcApportionApproach;
import com.ugam.collage.plus.model.EmpFsAccess;
import com.ugam.collage.plus.model.EmployeeCntRules;
import com.ugam.collage.plus.model.EmployeeMaster;
import com.ugam.collage.plus.model.EmployeeMonthlyAssignment;
import com.ugam.collage.plus.model.EmployeePcTagsTeamStruct;
import com.ugam.collage.plus.model.ProfitCentre;
import com.ugam.collage.plus.model.ProjectMaster;
import com.ugam.collage.plus.model.TabMonth;
import com.ugam.collage.plus.model.TabYear;
import com.ugam.collage.plus.model.TsComments;
import com.ugam.collage.plus.service.people_count.TeamStructureService;
import com.ugam.collage.plus.vo.IdTextVo;
import com.ugam.collage.plus.vo.PCTeamStructureVo;

@Transactional
public class TeamStructureServiceImpl implements TeamStructureService {

    @Autowired
    private EmpFsAccessDao empFsAccessDao;

    @Autowired
    private EmployeeMasterDao employeeMasterDao;

    @Autowired
    private EmployeeMonthlyAssignmentDao employeeMonthlyAssignmentDao;

    @Autowired
    private EmpClientProjectTeamStructDao empClientProjectTeamStructDao;

    @Autowired
    private ProjectMasterDao projectMasterDao;

    @Autowired
    private TsCommentsDao tsCommentsDao;

    @Autowired
    private EmpCntPcApportionApproachDao empCntPcApportionApproachDao;

    @Autowired
    private EmployeePcTagsTeamStructDao employeePcTagsTeamStructDao;

    /*
     * (non-Javadoc)
     *
     * @see com.ugam.collage.plus.service.people_count.TeamStructureService#
     * saveTeamStructure(com.ugam .collage.plus.vo.PCTeamStructureVo)
     */
    @Override
    public boolean saveTeamStructure(PCTeamStructureVo pcTeamStructureVo) {

        Integer monthId = pcTeamStructureVo.getMonthId();
        String costCentreId = pcTeamStructureVo.getCostCentreId();
        Integer yearId = pcTeamStructureVo.getYearId();

        TabMonth tabMonth = new TabMonth();
        tabMonth.setMonthId(monthId);

        TabYear tabYear = new TabYear();
        tabYear.setYearId(yearId);

        Date now = new Date();

        List<Integer> selectedEmps = pcTeamStructureVo.getSelectedEmp() != null ? pcTeamStructureVo.getSelectedEmp()
                : new ArrayList<Integer>();
        Set<Integer> selectedEmployees = new HashSet<Integer>(selectedEmps);

        Integer mgrEmployeeId = pcTeamStructureVo.getMgrEmployeeId();
        EmpFsAccess mgrEmpFsAccess = empFsAccessDao.read(mgrEmployeeId);

        List<EmployeeMonthlyAssignment> emaList = employeeMonthlyAssignmentDao.findByYearMonthCostCenterMgr(yearId,
                monthId, costCentreId, mgrEmployeeId);
        Map<Integer, Integer> employeeRulesVo = pcTeamStructureVo.getEmployeeRules();

        for (EmployeeMonthlyAssignment ema : emaList) {
            int employeeId = ema.getEmployeeMaster().getEmployeeId();
            if (selectedEmployees.contains(employeeId)) {

                EmployeeCntRules rule = new EmployeeCntRules();
                rule.setId(employeeRulesVo.get(employeeId));

                ema.setEmployeeCntRulesByClosingRuleId(rule);
                ema.setEmpFsAccessByTsClosingPickedUpBy(mgrEmpFsAccess);

                // Keep popping out the emplyees from the set, whatever is left
                // in the end are new selections
                selectedEmployees.remove(employeeId);
            } else {
                ema.setEmpFsAccessByTsClosingPickedUpBy(null);
            }
            ema.setUpdatedOn(now);
            employeeMonthlyAssignmentDao.saveOrUpdate(ema);
        }
        int countStatus = 1;
        for (Integer selectedEmployee : selectedEmployees) {
            List<EmployeeMonthlyAssignment> emaList2 = employeeMonthlyAssignmentDao
                    .findByYearMonthCostCenterEmp(yearId, monthId, costCentreId, selectedEmployee);

            if (emaList2.isEmpty()) {
                throw new IllegalArgumentException(
                        "no record found in employee_monhtly_assignment for employee ID: " + selectedEmployee);
            }
            EmployeeMonthlyAssignment ema = emaList2.get(0);
            ema.setEmpFsAccessByTsClosingPickedUpBy(mgrEmpFsAccess);

            EmployeeCntRules rule = new EmployeeCntRules();
            rule.setId(employeeRulesVo.get(selectedEmployee));
            ema.setEmployeeCntRulesByClosingRuleId(rule);
            ema.setUpdatedOn(now);
            employeeMonthlyAssignmentDao.saveOrUpdate(ema);

        }

        Set<Integer> assignedEmployeeSet = new HashSet<Integer>();
        Map<Integer, List<Integer>> assignedEmployees = pcTeamStructureVo.getAssignedEmployees();

        for (Integer assignedEmployee : assignedEmployees.keySet()) {
            assignedEmployeeSet.addAll(assignedEmployees.get(assignedEmployee));
        }

        List<EmpClientProjectTeamStruct> ecptsListToDelete = empClientProjectTeamStructDao.findByYearMonthTypeMgr(
                yearId, monthId, Constants.COUNT_CLASSIFICATION_TYPE_CLOSING, mgrEmployeeId);
        for (EmpClientProjectTeamStruct ecpts : ecptsListToDelete) {
            empClientProjectTeamStructDao.delete(ecpts);
        }

        CountClassification countClassification = new CountClassification();
        countClassification.setId(Constants.COUNT_CLASSIFICATION_TYPE_CLOSING);
        List<Integer> selectedProjectVos = pcTeamStructureVo.getSelectedProject() != null
                ? pcTeamStructureVo.getSelectedProject()
                : new ArrayList<Integer>();
        Set<Integer> selectedProjects = new HashSet<Integer>(selectedProjectVos);
        for (Integer projectId : assignedEmployees.keySet()) {
            if (!selectedProjects.contains(projectId)) {
                continue;
            }
            ProjectMaster project = projectMasterDao.read(projectId);
            CompanyMaster company = project.getCompanyMaster();

            List<Integer> projectEmpList = assignedEmployees.get(projectId);
            for (Integer employeeId : projectEmpList) {

                EmployeeMaster employee = new EmployeeMaster();
                employee.setEmployeeId(employeeId);

                EmpClientProjectTeamStruct ecmpts = new EmpClientProjectTeamStruct(employee, company,
                        countClassification, tabMonth, project, mgrEmpFsAccess, tabYear, now);
                empClientProjectTeamStructDao.create(ecmpts);
            }
        }

        List<EmpCntPcApportionApproach> empCntPcApportionApproachList = empCntPcApportionApproachDao
                .findByMonth(yearId, monthId, mgrEmployeeId, Constants.COUNT_CLASSIFICATION_TYPE_CLOSING);
        for (EmpCntPcApportionApproach empCntPcApportionApproach : empCntPcApportionApproachList) {
            empCntPcApportionApproachDao.delete(empCntPcApportionApproach);
        }
        Map<Integer, Integer> apportionApproachsVo = pcTeamStructureVo.getApportionApproachs();
        for (Integer employeeId : apportionApproachsVo.keySet()) {
            EmployeeMaster employeeMaster = new EmployeeMaster();
            employeeMaster.setEmployeeId(employeeId);
            int apportionApproach = apportionApproachsVo.get(employeeId);
            EmpCntPcApportionApproach empCntPcApportionApproach = new EmpCntPcApportionApproach(employeeMaster,
                    countClassification, tabMonth, mgrEmpFsAccess, tabYear, apportionApproach);
            empCntPcApportionApproachDao.create(empCntPcApportionApproach);
        }

        List<EmployeePcTagsTeamStruct> employeePcTagsTeamStructs = employeePcTagsTeamStructDao.findByMonth(yearId,
                monthId, mgrEmployeeId, Constants.COUNT_CLASSIFICATION_TYPE_CLOSING);
        for (EmployeePcTagsTeamStruct employeePcTagsTeamStruct : employeePcTagsTeamStructs) {
            employeePcTagsTeamStructDao.delete(employeePcTagsTeamStruct);
        }
        Map<Integer, Map<String, Float>> proportionsVo = pcTeamStructureVo.getProportions();
        for (Integer employeeId : proportionsVo.keySet()) {
            Map<String, Float> pcProportions = proportionsVo.get(employeeId);
            EmployeeMaster employeeMaster = new EmployeeMaster();
            employeeMaster.setEmployeeId(employeeId);

            for (String pcId : pcProportions.keySet()) {

                BigDecimal proportion = new BigDecimal(pcProportions.get(pcId));
                ProfitCentre profitCentre = new ProfitCentre();
                profitCentre.setProfitCentreId(pcId);
                EmployeePcTagsTeamStruct employeePcTagsTeamStruct = new EmployeePcTagsTeamStruct(employeeMaster,
                        profitCentre, countClassification, tabMonth, mgrEmpFsAccess, tabYear, proportion);
                employeePcTagsTeamStructDao.create(employeePcTagsTeamStruct);
            }
        }

        String additionsComment = pcTeamStructureVo.getAdditionsComment() == null ? ""
                : pcTeamStructureVo.getAdditionsComment().trim();
        String attritionsComment = pcTeamStructureVo.getAttritionsComment() == null ? ""
                : pcTeamStructureVo.getAttritionsComment().trim();
        String changesComment = pcTeamStructureVo.getChangesComment() == null ? ""
                : pcTeamStructureVo.getChangesComment().trim();
        if (!(additionsComment.isEmpty() || attritionsComment.isEmpty() || changesComment.isEmpty())) {
            TsComments comment = new TsComments(mgrEmpFsAccess, tabMonth, tabYear, additionsComment,
                    attritionsComment, changesComment, countStatus, "", now);
            tsCommentsDao.create(comment);
        }

        return true;
    }

    /*
     * (non-Javadoc)
     *
     * @see com.ugam.collage.plus.service.people_count.TeamStructureService#
     * getMgrEmployeeList()
     */
    @Override
    public List<IdTextVo> getMgrEmployeeList() {

        List<IdTextVo> mgrEmployeeVoList = new ArrayList<IdTextVo>();
        // TODO: This query simply returns all employees, need to control it
        // with RBAC
        List<EmpFsAccess> mgrEmployeeList = empFsAccessDao.findByActive();
        for (EmpFsAccess empFsAccess : mgrEmployeeList) {
            IdTextVo empVo = new IdTextVo(empFsAccess.getEmployeeId(),
                    empFsAccess.getPersonMaster().getDisplayName());
            mgrEmployeeVoList.add(empVo);
        }
        return mgrEmployeeVoList;
    }

    @Override
    public List<IdTextVo> getEmployees(Integer yearId, Integer monthId, String costCenterId,
            Integer mgrEmployeeId) {

        Set<EmployeeMaster> employeeSet = new HashSet<EmployeeMaster>();

        List<EmployeeMonthlyAssignment> emaList = employeeMonthlyAssignmentDao.findByYearMonthCostCenterMgr(yearId,
                monthId, costCenterId, mgrEmployeeId);
        for (EmployeeMonthlyAssignment employeeMonthlyAssignment : emaList) {
            EmployeeMaster employeeMaster = employeeMonthlyAssignment.getEmployeeMaster();
            employeeSet.add(employeeMaster);
        }

        // TODO: To be used in case of show all names option in UI
        // Set<EmployeeMaster> employeeOtherSet = new
        // HashSet<EmployeeMaster>(employeeMasterDao.findByYearMonthCostCenterNotUnderMgr(yearId,
        // monthId, costCenterId, mgrEmployeeId));
        //
        // for (EmployeeMaster employee : employeeMasterDao.findByActive()) {
        //
        // if (employeeOtherSet.contains(employee)) {
        // continue;
        // }
        //
        // employeeSet.add(employee);
        // }

        ArrayList<IdTextVo> empList = new ArrayList<IdTextVo>();
        for (EmployeeMaster employee : employeeSet) {
            IdTextVo employeeVo = new IdTextVo(employee.getEmployeeId(), employee.getEmployeeName());
            empList.add(employeeVo);
        }

        Collections.sort(empList);
        return empList;
    }

    /*
     * (non-Javadoc)
     *
     * @see com.ugam.collage.plus.service.people_count.TeamStructureService#
     * getMgrTeamData(java.lang.Integer, java.lang.Integer, java.lang.String,
     * java.lang.Integer)
     */
    @Override
    public PCTeamStructureVo getMgrTeamData(Integer yearId, Integer monthId, String costCentreId,
            Integer mgrEmployeeId) {

        Map<Integer, List<Integer>> assignedEmployees = new HashMap<Integer, List<Integer>>();
        Set<Integer> selectedProject = new HashSet<Integer>();
        Map<Integer, Integer> employeeRulesVo = new HashMap<Integer, Integer>();
        Set<Integer> selectedClient = new HashSet<Integer>();

        List<EmployeeMonthlyAssignment> emaList = employeeMonthlyAssignmentDao.findByYearMonthCostCenterMgr(yearId,
                monthId, costCentreId, mgrEmployeeId);
        for (EmployeeMonthlyAssignment ema : emaList) {
            if (ema.getEmpFsAccessByTsClosingPickedUpBy() == null) {
                continue;
            }
            Integer ruleId = ema.getEmployeeCntRulesByClosingRuleId() != null
                    ? ema.getEmployeeCntRulesByClosingRuleId().getId()
                    : null;
            Integer employeeId = ema.getEmployeeMaster().getEmployeeId();
            employeeRulesVo.put(employeeId, ruleId);
        }

        List<EmpClientProjectTeamStruct> teamStructure = empClientProjectTeamStructDao.findByYearMonthTypeMgr(
                yearId, monthId, Constants.COUNT_CLASSIFICATION_TYPE_CLOSING, mgrEmployeeId);
        for (EmpClientProjectTeamStruct struct : teamStructure) {
            ProjectMaster projectMaster = struct.getProjectMaster();
            Integer projectId = projectMaster.getProjectId();
            selectedProject.add(projectId);
            Integer companyId = projectMaster.getCompanyMaster().getCompanyId();
            selectedClient.add(companyId);

            List<Integer> projectList = assignedEmployees.get(projectId);
            if (projectList == null) {
                projectList = new ArrayList<Integer>();
                assignedEmployees.put(projectId, projectList);
            }
            projectList.add(struct.getEmployeeMaster().getEmployeeId());
        }

        Set<Integer> selectedEmployees = employeeRulesVo.keySet();

        PCTeamStructureVo teamStructureVo = new PCTeamStructureVo(assignedEmployees,
                new ArrayList<Integer>(selectedProject), employeeRulesVo, new ArrayList<Integer>(selectedClient));
        teamStructureVo.setSelectedEmp(new ArrayList<Integer>(selectedEmployees));

        Map<Integer, Integer> apportionApproachVos = new HashMap<Integer, Integer>();
        List<EmpCntPcApportionApproach> apportionApproachs = empCntPcApportionApproachDao.findByMonth(yearId,
                monthId, mgrEmployeeId, Constants.COUNT_CLASSIFICATION_TYPE_CLOSING);
        for (EmpCntPcApportionApproach apportionApproach : apportionApproachs) {
            apportionApproachVos.put(apportionApproach.getEmployeeMaster().getEmployeeId(),
                    apportionApproach.getApportionApproach());
        }
        teamStructureVo.setApportionApproachs(apportionApproachVos);

        Map<Integer, Map<String, Float>> proportionVos = new HashMap<Integer, Map<String, Float>>();

        List<EmployeePcTagsTeamStruct> employeePcTagsTeamStructs = employeePcTagsTeamStructDao.findByMonth(yearId,
                monthId, mgrEmployeeId, Constants.COUNT_CLASSIFICATION_TYPE_CLOSING);
        for (EmployeePcTagsTeamStruct employeePcTagsTeamStruct : employeePcTagsTeamStructs) {
            int employeeId = employeePcTagsTeamStruct.getEmployeeMaster().getEmployeeId();
            Map<String, Float> employeeMap = proportionVos.get(employeeId);
            if (employeeMap == null) {
                employeeMap = new HashMap<String, Float>();
                proportionVos.put(employeeId, employeeMap);
            }
            BigDecimal pcProportion = employeePcTagsTeamStruct.getProportion();
            String profitCentreId = employeePcTagsTeamStruct.getProfitCentre().getProfitCentreId();
            employeeMap.put(profitCentreId, pcProportion.floatValue());
        }

        teamStructureVo.setProportions(proportionVos);
        return teamStructureVo;
    }
}