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

Java tutorial

Introduction

Here is the source code for com.redhat.rhtracking.web.controller.PartnerController.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.PartnerService;
import com.redhat.rhtracking.events.EventStatus;
import static com.redhat.rhtracking.web.controller.Utils.getMessagesList;
import com.redhat.rhtracking.web.domain.PartnerInfo;
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.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 PartnerController {

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

    @Autowired
    private PartnerService partnerService;

    @RequestMapping(value = "/partner/add", method = RequestMethod.GET)
    public String saveView() {
        return "/partner/add";
    }

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

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

        Map<String, Object> request = new HashMap<>();
        request.put("name", partnerInfo.getName());
        request.put("contact", partnerInfo.getContact());
        request.put("telephone", partnerInfo.getTelephone());
        request.put("email", partnerInfo.getEmail());
        request.put("rfc", partnerInfo.getRfc());
        request.put("costType", partnerInfo.getCostType());

        logger.debug("saving " + request);

        Map<String, Object> response = partnerService.savePartner(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:/partner/";
    }

    @RequestMapping(value = "/partner/", 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> response = partnerService.listAllPartnersPaged(request);
        if (response.get("status") == EventStatus.SUCCESS) {
            model.addAttribute("pageNumber", page - 1);
            model.addAllAttributes(response);
        } else {
            getMessagesList(model).add("error::Ocurrio un error al obtener al procesar la solicitud");
            model.addAttribute("page", new HashMap<String, Object>());
        }

        return "/partner/list";
    }

    @RequestMapping(value = "/partner/{id}/details", method = RequestMethod.GET)
    public String show(@PathVariable long id, RedirectAttributes redirectAttributes, Model model) {
        List<String> messages = getMessagesList(model);
        Map<String, Object> request = new HashMap<>();
        request.put("id", id);
        Map<String, Object> response = partnerService.getPartnerById(request);

        model.addAllAttributes(response);

        return "/partner/details";
    }

    @ModelAttribute("partnerInfo")
    public PartnerInfo partnerInfo() {
        return new PartnerInfo();
    }

}