Package src

Source Code of src.EncontroMigration

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.Conteudo;
import br.com.procempa.modus.entity.Encontro;
import br.com.procempa.modus.entity.Persistent;
import br.com.procempa.modus.entity.Turma;

public class EncontroMigration extends EntityMigrator {

  public EncontroMigration(Connection connSource,
      Connection connDestination) {
    super(connSource, connDestination);
  }

  public PreparedStatement prepareStatement(ResultSet rs,
      PreparedStatement statement, List<String> messages) throws Exception {
    Encontro en = (Encontro) getObject(rs);

    statement.setLong(1, en.getId());
    statement.setString(2, en.getData());
    statement.setDate(3, new java.sql.Date(en.getTimestamp().getTime()));
   
    statement.setNull(4, Types.NULL);
   
    if (!(en.getConteudo() == null)){
      statement.setLong(5, en.getConteudo().getId());
    } else {
      statement.setNull(5, Types.NULL);
      messages.add("Registro de ENCONTRO (id: " + en.getId()
          + ") com chave estrangeira de Conteudo = NULL");
    }
   
    if (!(en.getTurma() == null)){
      statement.setLong(6, en.getTurma().getId());
    } else {
      statement.setNull(6, Types.NULL);
      messages.add("Registro de ENCONTRO (id: " + en.getId()
          + ") com chave estrangeira de Turma = NULL");
    }

    return statement;
  }

  public Persistent getObject(ResultSet rs) throws Exception {
    Encontro en = new Encontro();

    en.setId(rs.getLong(1));
    en.setData(rs.getString(2));
    en.setTimestamp(new Timestamp(rs.getDate(3).getTime()));
   
    if(rs.getString(4) == null){
      en.setConteudo(null);
    } else {
      Conteudo c = new Conteudo();
      en.setConteudo(c);
      en.getConteudo().setId(rs.getLong(4));
    }
   
    if(rs.getString(5) == null){
      en.setTurma(null);
    } else {
      Turma t = new Turma();
      en.setTurma(t);
      en.getTurma().setId(rs.getLong(5));
    }
   
    return en;
  }

  public String getInsertQuery() {
    return "INSERT INTO ENCONTRO(id, data, timestamp, user_id, "
        + "conteudo_id, turma_id) VALUES(?, ?, ?, ?, ?, ?)";
  }

  public String getSourceQuery() {
    return "SELECT e.idEncontro, e.data, e.timestamp, "
        + "e.conteudo_idConteudo, e.turma_idTurma, tur.idTurma, "
        + "co.idConteudo FROM ENCONTRO e LEFT JOIN CONTEUDO co "
        + "ON e.conteudo_idConteudo = co.idConteudo LEFT JOIN "
        + "TURMA tur ON e.turma_idTurma = tur.idTurma";
  }

  public void successMessage(ResultSet rs) throws Exception {
    System.out.println("Inserido encontro: " + rs.getLong(1) + " - "
        + rs.getString(3));
  }
}
TOP

Related Classes of src.EncontroMigration

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.