Java tutorial
/* * 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.CustomerService; import com.redhat.rhtracking.events.EventStatus; import com.redhat.rhtracking.web.domain.CustomerInfo; 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 CustomerController { private static final Logger logger = Logger.getLogger(CustomerController.class); @Autowired private CustomerService customerService; @RequestMapping(value = "/customer/add", method = RequestMethod.GET) public String saveView() { return "/customer/add"; } @RequestMapping(value = "/customer/add", method = RequestMethod.POST) public String save(@Valid @ModelAttribute("customerInfo") CustomerInfo customerInfo, BindingResult result, RedirectAttributes redirectAttributes, Model model, Principal principal) { List<String> messages = Utils.getMessagesList(model); redirectAttributes.addFlashAttribute("messages", messages); if (result.hasErrors()) { messages.add("warning::some field where empty."); redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.register", result); redirectAttributes.addFlashAttribute("customerInfo", customerInfo); return "redirect:/customer/add"; } Map<String, Object> request = new HashMap<>(); request.put("name", customerInfo.getName()); request.put("address", customerInfo.getAddress()); request.put("telephone", customerInfo.getTelephone()); request.put("billable", customerInfo.isBillable()); request.put("rfc", customerInfo.getRfc()); request.put("type", customerInfo.getType().toUpperCase()); Map<String, Object> response; response = customerService.addCustomer(request); if (response.get("status") == com.redhat.rhtracking.events.EventStatus.SUCCESS) messages.add("success::Usuario agregado correctamente."); else { messages.add("error::Ocurrio un error al agregar el usuario."); return "/customer/add"; } return "redirect:/customer/"; } @RequestMapping(value = "/customer/", 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); Map<String, Object> response = customerService.listAllCustomers(request); model.addAllAttributes(response); return "/customer/list"; } @RequestMapping(value = "/customer/{id}/details", method = RequestMethod.GET) public String show(@PathVariable long id, Model model, RedirectAttributes redirectAttributes) { List<String> messages = Utils.getFlashMessagesList(model, redirectAttributes); Map<String, Object> request = new HashMap<>(); request.put("id", id); Map<String, Object> response = customerService.findCustomer(request); if ((EventStatus) response.get("status") == EventStatus.SUCCESS) { model.addAllAttributes(response); } else { messages.add("error::Cliente no encontrado."); return "redirect:/customer/"; } return "/customer/details"; } @ModelAttribute("customerInfo") public CustomerInfo getCustomerInfo() { return new CustomerInfo(); } }