Commit 94d0c27c by Alonso Moreno Postigo

[EDIT] v0.1

parent ba1776aa
...@@ -2,12 +2,10 @@ package demojsoncrud.beans; ...@@ -2,12 +2,10 @@ package demojsoncrud.beans;
import java.io.Serializable; import java.io.Serializable;
/**
*
* @author Alonso
*/
public class PersonaBean implements Serializable { public class PersonaBean implements Serializable {
private static final long serialVersionUID = 4594333294248399680L;
private String codigo; private String codigo;
private String dni; private String dni;
private String apellidos; private String apellidos;
......
...@@ -4,6 +4,8 @@ import java.io.Serializable; ...@@ -4,6 +4,8 @@ import java.io.Serializable;
public class UbigeoBean implements Serializable { public class UbigeoBean implements Serializable {
private static final long serialVersionUID = 4594333294248399680L;
private String codigoDepartamento; private String codigoDepartamento;
private String nombreDepartamento; private String nombreDepartamento;
private String codigoProvincia; private String codigoProvincia;
...@@ -11,6 +13,9 @@ public class UbigeoBean implements Serializable { ...@@ -11,6 +13,9 @@ public class UbigeoBean implements Serializable {
private String codigoDistrito; private String codigoDistrito;
private String nombreDistrito; private String nombreDistrito;
public UbigeoBean() {
}
public String getCodigoDepartamento() { public String getCodigoDepartamento() {
return codigoDepartamento; return codigoDepartamento;
} }
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package demojsoncrud.dao; package demojsoncrud.dao;
import demojsoncrud.sqlserverdao.SqlserverDAOFactory; import demojsoncrud.sqlserverdao.SqlserverDAOFactory;
/**
*
* @author Alonso
*/
public abstract class DAOFactory { public abstract class DAOFactory {
public static final int SQL_SERVER = 1; public static final int SQL_SERVER = 1;
......
package demojsoncrud.dao; package demojsoncrud.dao;
import demojsoncrud.beans.PersonaBean;
import org.json.JSONObject; import org.json.JSONObject;
public interface PersonaDAO { public interface PersonaDAO {
public JSONObject listarPersona(String search, String draw, String start, String length); public JSONObject listarPersona(JSONObject datos);
public JSONObject registrarPersona(JSONObject datos); public JSONObject registrarPersona(JSONObject datos);
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package demojsoncrud.dao; package demojsoncrud.dao;
import demojsoncrud.beans.UbigeoBean;
import org.json.JSONObject; import org.json.JSONObject;
/**
*
* @author Alonso
*/
public interface UbigeoDAO { public interface UbigeoDAO {
public JSONObject listarDepartamento(); public JSONObject listarDepartamento();
......
package demojsoncrud.services; package demojsoncrud.services;
import demojsoncrud.beans.PersonaBean;
import demojsoncrud.dao.DAOFactory; import demojsoncrud.dao.DAOFactory;
import demojsoncrud.dao.PersonaDAO; import demojsoncrud.dao.PersonaDAO;
import org.json.JSONObject; import org.json.JSONObject;
...@@ -8,14 +7,14 @@ import org.json.JSONObject; ...@@ -8,14 +7,14 @@ import org.json.JSONObject;
public class PersonaService { public class PersonaService {
DAOFactory daoFactory = DAOFactory.getDAOFactory(DAOFactory.SQL_SERVER); DAOFactory daoFactory = DAOFactory.getDAOFactory(DAOFactory.SQL_SERVER);
PersonaDAO service = daoFactory.getPersonaDAO(); PersonaDAO dao = daoFactory.getPersonaDAO();
public JSONObject listarPersona(String search, String draw, String start, String length) { public JSONObject listarPersona(JSONObject datos) {
JSONObject jsonReturn = null; JSONObject jsonReturn = null;
try { try {
jsonReturn = service.listarPersona(search, draw, start, length); jsonReturn = dao.listarPersona(datos);
} catch (Exception e) { } catch (Exception e) {
e.getMessage(); System.out.println("Error PersonaService >> listarPersona >> " + e.getMessage());
} }
return jsonReturn; return jsonReturn;
} }
...@@ -23,9 +22,9 @@ public class PersonaService { ...@@ -23,9 +22,9 @@ public class PersonaService {
public JSONObject registrarPersona(JSONObject datos) { public JSONObject registrarPersona(JSONObject datos) {
JSONObject jsonReturn = null; JSONObject jsonReturn = null;
try { try {
jsonReturn = service.registrarPersona(datos); jsonReturn = dao.registrarPersona(datos);
} catch (Exception e) { } catch (Exception e) {
e.getMessage(); System.out.println("Error PersonaService >> registrarPersona >> " + e.getMessage());
} }
return jsonReturn; return jsonReturn;
} }
...@@ -33,9 +32,9 @@ public class PersonaService { ...@@ -33,9 +32,9 @@ public class PersonaService {
public JSONObject editarPersona(JSONObject datos) { public JSONObject editarPersona(JSONObject datos) {
JSONObject jsonReturn = null; JSONObject jsonReturn = null;
try { try {
jsonReturn = service.editarPersona(datos); jsonReturn = dao.editarPersona(datos);
} catch (Exception e) { } catch (Exception e) {
e.getMessage(); System.out.println("Error PersonaService >> editarPersona >> " + e.getMessage());
} }
return jsonReturn; return jsonReturn;
} }
...@@ -43,8 +42,9 @@ public class PersonaService { ...@@ -43,8 +42,9 @@ public class PersonaService {
public JSONObject cambiarEstado(JSONObject datos) { public JSONObject cambiarEstado(JSONObject datos) {
JSONObject respuesta = null; JSONObject respuesta = null;
try { try {
respuesta = service.cambiarEstado(datos); respuesta = dao.cambiarEstado(datos);
} catch (Exception e) { } catch (Exception e) {
System.out.println("Error PersonaService >> cambiarEstado >> " + e.getMessage());
} }
return respuesta; return respuesta;
} }
......
package demojsoncrud.services; package demojsoncrud.services;
import demojsoncrud.beans.UbigeoBean;
import demojsoncrud.dao.DAOFactory; import demojsoncrud.dao.DAOFactory;
import demojsoncrud.dao.UbigeoDAO; import demojsoncrud.dao.UbigeoDAO;
import org.json.JSONObject; import org.json.JSONObject;
...@@ -8,13 +7,14 @@ import org.json.JSONObject; ...@@ -8,13 +7,14 @@ import org.json.JSONObject;
public class UbigeoService { public class UbigeoService {
DAOFactory daoFactory = DAOFactory.getDAOFactory(DAOFactory.SQL_SERVER); DAOFactory daoFactory = DAOFactory.getDAOFactory(DAOFactory.SQL_SERVER);
UbigeoDAO service = daoFactory.getUbigeoDAO(); UbigeoDAO dao = daoFactory.getUbigeoDAO();
public JSONObject listarDepartamento() { public JSONObject listarDepartamento() {
JSONObject jsonReturn = null; JSONObject jsonReturn = null;
try { try {
jsonReturn = service.listarDepartamento(); jsonReturn = dao.listarDepartamento();
} catch (Exception e) { } catch (Exception e) {
System.out.println("Error UbigeoService >> listarDepartamento >> " + e.getMessage());
} }
return jsonReturn; return jsonReturn;
} }
...@@ -22,8 +22,9 @@ public class UbigeoService { ...@@ -22,8 +22,9 @@ public class UbigeoService {
public JSONObject listarProvincia(JSONObject datos) { public JSONObject listarProvincia(JSONObject datos) {
JSONObject jsonReturn = null; JSONObject jsonReturn = null;
try { try {
jsonReturn = service.listarProvincia(datos); jsonReturn = dao.listarProvincia(datos);
} catch (Exception e) { } catch (Exception e) {
System.out.println("Error UbigeoService >> listarProvincia >> " + e.getMessage());
} }
return jsonReturn; return jsonReturn;
} }
...@@ -31,8 +32,9 @@ public class UbigeoService { ...@@ -31,8 +32,9 @@ public class UbigeoService {
public JSONObject listarDistrito(JSONObject datos) { public JSONObject listarDistrito(JSONObject datos) {
JSONObject jsonReturn = null; JSONObject jsonReturn = null;
try { try {
jsonReturn = service.listarDistrito(datos); jsonReturn = dao.listarDistrito(datos);
} catch (Exception e) { } catch (Exception e) {
System.out.println("Error UbigeoService >> listarDistrito >> " + e.getMessage());
} }
return jsonReturn; return jsonReturn;
} }
......
package demojsoncrud.servlets; package demojsoncrud.servlets;
import demojsoncrud.services.PersonaService; import demojsoncrud.services.PersonaService;
import static demojsoncrud.utilities.CustomHttpServletRequest.getBodyJsonObject;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import javax.servlet.ServletException; import javax.servlet.ServletException;
...@@ -46,12 +45,15 @@ public class PersonaServlet extends HttpServlet { ...@@ -46,12 +45,15 @@ public class PersonaServlet extends HttpServlet {
PrintWriter pw = response.getWriter(); PrintWriter pw = response.getWriter();
PersonaService service = new PersonaService(); PersonaService service = new PersonaService();
String draw = request.getParameter("draw");
String start = request.getParameter("start");
String length = request.getParameter("length");
String search = request.getParameter("search"); String search = request.getParameter("search");
JSONObject json = service.listarPersona(search, draw, start, length); JSONObject datos = new JSONObject();
datos.put("draw", request.getParameter("draw"));
datos.put("start", request.getParameter("start"));
datos.put("length", request.getParameter("length"));
datos.put("search", search);
JSONObject json = service.listarPersona(datos);
pw.print(json); pw.print(json);
} }
...@@ -59,9 +61,8 @@ public class PersonaServlet extends HttpServlet { ...@@ -59,9 +61,8 @@ public class PersonaServlet extends HttpServlet {
response.setContentType("application/json"); response.setContentType("application/json");
PrintWriter pw = response.getWriter(); PrintWriter pw = response.getWriter();
PersonaService service = new PersonaService(); PersonaService service = new PersonaService();
JSONObject datos = new JSONObject(); JSONObject datos = new JSONObject(request.getParameter("json"));
datos = getBodyJsonObject(request); JSONObject respuesta = service.registrarPersona(datos);
JSONObject respuesta = service.registrarPersona(datos.getJSONObject("json"));
pw.print(respuesta); pw.print(respuesta);
} }
...@@ -70,9 +71,8 @@ public class PersonaServlet extends HttpServlet { ...@@ -70,9 +71,8 @@ public class PersonaServlet extends HttpServlet {
response.setContentType("application/json"); response.setContentType("application/json");
PrintWriter pw = response.getWriter(); PrintWriter pw = response.getWriter();
PersonaService service = new PersonaService(); PersonaService service = new PersonaService();
JSONObject datos = new JSONObject(); JSONObject datos = new JSONObject(request.getParameter("json"));
datos = getBodyJsonObject(request); JSONObject respuesta = service.editarPersona(datos);
JSONObject respuesta = service.editarPersona(datos.getJSONObject("json"));
pw.print(respuesta); pw.print(respuesta);
} }
...@@ -80,9 +80,8 @@ public class PersonaServlet extends HttpServlet { ...@@ -80,9 +80,8 @@ public class PersonaServlet extends HttpServlet {
response.setContentType("application/json"); response.setContentType("application/json");
PrintWriter pw = response.getWriter(); PrintWriter pw = response.getWriter();
PersonaService service = new PersonaService(); PersonaService service = new PersonaService();
JSONObject datos = new JSONObject(); JSONObject datos = new JSONObject(request.getParameter("json"));
datos = getBodyJsonObject(request); JSONObject respuesta = service.cambiarEstado(datos);
JSONObject respuesta = service.cambiarEstado(datos.getJSONObject("json"));
System.out.println(respuesta); System.out.println(respuesta);
pw.print(respuesta); pw.print(respuesta);
} }
......
package demojsoncrud.servlets; package demojsoncrud.servlets;
import demojsoncrud.services.UbigeoService; import demojsoncrud.services.UbigeoService;
import static demojsoncrud.utilities.CustomHttpServletRequest.getBodyJsonObject;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import javax.servlet.ServletException; import javax.servlet.ServletException;
...@@ -16,12 +15,28 @@ public class UbigeoServlet extends HttpServlet { ...@@ -16,12 +15,28 @@ public class UbigeoServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String param = request.getParameter("accion"); String param = request.getParameter("accion");
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
if ("listarDepartamento".equals(param)) { if (null != param) {
switch (param) {
case "listarDepartamento":
listarDepartamento(request, response); listarDepartamento(request, response);
} else if ("listarProvincia".equals(param)) { break;
case "listarProvincia":
listarProvincia(request, response); listarProvincia(request, response);
} else if ("listarDistrito".equals(param)) { break;
case "listarDistrito":
listarDistrito(request, response); listarDistrito(request, response);
break;
default:
response.setContentType("application/json");
PrintWriter pw = response.getWriter();
JSONObject respuesta = new JSONObject();
respuesta
.put("status", false)
.put("message", "No existe la url solicitada.")
.put("data", "");
pw.print(respuesta);
break;
}
} }
} }
...@@ -37,9 +52,8 @@ public class UbigeoServlet extends HttpServlet { ...@@ -37,9 +52,8 @@ public class UbigeoServlet extends HttpServlet {
response.setContentType("application/json"); response.setContentType("application/json");
PrintWriter pw = response.getWriter(); PrintWriter pw = response.getWriter();
UbigeoService service = new UbigeoService(); UbigeoService service = new UbigeoService();
JSONObject datos = new JSONObject(); JSONObject datos = new JSONObject(request.getParameter("json"));
datos = getBodyJsonObject(request); JSONObject respuesta = service.listarProvincia(datos);
JSONObject respuesta = service.listarProvincia(datos.getJSONObject("json"));
pw.print(respuesta); pw.print(respuesta);
} }
...@@ -47,9 +61,8 @@ public class UbigeoServlet extends HttpServlet { ...@@ -47,9 +61,8 @@ public class UbigeoServlet extends HttpServlet {
response.setContentType("application/json"); response.setContentType("application/json");
PrintWriter pw = response.getWriter(); PrintWriter pw = response.getWriter();
UbigeoService service = new UbigeoService(); UbigeoService service = new UbigeoService();
JSONObject datos = new JSONObject(); JSONObject datos = new JSONObject(request.getParameter("json"));
datos = getBodyJsonObject(request); JSONObject respuesta = service.listarDistrito(datos);
JSONObject respuesta = service.listarDistrito(datos.getJSONObject("json"));
pw.print(respuesta); pw.print(respuesta);
} }
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package demojsoncrud.sqlserverdao; package demojsoncrud.sqlserverdao;
import demojsoncrud.beans.UbigeoBean; import demojsoncrud.beans.UbigeoBean;
...@@ -15,15 +10,11 @@ import java.sql.SQLException; ...@@ -15,15 +10,11 @@ import java.sql.SQLException;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
/**
*
* @author Alonso
*/
public class UbigeoSqlserverDAO implements UbigeoDAO { public class UbigeoSqlserverDAO implements UbigeoDAO {
@Override @Override
public JSONObject listarDepartamento() { public JSONObject listarDepartamento() {
JSONObject jsonReturn = new JSONObject(); JSONObject jsonReturn = null;
JSONArray data = new JSONArray(); JSONArray data = new JSONArray();
PreparedStatement psListarDepartamento = null; PreparedStatement psListarDepartamento = null;
ResultSet rsListarDepartamento = null; ResultSet rsListarDepartamento = null;
...@@ -60,6 +51,7 @@ public class UbigeoSqlserverDAO implements UbigeoDAO { ...@@ -60,6 +51,7 @@ public class UbigeoSqlserverDAO implements UbigeoDAO {
} catch (SQLException e) { } catch (SQLException e) {
response.setMessage("Error al listarDepartamento -> " + e.getMessage() + " Error code [" + e.getErrorCode() + "]."); response.setMessage("Error al listarDepartamento -> " + e.getMessage() + " Error code [" + e.getErrorCode() + "].");
response.setStatus(false); response.setStatus(false);
System.err.println("Error al registrar -> " + e.getMessage() + "[" + e.getErrorCode() + "]");
} finally { } finally {
try { try {
if (rsListarDepartamento != null) { if (rsListarDepartamento != null) {
...@@ -72,21 +64,19 @@ public class UbigeoSqlserverDAO implements UbigeoDAO { ...@@ -72,21 +64,19 @@ public class UbigeoSqlserverDAO implements UbigeoDAO {
connection.close(); connection.close();
} }
} catch (SQLException e) { } catch (SQLException e) {
response.setMessage("Error al listarDepartamento -> " + e.getMessage() + " Error code [" + e.getErrorCode() + "]."); System.err.println("Error: ha ocurrido un error al intentar cerrar las conexiones y/o liberacion de recursos -> " + e.getMessage());
response.setStatus(false);
} }
} }
jsonReturn = new JSONObject(response); jsonReturn = new JSONObject(response);
return jsonReturn; return jsonReturn;
} }
@Override @Override
public JSONObject listarProvincia(JSONObject datos) { public JSONObject listarProvincia(JSONObject datos) {
System.out.println(datos); System.out.println(datos);
JSONObject jsonReturn = new JSONObject(); JSONObject jsonReturn = null;
JSONArray data = new JSONArray(); JSONArray data = new JSONArray();
PreparedStatement psListarProvincia = null; PreparedStatement psListarProvincia = null;
ResultSet rsListarProvincia = null; ResultSet rsListarProvincia = null;
...@@ -125,6 +115,7 @@ public class UbigeoSqlserverDAO implements UbigeoDAO { ...@@ -125,6 +115,7 @@ public class UbigeoSqlserverDAO implements UbigeoDAO {
} catch (SQLException e) { } catch (SQLException e) {
response.setMessage("Error al listarProvincia -> " + e.getMessage() + " Error code [" + e.getErrorCode() + "]."); response.setMessage("Error al listarProvincia -> " + e.getMessage() + " Error code [" + e.getErrorCode() + "].");
response.setStatus(false); response.setStatus(false);
System.err.println("Error al registrar -> " + e.getMessage() + "[" + e.getErrorCode() + "]");
} finally { } finally {
try { try {
if (rsListarProvincia != null) { if (rsListarProvincia != null) {
...@@ -137,20 +128,18 @@ public class UbigeoSqlserverDAO implements UbigeoDAO { ...@@ -137,20 +128,18 @@ public class UbigeoSqlserverDAO implements UbigeoDAO {
connection.close(); connection.close();
} }
} catch (SQLException e) { } catch (SQLException e) {
response.setMessage("Error al listarProvincia -> " + e.getMessage() + " Error code [" + e.getErrorCode() + "]."); System.err.println("Error: ha ocurrido un error al intentar cerrar las conexiones y/o liberacion de recursos -> " + e.getMessage());
response.setStatus(false);
} }
} }
jsonReturn = new JSONObject(response); jsonReturn = new JSONObject(response);
return jsonReturn; return jsonReturn;
} }
@Override @Override
public JSONObject listarDistrito(JSONObject datos) { public JSONObject listarDistrito(JSONObject datos) {
JSONObject jsonReturn = new JSONObject(); JSONObject jsonReturn = null;
JSONArray data = new JSONArray(); JSONArray data = new JSONArray();
PreparedStatement psListarDistrito = null; PreparedStatement psListarDistrito = null;
ResultSet rsListarDistrito = null; ResultSet rsListarDistrito = null;
...@@ -189,6 +178,7 @@ public class UbigeoSqlserverDAO implements UbigeoDAO { ...@@ -189,6 +178,7 @@ public class UbigeoSqlserverDAO implements UbigeoDAO {
} catch (SQLException e) { } catch (SQLException e) {
response.setMessage("Error al listarDistrito -> " + e.getMessage() + " Error code [" + e.getErrorCode() + "]."); response.setMessage("Error al listarDistrito -> " + e.getMessage() + " Error code [" + e.getErrorCode() + "].");
response.setStatus(false); response.setStatus(false);
System.err.println("Error al registrar -> " + e.getMessage() + "[" + e.getErrorCode() + "]");
} finally { } finally {
try { try {
if (rsListarDistrito != null) { if (rsListarDistrito != null) {
...@@ -201,14 +191,11 @@ public class UbigeoSqlserverDAO implements UbigeoDAO { ...@@ -201,14 +191,11 @@ public class UbigeoSqlserverDAO implements UbigeoDAO {
connection.close(); connection.close();
} }
} catch (SQLException e) { } catch (SQLException e) {
response.setMessage("Error al listarDistrito -> " + e.getMessage() + " Error code [" + e.getErrorCode() + "]."); System.err.println("Error: ha ocurrido un error al intentar cerrar las conexiones y/o liberacion de recursos -> " + e.getMessage());
response.setStatus(false);
} }
} }
jsonReturn = new JSONObject(response); jsonReturn = new JSONObject(response);
return jsonReturn; return jsonReturn;
} }
......
package demojsoncrud.utilities;
import java.io.BufferedReader;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
public class CustomHttpServletRequest {
public static JSONObject getBodyJsonObject(HttpServletRequest request) throws IOException {
String body = "";
if (request.getMethod().equals("POST")) {
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = null;
bufferedReader = request.getReader(); // swallow silently -- can't get body, won't
char[] charBuffer = new char[128];
int bytesRead;
while ((bytesRead = bufferedReader.read(charBuffer)) != -1) {
sb.append(charBuffer, 0, bytesRead);
}
if (bufferedReader != null) {
bufferedReader.close(); // swallow silently -- can't get body, won't
}
body = sb.toString();
}
JSONObject respuesta = new JSONObject(body);
return respuesta;
}
}
...@@ -10,7 +10,12 @@ public class ResponseHelper implements Serializable { ...@@ -10,7 +10,12 @@ public class ResponseHelper implements Serializable {
private String message; private String message;
public ResponseHelper() { public ResponseHelper() {
}
public ResponseHelper(JSONObject data, boolean status, String message) {
this.data = data;
this.status = status;
this.message = message;
} }
public JSONObject getData() { public JSONObject getData() {
......
/** /**
* Facilita el uso de fetch
* @method fetchSo
* @param {String} url nombre de objeto
* @param {object} object mensaje a mostrar
* @returns {Promise} Promise retorna status, msg
*/
let fetchSo = (url, object) => {
return new Promise(
(resolve, reject) => {
let header = {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json; charset=utf-8'
}
let requestInfo = {
method: 'post',
headers: header,
body: JSON.stringify({json: object})
}
fetch(url, requestInfo)
.then((res) => {
return res.json()
})
.then((res) => {
if (res.status) {
resolve(res)
} else {
reject(res)
}
})
.catch((error) => {
reject(error)
})
}
)
}
/**
* Crea los elementos option del array que se pase como parametro * Crea los elementos option del array que se pase como parametro
* @method createSelectOptions * @method createSelectOptions
* @param {JSONArray} obj array de objetos * @param {JSONArray} obj array de objetos
...@@ -52,7 +15,7 @@ let createSelectOptions = (obj, valueName, textName) => { ...@@ -52,7 +15,7 @@ let createSelectOptions = (obj, valueName, textName) => {
} }
let customSwal = { let customSwal = {
alert (title, text, type) { alert(title, text, type) {
let colors = { let colors = {
success: '#66BB6A', success: '#66BB6A',
error: '#EF5350', error: '#EF5350',
...@@ -92,35 +55,15 @@ let getClosest = (elem, selector) => { ...@@ -92,35 +55,15 @@ let getClosest = (elem, selector) => {
function (s) { function (s) {
var matches = (this.document || this.ownerDocument).querySelectorAll(s), var matches = (this.document || this.ownerDocument).querySelectorAll(s),
i = matches.length i = matches.length
while (--i >= 0 && matches.item(i) !== this) {} while (--i >= 0 && matches.item(i) !== this) {
}
return i > -1 return i > -1
} }
} }
// Get closest match // Get closest match
for (; elem && elem !== document; elem = elem.parentNode) { for (; elem && elem !== document; elem = elem.parentNode) {
if (elem.matches(selector)) return elem if (elem.matches(selector))
return elem
} }
return null return null
} }
// function imprimirAlerta (title, text, type) {
// var s_col =
// var e_col = '#EF5350'
// var w_col = '#FF7043'
// var btnCol
//
// if (type === 'error') {
// btnCol = e_col
// } else if (type === 'warning') {
// btnCol = w_col
// } else {
// btnCol = s_col
// }
//
// swal({
// title: title,
// text: text,
// confirmButtonColor: btnCol,
// type: type
// })
//
// }
\ No newline at end of file
let jqueryValidateConfig = () => { let jqueryValidateConfig = () => {
function stripHtml (value) { function stripHtml(value) {
// remove html tags and space chars // remove html tags and space chars
return value.replace(/<.[^<>]*?>/g, ' ').replace(/&nbsp;|&#160;/gi, ' ') return value.replace(/<.[^<>]*?>/g, ' ').replace(/&nbsp;|&#160;/gi, ' ')
// remove numbers and punctuation // remove numbers and punctuation
...@@ -57,7 +57,7 @@ let jqueryValidateConfig = () => { ...@@ -57,7 +57,7 @@ let jqueryValidateConfig = () => {
}) })
} }
let persona = { let persona = {
listar () { listar() {
let txtCriterioBusqueda = $('#txt-busqueda').val().trim() let txtCriterioBusqueda = $('#txt-busqueda').val().trim()
return new Promise((resolve) => { return new Promise((resolve) => {
$('#tbl-persona').DataTable().destroy() $('#tbl-persona').DataTable().destroy()
...@@ -136,18 +136,26 @@ let persona = { ...@@ -136,18 +136,26 @@ let persona = {
}) })
}) })
}, },
cambiarEstado (datos) { cambiarEstado(datos) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fetchSo('../PersonaServlet?accion=cambiarEstado', datos) $.ajax({
.then((data) => { url: '../PersonaServlet',
dataType: 'json',
type: 'POST',
data: {
accion: 'cambiarEstado',
json: JSON.stringify(datos)
}, beforeSend: function (xhr) {
}, success: function (data, textStatus, jqXHR) {
resolve(data) resolve(data)
}) }, error: function (jqXHR, textStatus, errorThrown) {
.catch((data) => { reject("Error al cambiar estado")
reject(data) }
}) })
}) })
}, },
registrar () { registrar() {
let json = { let json = {
dni: $('#txt_numero_documento').val().trim(), dni: $('#txt_numero_documento').val().trim(),
apellidos: $('#txt_apellidos').val().trim(), apellidos: $('#txt_apellidos').val().trim(),
...@@ -156,6 +164,22 @@ let persona = { ...@@ -156,6 +164,22 @@ let persona = {
correo: $('#txt_correo').val().trim() correo: $('#txt_correo').val().trim()
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
$.ajax({
url: '../PersonaServlet',
dataType: 'json',
type: 'POST',
data: {
accion: 'registrarPersona',
json: JSON.stringify(json)
}, beforeSend: function (xhr) {
}, success: function (data, textStatus, jqXHR) {
resolve(data)
}, error: function (jqXHR, textStatus, errorThrown) {
reject("Error al registrar personal")
}
})
fetchSo('../PersonaServlet?accion=registrarPersona', json) fetchSo('../PersonaServlet?accion=registrarPersona', json)
.then((data) => { .then((data) => {
resolve(data) resolve(data)
...@@ -165,7 +189,7 @@ let persona = { ...@@ -165,7 +189,7 @@ let persona = {
}) })
}) })
}, },
editar () { editar() {
let json = { let json = {
apellidos: $('#txt_apellidos_editar').val().trim(), apellidos: $('#txt_apellidos_editar').val().trim(),
nombres: $('#txt_nombres_editar').val().trim(), nombres: $('#txt_nombres_editar').val().trim(),
...@@ -174,12 +198,20 @@ let persona = { ...@@ -174,12 +198,20 @@ let persona = {
codigo: localStorage.getItem('codigoPersona') codigo: localStorage.getItem('codigoPersona')
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fetchSo('../PersonaServlet?accion=editarPersona', json) $.ajax({
.then((data) => { url: '../PersonaServlet',
dataType: 'json',
type: 'POST',
data: {
accion: 'editarPersona',
json: JSON.stringify(json)
}, beforeSend: function (xhr) {
}, success: function (data, textStatus, jqXHR) {
resolve(data) resolve(data)
}) }, error: function (jqXHR, textStatus, errorThrown) {
.catch((data) => { reject("Error al editar persona")
reject(data) }
}) })
}) })
} }
......
let ubigeo = { let ubigeo = {
consultarDepartamento () { consultarDepartamento() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fetchSo('../UbigeoServlet?accion=listarDepartamento') $.ajax({
.then((data) => { url: '../UbigeoServlet',
dataType: 'json',
type: 'POST',
data: {
accion: 'listarDepartamento'
}, beforeSend: function (xhr) {
}, success: function (data, textStatus, jqXHR) {
resolve(data) resolve(data)
}) }, error: function (jqXHR, textStatus, errorThrown) {
.catch((data) => { reject("Error al listar departamentos")
reject(data) }
}) })
}) })
}, },
consultarProvincia (codigoDepartamento) { consultarProvincia(codigoDepartamento) {
let json = { let json = {
codigoDepartamento: codigoDepartamento codigoDepartamento: codigoDepartamento
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fetchSo('../UbigeoServlet?accion=listarProvincia', json) $.ajax({
.then((data) => { url: '../UbigeoServlet',
dataType: 'json',
type: 'POST',
data: {
accion: 'listarProvincia',
json: JSON.stringify(json)
}, beforeSend: function (xhr) {
}, success: function (data, textStatus, jqXHR) {
resolve(data) resolve(data)
}) }, error: function (jqXHR, textStatus, errorThrown) {
.catch((data) => { reject("Error al listar provincias")
reject(data) }
}) })
}) })
}, },
consultarDistrito (codigoDepartamento, codigoProvincia) { consultarDistrito(codigoDepartamento, codigoProvincia) {
let json = { let json = {
codigoProvincia: codigoProvincia, codigoProvincia: codigoProvincia,
codigoDepartamento: codigoDepartamento codigoDepartamento: codigoDepartamento
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fetchSo('../UbigeoServlet?accion=listarDistrito', json) $.ajax({
.then((data) => { url: '../UbigeoServlet',
dataType: 'json',
type: 'POST',
data: {
accion: 'listarDistrito',
json: JSON.stringify(json)
}, beforeSend: function (xhr) {
}, success: function (data, textStatus, jqXHR) {
resolve(data) resolve(data)
}) }, error: function (jqXHR, textStatus, errorThrown) {
.catch((data) => { reject("Error al listar distritos")
reject(data) }
}) })
}) })
} }
...@@ -54,6 +78,8 @@ let asignarEventos = () => { ...@@ -54,6 +78,8 @@ let asignarEventos = () => {
slProvincia.innerHTML = opt slProvincia.innerHTML = opt
}) })
.then(() => { .then(() => {
slDistrito.innerHTML = '<option value="0">[ SELECCIONE ]</option>'
$('#select_distrito').selectpicker('refresh')
$('#select_provincia').selectpicker('refresh') $('#select_provincia').selectpicker('refresh')
}) })
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment