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

Java tutorial

Introduction

Here is the source code for com.redhat.rhtracking.web.controller.DeliveryMatrixController.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.DeliveryMatrixService;
import com.redhat.rhtracking.events.EventStatus;
import static com.redhat.rhtracking.web.controller.Utils.getFlashMessagesList;
import com.redhat.rhtracking.web.domain.DeliveryMatrixInfo;
import com.redhat.rhtracking.web.domain.DeliveryMatrixStatusInfo;
import java.security.Principal;
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.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 DeliveryMatrixController {

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

    @Autowired
    private DeliveryMatrixService deliveryMatrixService;

    @RequestMapping(value = "/matrix/add", method = RequestMethod.GET)
    public String saveView(@RequestParam(required = true) String id, Model model) {
        //Map<String, Object> opportunities = deliveryMatrixService.listAllOpportunities(new HashMap<String, Object>());
        //model.addAttribute("opportunities", opportunities.get("list"));
        Map<String, Object> request = new HashMap<>();
        request.put("identifier", id);
        Map<String, Object> opportunity = deliveryMatrixService.findOpportunityBy(request);
        model.addAttribute("opportunity", opportunity);
        logger.debug("opportunity " + opportunity);

        return "/matrix/add";
    }

    @RequestMapping(value = "/matrix/add", method = RequestMethod.POST)
    public String save(@Valid @ModelAttribute("deliveryMatrixInfo") DeliveryMatrixInfo deliveryMatrixInfo,
            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("deliveryMatrixInfo", deliveryMatrixInfo);
            return "redirect:/matrix/add?id=" + deliveryMatrixInfo.getOpportunity();
        }

        Map<String, Object> request = new HashMap<>();

        request.put("salesOrder", deliveryMatrixInfo.getSalesOrder());
        request.put("opportunityIdentifier", deliveryMatrixInfo.getOpportunity());
        request.put("startDate", deliveryMatrixInfo.getStartDate());
        request.put("endDate", deliveryMatrixInfo.getEndDate());
        request.put("payNow", deliveryMatrixInfo.isPayNow());
        request.put("deliveryStatus", deliveryMatrixInfo.getDeliveryStatus());
        request.put("deliverySubStatus", deliveryMatrixInfo.getDeliverySubStatus());
        request.put("deliveryProbability", deliveryMatrixInfo.getDeliveryProbability());

        Map<String, Object> response = deliveryMatrixService.saveDeliveryMatrix(request);
        if (response.get("status") == com.redhat.rhtracking.events.EventStatus.SUCCESS)
            messages.add("success::Agregado correctamente");
        else
            messages.add("error::Ocurrio un error al registrar la matriz de entrega.");
        return "redirect:/opportunity/show?id=" + deliveryMatrixInfo.getOpportunity();
    }

    @RequestMapping(value = "/opportunity/updateStatus", method = RequestMethod.POST)
    public String updateStatus(@RequestParam(required = true) String id,
            @Valid @ModelAttribute("deliveryMatrixStatusInfo") DeliveryMatrixStatusInfo dmInfo,
            BindingResult result, RedirectAttributes redirectAttributes, Model model, Principal principal) {
        List<String> messages = getFlashMessagesList(model, redirectAttributes);
        if (result.hasErrors()) {
            messages.add("warning::Los datos introducidos contienen errores.");
            return "redirect:/opportunity/show?id=" + id;
        }

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

        // verifica recibido
        if ((EventStatus) opportunity.get("status") != EventStatus.SUCCESS) {
            messages.add("warning::Ocurrio un error al actualizar el estado.");
            return "redirect:/opportunity/show?id=" + id;
        } else if (!opportunity.containsKey("deliveryMatrix")) {
            messages.add("warning::No se encontro la matrix de delivery");
            return "redirect:/opportunity/show?id=" + id;
        }

        Map<String, Object> deliverymatrix = (Map<String, Object>) opportunity.get("deliveryMatrix");
        deliverymatrix.put("deliveryStatus", dmInfo.getDeliveryStatus());
        deliverymatrix.put("deliverySubStatus", dmInfo.getDeliverySubStatus());
        deliverymatrix.put("deliveryProbability", dmInfo.getDeliveryProbability());

        // update delivery Matrix
        Map<String, Object> updatedm = deliveryMatrixService.updateDeliveryMatrix(deliverymatrix);
        if ((EventStatus) opportunity.get("status") != EventStatus.SUCCESS)
            messages.add("warning::Ocurrio un error al actualizar el estado.");
        else
            messages.add("info::Estado actualizado.");
        return "redirect:/opportunity/show?id=" + id;
    }

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

        model.addAllAttributes(listAllConsultantsPaged);
        return "/matrix/list";
    }

    @ModelAttribute("deliveryMatrixInfo")
    public DeliveryMatrixInfo getDeliveryMatrixInfo() {
        return new DeliveryMatrixInfo();
    }

    @ModelAttribute("deliveryMatrixStatusInfo")
    public DeliveryMatrixStatusInfo deliveryMatrixStatusInfo() {
        return new DeliveryMatrixStatusInfo();
    }

}