package spring3.controller;
//import net.viralpatel.spring3.form.Contact;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import spring3.form.Contact;
import spring3.form.ContactForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.servlet.ModelAndView;
import spring3.validator.ContactValidator;
import webservice.ServicioContactoStub;
import webservice.ServicioContactoStub.AgregarContacto;
import webservice.ServicioContactoStub.AgregarContactoResponse;
import webservice.ServicioContactoStub.ContactoVO;
import webservice.ServicioContactoStub.MostrarContacto;
import webservice.ServicioContactoStub.MostrarContactoResponse;
/**
* Controlador de jsp de contacto y mostrar registros
* @author Victor
*
*/
@Controller
public class ContactController {
/**
* Objeto instanciado a la clase de validacion
*/
private ContactValidator userValidator;
/**
* Constructor Inicial
* @param userValidator
*/
@Autowired
public ContactController(ContactValidator userValidator) {
this.userValidator = userValidator;
}
/**
* Agregar Contacto
* @param contact
* @param result
* @return
*/
@RequestMapping(value = "/addContact", method = RequestMethod.POST)
public ModelAndView addContact(@ModelAttribute("contact") Contact contact, BindingResult result) {
//VALIDACION DEL OBJETO
userValidator.validate(contact, result);
//SI TIENE ERROR VUELVE AL FORMULARIO
if(result.hasErrors()) {
System.out.println("ERROR");
ModelAndView mav = new ModelAndView("contact");
// mav.addObject("errors",result.getFieldErrors());
mav.addObject("contact",contact);
return mav;
}
System.out.println("First Name:" + contact.getFirstname() +
"Last Name:" + contact.getLastname());
//MANEJO ERROR EN CASO DE CAERSE LA CONEXION CON EL COMPONENTE DE SERVICIO
try {
ServicioContactoStub oStub = new ServicioContactoStub();
// AGREGA
ContactoVO oContactoVO = new ContactoVO();
oContactoVO.setNombre(contact.getFirstname());
oContactoVO.setApellido(contact.getLastname());
oContactoVO.setMail(contact.getEmail());
oContactoVO.setTelefono(contact.getTelephone());
AgregarContacto oAgregarContacto = new AgregarContacto();
oAgregarContacto.setOContactoVO(oContactoVO);
AgregarContactoResponse objResponse = oStub.agregarContacto(oAgregarContacto);
String mensaje = objResponse.get_return();
System.out.println(mensaje);
return new ModelAndView("saludo", "message", mensaje);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return new ModelAndView("error", "message", "ERROR");
}
}
/**
* Precarga jsp de contacto instanciado el objeto contacto
* @return
*/
@RequestMapping("/contacts")
public ModelAndView showContacts() {
ModelAndView mav = new ModelAndView("contact");
mav.addObject("contact",new Contact());
return mav;
}
/**
* Muestra los registros existentes
* @return
*/
@RequestMapping("/show")
public ModelAndView helloWorld() {
//String message = "Hola Mundo Spring se ha instaldo correctamente";
List<Contact> contacts = new ArrayList<Contact>();
contacts.add(new Contact("Barack", "Obama", "barack.o@whitehouse.com", "147-852-965"));
contacts.add(new Contact("George", "Bush", "george.b@whitehouse.com", "785-985-652"));
contacts.add(new Contact("Bill", "Clinton", "bill.c@whitehouse.com", "236-587-412"));
contacts.add(new Contact("Ronald", "Reagan", "ronald.r@whitehouse.com", "369-852-452"));
ContactForm contactForm = new ContactForm();
try {
ServicioContactoStub oStub = new ServicioContactoStub();
MostrarContacto oMostrarContacto = new MostrarContacto();
MostrarContactoResponse objResponde = oStub.mostrarContacto(oMostrarContacto);
ContactoVO[] contacts2= objResponde.get_return();
contactForm.setContacts(contacts2);
return new ModelAndView("showcontactrow" , "contactForm", contactForm);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return new ModelAndView("error", "message", "ERROR");
}
}
}