com.redhat.rhtracking.web.controller.ConsultantController.java Source code

Java tutorial

Introduction

Here is the source code for com.redhat.rhtracking.web.controller.ConsultantController.java

Source

/*
 * Copyright (C) 2015 Nuuptech
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.redhat.rhtracking.web.controller;

import com.redhat.rhtracking.core.services.ConsultantService;
import com.redhat.rhtracking.core.services.DeliveryMatrixService;
import com.redhat.rhtracking.core.services.DeliveryService;
import com.redhat.rhtracking.core.services.PartnerService;
import com.redhat.rhtracking.events.EventStatus;
import static com.redhat.rhtracking.web.controller.Utils.getFlashMessagesList;
import static com.redhat.rhtracking.web.controller.Utils.getMessagesList;
import com.redhat.rhtracking.web.domain.AssignationInfo;
import com.redhat.rhtracking.web.domain.ConsultantInfo;
import com.redhat.rhtracking.web.domain.DeliveryInfo;
import java.math.BigDecimal;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
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.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 *
 * @author marco-g8
 */
@Controller
public class ConsultantController {

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

    @Autowired
    private ConsultantService consultantService;
    @Autowired
    private DeliveryService deliveryService;
    @Autowired
    private DeliveryMatrixService deliveryMatrixService;

    @Autowired
    private PartnerService partnerService;

    @RequestMapping(value = "/consultant/add", method = RequestMethod.GET)
    public String saveView(Model model) {
        Map<String, Object> partners = partnerService.listAllPartnersSimple(new HashMap<String, Object>());
        model.addAllAttributes(partners);
        return "/consultant/add";
    }

    @RequestMapping(value = "/consultant/add", method = RequestMethod.POST)
    public String save(@Valid @ModelAttribute("consultantInfo") ConsultantInfo consultantInfo, BindingResult result,
            RedirectAttributes redirectAttributes, Model model, Principal principal) {
        List<String> messages = Utils.getMessagesList(model);
        redirectAttributes.addFlashAttribute("messages", messages);

        if (result.hasErrors()) {
            messages.add("warning::Datos incorrectos");
            redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.register", result);
            redirectAttributes.addFlashAttribute("consultantInfo", consultantInfo);
            return "redirect:/consultant/add";
        }

        Map<String, Object> request = new HashMap<>();
        request.put("name", consultantInfo.getName());
        request.put("partnerId", consultantInfo.getPartner());

        List<String> assignableHours = new ArrayList<>();
        if (consultantInfo.isPlatform())
            assignableHours.add("PLATFORM");
        if (consultantInfo.isMiddleware())
            assignableHours.add("MIDDLEWARE");
        if (consultantInfo.isWorkshop())
            assignableHours.add("WORKSHOP");
        request.put("assignableHours", assignableHours);

        Map<String, Object> response = consultantService.saveConsultant(request);
        if (response.get("status") == EventStatus.SUCCESS)
            messages.add("success::Consultor registrado");
        else {
            messages.add("error::Ocurrio un error al registrar el consultor.");
            redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.register", result);
            redirectAttributes.addFlashAttribute("consultantInfo", consultantInfo);
            return "redirect:/consultant/add";
        }

        return "redirect:/consultant/";
    }

