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.Equipamento;
import br.com.procempa.modus.entity.Persistent;
import br.com.procempa.modus.entity.Telecentro;
public class EquipamentoMigration extends EntityMigrator {
public EquipamentoMigration(Connection connSource,
Connection connDestination) {
super(connSource, connDestination);
}
public PreparedStatement prepareStatement(ResultSet rs,
PreparedStatement statement, List<String> messages) throws Exception {
Equipamento eq = (Equipamento) getObject(rs);
statement.setLong(1, eq.getId());
statement.setInt(2, eq.getPort());
statement.setDate(3, new java.sql.Date(eq.getTimestamp().getTime()));
statement.setInt(4, eq.getStatus());
statement.setString(5, eq.getIpAddress());
statement.setString(6, eq.getRotulo());
statement.setString(7, eq.getDiscoRigido());
statement.setString(8, eq.getMarca());
statement.setString(9, eq.getProcessador());
statement.setString(10, eq.getMemoria());
statement.setBoolean(11, eq.getDisponivel());
statement.setNull(12, Types.NULL);
if (!(eq.getTelecentro() == null)) {
statement.setLong(13, eq.getTelecentro().getId());
} else {
statement.setNull(13, Types.NULL);
}
return statement;
}
public Persistent getObject(ResultSet rs) throws Exception {
Equipamento eq = new Equipamento();
eq.setId(rs.getLong(1));
eq.setTimestamp(new Timestamp(rs.getDate(2).getTime()));
eq.setRotulo(rs.getString(3));
eq.setDiscoRigido(rs.getString(4));
eq.setMarca(rs.getString(5));
eq.setProcessador(rs.getString(6));
eq.setMemoria(rs.getString(7));
if (rs.getString(8) == null) {
eq.setTelecentro(null);
} else {
Telecentro t = new Telecentro();
eq.setTelecentro(t);
eq.getTelecentro().setId(rs.getLong(8));
}
eq.setIpAddress(rs.getString(9));
eq.setStatus(rs.getInt(10));
eq.setPort(rs.getInt(11));
eq.setDisponivel(rs.getBoolean(12));
return eq;
}
public String getInsertQuery() {
return "INSERT INTO EQUIPAMENTO(id, port, timestamp, status, ipAddress, "
+ "rotulo, discoRigido, marca, processador, memoria, disponivel, "
+ "user_id, telecentro_id) "
+ "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
}
public String getSourceQuery() {
return "SELECT e.idEquipamento, e.timestamp, e.rotulo, "
+ "e.discoRigido, e.marca, e.processador, e.memoria, "
+ "e.telecentro_idTelecentro, e.ipAddress, e.status, e.port, "
+ "e.disponivel, t.idTelecentro "
+ "FROM EQUIPAMENTO e LEFT JOIN TELECENTRO t "
+ "ON e.telecentro_idTelecentro = t.idTelecentro";
}
public void successMessage(ResultSet rs) throws Exception {
System.out.println("Inserido equipamento: " + rs.getLong(1) + " - "
+ rs.getString(3));
}
}