Cambio a ES6

parent fc9fe877
<component name="ProjectDictionaryState">
<dictionary name="sistem16user" />
</component>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false">
<option name="processCode" value="true" />
<option name="processLiterals" value="true" />
<option name="processComments" value="true" />
</inspection_tool>
</profile>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/proyecto-estandar.iml" filepath="$PROJECT_DIR$/.idea/proyecto-estandar.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
<excludeFolder url="file://$MODULE_DIR$/temp" />
<excludeFolder url="file://$MODULE_DIR$/tmp" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectTasksOptions" suppressed-tasks="Babel" />
</project>
\ No newline at end of file
......@@ -9,12 +9,14 @@ public interface PersonaDAO {
public JSONObject listarPersona(String search, String draw, String start, String length);
public JSONObject registrarPersona(PersonaBean bean);
public JSONObject registrarPersona(JSONObject datos);
public JSONObject editarPersona(PersonaBean bean);
public JSONObject editarPersona(JSONObject datos);
public JSONObject activarPersona(PersonaBean bean);
public JSONObject desactivarPersona(PersonaBean bean);
public JSONObject cambiarEstado(JSONObject datos);
}
......@@ -30,20 +30,20 @@ public class PersonaService {
return jsonReturn;
}
public JSONObject registrarPersona(PersonaBean bean) {
public JSONObject registrarPersona(JSONObject datos) {
JSONObject jsonReturn = null;
try {
jsonReturn = service.registrarPersona(bean);
jsonReturn = service.registrarPersona(datos);
} catch (Exception e) {
e.getMessage();
}
return jsonReturn;
}
public JSONObject editarPersona(PersonaBean bean) {
public JSONObject editarPersona(JSONObject datos) {
JSONObject jsonReturn = null;
try {
jsonReturn = service.editarPersona(bean);
jsonReturn = service.editarPersona(datos);
} catch (Exception e) {
e.getMessage();
}
......@@ -67,5 +67,14 @@ public class PersonaService {
}
return jsonReturn;
}
public JSONObject cambiarEstado(JSONObject datos) {
JSONObject respuesta = null;
try {
respuesta = service.cambiarEstado(datos);
} catch (Exception e) {
}
return respuesta;
}
}
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package demojsoncrud.servlets;
import demojsoncrud.beans.PersonaBean;
import demojsoncrud.services.PersonaService;
import static demojsoncrud.utilities.CustomHttpServletRequest.getBodyJsonObject;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
......@@ -27,6 +28,8 @@ public class PersonaServlet extends HttpServlet {
activarPersona(request, response);
} else if ("desactivarPersona".equals(param)) {
desactivarPersona(request, response);
} else if ("cambiarEstado".equals(param)) {
cambiarEstado(request, response);
}
}
......@@ -56,54 +59,21 @@ public class PersonaServlet extends HttpServlet {
response.setContentType("application/json");
PrintWriter pw = response.getWriter();
PersonaService service = new PersonaService();
String json = request.getParameter("json");
JSONObject jsonObject = new JSONObject(json);
String dni = jsonObject.getString("dni");
String apellidos = jsonObject.getString("apellidos");
String nombres = jsonObject.getString("nombres");
String edad = jsonObject.getString("edad");
String correo = jsonObject.getString("correo");
PersonaBean bean = new PersonaBean();
bean.setDni(dni);
bean.setApellidos(apellidos);
bean.setNombres(nombres);
bean.setEdad(edad);
bean.setCorreo(correo);
JSONObject jsonReturn = service.registrarPersona(bean);
pw.print(jsonReturn);
JSONObject datos = new JSONObject();
datos = getBodyJsonObject(request);
JSONObject respuesta = service.registrarPersona(datos.getJSONObject("json"));
pw.print(respuesta);
}
private void editarPersona(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("application/json");
PersonaService service = new PersonaService();
PrintWriter pw = response.getWriter();
String json = request.getParameter("json");
JSONObject jsonObject = new JSONObject(json);
String apellidos = jsonObject.getString("apellidos");
String nombres = jsonObject.getString("nombres");
String edad = jsonObject.getString("edad");
String correo = jsonObject.getString("correo");
String codigo = jsonObject.getString("codigo");
PersonaBean bean = new PersonaBean();
bean.setApellidos(apellidos);
bean.setNombres(nombres);
bean.setEdad(edad);
bean.setCorreo(correo);
bean.setCodigo(codigo);
JSONObject jsonReturn = service.editarPersona(bean);
pw.print(jsonReturn);
PersonaService service = new PersonaService();
JSONObject datos = new JSONObject();
datos = getBodyJsonObject(request);
JSONObject respuesta = service.editarPersona(datos.getJSONObject("json"));
pw.print(respuesta);
}
private void activarPersona(HttpServletRequest request, HttpServletResponse response) throws IOException {
......@@ -140,4 +110,15 @@ public class PersonaServlet extends HttpServlet {
pw.print(jsonReturn);
}
private void cambiarEstado(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("application/json");
PrintWriter pw = response.getWriter();
PersonaService service = new PersonaService();
JSONObject datos = new JSONObject();
datos = getBodyJsonObject(request);
JSONObject respuesta = service.cambiarEstado(datos.getJSONObject("json"));
System.out.println(respuesta);
pw.print(respuesta);
}
}
......@@ -27,15 +27,15 @@ public class PersonaSqlserverDAO implements PersonaDAO {
try {
connection = SqlserverDAOFactory.obtenerConexion(base);
String sql
= "select "
+ "a.codigo, "
+ "a.dni, "
+ "a.apellidos, "
+ "a.nombres, "
+ "a.edad, "
+ "a.correo, "
+ "a.estado "
+ "from persona a ";
= "select "
+ "a.codigo, "
+ "a.dni, "
+ "a.apellidos, "
+ "a.nombres, "
+ "a.edad, "
+ "a.correo, "
+ "a.estado "
+ "from persona a ";
psListarPersona = connection.prepareStatement(sql);
rsListarPersona = psListarPersona.executeQuery();
......@@ -110,18 +110,18 @@ public class PersonaSqlserverDAO implements PersonaDAO {
try {
connection = SqlserverDAOFactory.obtenerConexion(base);
String sql
= "select top " + length + " "
+ "a.codigo, "
+ "a.dni, "
+ "a.apellidos, "
+ "a.nombres, "
+ "a.edad, "
+ "a.correo, "
+ "a.estado "
+ "from persona a "
+ "where a.codigo not in (select top " + start + " b.codigo from persona b order by 1 desc) "
+ condicion + " "
+ "order by 1 desc";
= "select top " + length + " "
+ "a.codigo, "
+ "a.dni, "
+ "a.apellidos, "
+ "a.nombres, "
+ "a.edad, "
+ "a.correo, "
+ "a.estado "
+ "from persona a "
+ "where a.codigo not in (select top " + start + " b.codigo from persona b order by 1 desc) "
+ condicion + " "
+ "order by 1 desc";
psListarPersona = connection.prepareStatement(sql);
rsListarPersona = psListarPersona.executeQuery();
......@@ -178,7 +178,8 @@ public class PersonaSqlserverDAO implements PersonaDAO {
}
@Override
public JSONObject registrarPersona(PersonaBean bean) {
public JSONObject registrarPersona(JSONObject datos) {
System.out.println(datos);
JSONObject jsonReturn = new JSONObject();
int resultDni = 0;
int resultPersona = 0;
......@@ -191,22 +192,22 @@ public class PersonaSqlserverDAO implements PersonaDAO {
String sqlGetDni = "select count(1) dni from persona where dni = ?";
PreparedStatement psGetDni = connection.prepareStatement(sqlGetDni);
psGetDni.setString(1, bean.getDni());
psGetDni.setString(1, datos.getString("dni"));
ResultSet rsGetDni = psGetDni.executeQuery();
rsGetDni.next();
resultDni = rsGetDni.getInt("dni");
if (resultDni == 0) {
String sql
= "insert into persona (dni, apellidos, nombres, edad, correo, estado) values "
+ "(?,RTRIM(UPPER(?)),RTRIM(UPPER(?)),?,RTRIM(UPPER(?)),1)";
= "insert into persona (dni, apellidos, nombres, edad, correo, estado) values "
+ "(?,RTRIM(UPPER(?)),RTRIM(UPPER(?)),?,RTRIM(UPPER(?)),1)";
int c = 1; //contador para las columnas
psInsertarPersona = connection.prepareStatement(sql);
psInsertarPersona.setString(c++, bean.getDni());
psInsertarPersona.setString(c++, bean.getApellidos());
psInsertarPersona.setString(c++, bean.getNombres());
psInsertarPersona.setString(c++, bean.getEdad());
psInsertarPersona.setString(c++, bean.getCorreo());
psInsertarPersona.setString(c++, datos.getString("dni"));
psInsertarPersona.setString(c++, datos.getString("apellidos"));
psInsertarPersona.setString(c++, datos.getString("nombres"));
psInsertarPersona.setString(c++, datos.getString("edad"));
psInsertarPersona.setString(c++, datos.getString("correo"));
resultPersona = psInsertarPersona.executeUpdate();
if (resultPersona == 1) {
response.setMessage("Registro correcto!");
......@@ -239,12 +240,12 @@ public class PersonaSqlserverDAO implements PersonaDAO {
}
jsonReturn = new JSONObject(response);
System.out.println(jsonReturn);
return jsonReturn;
}
@Override
public JSONObject editarPersona(PersonaBean bean) {
public JSONObject editarPersona(JSONObject datos) {
JSONObject jsonReturn = new JSONObject();
String base = "demojsoncrud";
Connection connection = null;
......@@ -255,20 +256,20 @@ public class PersonaSqlserverDAO implements PersonaDAO {
try {
connection = SqlserverDAOFactory.obtenerConexion(base);
String sql
= "update persona "
+ "set apellidos = RTRIM(UPPER(?)), "
+ "nombres = RTRIM(UPPER(?)), "
+ "edad = ?, "
+ "correo = RTRIM(UPPER(?)) "
+ "where codigo = ?";
= "update persona "
+ "set apellidos = RTRIM(UPPER(?)), "
+ "nombres = RTRIM(UPPER(?)), "
+ "edad = ?, "
+ "correo = RTRIM(UPPER(?)) "
+ "where codigo = ?";
psEditarPersona = connection.prepareStatement(sql);
int q = 1; // CONTADOR PARA LAS COLUMNAS
psEditarPersona.setString(q++, bean.getApellidos());
psEditarPersona.setString(q++, bean.getNombres());
psEditarPersona.setString(q++, bean.getEdad());
psEditarPersona.setString(q++, bean.getCorreo());
psEditarPersona.setString(q++, bean.getCodigo());
psEditarPersona.setString(q++, datos.getString("apellidos"));
psEditarPersona.setString(q++, datos.getString("nombres"));
psEditarPersona.setString(q++, datos.getString("edad"));
psEditarPersona.setString(q++, datos.getString("correo"));
psEditarPersona.setString(q++, datos.getString("codigo"));
resultPersona = psEditarPersona.executeUpdate();
if (resultPersona == 1) {
......@@ -313,9 +314,9 @@ public class PersonaSqlserverDAO implements PersonaDAO {
try {
connection = SqlserverDAOFactory.obtenerConexion(base);
String sql
= "update persona "
+ "set estado = 1 "
+ "where codigo = ?";
= "update persona "
+ "set estado = 1 "
+ "where codigo = ?";
psEditarPersona = connection.prepareStatement(sql);
......@@ -364,9 +365,9 @@ public class PersonaSqlserverDAO implements PersonaDAO {
try {
connection = SqlserverDAOFactory.obtenerConexion(base);
String sql
= "update persona "
+ "set estado = 0 "
+ "where codigo = ?";
= "update persona "
+ "set estado = 0 "
+ "where codigo = ?";
psEditarPersona = connection.prepareStatement(sql);
psEditarPersona.setString(1, bean.getCodigo());
......@@ -401,4 +402,55 @@ public class PersonaSqlserverDAO implements PersonaDAO {
return jsonReturn;
}
@Override
public JSONObject cambiarEstado(JSONObject datos) {
System.out.println(datos);
JSONObject jsonReturn = new JSONObject();
String base = "demojsoncrud";
Connection connection = null;
PreparedStatement psEditarPersona = null;
int resultPersona = 0;
ResponseHelper response = new ResponseHelper();
try {
connection = SqlserverDAOFactory.obtenerConexion(base);
String sql
= "UPDATE persona "
+ " SET estado = ? "
+ "WHERE codigo = ?;";
psEditarPersona = connection.prepareStatement(sql);
psEditarPersona.setString(1, datos.getString("estado").equals("1") ? "0" : "1");
psEditarPersona.setString(2, datos.getString("codigo"));
resultPersona = psEditarPersona.executeUpdate();
if (resultPersona == 1) {
response.setMessage("Se actualizo correctamente");
response.setStatus(true); // ACTUALIZO
} else {
response.setMessage("Error al actualizar");
response.setStatus(false); // ERROR
}
} catch (SQLException e) {
response.setStatus(false);
response.setMessage("Error al actualizar -> " + e.getMessage() + "[" + e.getErrorCode() + "]");
} finally {
try {
if (connection != null) {
connection.close();
}
if (psEditarPersona != null) {
psEditarPersona.close();
}
} catch (SQLException e) {
response.setStatus(false);
response.setMessage("Error al actualizar -> " + e.getMessage() + "[" + e.getErrorCode() + "]");
}
}
jsonReturn = new JSONObject(response);
return jsonReturn;
}
}
/*
* 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;
import demojsoncrud.dao.DAOFactory;
......@@ -12,10 +7,6 @@ import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
*
* @author Alonso
*/
public class SqlserverDAOFactory extends DAOFactory {
public static Connection obtenerConexion(String base) {
......@@ -43,10 +34,6 @@ public class SqlserverDAOFactory extends DAOFactory {
return connection;
}
// public static void main(String[] args) {
// SqlserverDAOFactory sqlserverDAOFactory = new SqlserverDAOFactory();
// System.out.println(sqlserverDAOFactory.obtenerConexion("demojsoncrud"));
// }
@Override
public PersonaDAO getPersonaDAO() {
return new PersonaSqlserverDAO();
......
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;
}
}
/**
* 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)
})
}
)
}
let customSwal = {
alert (title, text, type) {
let colors = {
success: '#66BB6A',
error: '#EF5350',
warning: '#FF7043'
}
let btnColor = colors[type]
swal({
title: title,
text: text,
confirmButtonColor: btnColor,
type: type
})
}
}
// 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
<%--
Document : index
Created on : 05-ene-2018, 20:49:32
Author : Alonso
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
......@@ -18,8 +12,6 @@
<body>
<%@include file="templates/header-body.jsp" %>
<!-- content -->
<div class="row">
<div class="col-md-6 col-md-offset-3">
......@@ -35,7 +27,7 @@
<label class="control-label display-block">Ingresar criterio de búsqueda: </label>
<div class="input-group input-group-xlg">
<span class="input-group-addon"><i class="icon-search4"></i></span>
<input id="txt_criterio_busqueda" type="text" class="form-control" placeholder="Ingresar criterio">
<input id="txt-busqueda" type="text" class="form-control" placeholder="Ingresar criterio">
</div>
</div>
</div>
......@@ -50,41 +42,36 @@
</div>
</div>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="col-md-12">
<div class="panel panel-flat border-top-xlg border-top-primary">
<div class="panel-heading">
<h6 class="panel-title text-semibold"><i class=" icon-list3 position-left"></i> Listado persona</h6>
</div>
<div class="panel-body">
<div class="row">
<div class="col-md-12 text-center">
<button id="btn_agregar_persona" type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal_registrar_persona"><i class="icon-plus3 position-left"></i> Agregar</button>
</div>
</div><br>
<div class="row">
<div class="col-md-12">
<div class="table-responsive">
<table class="table table-striped table-sm" id="tabla_persona">
<thead>
<tr>
<th>#</th>
<th>DNI</th>
<th>APELLIDOS Y NOMBRES</th>
<th>EDAD</th>
<th>CORREO ELECTRONICO</th>
<th>ESTADO</th>
<th>ACCIONES</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="table-responsive">
<table class="table table-striped table-sm" id="tbl-persona">
<thead>
<tr>
<th>#</th>
<th>DNI</th>
<th>APELLIDOS Y NOMBRES</th>
<th>EDAD</th>
<th>CORREO ELECTRONICO</th>
<th>ESTADO</th>
<th>ACCIONES</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
......@@ -120,7 +107,7 @@
</div>
</div>
</div>
<div class="col-md-4">
<div class="col-md-4">
<div class="form-group">
<label class="control-label display-block">Nombres:<span class="text-danger"> (*)</span></label>
<div class="input-group">
......@@ -197,7 +184,7 @@
</div>
</div>
</div>
<div class="col-md-4">
<div class="col-md-4">
<div class="form-group">
<label class="control-label display-block">Nombres:<span class="text-danger"> (*)</span></label>
<div class="input-group">
......@@ -244,8 +231,6 @@
</div>
<!-- /modal_editar_persona -->
<%@include file="templates/footer-body.jsp" %>
<!--js plantilla-->
......@@ -277,6 +262,7 @@
<!--js plantilla-->
<!--mi js-->
<script src="../js/general.js" type="text/javascript"></script>
<script src="../js/pages/persona.js" type="text/javascript"></script>
<!--mi js-->
......
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