Package src

Source Code of src.FilaInscricaoMigration

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

public class FilaInscricaoMigration extends EntityMigrator {

  Long idCurso;

  Long idTurma;

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

  }

  public FilaInscricaoMigration(Connection connSource,
      Connection connDestination, Long idCurso, Long idTurma) {
    super(connSource, connDestination);
    this.idCurso = idCurso;
    this.idTurma = idTurma;

  }

  public String getInsertQuery() {
    return "INSERT INTO FILAINSCRICAO(id, timestamp, user_id, "
        + "curso_id, usuario_id) " + "VALUES(?, ?, ?, ?, ?)";
  }

  public Persistent getObject(ResultSet rs) throws Exception {
    FilaInscricao filaInscricao = new FilaInscricao();

    filaInscricao.setId(rs.getLong(1));
    filaInscricao.setTimestamp(new Timestamp(rs.getDate(2).getTime()));

    if (rs.getString(6) == null) {
      filaInscricao.setUsuario(null);
    } else {
      Usuario u = new Usuario();
      filaInscricao.setUsuario(u);
      filaInscricao.getUsuario().setId(rs.getLong(4));
    }

    Curso c = new Curso();
    filaInscricao.setCurso(c);
    filaInscricao.getCurso().setId(this.idCurso);

    return filaInscricao;
  }

  public String getSourceQuery() {
    return "SELECT i.idInscricao, i.timestamp, i.turma_idTurma, "
        + "i.usuario_idUsuario, i.user_id, u.idUsuario FROM INSCRICAO i "
        + "LEFT JOIN USUARIO u on u.idUsuario = i.usuario_idUsuario WHERE i.turma_idTurma = "
        + this.idTurma;
  }

  public PreparedStatement prepareStatement(ResultSet rs,
      PreparedStatement statement, List<String> messages)
      throws Exception {

    FilaInscricao filaInscricao = (FilaInscricao) getObject(rs);

    statement.setLong(1, filaInscricao.getId());
    statement.setDate(2, new java.sql.Date(filaInscricao.getTimestamp()
        .getTime()));
    statement.setNull(3, Types.NULL);
    statement.setLong(4, filaInscricao.getCurso().getId());

    if (!(filaInscricao.getUsuario() == null)) {
      statement.setLong(5, filaInscricao.getUsuario().getId());
    } else {
      statement.setNull(5, Types.NULL);
      messages.add("Registro de INSCRICAO - FILAINSCRICAO (id: "
          + filaInscricao.getId()
          + ") com chave estrangeira de Usuario = NULL");
    }
   
    /**
     * TODO Antes de inserir o usuario na  fila de inscricao ver se
     * ele nao esta inscrito no curso
     */

//    List<Inscricao> list = InscricaoDataServices.getList(CursoDataServices
//        .getCurso(filaInscricao.getCurso().getId()));
//    for (Inscricao inscricao : list) {
//      if (inscricao.getUsuario().getId() == filaInscricao.getUsuario().getId()){
//        System.out.println("Usuario j� inscrito em uma turma desse curso.");
//        messages.add("Usuario j� inscrito em uma turma desse curso.");
//      }
//    }
   
    return statement;
  }

  public void successMessage(ResultSet rs) throws Exception {
    System.out.println("Inserido inscricao: " + rs.getLong(1) + " - "
        + "na fila de inscricao");
  }

}
TOP

Related Classes of src.FilaInscricaoMigration

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.