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 org.apache.commons.lang.StringUtils;
import br.com.procempa.modus.entity.Curso;
import br.com.procempa.modus.entity.Persistent;
import br.com.procempa.modus.entity.Turma;
public class TurmaMigration extends EntityMigrator {
public TurmaMigration(Connection connSource, Connection connDestination) {
super(connSource, connDestination);
}
public PreparedStatement prepareStatement(ResultSet rs,
PreparedStatement statement, List<String> messages)
throws Exception {
Turma tur = (Turma) getObject(rs);
statement.setLong(1, tur.getId());
statement.setDate(2, new java.sql.Date(tur.getTimestamp().getTime()));
statement.setString(3, tur.getNome());
statement.setString(4, tur.getHorario());
statement.setString(5, tur.getPeriodo());
statement.setInt(6, tur.getVagas());
statement.setBoolean(7, tur.getAberta());
statement.setNull(8, Types.NULL);
if (!(tur.getCurso() == null)){
statement.setLong(9, tur.getCurso().getId());
} else {
statement.setNull(9, Types.NULL);
messages.add("Registro de TURMA (id: " + tur.getId()
+ ") com chave estrangeira de Curso = NULL");
}
return statement;
}
public Persistent getObject(ResultSet rs) throws Exception {
Turma tur = new Turma();
tur.setId(rs.getLong(1));
tur.setTimestamp(new Timestamp(rs.getDate(2).getTime()));
tur.setNome(StringUtils.defaultIfEmpty(rs.getString(3), ""));
tur.setHorario(StringUtils.defaultIfEmpty(rs.getString(4), ""));
tur.setPeriodo(StringUtils.defaultIfEmpty(rs.getString(5), ""));
tur.setVagas(rs.getInt(6));
tur.setAberta(rs.getBoolean(7));
if(rs.getString(8) == null){
tur.setCurso(null);
} else {
Curso c = new Curso();
tur.setCurso(c);
tur.getCurso().setId(rs.getLong(8));
}
return tur;
}
public String getSourceQuery() {
return "SELECT tur.idTurma, tur.timestamp, tur.nome, "
+ "tur.horario, tur.periodo, tur.vagas, tur.aberta, "
+ "tur.curso_idCurso, cur.idCurso FROM TURMA tur "
+ "LEFT JOIN CURSO cur ON tur.curso_idCurso = cur.idCurso";
}
public String getInsertQuery() {
return "INSERT INTO TURMA(id, timestamp, nome, horario, periodo, "
+ "vagas, aberta, user_id, curso_id) "
+ "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)";
}
public void successMessage(ResultSet rs) throws Exception {
System.out.println("Inserido turma: " + rs.getLong(1) + " - "
+ rs.getString(3));
}
}