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.Inscricao;
import br.com.procempa.modus.entity.Persistent;
import br.com.procempa.modus.entity.Turma;
import br.com.procempa.modus.entity.Usuario;
public class InscricaoMigration extends EntityMigrator {
public InscricaoMigration(Connection connSource, Connection connDestination) {
super(connSource, connDestination);
}
public PreparedStatement prepareStatement(ResultSet rs,
PreparedStatement statement, List<String> messages)
throws Exception {
Inscricao i = (Inscricao) getObject(rs);
statement.setLong(1, i.getId());
statement.setDate(2, new java.sql.Date(i.getTimestamp().getTime()));
statement.setNull(3, Types.NULL);
if (!(i.getTurma() == null)) {
statement.setLong(4, i.getTurma().getId());
} else {
statement.setNull(4, Types.NULL);
messages.add("Registro de INSCRICAO (id: " + i.getId()
+ ") com chave estrangeira de Turma = NULL");
}
if (!(i.getUsuario() == null)) {
statement.setLong(5, i.getUsuario().getId());
} else {
statement.setNull(5, Types.NULL);
messages.add("Registro de INSCRICAO (id: " + i.getId()
+ ") com chave estrangeira de Usuario = NULL");
}
return statement;
}
public Persistent getObject(ResultSet rs) throws Exception {
Inscricao i = new Inscricao();
i.setId(rs.getLong(1));
i.setTimestamp(new Timestamp(rs.getDate(2).getTime()));
if (rs.getString(3) == null) {
i.setTurma(null);
} else {
Turma t = new Turma();
i.setTurma(t);
i.getTurma().setId(rs.getLong(3));
}
if (rs.getString(4) == null) {
i.setUsuario(null);
} else {
Usuario u = new Usuario();
i.setUsuario(u);
i.getUsuario().setId(rs.getLong(4));
}
return i;
}
public String getInsertQuery() {
return "INSERT INTO INSCRICAO(id, timestamp, user_id, turma_id, usuario_id) "
+ "VALUES(?, ?, ?, ?, ?)";
}
public String getSourceQuery() {
return "SELECT i.idInscricao, i.timestamp, i.turma_idTurma, "
+ "i.usuario_idUsuario, u.idUsuario, tur.idTurma "
+ "FROM INSCRICAO i LEFT JOIN USUARIO u "
+ "ON i.usuario_idUsuario = u.idUsuario LEFT JOIN "
+ "TURMA tur ON i.turma_idTurma = tur.idTurma";
}
public void successMessage(ResultSet rs) throws Exception {
System.out.println("Inserido inscri��o: " + rs.getLong(1) + " - "
+ rs.getString(3));
}
}