Package src

Source Code of src.ConteudoMigration

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

public class ConteudoMigration extends EntityMigrator {

  public ConteudoMigration(Connection connSource,
      Connection connDestination) {
    super(connSource, connDestination);

  }

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

    statement.setLong(1, con.getId());
    statement.setDate(2, new java.sql.Date(con.getTimestamp().getTime()));
    statement.setString(3, con.getDescricao());
    statement.setInt(4, con.getHorasAula());
    statement.setString(5, con.getNome());
    statement.setNull(6, Types.NULL);
   
    if (!(con.getCurso() == null)){
      statement.setLong(7, con.getCurso().getId());
    } else {
      statement.setNull(7, Types.NULL);
      messages.add("Registro de CONTEUDO (id: " + con.getId()
          + ") com chave estrangeira de Curso = NULL");
    }
 
    return statement;
  }

  public Persistent getObject(ResultSet rs) throws Exception {
    Conteudo con = new Conteudo();

    con.setId(rs.getLong(1));
    con.setTimestamp(new Timestamp(rs.getDate(2).getTime()));
    con.setHorasAula(rs.getInt(3));
    con.setNome(rs.getString(4));
   
    if(rs.getString(5) == null){
      con.setCurso(null);
    } else {
      Curso c = new Curso();
      con.setCurso(c);
      con.getCurso().setId(rs.getLong(5));
    }
   
    con.setDescricao(rs.getString(6));

    return con;
  }

  public String getInsertQuery() {

    return "INSERT INTO CONTEUDO(id, timestamp, descricao, horasAula, "
        + "nome, user_id, curso_id) VALUES(?, ?, ?, ?, ?, ?, ?)";
  }

  public String getSourceQuery() {
    return "SELECT co.idConteudo, co.timestamp, co.horasAula, "
        + "co.nome, co.curso_idCurso, co.descricao, cur.idCurso "
        + "FROM CONTEUDO co LEFT JOIN CURSO cur "
        + "ON co.curso_idCurso = cur.idCurso";
  }

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

Related Classes of src.ConteudoMigration

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.