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

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.json.JSONObject;
import trismegistoplanilla.beans.TokenFichaBean;
import trismegistoplanilla.dao.TokenFichaDAO;
import trismegistoplanilla.utilities.ResponseHelper;
import trismegistoplanilla.utilities.Variables;

public class TokenFichaMysqlDAO implements TokenFichaDAO {

	@Override
	public JSONObject validarTokenURL(TokenFichaBean tf) {
17
		System.out.println("TokenFichaMysqlDAO: validarTokenURL");
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
		JSONObject jsonObjValidarTokenURL = null;
		ResponseHelper response = new ResponseHelper();
		int tokenValidoURL = 0;

		String sql = ""
			+ "select "
			+ "count(1) tokenValidoURL "
			+ "from token_ficha "
			+ "where codigo_token_ficha = ? and "
			+ "token = ? ";
		
		Connection conexion = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
33
			conexion = MysqlDAOFactory.obtenerConexion(Variables.MYSQL_NUEVO_BD_NAME);
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
			ps = conexion.prepareStatement(sql);
			ps.setInt(1, tf.getCodigoTokenFicha());
			ps.setString(2, tf.getToken());
			rs = ps.executeQuery();
			rs.next();
			tokenValidoURL = rs.getInt("tokenValidoURL");
			if (tokenValidoURL > 0) {
				response.setStatus(true);
				response.setMessage("El token es válido");
			} else {
				response.setStatus(false);
				// error token no valido
				response.setMessage("El token no existe o ha caducado");
			}
		} 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 (conexion != null) {
					conexion.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		jsonObjValidarTokenURL = new JSONObject(response);
		return jsonObjValidarTokenURL;
	}

	@Override
	public JSONObject validarToken(TokenFichaBean tf) {
73
		System.out.println("TokenFichaMysqlDAO: validarToken");
74 75 76 77 78 79 80 81 82 83 84
		JSONObject jsonObjValidarToken = null;
		ResponseHelper response = new ResponseHelper();
		int tokenValido = 0;

		String sql = ""
			+ "select "
			+ "count(1) tokenValido "
			+ "from token_ficha "
			+ "where "
			+ "codigo_token_ficha = ? and "
			+ "token = ? and "
85 86
			+ "fecha_expiracion >= now() and  "
			+ "estado_registro = 1 ";
87 88 89 90 91
		
		Connection conexion = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
92
			conexion = MysqlDAOFactory.obtenerConexion(Variables.MYSQL_NUEVO_BD_NAME);
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
			ps = conexion.prepareStatement(sql);
			ps.setInt(1, tf.getCodigoTokenFicha());
			ps.setString(2, tf.getToken());
			rs = ps.executeQuery();
			rs.next();
			tokenValido = rs.getInt("tokenValido");
			if (tokenValido > 0) {
				response.setStatus(true);
				response.setMessage("El token es válido");
			} else {
				response.setStatus(false);
				response.setMessage("Error, el token no existe o esta inhabilitado");
			}
		} 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 (conexion != null) {
					conexion.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		jsonObjValidarToken = new JSONObject(response);
		return jsonObjValidarToken;
	}

	@Override
	public JSONObject desactivarToken(TokenFichaBean tf) {
131
		System.out.println("TokenFichaMysqlDAO: desactivarToken");
132 133 134 135 136 137 138 139 140 141 142 143
		JSONObject jsonObjDesactivarToken = null;
		ResponseHelper response = new ResponseHelper();

		String sql = ""
			+ "update token_ficha "
			+ "set estado_registro = 0 "
			+ "where codigo_token_ficha = ? and estado_registro = 1";
		
		Connection conexion = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
144
			conexion = MysqlDAOFactory.obtenerConexion(Variables.MYSQL_NUEVO_BD_NAME);
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
			conexion.setAutoCommit(false);
			ps = conexion.prepareStatement(sql);
			ps.setInt(1, tf.getCodigoTokenFicha());
			int desactivarToken = ps.executeUpdate();
			if (desactivarToken > 0) {
				conexion.commit();
				response.setStatus(true);
				response.setMessage("La token se ha desactivado con éxito.");
			} else {
				response.setStatus(false);
				response.setMessage("Error al desactivar el token, no se ha encontrado ningún registro.");
				conexion.rollback();
			}
		} 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 (conexion != null) {
					conexion.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		jsonObjDesactivarToken = new JSONObject(response);
		return jsonObjDesactivarToken;
	}

	@Override
	public JSONObject obtenerSedeAreaCargo(JSONObject datos) {
183
		System.out.println("TokenFichaMysqlDAO: obtenerSedeAreaCargo");
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
		JSONObject jObject;
		ResponseHelper response = new ResponseHelper();
		Connection cnx = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = ""
			+ "select top 1 "
			+ "token_ficha.codigo_sede_area codigoSedeArea, "
			+ "token_ficha.codigo_area_cargo codigoAreaCargo, "
			+ "sede.nombre nombreSede, "
			+ "area.nombre nombreArea, "
			+ "cargo.nombre nombreCargo "
			+ "FROM token_ficha "
			+ "inner join ficha ON ficha.codigo_ficha = token_ficha.codigo_ficha "
			+ "inner join persona ON persona.codigo_persona = ficha.codigo_persona "
			+ "inner join sede_area on token_ficha.codigo_sede_area = sede_area.codigo_sede_area "
			+ "inner join sede ON sede.codigo_sede = sede_area.codigo_sede "
			+ "inner join area ON area.codigo_area = sede_area.codigo_area "
			+ "inner join area_cargo on token_ficha.codigo_area_cargo = area_cargo.codigo_area_cargo "
			+ "inner join cargo ON cargo.codigo_cargo = area_cargo.codigo_cargo "
			+ "where persona.codigo_persona = ? "
			+ "order by 1 desc";

		try {
208
			cnx = MysqlDAOFactory.obtenerConexion(Variables.MYSQL_NUEVO_BD_NAME);
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
			ps = cnx.prepareStatement(sql);
			ps.setInt(1, datos.getInt("codigoPersona"));
			rs = ps.executeQuery();
			if (rs.next()) {
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("codigoSedeArea", rs.getInt("codigoSedeArea"));
				jsonObject.put("codigoAreaCargo", rs.getInt("codigoAreaCargo"));
				jsonObject.put("sede", rs.getString("nombreSede"));
				jsonObject.put("area", rs.getString("nombreArea"));
				jsonObject.put("cargo", rs.getString("nombreCargo"));

				response.setStatus(true);
				response.setMessage("Se obtuvo la sede, area y cargo del personal");
				response.setData(new JSONObject().put("datosAdministrativos", jsonObject));

			} else {
				response.setStatus(false);
				response.setMessage("No se encontraron sede - area - cargos del personal.");
			}
		} 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 (cnx != null) {
					cnx.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

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