Package com.m2m.action

Source Code of com.m2m.action.RegistroAction

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.m2m.action;

import com.m2m.bl.UsuariosBL;
import com.m2m.commons.Constantes;
import com.m2m.modelo.Usuario;
import com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.actions.DispatchAction;

/**
*
* @author Clara
*/
public class RegistroAction extends DispatchAction {

    protected static Logger logger = Logger.getLogger(RegistroAction.class);

    private final static String ENTRAR = "irRegistro";
    private final static String SUCCESS = "irPreferencias";

    private UsuariosBL usuariosBL;


  public void setUsuariosBL(UsuariosBL usuariosBL){
    logger.info("usuariosBL");
    this.usuariosBL = usuariosBL;
  }

    public ActionForward irRegistro(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
       
         return mapping.findForward(ENTRAR);

     }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
     public ActionForward registrarse(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
       
            logger.info("Entrando en registro de usuario...");
            RegistroActionForm regForm = (RegistroActionForm) form;
            request.removeAttribute("errors");
            ActionErrors errors = new ActionErrors();

            //Validación de los campos del formulario
            if (regForm.getNombre() == null || regForm.getNombre().length() < 1) {
                errors.add("nombre", new ActionMessage("registro.error.nombre"));
            }
            if (regForm.getPass() == null || regForm.getPass().length() < 6) {
                errors.add("pass", new ActionMessage("registro.error.password"));
            }
            if (regForm.getPass2() == null || regForm.getPass2().length() < 6
                    || !regForm.getPass().equals(regForm.getPass())) {
                errors.add("pass2", new ActionMessage("registro.error.passwordMatch"));
            }
            if (regForm.getEmail() == null || regForm.getEmail().length() < 5
                    || ( regForm.getEmail().indexOf("@") < 0 )
                    || regForm.getEmail().indexOf(".") < 0
                    || ( regForm.getEmail().indexOf(".") + 2)> regForm.getEmail().length()
                ) {
                errors.add("email", new ActionMessage("registro.error.email"));
            }

            //Validación del captcha
            String userCaptchaResponse = request.getParameter("jcaptcha");
            boolean captchaPassed = SimpleImageCaptchaServlet.
                    validateResponse(request, userCaptchaResponse);
            if(!captchaPassed){
                errors.add("jcaptcha",new ActionMessage("registro.error.jcaptcha") );
            }

            setUsuariosBL(new UsuariosBL());
            //Compruebo si existe el usuario
            if(errors.size()<=0){
                if (usuariosBL.checkNameExists(regForm.getNombre())){
                    //FIXME: internacionalizar
                errors.add( "nombre", new ActionMessage("registro.error.nombre.registrado"));
                }
            }
            Usuario usuario = null;
            //Si hubo errores se muestran. En caso contrario vamos a éxito
            if(errors.size()>0){
                errors.add("descErrores", new ActionMessage("registro.error.general"));
                this.addErrors(request, errors);
                return mapping.getInputForward();
            }else{
               usuario = usuariosBL.saveUser(regForm.getNombre(), regForm.getPass(),
                        regForm.getEmail(),regForm.getUsuarioSkype() );
            }
            //Simulo también el login del usuario y lo mando a "preferencias"
            //para que las rellene
            request.getSession().setAttribute(Constantes.USER_INFO,
                        usuario);
            return mapping.findForward(SUCCESS);

    }

}
TOP

Related Classes of com.m2m.action.RegistroAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.