package enterprise.web.toolkit.controller.user;
import java.util.ArrayList;
import java.util.List;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.apache.log4j.Logger;
import enterprise.web.tookit.exception.ValidationException;
import enterprise.web.tookit.message.Message;
import enterprise.web.tookit.message.StatusCodeMessage;
import enterprise.web.tookit.user.User;
import enterprise.web.tookit.user.UserBeanRemote;
/**
* @since 22/12/2012
*
* @author regisrocha
*
* @description Classe para atender solicitacoes referente aos dados de usuario, como cadastro, alteracao de
* senha.
*
*/
@Path("/user")
public class UserController {
/**
* LOG
*/
private final Logger LOG = Logger.getLogger(UserController.class);
@POST
@Path("/create/{userName}/{userPassword}/{confirmPassword}/{email}/{lastName}")
@Produces(MediaType.APPLICATION_JSON)
public List<Message> createUser(@PathParam("userName") String userName,
@PathParam("userPassword") String userPassword, @PathParam("confirmPassword") String confirmPassword,
@PathParam("email") String email, @PathParam("lastName") String lastName) {
LOG.info("Create user");
List<Message> listMessages = new ArrayList<Message>();
try {
final User user = new User(null, email, userName, lastName, userPassword, confirmPassword, null);
this.validarExistenciaEmailUsuario(email);
user.validateUserToInsert();
this.getServiceUserBean().createUser(user);
listMessages = new ArrayList<Message>();
listMessages.add(new Message(Boolean.FALSE, StatusCodeMessage.CREATED.getIdStatusMessage(),
"Usu�rio criado com sucesso."));
listMessages.add(new Message());
} catch (ValidationException e) {
LOG.error("", e);
listMessages = new ArrayList<Message>();
listMessages.add(new Message(Boolean.TRUE, StatusCodeMessage.INTERNAL_SERVER_ERROR.getIdStatusMessage(), e
.getMessage()));
} catch (Exception e) {
LOG.error("", e);
listMessages = new ArrayList<Message>();
listMessages.add(new Message(Boolean.TRUE, StatusCodeMessage.INTERNAL_SERVER_ERROR.getIdStatusMessage(),
"Ocorreu um erro ao gravar o usu�rio."));
}
return listMessages;
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/verificarDisponibilidade/{email}")
public Message vificarDisponibailidadeDeEmailDeUsuario(@PathParam("email") String email) {
Message message = new Message(Boolean.FALSE, null, null);
try {
final User user = new User();
user.setEmail(email);
user.validarEmail();
this.validarExistenciaEmailUsuario(email);
} catch (ValidationException e) {
LOG.error("", e);
message = new Message(Boolean.TRUE, StatusCodeMessage.NOT_ACCEPTABLE.getIdStatusMessage(), e.getMessage());
} catch (Exception e) {
LOG.error("Erro ao verificar Disponibilidade de email ", e);
message = new Message(Boolean.TRUE, StatusCodeMessage.NOT_ACCEPTABLE.getIdStatusMessage(), e.getMessage());
}
return message;
}
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Path("/recuperarSenha/{email}")
public Message recuperarSenha(@PathParam("email") String username) {
Message message = new Message(Boolean.FALSE, null, null);
try {
this.getServiceUserBean().recoverPassword(username);
} catch (Exception e) {
LOG.error("", e);
message = new Message(Boolean.TRUE, StatusCodeMessage.INTERNAL_SERVER_ERROR.getIdStatusMessage(),
e.getMessage());
}
return message;
}
/**
* @description Verifica se o email de usuario ja existe.
*
* @param String
*
* @throws NamingException
* - Caso ocorra algum erro ao obter referencia ao EJB.
*
* @throws ValidationException
* - Caso o email de usuario ja exista.
*/
private final void validarExistenciaEmailUsuario(final String email) throws ValidationException {
try {
final UserBeanRemote userBean = this.getServiceUserBean();
if (userBean.emailExist(email)) {
throw new ValidationException("E-mail de usu�rio ja existente.");
}
} catch (NamingException e) {
throw new ValidationException("Erro durante a verificacao da existencia do e-mail do usuario", e);
}
}
/**
* @since 11/01/13
*
* @description Realiza lookp para obter referencia ao UserBeanRemote.
*
* @return UserBeanRemote
*
* @throws NamingException
* Caso ocorra algum erro ao fazer lookup do EJB UserBeanRemote
*/
private UserBeanRemote getServiceUserBean() throws NamingException {
try {
final InitialContext context = new InitialContext();
final UserBeanRemote userBean = (UserBeanRemote) context.lookup("/ejb/UserBean");
return userBean;
} catch (NamingException e) {
LOG.error("Erro ao Obter UserBeanRemote: ", e);
throw e;
}
}
}