package src;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import br.com.procempa.modus.entity.Deficiencia;
import br.com.procempa.modus.entity.Endereco;
import br.com.procempa.modus.entity.Escolaridade;
import br.com.procempa.modus.entity.Persistent;
import br.com.procempa.modus.entity.Usuario;
public class UsuarioMigration extends EntityMigrator {
public UsuarioMigration(Connection connSource, Connection connDestination) {
super(connSource, connDestination);
}
public PreparedStatement prepareStatement(ResultSet rs,
PreparedStatement statement, List<String> messages) throws Exception {
Usuario user = (Usuario) getObject(rs);
statement.setLong(1, user.getId());
statement.setDate(2, new java.sql.Date(user.getTimestamp().getTime()));
statement.setString(3, user.getNome());
statement.setString(4, user.getEmail());
statement.setString(5, user.getEndereco().getBairro());
statement.setString(6, user.getEndereco().getCep());
statement.setString(7, user.getEndereco().getCidade());
statement.setString(8, user.getEndereco().getComplemento());
statement.setString(9, user.getEndereco().getLogradouro());
statement.setString(10, user.getEndereco().getNumero());
statement.setString(11, user.getEndereco().getPais());
statement.setString(12, user.getEndereco().getUf());
statement.setString(13, user.getTelefone());
statement.setString(14, user.getRg());
statement.setString(15, user.getEmissor());
statement.setDate(16, new java.sql.Date(user.getDataNascimento()
.getTime()));
statement.setBoolean(17, user.getEstudante());
statement.setString(18, user.getOcupacao());
statement.setBoolean(19, user.getTrabalha());
statement.setInt(20, user.getSexo());
statement.setInt(21, user.getEstadoCivil());
statement.setInt(22, user.getRaca());
statement.setInt(23, user.getEscolaridade().getNivel());
statement.setString(24, user.getEscolaridade().getSerie());
statement.setDate(25, new java.sql.Date(user.getDataCadastro()
.getTime()));
statement.setString(26, user.getSenha());
statement.setInt(27, user.getPerfil());
statement.setString(28, user.getObservacao());
statement.setInt(29, user.getDeficiencia());
statement.setNull(30, Types.NULL);
return statement;
}
public Persistent getObject(ResultSet rs) throws Exception {
Usuario user = new Usuario();
user.setId(rs.getLong(1));
user.setTimestamp(new Timestamp(rs.getDate(2).getTime()));
user.setNome(rs.getString(3));
user.setEmail(rs.getString(13));
user.setEndereco(new Endereco());
user.getEndereco().setBairro(
StringUtils.defaultIfEmpty(rs.getString(4), ""));
user.getEndereco().setCep(
StringUtils.defaultIfEmpty(rs.getString(5), ""));
user.getEndereco().setCidade(
StringUtils.defaultIfEmpty(rs.getString(6), ""));
user.getEndereco().setComplemento(
StringUtils.defaultIfEmpty(rs.getString(7), ""));
user.getEndereco().setLogradouro(
StringUtils.defaultIfEmpty(rs.getString(8), ""));
user.getEndereco().setNumero(
StringUtils.defaultIfEmpty(rs.getString(9), ""));
user.getEndereco().setPais(
StringUtils.defaultIfEmpty(rs.getString(10), ""));
user.getEndereco().setUf(
StringUtils.defaultIfEmpty(rs.getString(11), ""));
user.setTelefone(rs.getString(12));
user.setRg(rs.getString(14));
user.setEmissor(StringUtils.defaultIfEmpty(rs.getString(15), ""));
if (rs.getDate(16) == null) {
user.setDataNascimento(new Date(new java.util.Date().getTime()));
} else {
user.setDataNascimento(rs.getDate(16));
}
user.setEstudante(rs.getBoolean(17));
user.setOcupacao(rs.getString(18));
user.setTrabalha(rs.getBoolean(19));
user.setSexo(rs.getInt(20));
user.setEstadoCivil(rs.getInt(21));
user.setRaca(rs.getInt(22));
user.setEscolaridade(new Escolaridade());
user.getEscolaridade().setNivel(rs.getInt(25));
user.getEscolaridade().setSerie(
StringUtils.defaultIfEmpty(rs.getString(26), ""));
if (rs.getDate(27) == null) {
user.setDataCadastro(new Date(new java.util.Date().getTime()));
} else {
user.setDataCadastro(rs.getDate(27));
}
user.setSenha(StringUtils.defaultIfEmpty(rs.getString(28), ""));
user.setPerfil(rs.getInt(29));
user.setObservacao(StringUtils.defaultIfEmpty(rs.getString(30), ""));
user.setDeficiencia(Deficiencia.NENHUMA);
return user;
}
public String getSourceQuery() {
return "SELECT * FROM USUARIO";
}
public String getInsertQuery() {
return "INSERT INTO USUARIO(id, timestamp, nome, email, "
+ "bairro, cep, cidade, complemento, logradouro, numero, pais, uf, "
+ "telefone, rg, emissor, dataNascimento, estudante, ocupacao, "
+ "trabalha, sexo, estadoCivil, raca, nivel, serie, "
+ "dataCadastro, senha, perfil, observacao, deficiencia, user_id) "
+ "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, "
+ "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, "
+ "?, ?, ?, ?, ?, ?, ?, ?)";
}
public void successMessage(ResultSet rs) throws Exception {
System.out.println("Inserido usu�rio: " + rs.getLong(1) + " - "
+ rs.getString(3));
}
}