com.ugam.collage.plus.controller.people_count.TeamStructureController.java Source code

Java tutorial

Introduction

Here is the source code for com.ugam.collage.plus.controller.people_count.TeamStructureController.java

Source

/**
 * Class Name:         DerivePeopleCountController
 *
 * Created By:         Rishi.c2
 * Created On:         4:34:54 PM, Sep 17, 2014
 *
 * Modified By:         Rishi.c2
 * Modified On:         4:34:54 PM, Sep 22, 2014
 *
 * Description:
 *
 * Copyright (c) 2014 Ugam Interactive. All rights reserved.
 *
 * Use is subject to license terms.
 */

package com.ugam.collage.plus.controller.people_count;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ugam.collage.plus.auth.service.AuthService;
import com.ugam.collage.plus.service.people_count.TeamStructureService;
import com.ugam.collage.plus.vo.IdTextVo;
import com.ugam.collage.plus.vo.PCTeamStructureVo;

@Controller
@RequestMapping(value = "/secure/people-count/team-structure")
public class TeamStructureController {

    private static final Logger logger = LoggerFactory.getLogger(TeamStructureController.class);

    @Autowired
    private AuthService authService;

    @Autowired
    TeamStructureService teamStructureService;

    @RequestMapping(value = "/manage", method = RequestMethod.GET)
    public String manage(Locale locale, HttpServletRequest httpServletRequest, Model model) {

        logger.debug("Loading Manage Team Structure Page");

        List<String> accesslevels = authService.userAccessLevels();

        if (accesslevels.contains("L1") || accesslevels.contains("L2") || accesslevels.contains("L3")) {

            return "team-structure-manage";
        } else {
            return "error-not-authorized";
        }
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public @ResponseBody Map<String, Object> save(HttpServletRequest httpServletRequest,
            @ModelAttribute("pcTeamStructureVo") PCTeamStructureVo pcTeamStructureVo, Locale locale, Model model) {

        Map<String, Object> returnData = new HashMap<String, Object>();
        try {

            int loggedInUserId = authService.getLoggedInUserDetails().getId();
            if (loggedInUserId != pcTeamStructureVo.getMgrEmployeeId() && !authService.userHasAccessLevel("L3")) {

                return returnData;
            }

            Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
            pcTeamStructureVo.extractAssignedEmployees(parameterMap);
            pcTeamStructureVo.extractProjectEmpCount(parameterMap);
            pcTeamStructureVo.extractEmpRules(parameterMap);
            pcTeamStructureVo.extractProportions(parameterMap);
            pcTeamStructureVo.extractApportionApproachs(parameterMap);

            teamStructureService.saveTeamStructure(pcTeamStructureVo);

            returnData.put("success", "true");
            returnData.put("message", "Saved");
        } catch (Exception e) {
            returnData.put("success", "false");
            returnData.put("message", "Error reading/saving Data from UI - " + e.getLocalizedMessage());
            logger.error("Error reading/saving Data from UI", e);
        }

        return returnData;

    }

    @RequestMapping(value = "/mgrEmployeeList", headers = "Accept=application/json")
    public @ResponseBody List<IdTextVo> mgrEmployeeList(Locale locale, Model model) {

        List<IdTextVo> mgrEmployeeList = teamStructureService.getMgrEmployeeList();
        return mgrEmployeeList;
    }

    @RequestMapping(value = "/employeeList/{yearId}/{monthId}/{costCenterId}/{mgrEmployeeId}", method = RequestMethod.GET, headers = "Accept=application/json")
    public @ResponseBody List<IdTextVo> employeeList(@PathVariable Integer yearId, @PathVariable Integer monthId,
            @PathVariable String costCenterId, @PathVariable Integer mgrEmployeeId, Locale locale, Model model) {

        List<IdTextVo> employeeMasterVoList = teamStructureService.getEmployees(yearId, monthId, costCenterId,
                mgrEmployeeId);
        Collections.sort(employeeMasterVoList);
        return employeeMasterVoList;

    }

    @RequestMapping(value = "/mgrTeamData/{yearId}/{monthId}/{costCenterId}/{mgrEmployeeId}", method = RequestMethod.GET, headers = "Accept=application/json")
    public @ResponseBody PCTeamStructureVo mgrTeamData(@PathVariable Integer yearId, @PathVariable Integer monthId,
            @PathVariable String costCenterId, @PathVariable Integer mgrEmployeeId, Locale locale, Model model) {

        PCTeamStructureVo employeeMasterVoList = teamStructureService.getMgrTeamData(yearId, monthId, costCenterId,
                mgrEmployeeId);

        return employeeMasterVoList;

    }

    @RequestMapping(value = "/downLoadFile", method = RequestMethod.GET)
    public void downLoadFile(HttpServletRequest request, HttpServletResponse response) {

        try {
            String docDir = System.getProperty("docDir");
            String fileName = request.getParameter("fileName");
            File file = new File(docDir + "/" + fileName);
            InputStream in = new BufferedInputStream(new FileInputStream(file));

            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);

            ServletOutputStream out = response.getOutputStream();
            IOUtils.copy(in, out);
            response.flushBuffer();
        } catch (Exception e) {
            logger.error("", e);
        }
    }
}