Package src

Source Code of src.VisitaMigration

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

public class VisitaMigration extends EntityMigrator {

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

  }

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

    statement.setLong(1, v.getId());
    statement.setDate(2, new java.sql.Date(v.getTimestamp().getTime()));
    statement.setString(3, v.getObservacao());
    statement.setDate(4, new java.sql.Date(v.getDataInicio().getTime()));

    if (v.getDataFim() == null) {
      statement.setNull(5, Types.NULL);
    } else {
      statement.setDate(5, new java.sql.Date(v.getDataFim().getTime()));
    }

    statement.setBoolean(6, v.getMotivo().getCurso());
    statement.setBoolean(7, v.getMotivo().getChat());
    statement.setBoolean(8, v.getMotivo().getEmail());
    statement.setBoolean(9, v.getMotivo().getEscolar());
    statement.setBoolean(10, v.getMotivo().getJogo());
    statement.setBoolean(11, v.getMotivo().getOficina());
    statement.setBoolean(12, v.getMotivo().getOutro());
    statement.setBoolean(13, v.getMotivo().getWeb());
    statement.setNull(14, Types.NULL);

    if (!(v.getEquipamento() == null)) {
      statement.setLong(17, v.getEquipamento().getId());
    } else {
      statement.setNull(17, Types.NULL);
    }

    if (!(v.getTelecentro() == null)) {
      statement.setLong(15, v.getTelecentro().getId());
    } else {
      statement.setNull(15, Types.NULL);
      messages.add("Registro de VISITA (id: " + v.getId()
          + ") com chave estrangeira de Telecentro = NULL");
    }
    if (!(v.getUsuario() == null)) {
      statement.setLong(16, v.getUsuario().getId());
    } else {
      statement.setNull(16, Types.NULL);
      messages.add("Registro de VISITA (id: " + v.getId()
          + ") com chave estrangeira de Usuario = NULL");
    }
    return statement;
  }

  public Persistent getObject(ResultSet rs) throws Exception {
    Visita v = new Visita();

    v.setId(rs.getLong(1));
    v.setTimestamp(new Timestamp(rs.getDate(2).getTime()));
    v.setDataInicio(new Timestamp(rs.getDate(3).getTime()));
    if (rs.getString(4) == null) {
      v.setDataFim(null);
    } else {
      v.setDataFim(new Timestamp(rs.getDate(4).getTime()));
    }
    v.getMotivo().setEmail(rs.getBoolean(5));
    v.getMotivo().setChat(rs.getBoolean(6));
    v.getMotivo().setCurso(rs.getBoolean(7));
    v.getMotivo().setEscolar(rs.getBoolean(8));
    v.getMotivo().setJogo(rs.getBoolean(9));
    v.getMotivo().setOficina(rs.getBoolean(10));
    v.getMotivo().setOutro(rs.getBoolean(11));
    v.getMotivo().setWeb(rs.getBoolean(12));
    v.setObservacao(rs.getString(13));

    if (rs.getString(14).equals(null)) {
      v.setTelecentro(null);
    } else {
      Telecentro t = new Telecentro();
      v.setTelecentro(t);
      v.getTelecentro().setId(rs.getLong(14));
    }

    if (rs.getString(15).equals(null)) {
      v.setUsuario(null);
    } else {
      Usuario u = new Usuario();
      v.setUsuario(u);
      v.getUsuario().setId(rs.getLong(15));
    }

    if (rs.getString(16) == null) {
      v.setEquipamento(null);
    } else {
      Equipamento e = new Equipamento();
      v.setEquipamento(e);
      v.getEquipamento().setId(rs.getLong(16));
    }

    return v;
  }

  public String getInsertQuery() {
    return "INSERT INTO VISITA(id, timestamp, observacao, dataInicio, "
        + "dataFim, curso, chat, email, escolar, jogo, oficina, outro, "
        + "web, user_id, telecentro_id, usuario_id, equipamento_id) "
        + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  }

  public String getSourceQuery() {
    return "SELECT v.idVisita, v.timestamp, v.dataInicio, "
        + "v.dataFim, v.email, v.chat, v.curso, v.escolar, v.jogo, "
        + "v.oficina, v.outro, v.web, v.observacao, "
        + "v.telecentro_idTelecentro, v.usuario_idUsuario, "
        + "v.equipamento_idEquipamento, u.idUsuario, tel.idTelecentro, e.idEquipamento "
        + "FROM VISITA v LEFT JOIN TELECENTRO tel "
        + "ON v.telecentro_idTelecentro = tel.idTelecentro "
        + "LEFT JOIN USUARIO u ON v.usuario_idUsuario = u.idUsuario "
        + "LEFT JOIN EQUIPAMENTO e ON v.equipamento_idEquipamento = e.idEquipamento";
  }

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

  }
}
TOP

Related Classes of src.VisitaMigration

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.