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.Telecentro;
public class CursoMigration extends EntityMigrator {
public CursoMigration(Connection connSource, Connection connDestination) {
super(connSource, connDestination);
}
public PreparedStatement prepareStatement(ResultSet rs,
PreparedStatement statement, List<String> messages) throws Exception {
Curso cur = (Curso) getObject(rs);
statement.setLong(1, cur.getId());
statement.setDate(2, new java.sql.Date(cur.getTimestamp().getTime()));
statement.setString(3, cur.getLocal());
statement.setString(4, cur.getNome());
statement.setInt(5, cur.getAssiduidade());
statement.setInt(6, cur.getCargaHorario());
statement.setString(7, cur.getEmenta());
statement.setNull(8, Types.NULL);
if (!(cur.getTelecentro() == null)){
statement.setLong(9, cur.getTelecentro().getId());
} else {
statement.setNull(9, Types.NULL);
}
return statement;
}
public Persistent getObject(ResultSet rs) throws Exception {
Curso cur = new Curso();
cur.setId(rs.getLong(1));
cur.setTimestamp(new Timestamp(rs.getDate(2).getTime()));
cur.setLocal(StringUtils.defaultIfEmpty(rs.getString(3), ""));
cur.setNome(StringUtils.defaultIfEmpty(rs.getString(4), ""));
cur.setAssiduidade(rs.getInt(5));
cur.setCargaHorario(rs.getInt(6));
if(rs.getString(7) == null){
cur.setTelecentro(null);
} else {
Telecentro t = new Telecentro();
cur.setTelecentro(t);
cur.getTelecentro().setId(rs.getLong(7));
}
cur.setEmenta(StringUtils.defaultIfEmpty(rs.getString(8), ""));
return cur;
}
public String getInsertQuery() {
return "INSERT INTO CURSO(id, timestamp, local, nome, assiduidade, "
+ "cargaHorario, ementa, user_id, telecentro_id) "
+ "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)";
}
public String getSourceQuery() {
return "SELECT cur.idCurso, cur.timestamp, cur.local, "
+ "cur.nome, cur.assiduidade, cur.cargaHorario, "
+ "cur.telecentro_idTelecentro, cur.ementa, t.idTelecentro "
+ "FROM CURSO cur LEFT JOIN TELECENTRO t "
+ "ON cur.telecentro_idTelecentro = t.idTelecentro";
}
public void successMessage(ResultSet rs) throws Exception {
System.out.println("Inserido curso: " + rs.getLong(1) + " - "
+ rs.getString(3));
}
}