EstadoEstudioMysqlDAO.java 4.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
package trismegistoplanilla.mysqldao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.json.JSONArray;
import org.json.JSONObject;
import trismegistoplanilla.beans.EstadoEstudioBean;
import trismegistoplanilla.dao.EstadoEstudioDAO;
import trismegistoplanilla.utilities.ResponseHelper;
import trismegistoplanilla.utilities.Variables;

public class EstadoEstudioMysqlDAO implements EstadoEstudioDAO {

	@Override
	public JSONObject validarExistenciaEstadoEstudio(EstadoEstudioBean estadoEstudio) {
18
		System.out.println("EstadoEstudioMysqlDAO: validarExistenciaEstadoEstudio");
19 20 21 22 23 24 25 26
		JSONObject jsonValidarExistenciaEstadoEstudio = null;
		ResponseHelper response = new ResponseHelper();
		int existeEstadoEstudio = 0;

		Connection connection = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
27
			connection = MysqlDAOFactory.obtenerConexion(Variables.MYSQL_NUEVO_BD_NAME);
28 29 30 31
			String sql
				= "select "
				+ "count (1) existeEstadoEstudio "
				+ "from nivel_estado "
Luis Gangas committed
32 33
				+ "inner join nivel_estudio ON nivel_estudio.codigo_nivel_estudio = nivel_estado.codigo_nivel_estudio "
				+ "inner join estado_estudio ON estado_estudio.codigo_estado_estudio = nivel_estado.codigo_estado_estudio "
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
				+ "where nivel_estudio.estado_registro = 1 "
				+ "and nivel_estado.codigo_nivel_estudio = ? "
				+ "and nivel_estado.codigo_estado_estudio = ?";
			ps = connection.prepareStatement(sql);
			ps.setInt(1, estadoEstudio.getCodigoNivelEstudio());
			ps.setInt(2, estadoEstudio.getCodigoEstadoEstudio());
			rs = ps.executeQuery();
			rs.next();
			existeEstadoEstudio = rs.getInt("existeEstadoEstudio");
			if (existeEstadoEstudio > 0) {
				response.setStatus(true);
				response.setMessage("El estado estudio seleccionado existe.");
			} else {
				response.setStatus(false);
				response.setMessage("El estado estudio seleccionado no existe.");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			response.setStatus(false);
			response.setMessage("Error: " + e.getMessage() + " \n Error Code: [" + e.getErrorCode() + "]");
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		jsonValidarExistenciaEstadoEstudio = new JSONObject(response);
		return jsonValidarExistenciaEstadoEstudio;
	}

	@Override
	public JSONObject listarEstadoEstudio(EstadoEstudioBean estadoEstudio) {
75
		System.out.println("EstadoEstudioMysqlDAO: listarEstadoEstudio");
76 77 78 79 80 81 82 83 84
		JSONObject jsonListarEstadoEstudio = null;
		JSONArray jsonArrayListarEstadoEstudio = new JSONArray();
		ResponseHelper response = new ResponseHelper();

		Connection connection = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
85
			connection = MysqlDAOFactory.obtenerConexion(Variables.MYSQL_NUEVO_BD_NAME);
86 87 88 89 90
			String sql
				= "select "
				+ "estado_estudio.codigo_estado_estudio codigoEstadoEstudio, "
				+ "estado_estudio.nombre nombreEstadoEstudio "
				+ "from nivel_estado "
Luis Gangas committed
91
				+ "inner join estado_estudio ON estado_estudio.codigo_estado_estudio = nivel_estado.codigo_estado_estudio "
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
				+ "where estado_estudio.estado_registro = 1 and nivel_estado.codigo_nivel_estudio = ?";
			ps = connection.prepareStatement(sql);
			ps.setInt(1, estadoEstudio.getCodigoNivelEstudio());
			rs = ps.executeQuery();
			while (rs.next()) {
				estadoEstudio.setCodigoEstadoEstudio(rs.getInt("codigoEstadoEstudio"));
				estadoEstudio.setNombre(rs.getString("nombreEstadoEstudio"));
				JSONObject jsonObjEstadoEstudio = new JSONObject(estadoEstudio);
				jsonArrayListarEstadoEstudio.put(jsonObjEstadoEstudio);
			}

			JSONObject jsonObjEstadoEstudio = new JSONObject();
			jsonObjEstadoEstudio.put("estadoestudio", jsonArrayListarEstadoEstudio);

			response.setStatus(true);
			response.setMessage("Los estados de estudio se han listado correctamente");
			response.setData(jsonObjEstadoEstudio);

		} catch (SQLException e) {
			e.printStackTrace();
			response.setStatus(false);
			response.setMessage("Error: " + e.getMessage() + " \n Error Code: [" + e.getErrorCode() + "]");
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		jsonListarEstadoEstudio = new JSONObject(response);
		return jsonListarEstadoEstudio;
	}
}