    @RequestMapping(value = "/consultant/", method = RequestMethod.GET)
    public String list(@RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "20") int size, Model model) {
        Map<String, Object> request = new HashMap<>();
        request.put("pageNumber", page - 1);
        request.put("pageSize", size > 10 ? size : 20);
        Map<String, Object> listAllConsultantsPaged = consultantService.listAllConsultantsPaged(request);

        if ((EventStatus) listAllConsultantsPaged.get("status") == EventStatus.SUCCESS) {
            model.addAttribute("pageNumber", page - 1);
            model.addAllAttributes(listAllConsultantsPaged);
        } else {
            getMessagesList(model).add("error::Ocurrio un error al obtener al procesar la solicitud");
            model.addAttribute("page", new HashMap<String, Object>());
        }
        return "/consultant/list";
    }

    @RequestMapping(value = "/consultant/{id}/details", method = RequestMethod.GET)
    public String show(@PathVariable long id, Model model) {
        Map<String, Object> request = new HashMap<>();
        request.put("id", id);
        Map<String, Object> consultant = consultantService.getConsultant(request);
        model.addAllAttributes(consultant);

        Map<String, Object> listAllConsultorAsignations = deliveryService.listAllConsultorAsignations(request);
        model.addAttribute("assignations", listAllConsultorAsignations.get("list"));

        return "/consultant/details";
    }

    @RequestMapping(value = "/consultant/assignation", method = RequestMethod.GET)
    public String assignationView(@RequestParam(required = true) String id,
            @RequestParam(required = true) String type, Model model) {
        Map<String, Object> consultants = consultantService.listAllConsultants(new HashMap<String, Object>());
        model.addAttribute("consultants", (List<Map<String, Object>>) consultants.get("list"));

        Map<String, Object> request = new HashMap<>();
        request.put("identifier", id);
        Map<String, Object> opportunity = deliveryMatrixService.findOpportunityBy(request);
        model.addAllAttributes(opportunity);

        Map<String, Object> hours = deliveryMatrixService.findOpportunityHours(request);
        int hour = -1;
        BigDecimal maxPay = BigDecimal.ZERO;
        for (Map<String, Object> h : (List<Map<String, Object>>) hours.get("list")) {
            String ht = (String) h.get("hourType");
            if (ht.equals(type.toUpperCase())) {
                logger.debug("found" + h);

                hour = (int) h.get("quantity") - (int) h.get("hoursToAccrued");
                maxPay = (BigDecimal) h.get("unitPrice");
                break;
            }
        }
        model.addAttribute("hour", hour);
        model.addAttribute("maxPay", maxPay);
        model.addAttribute("hourType", type);

        return "/consultant/assignation";
    }

    @RequestMapping(value = "/consultant/assignation", method = RequestMethod.POST)
    public String assignation(@Valid @ModelAttribute("assignationInfo") AssignationInfo assignationInfo,
            BindingResult result, RedirectAttributes redirectAttributes, Model model, Principal principal) {

        List<String> messages = getFlashMessagesList(model, redirectAttributes);
        logger.debug(assignationInfo.getOpportunity());
        if (result.hasErrors()) {
            messages.add("warning::Datos incorrectos");
            redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.register", result);
            return "redirect:/consultant/assignation?id=" + assignationInfo.getOpportunity() + "&type="
                    + assignationInfo.getType();
        }

        Map<String, Object> request = new HashMap<>();
        request.put("opportunityIdentifier", assignationInfo.getOpportunity());
        request.put("consultantId", assignationInfo.getConsultant());
        request.put("assignedHours", assignationInfo.getHours());
        request.put("hourType", assignationInfo.getType().toUpperCase());
        request.put("rate", assignationInfo.getPrice());
        request.put("assignmentDate", new Date());
        request.put("active", true);

        logger.debug("assign consultant " + request);

        logger.debug("sss " + request);

        Map<String, Object> assignConsultant = deliveryService.assignConsultant(request);
        if (((EventStatus) assignConsultant.get("status")) == EventStatus.SUCCESS)
            messages.add("success::Consultor asignado correctamente");
        else
            messages.add("errror::Ocurrio un error al asignar al consultor");

        return "redirect:/opportunity/show?id=" + assignationInfo.getOpportunity();
    }

    @RequestMapping(value = "/consultant/delivery", method = RequestMethod.GET)
    public String saveDeliveryView(@RequestParam(required = true) long aid, @RequestParam(required = true) long cid,
            Model model) {

        model.addAttribute("consultantid", cid);
        model.addAttribute("assignationid", aid);

        return "/consultant/delivery";
    }

    @RequestMapping(value = "/consultant/delivery", method = RequestMethod.POST)
    public String saveDelivery(@Valid @ModelAttribute("deliveryInfo") DeliveryInfo deliveryInfo,
            BindingResult result, RedirectAttributes redirectAttributes, Model model, Principal principal) {
        List<String> messages = getFlashMessagesList(model, redirectAttributes);
        if (result.hasErrors()) {
            logger.debug("has errors");
            messages.add("warning::Datos incorrectos");
            redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.register", result);
            redirectAttributes.addFlashAttribute("deliveryInfo", deliveryInfo);

            return "redirect:/consultant/delivery?cid=" + deliveryInfo.getConsultantId() + "&aid="
                    + deliveryInfo.getAssignationId();
        }

        logger.debug("cid " + deliveryInfo.getConsultantId());
        logger.debug("aid " + deliveryInfo.getAssignationId());
        logger.debug("hrs " + deliveryInfo.getHoursDelivered());

        Map<String, Object> request = new HashMap<>();
        request.put("consultantAssignmentId", deliveryInfo.getAssignationId());
        request.put("deliveryDate", new Date());
        request.put("deliveredHours", deliveryInfo.getHoursDelivered());
        Map<String, Object> registerDeliveredHours = deliveryService.registerDeliveredHours(request);

        if ((EventStatus) registerDeliveredHours.get("status") == EventStatus.SUCCESS)
            messages.add("success::Horas registradas correctamente");
        else
            messages.add("error::Ocurrio un error al registrar las horas");

        return "redirect:/consultant/" + deliveryInfo.getConsultantId() + "/details";
    }

    @ModelAttribute("consultantInfo")
    public ConsultantInfo getConsultantInfo() {
        return new ConsultantInfo();
    }

    @ModelAttribute("assignationInfo")
    public AssignationInfo assignationInfo() {
        return new AssignationInfo();
    }

    @ModelAttribute("deliveryInfo")
    public DeliveryInfo deliveryInfo() {
        return new DeliveryInfo();
    }
}