package voxo.server.actions;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.SQLException;
import java.util.ArrayList;
import voxo.common.entities.User;
import voxo.common.enums.EnumPacket;
import voxo.common.packets.LoginPacket;
import voxo.common.packets.RegisterPacket;
import voxo.server.Messages;
import voxo.server.exceptions.RequestException;
import voxo.server.managers.UserManager;
public class UserRegisterAction {
public UserRegisterAction(InetAddress ip, RegisterPacket o) throws UnknownHostException, IOException, SQLException, RequestException {
// Validators
if (o.getEmail().matches(Messages.getString("UserRegisterAction.CFG_RegexEmail"))) { //$NON-NLS-1$
// TODO
new SendMessageAction(Messages.getString("UserRegisterAction.InvalidEmail"), new User(null, null, null, null, ip.getHostAddress(), null, null), EnumPacket.SERVER_Error); //$NON-NLS-1$
throw new RequestException(Messages.getString("UserRegisterAction.ERR_FailedEmailRegex")); //$NON-NLS-1$
}
if (o.getPwd().length() < 6) {
new SendMessageAction(Messages.getString("UserRegisterAction.InvalidPassword"), new User(null, null, null, null, ip.getHostAddress(), null, null), EnumPacket.SERVER_Error); //$NON-NLS-1$
throw new RequestException(Messages.getString("UserRegisterAction.ERR_FailedPassword")); //$NON-NLS-1$
}
// Check existing
ArrayList<User> alChecks = UserManager.searchUserOr(new User(null, o.getUsername(), null, o.getEmail(), null, null, null));
if (alChecks.size() > 0) {
if (alChecks.get(0).getEmail() == o.getEmail()) {
new SendMessageAction(Messages.getString("UserRegisterAction.EmailUsed"), new User(null, null, null, null, ip.getHostAddress(), null, null), EnumPacket.SERVER_Error); //$NON-NLS-1$
throw new RequestException(Messages.getString("UserRegisterAction.ERR_EmailExist")); //$NON-NLS-1$
} else if (alChecks.get(0).getUsername() == o.getUsername()) {
new SendMessageAction(Messages.getString("UserRegisterAction.UsernameUsed"), new User(null, null, null, null, ip.getHostAddress(), null, null), EnumPacket.SERVER_Error); //$NON-NLS-1$
throw new RequestException(Messages.getString("UserRegisterAction.ERR_UsernameExist")); //$NON-NLS-1$
}
return;
}
// Create user
User u = new User(null, o.getUsername(), o.getPwd(), o.getEmail(), null, false, null);
// Save user
UserManager.createUser(u);
// Login user
new UserLoginAction(ip, new LoginPacket(o.getUsername(), o.getPwd()));
}
}