package src;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.List;
import br.com.procempa.modus.entity.Encontro;
import br.com.procempa.modus.entity.Inscricao;
import br.com.procempa.modus.entity.Persistent;
import br.com.procempa.modus.entity.Presenca;
public class PresencaMigration extends EntityMigrator {
public PresencaMigration(Connection connSource, Connection connDestination) {
super(connSource, connDestination);
}
public PreparedStatement prepareStatement(ResultSet rs,
PreparedStatement statement, List<String> messages)
throws Exception {
Presenca p = (Presenca) getObject(rs);
statement.setLong(1, p.getId());
statement.setDate(2, new java.sql.Date(p.getTimestamp().getTime()));
statement.setBoolean(3, p.getPresente());
statement.setNull(4, Types.NULL);
if (!(p.getEncontro() == null)) {
statement.setLong(5, p.getEncontro().getId());
} else {
statement.setNull(5, Types.NULL);
messages.add("Registro de PRESENCA (id: " + p.getId()
+ ") com chave estrangeira de Encontro = NULL");
}
if (!(p.getInscricao() == null)) {
statement.setLong(6, p.getInscricao().getId());
} else {
statement.setNull(6, Types.NULL);
messages.add("Registro de PRESENCA (id: " + p.getId()
+ ") com chave estrangeira de Inscri��o = NULL");
}
return statement;
}
public Persistent getObject(ResultSet rs) throws Exception {
Presenca p = new Presenca();
p.setId(rs.getLong(1));
p.setTimestamp(new Timestamp(rs.getDate(2).getTime()));
p.setPresente(rs.getBoolean(3));
if(rs.getString(7) == null){
p.setEncontro(null);
} else {
Encontro e = new Encontro();
p.setEncontro(e);
p.getEncontro().setId(rs.getLong(4));
}
if(rs.getString(6) == null){
p.setInscricao(null);
} else {
Inscricao i = new Inscricao();
p.setInscricao(i);
p.getInscricao().setId(rs.getLong(5));
}
return p;
}
public String getSourceQuery() {
return "SELECT p.idPresenca, p.timestamp, p.presente, "
+ "p.encontro_idEncontro, p.inscricao_idInscricao, "
+ "i.idInscricao, e.idEncontro "
+ "FROM PRESENCA p LEFT JOIN INSCRICAO i "
+ "ON p.inscricao_idInscricao = i.idInscricao "
+ "LEFT JOIN ENCONTRO e "
+ "ON p.encontro_idEncontro = e.idEncontro";
}
public String getInsertQuery() {
return "INSERT INTO PRESENCA(id, timestamp, presente, user_id, "
+ "encontro_id, inscricao_id) " + "VALUES(?, ?, ?, ?, ?, ?)";
}
public void successMessage(ResultSet rs) throws Exception {
System.out.println("Inserido presen�a: " + rs.getLong(1) + " - "
+ rs.getString(3));
}
}