Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
proyecto-estandar
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Israel Gutierrez Salazar
proyecto-estandar
Commits
94d0c27c
Commit
94d0c27c
authored
Mar 28, 2018
by
Alonso Moreno Postigo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[EDIT] v0.1
parent
ba1776aa
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
1285 additions
and
1318 deletions
+1285
-1318
PersonaBean.java
src/java/demojsoncrud/beans/PersonaBean.java
+53
-55
UbigeoBean.java
src/java/demojsoncrud/beans/UbigeoBean.java
+59
-54
DAOFactory.java
src/java/demojsoncrud/dao/DAOFactory.java
+11
-20
PersonaDAO.java
src/java/demojsoncrud/dao/PersonaDAO.java
+1
-2
UbigeoDAO.java
src/java/demojsoncrud/dao/UbigeoDAO.java
+3
-13
PersonaService.java
src/java/demojsoncrud/services/PersonaService.java
+37
-38
UbigeoService.java
src/java/demojsoncrud/services/UbigeoService.java
+26
-24
PersonaServlet.java
src/java/demojsoncrud/servlets/PersonaServlet.java
+66
-67
UbigeoServlet.java
src/java/demojsoncrud/servlets/UbigeoServlet.java
+50
-37
PersonaSqlserverDAO.java
src/java/demojsoncrud/sqlserverdao/PersonaSqlserverDAO.java
+258
-249
UbigeoSqlserverDAO.java
src/java/demojsoncrud/sqlserverdao/UbigeoSqlserverDAO.java
+176
-189
CustomHttpServletRequest.java
...java/demojsoncrud/utilities/CustomHttpServletRequest.java
+0
-31
ResponseHelper.java
src/java/demojsoncrud/utilities/ResponseHelper.java
+36
-31
general.js
web/js/general.js
+44
-102
persona.js
web/js/pages/persona.js
+358
-326
ubigeo.js
web/js/pages/ubigeo.js
+107
-80
No files found.
src/java/demojsoncrud/beans/PersonaBean.java
View file @
94d0c27c
...
@@ -2,77 +2,75 @@ package demojsoncrud.beans;
...
@@ -2,77 +2,75 @@ package demojsoncrud.beans;
import
java.io.Serializable
;
import
java.io.Serializable
;
/**
*
* @author Alonso
*/
public
class
PersonaBean
implements
Serializable
{
public
class
PersonaBean
implements
Serializable
{
private
String
codigo
;
private
static
final
long
serialVersionUID
=
4594333294248399680L
;
private
String
dni
;
private
String
apellidos
;
private
String
nombres
;
private
String
edad
;
private
String
correo
;
private
String
estado
;
public
PersonaBean
()
{
private
String
codigo
;
}
private
String
dni
;
private
String
apellidos
;
private
String
nombres
;
private
String
edad
;
private
String
correo
;
private
String
estado
;
public
String
getCodigo
()
{
public
PersonaBean
()
{
return
codigo
;
}
}
public
void
setCodigo
(
String
codigo
)
{
public
String
getCodigo
(
)
{
this
.
codigo
=
codigo
;
return
codigo
;
}
}
public
String
getDni
(
)
{
public
void
setCodigo
(
String
codigo
)
{
return
dni
;
this
.
codigo
=
codigo
;
}
}
public
void
setDni
(
String
dni
)
{
public
String
getDni
(
)
{
this
.
dni
=
dni
;
return
dni
;
}
}
public
String
getApellidos
(
)
{
public
void
setDni
(
String
dni
)
{
return
apellidos
;
this
.
dni
=
dni
;
}
}
public
void
setApellidos
(
String
apellidos
)
{
public
String
getApellidos
(
)
{
this
.
apellidos
=
apellidos
;
return
apellidos
;
}
}
public
String
getNombres
(
)
{
public
void
setApellidos
(
String
apellidos
)
{
return
nombre
s
;
this
.
apellidos
=
apellido
s
;
}
}
public
void
setNombres
(
String
nombres
)
{
public
String
getNombres
(
)
{
this
.
nombres
=
nombres
;
return
nombres
;
}
}
public
String
getEdad
(
)
{
public
void
setNombres
(
String
nombres
)
{
return
edad
;
this
.
nombres
=
nombres
;
}
}
public
void
setEdad
(
String
edad
)
{
public
String
getEdad
(
)
{
this
.
edad
=
edad
;
return
edad
;
}
}
public
String
getCorreo
(
)
{
public
void
setEdad
(
String
edad
)
{
return
correo
;
this
.
edad
=
edad
;
}
}
public
void
setCorreo
(
String
correo
)
{
public
String
getCorreo
(
)
{
this
.
correo
=
correo
;
return
correo
;
}
}
public
String
getEstado
(
)
{
public
void
setCorreo
(
String
correo
)
{
return
estad
o
;
this
.
correo
=
corre
o
;
}
}
public
void
setEstado
(
String
estado
)
{
public
String
getEstado
()
{
this
.
estado
=
estado
;
return
estado
;
}
}
public
void
setEstado
(
String
estado
)
{
this
.
estado
=
estado
;
}
}
}
src/java/demojsoncrud/beans/UbigeoBean.java
View file @
94d0c27c
...
@@ -4,59 +4,64 @@ import java.io.Serializable;
...
@@ -4,59 +4,64 @@ import java.io.Serializable;
public
class
UbigeoBean
implements
Serializable
{
public
class
UbigeoBean
implements
Serializable
{
private
String
codigoDepartamento
;
private
static
final
long
serialVersionUID
=
4594333294248399680L
;
private
String
nombreDepartamento
;
private
String
codigoProvincia
;
private
String
codigoDepartamento
;
private
String
nombreProvincia
;
private
String
nombreDepartamento
;
private
String
codigoDistrito
;
private
String
codigoProvincia
;
private
String
nombreDistrito
;
private
String
nombreProvincia
;
private
String
codigoDistrito
;
public
String
getCodigoDepartamento
()
{
private
String
nombreDistrito
;
return
codigoDepartamento
;
}
public
UbigeoBean
()
{
}
public
void
setCodigoDepartamento
(
String
codigoDepartamento
)
{
this
.
codigoDepartamento
=
codigoDepartamento
;
public
String
getCodigoDepartamento
()
{
}
return
codigoDepartamento
;
}
public
String
getNombreDepartamento
()
{
return
nombreDepartamento
;
public
void
setCodigoDepartamento
(
String
codigoDepartamento
)
{
}
this
.
codigoDepartamento
=
codigoDepartamento
;
}
public
void
setNombreDepartamento
(
String
nombreDepartamento
)
{
this
.
nombreDepartamento
=
nombreDepartamento
;
public
String
getNombreDepartamento
()
{
}
return
nombreDepartamento
;
}
public
String
getCodigoProvincia
()
{
return
codigoProvincia
;
public
void
setNombreDepartamento
(
String
nombreDepartamento
)
{
}
this
.
nombreDepartamento
=
nombreDepartamento
;
}
public
void
setCodigoProvincia
(
String
codigoProvincia
)
{
this
.
codigoProvincia
=
codigoProvincia
;
public
String
getCodigoProvincia
()
{
}
return
codigoProvincia
;
}
public
String
getNombreProvincia
()
{
return
nombreProvincia
;
public
void
setCodigoProvincia
(
String
codigoProvincia
)
{
}
this
.
codigoProvincia
=
codigoProvincia
;
}
public
void
setNombreProvincia
(
String
nombreProvincia
)
{
this
.
nombreProvincia
=
nombreProvincia
;
public
String
getNombreProvincia
()
{
}
return
nombreProvincia
;
}
public
String
getCodigoDistrito
()
{
return
codigoDistrito
;
public
void
setNombreProvincia
(
String
nombreProvincia
)
{
}
this
.
nombreProvincia
=
nombreProvincia
;
}
public
void
setCodigoDistrito
(
String
codigoDistrito
)
{
this
.
codigoDistrito
=
codigoDistrito
;
public
String
getCodigoDistrito
()
{
}
return
codigoDistrito
;
}
public
String
getNombreDistrito
()
{
return
nombreDistrito
;
public
void
setCodigoDistrito
(
String
codigoDistrito
)
{
}
this
.
codigoDistrito
=
codigoDistrito
;
}
public
void
setNombreDistrito
(
String
nombreDistrito
)
{
this
.
nombreDistrito
=
nombreDistrito
;
public
String
getNombreDistrito
()
{
}
return
nombreDistrito
;
}
public
void
setNombreDistrito
(
String
nombreDistrito
)
{
this
.
nombreDistrito
=
nombreDistrito
;
}
}
}
src/java/demojsoncrud/dao/DAOFactory.java
View file @
94d0c27c
/*
* 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
;
public
static
final
int
MYSQL
=
2
;
public
static
final
int
MYSQL
=
2
;
public
static
DAOFactory
getDAOFactory
(
int
whichFactory
)
{
public
static
DAOFactory
getDAOFactory
(
int
whichFactory
)
{
switch
(
whichFactory
)
{
switch
(
whichFactory
)
{
case
SQL_SERVER:
case
SQL_SERVER:
return
new
SqlserverDAOFactory
();
return
new
SqlserverDAOFactory
();
default
:
default
:
return
null
;
return
null
;
}
}
}
}
public
abstract
PersonaDAO
getPersonaDAO
();
public
abstract
PersonaDAO
getPersonaDAO
();
public
abstract
UbigeoDAO
getUbigeoDAO
();
public
abstract
UbigeoDAO
getUbigeoDAO
();
}
}
src/java/demojsoncrud/dao/PersonaDAO.java
View file @
94d0c27c
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
);
...
...
src/java/demojsoncrud/dao/UbigeoDAO.java
View file @
94d0c27c
/*
* 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
();
public
JSONObject
listarProvincia
(
JSONObject
datos
);
public
JSONObject
listarProvincia
(
JSONObject
datos
);
public
JSONObject
listarDistrito
(
JSONObject
datos
);
public
JSONObject
listarDistrito
(
JSONObject
datos
);
}
}
src/java/demojsoncrud/services/PersonaService.java
View file @
94d0c27c
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
;
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
;
}
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
;
}
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
;
public
JSONObject
cambiarEstado
(
JSONObject
datos
)
{
}
JSONObject
respuesta
=
null
;
try
{
public
JSONObject
cambiarEstado
(
JSONObject
datos
)
{
respuesta
=
service
.
cambiarEstado
(
datos
);
JSONObject
respuesta
=
null
;
}
catch
(
Exception
e
)
{
try
{
}
respuesta
=
dao
.
cambiarEstado
(
datos
);
return
respuesta
;
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"Error PersonaService >> cambiarEstado >> "
+
e
.
getMessage
());
}
}
return
respuesta
;
}
}
}
\ No newline at end of file
src/java/demojsoncrud/services/UbigeoService.java
View file @
94d0c27c
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
;
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
;
}
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
;
}
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
;
}
}
}
src/java/demojsoncrud/servlets/PersonaServlet.java
View file @
94d0c27c
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
;
...
@@ -12,79 +11,79 @@ import org.json.JSONObject;
...
@@ -12,79 +11,79 @@ import org.json.JSONObject;
public
class
PersonaServlet
extends
HttpServlet
{
public
class
PersonaServlet
extends
HttpServlet
{
@Override
@Override
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"
);
switch
(
param
)
{
switch
(
param
)
{
case
"listarPersona"
:
case
"listarPersona"
:
listarPersona
(
request
,
response
);
listarPersona
(
request
,
response
);
break
;
break
;
case
"registrarPersona"
:
case
"registrarPersona"
:
registrarPersona
(
request
,
response
);
registrarPersona
(
request
,
response
);
break
;
break
;
case
"editarPersona"
:
case
"editarPersona"
:
editarPersona
(
request
,
response
);
editarPersona
(
request
,
response
);
break
;
break
;
case
"cambiarEstado"
:
case
"cambiarEstado"
:
cambiarEstado
(
request
,
response
);
cambiarEstado
(
request
,
response
);
break
;
break
;
default
:
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
;
}
}
private
void
listarPersona
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
{
response
.
setContentType
(
"application/json"
);
response
.
setContentType
(
"application/json"
);
PrintWriter
pw
=
response
.
getWriter
();
PrintWriter
pw
=
response
.
getWriter
();
PersonaService
service
=
new
PersonaService
();
JSONObject
respuesta
=
new
JSONObject
();
respuesta
.
put
(
"status"
,
false
)
.
put
(
"message"
,
"No existe la url solicitada."
)
.
put
(
"data"
,
""
);
pw
.
print
(
respuesta
);
break
;
}
}
String
draw
=
request
.
getParameter
(
"draw"
);
private
void
listarPersona
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
{
String
start
=
request
.
getParameter
(
"start
"
);
response
.
setContentType
(
"application/json
"
);
String
length
=
request
.
getParameter
(
"length"
);
PrintWriter
pw
=
response
.
getWriter
(
);
String
search
=
request
.
getParameter
(
"search"
);
PersonaService
service
=
new
PersonaService
(
);
JSONObject
json
=
service
.
listarPersona
(
search
,
draw
,
start
,
length
);
String
search
=
request
.
getParameter
(
"search"
);
pw
.
print
(
json
);
}
private
void
registrarPersona
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
{
JSONObject
datos
=
new
JSONObject
();
response
.
setContentType
(
"application/json"
);
datos
.
put
(
"draw"
,
request
.
getParameter
(
"draw"
));
PrintWriter
pw
=
response
.
getWriter
();
datos
.
put
(
"start"
,
request
.
getParameter
(
"start"
));
PersonaService
service
=
new
PersonaService
();
datos
.
put
(
"length"
,
request
.
getParameter
(
"length"
));
JSONObject
datos
=
new
JSONObject
();
datos
.
put
(
"search"
,
search
);
datos
=
getBodyJsonObject
(
request
);
JSONObject
respuesta
=
service
.
registrarPersona
(
datos
.
getJSONObject
(
"json"
));
pw
.
print
(
respuesta
);
}
JSONObject
json
=
service
.
listarPersona
(
datos
);
pw
.
print
(
json
);
}
private
void
editarPersona
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
{
private
void
registrarPersona
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
{
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
.
editarPersona
(
datos
.
getJSONObject
(
"json"
));
pw
.
print
(
respuesta
);
pw
.
print
(
respuesta
);
}
private
void
cambiarEstado
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
{
}
response
.
setContentType
(
"application/json"
);
PrintWriter
pw
=
response
.
getWriter
();
private
void
editarPersona
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
{
PersonaService
service
=
new
PersonaService
();
response
.
setContentType
(
"application/json"
);
JSONObject
datos
=
new
JSONObject
();
PrintWriter
pw
=
response
.
getWriter
();
datos
=
getBodyJsonObject
(
request
);
PersonaService
service
=
new
PersonaService
();
JSONObject
respuesta
=
service
.
cambiarEstado
(
datos
.
getJSONObject
(
"json"
));
JSONObject
datos
=
new
JSONObject
(
request
.
getParameter
(
"json"
));
System
.
out
.
println
(
respuesta
);
JSONObject
respuesta
=
service
.
editarPersona
(
datos
);
pw
.
print
(
respuesta
);
pw
.
print
(
respuesta
);
}
}
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
(
request
.
getParameter
(
"json"
));
JSONObject
respuesta
=
service
.
cambiarEstado
(
datos
);
System
.
out
.
println
(
respuesta
);
pw
.
print
(
respuesta
);
}
}
}
src/java/demojsoncrud/servlets/UbigeoServlet.java
View file @
94d0c27c
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
;
...
@@ -12,45 +11,59 @@ import org.json.JSONObject;
...
@@ -12,45 +11,59 @@ import org.json.JSONObject;
public
class
UbigeoServlet
extends
HttpServlet
{
public
class
UbigeoServlet
extends
HttpServlet
{
@Override
@Override
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
)
{
listarDepartamento
(
request
,
response
);
switch
(
param
)
{
}
else
if
(
"listarProvincia"
.
equals
(
param
))
{
case
"listarDepartamento"
:
listarProvincia
(
request
,
response
);
listarDepartamento
(
request
,
response
);
}
else
if
(
"listarDistrito"
.
equals
(
param
))
{
break
;
listarDistrito
(
request
,
response
);
case
"listarProvincia"
:
}
listarProvincia
(
request
,
response
);
break
;
case
"listarDistrito"
:
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
;
}
}
}
}
private
void
listarDepartamento
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
{
private
void
listarDepartamento
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
{
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
json
=
service
.
listarDepartamento
();
JSONObject
json
=
service
.
listarDepartamento
();
pw
.
print
(
json
);
pw
.
print
(
json
);
}
}
private
void
listarProvincia
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
{
private
void
listarProvincia
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
{
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
);
}
}
private
void
listarDistrito
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
{
private
void
listarDistrito
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
{
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
);
}
}
}
}
src/java/demojsoncrud/sqlserverdao/PersonaSqlserverDAO.java
View file @
94d0c27c
...
@@ -8,275 +8,284 @@ import java.sql.PreparedStatement;
...
@@ -8,275 +8,284 @@ import java.sql.PreparedStatement;
import
java.sql.ResultSet
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.SQLException
;
import
org.json.JSONArray
;
import
org.json.JSONArray
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
import
org.json.JSONObject
;
public
class
PersonaSqlserverDAO
implements
PersonaDAO
{
public
class
PersonaSqlserverDAO
implements
PersonaDAO
{
@Override
@Override
public
JSONObject
listarPersona
(
String
search
,
String
draw
,
String
start
,
String
length
)
{
public
JSONObject
listarPersona
(
JSONObject
datos
)
{
JSONObject
jsonReturn
=
new
JSONObject
();
JSONObject
jsonReturn
=
new
JSONObject
();
JSONArray
data
=
new
JSONArray
();
JSONArray
data
=
new
JSONArray
();
PreparedStatement
psListarPersona
=
null
;
PreparedStatement
psListarPersona
=
null
,
psCantidadPersonas
=
null
;
ResultSet
rsListarPersona
=
null
;
ResultSet
rsListarPersona
=
null
,
rsCantidadPersonas
=
null
;
Connection
connection
=
null
;
Connection
connection
=
null
;
String
base
=
"demojsoncrud"
;
String
base
=
"demojsoncrud"
;
String
condicion
=
""
;
String
condicion
=
""
;
String
condiciones
=
" (a.nombres like '%"
+
search
+
"%' or a.apellidos like '%"
+
search
+
"%') "
;
int
numeroFilas
=
Integer
.
parseInt
(
start
)
+
1
;
//Contador para el numero de filas a listar
int
numeroFilas
=
Integer
.
parseInt
(
datos
.
getString
(
"start"
)
)
+
1
;
//Contador para el numero de filas a listar
int
cantidadPersonas
=
0
;
int
cantidadPersonas
=
1
;
if
(!
search
.
equals
(
""
))
{
if
(!
datos
.
getString
(
"search"
).
equals
(
""
))
{
condicion
=
" and "
+
condiciones
;
condicion
=
" and (a.nombres like '%"
+
datos
.
getString
(
"search"
)
+
"%' or a.apellidos like '%"
+
datos
.
getString
(
"search"
)
+
"%') "
;
}
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"
;
psListarPersona
=
connection
.
prepareStatement
(
sql
);
rsListarPersona
=
psListarPersona
.
executeQuery
();
while
(
rsListarPersona
.
next
())
{
PersonaBean
personaBean
=
new
PersonaBean
();
int
c
=
1
;
//Contador para las columnas
personaBean
.
setCodigo
(
rsListarPersona
.
getString
(
c
++));
personaBean
.
setDni
(
rsListarPersona
.
getString
(
c
++));
personaBean
.
setApellidos
(
rsListarPersona
.
getString
(
c
++));
personaBean
.
setNombres
(
rsListarPersona
.
getString
(
c
++));
personaBean
.
setEdad
(
rsListarPersona
.
getString
(
c
++));
personaBean
.
setCorreo
(
rsListarPersona
.
getString
(
c
++));
personaBean
.
setEstado
(
rsListarPersona
.
getString
(
c
++));
JSONObject
obj
=
new
JSONObject
(
personaBean
);
data
.
put
(
obj
);
}
if
(!
search
.
equals
(
""
))
{
condicion
=
" where "
+
condiciones
;
}
String
sqlCantidadPersonas
=
"select count(1) as cantidadPersonas from persona a "
+
condicion
;
PreparedStatement
psCantidadPersonas
=
connection
.
prepareStatement
(
sqlCantidadPersonas
);
ResultSet
rsCantidadPersonas
=
psCantidadPersonas
.
executeQuery
();
rsCantidadPersonas
.
next
();
cantidadPersonas
=
rsCantidadPersonas
.
getInt
(
"cantidadPersonas"
);
jsonReturn
.
put
(
"data"
,
data
);
jsonReturn
.
put
(
"recordsFiltered"
,
cantidadPersonas
);
jsonReturn
.
put
(
"recordsTotal"
,
cantidadPersonas
);
jsonReturn
.
put
(
"draw"
,
draw
);
}
catch
(
SQLException
|
JSONException
e
)
{
jsonReturn
.
put
(
"Error"
,
"Error -> "
+
e
.
getMessage
());
}
finally
{
try
{
if
(
rsListarPersona
!=
null
)
{
rsListarPersona
.
close
();
}
if
(
psListarPersona
!=
null
)
{
psListarPersona
.
close
();
}
if
(
connection
!=
null
)
{
connection
.
close
();
}
}
catch
(
SQLException
e
)
{
jsonReturn
.
put
(
"Error"
,
"Error inesperado -> "
+
e
.
getMessage
()
+
" ["
+
e
.
getErrorCode
()
+
"]"
);
}
}
return
jsonReturn
;
}
}
@Override
try
{
public
JSONObject
registrarPersona
(
JSONObject
datos
)
{
connection
=
SqlserverDAOFactory
.
obtenerConexion
(
base
);
System
.
out
.
println
(
datos
);
String
sql
JSONObject
jsonReturn
=
new
JSONObject
();
=
"select top "
+
datos
.
getString
(
"length"
)
+
" "
int
resultDni
=
0
;
+
"a.codigo, "
int
resultPersona
=
0
;
+
"a.dni, "
String
base
=
"demojsoncrud"
;
+
"a.apellidos, "
PreparedStatement
psInsertarPersona
=
null
;
+
"a.nombres, "
Connection
connection
=
null
;
+
"a.edad, "
ResponseHelper
response
=
new
ResponseHelper
();
+
"a.correo, "
try
{
+
"a.estado "
connection
=
SqlserverDAOFactory
.
obtenerConexion
(
base
);
+
"from persona a "
+
"where a.codigo not in (select top "
+
datos
.
getString
(
"start"
)
+
" b.codigo from persona b order by 1 desc) "
String
sqlGetDni
=
"select count(1) dni from persona where dni = ?"
;
+
condicion
+
" "
PreparedStatement
psGetDni
=
connection
.
prepareStatement
(
sqlGetDni
);
+
"order by 1 desc"
;
psGetDni
.
setString
(
1
,
datos
.
getString
(
"dni"
));
System
.
out
.
println
(
sql
);
ResultSet
rsGetDni
=
psGetDni
.
executeQuery
();
psListarPersona
=
connection
.
prepareStatement
(
sql
);
rsGetDni
.
next
();
rsListarPersona
=
psListarPersona
.
executeQuery
();
resultDni
=
rsGetDni
.
getInt
(
"dni"
);
while
(
rsListarPersona
.
next
())
{
if
(
resultDni
==
0
)
{
PersonaBean
personaBean
=
new
PersonaBean
();
String
sql
int
c
=
1
;
//Contador para las columnas
=
"insert into persona (dni, apellidos, nombres, edad, correo, estado) values "
personaBean
.
setCodigo
(
rsListarPersona
.
getString
(
c
++));
+
"(?,RTRIM(UPPER(?)),RTRIM(UPPER(?)),?,RTRIM(UPPER(?)),1)"
;
personaBean
.
setDni
(
rsListarPersona
.
getString
(
c
++));
int
c
=
1
;
//contador para las columnas
personaBean
.
setApellidos
(
rsListarPersona
.
getString
(
c
++));
psInsertarPersona
=
connection
.
prepareStatement
(
sql
);
personaBean
.
setNombres
(
rsListarPersona
.
getString
(
c
++));
psInsertarPersona
.
setString
(
c
++,
datos
.
getString
(
"dni"
));
personaBean
.
setEdad
(
rsListarPersona
.
getString
(
c
++));
psInsertarPersona
.
setString
(
c
++,
datos
.
getString
(
"apellidos"
));
personaBean
.
setCorreo
(
rsListarPersona
.
getString
(
c
++));
psInsertarPersona
.
setString
(
c
++,
datos
.
getString
(
"nombres"
));
personaBean
.
setEstado
(
rsListarPersona
.
getString
(
c
++));
psInsertarPersona
.
setString
(
c
++,
datos
.
getString
(
"edad"
));
JSONObject
obj
=
new
JSONObject
(
personaBean
);
psInsertarPersona
.
setString
(
c
++,
datos
.
getString
(
"correo"
));
obj
.
put
(
"item"
,
numeroFilas
++);
resultPersona
=
psInsertarPersona
.
executeUpdate
();
data
.
put
(
obj
);
if
(
resultPersona
==
1
)
{
}
response
.
setMessage
(
"Registro correcto!"
);
response
.
setStatus
(
true
);
String
sqlCantidadPersonas
=
"select count(1) as cantidadPersonas from persona a where a.estado = 1 "
+
condicion
;
}
else
{
System
.
out
.
println
(
sqlCantidadPersonas
);
response
.
setMessage
(
"Error al registrar"
);
psCantidadPersonas
=
connection
.
prepareStatement
(
sqlCantidadPersonas
);
response
.
setStatus
(
false
);
// ERROR
rsCantidadPersonas
=
psCantidadPersonas
.
executeQuery
();
}
rsCantidadPersonas
.
next
();
cantidadPersonas
=
rsCantidadPersonas
.
getInt
(
"cantidadPersonas"
);
}
else
{
response
.
setMessage
(
"El documento ya existe en mi base de datos. Prueba otro."
);
jsonReturn
.
put
(
"data"
,
data
);
response
.
setStatus
(
false
);
// DNI REPETIDO
jsonReturn
.
put
(
"recordsFiltered"
,
cantidadPersonas
);
}
jsonReturn
.
put
(
"recordsTotal"
,
cantidadPersonas
);
jsonReturn
.
put
(
"draw"
,
datos
.
getString
(
"draw"
));
}
catch
(
SQLException
e
)
{
response
.
setStatus
(
false
);
}
catch
(
SQLException
e
)
{
response
.
setMessage
(
"Error al registrar -> "
+
e
.
getMessage
()
+
"["
+
e
.
getErrorCode
()
+
"]"
);
jsonReturn
.
put
(
"Error"
,
"Error -> "
+
e
.
getMessage
()
+
" Error code ["
+
e
.
getErrorCode
()
+
"]"
);
}
finally
{
System
.
err
.
println
(
"Error al registrar -> "
+
e
.
getMessage
()
+
"["
+
e
.
getErrorCode
()
+
"]"
);
try
{
}
finally
{
if
(
connection
!=
null
)
{
try
{
connection
.
close
();
if
(
rsCantidadPersonas
!=
null
)
{
}
rsCantidadPersonas
.
close
();
if
(
psInsertarPersona
!=
null
)
{
psInsertarPersona
.
close
();
}
}
catch
(
SQLException
e
)
{
response
.
setStatus
(
false
);
response
.
setMessage
(
"Error al registrar -> "
+
e
.
getMessage
()
+
"["
+
e
.
getErrorCode
()
+
"]"
);
}
}
}
if
(
rsListarPersona
!=
null
)
{
jsonReturn
=
new
JSONObject
(
response
);
rsListarPersona
.
close
();
System
.
out
.
println
(
jsonReturn
);
}
return
jsonReturn
;
if
(
psCantidadPersonas
!=
null
)
{
psCantidadPersonas
.
close
();
}
if
(
psListarPersona
!=
null
)
{
psListarPersona
.
close
();
}
if
(
connection
!=
null
)
{
connection
.
close
();
}
}
catch
(
SQLException
e
)
{
System
.
err
.
println
(
"Error: ha ocurrido un error al intentar cerrar las conexiones y/o liberacion de recursos -> "
+
e
.
getMessage
());
}
}
}
@Override
return
jsonReturn
;
public
JSONObject
editarPersona
(
JSONObject
datos
)
{
}
JSONObject
jsonReturn
=
new
JSONObject
();
String
base
=
"demojsoncrud"
;
@Override
Connection
connection
=
null
;
public
JSONObject
registrarPersona
(
JSONObject
datos
)
{
PreparedStatement
psEditarPersona
=
null
;
JSONObject
jsonReturn
=
null
;
int
resultPersona
=
0
;
int
resultDni
=
0
;
ResponseHelper
response
=
new
ResponseHelper
();
int
resultPersona
=
0
;
String
base
=
"demojsoncrud"
;
try
{
PreparedStatement
psInsertarPersona
=
null
,
psGetDni
=
null
;
connection
=
SqlserverDAOFactory
.
obtenerConexion
(
base
);
ResultSet
rsGetDni
=
null
;
String
sql
Connection
connection
=
null
;
=
"update persona "
ResponseHelper
response
=
new
ResponseHelper
();
+
"set apellidos = RTRIM(UPPER(?)), "
try
{
+
"nombres = RTRIM(UPPER(?)), "
connection
=
SqlserverDAOFactory
.
obtenerConexion
(
base
);
+
"edad = ?, "
+
"correo = RTRIM(UPPER(?)) "
String
sqlGetDni
=
"select count(1) dni from persona where dni = ?"
;
+
"where codigo = ?"
;
psGetDni
=
connection
.
prepareStatement
(
sqlGetDni
);
psGetDni
.
setString
(
1
,
datos
.
getString
(
"dni"
));
psEditarPersona
=
connection
.
prepareStatement
(
sql
);
rsGetDni
=
psGetDni
.
executeQuery
();
int
q
=
1
;
// CONTADOR PARA LAS COLUMNAS
rsGetDni
.
next
();
psEditarPersona
.
setString
(
q
++,
datos
.
getString
(
"apellidos"
));
resultDni
=
rsGetDni
.
getInt
(
"dni"
);
psEditarPersona
.
setString
(
q
++,
datos
.
getString
(
"nombres"
));
psEditarPersona
.
setString
(
q
++,
datos
.
getString
(
"edad"
));
if
(
resultDni
==
0
)
{
psEditarPersona
.
setString
(
q
++,
datos
.
getString
(
"correo"
));
String
sql
psEditarPersona
.
setString
(
q
++,
datos
.
getString
(
"codigo"
));
=
"insert into persona (dni, apellidos, nombres, edad, correo, estado) values "
resultPersona
=
psEditarPersona
.
executeUpdate
();
+
"(?,RTRIM(UPPER(?)),RTRIM(UPPER(?)),?,RTRIM(UPPER(?)),1)"
;
int
c
=
1
;
//contador para las columnas
if
(
resultPersona
==
1
)
{
psInsertarPersona
=
connection
.
prepareStatement
(
sql
);
response
.
setMessage
(
"Se actualizo correctamente"
);
psInsertarPersona
.
setString
(
c
++,
datos
.
getString
(
"dni"
));
response
.
setStatus
(
true
);
// ACTUALIZO
psInsertarPersona
.
setString
(
c
++,
datos
.
getString
(
"apellidos"
));
}
else
{
psInsertarPersona
.
setString
(
c
++,
datos
.
getString
(
"nombres"
));
response
.
setMessage
(
"Error al actualizar"
);
psInsertarPersona
.
setString
(
c
++,
datos
.
getString
(
"edad"
));
response
.
setStatus
(
false
);
// ERROR
psInsertarPersona
.
setString
(
c
++,
datos
.
getString
(
"correo"
));
}
resultPersona
=
psInsertarPersona
.
executeUpdate
();
if
(
resultPersona
==
1
)
{
}
catch
(
SQLException
e
)
{
response
.
setMessage
(
"Registro correcto!"
);
response
.
setStatus
(
false
);
response
.
setStatus
(
true
);
response
.
setMessage
(
"Error al actualizar -> "
+
e
.
getMessage
()
+
"["
+
e
.
getErrorCode
()
+
"]"
);
}
else
{
}
finally
{
response
.
setMessage
(
"No se pudo registrar a la persona"
);
try
{
response
.
setStatus
(
false
);
// ERROR
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
);
}
else
{
response
.
setMessage
(
"El documento ya existe en mi base de datos. Prueba otro."
);
response
.
setStatus
(
false
);
// DNI REPETIDO
}
}
catch
(
SQLException
e
)
{
response
.
setStatus
(
false
);
response
.
setMessage
(
"Error al registrar -> "
+
e
.
getMessage
()
+
"["
+
e
.
getErrorCode
()
+
"]"
);
System
.
err
.
println
(
"Error al registrar -> "
+
e
.
getMessage
()
+
"["
+
e
.
getErrorCode
()
+
"]"
);
}
finally
{
try
{
if
(
rsGetDni
!=
null
)
{
rsGetDni
.
close
();
}
if
(
psGetDni
!=
null
)
{
psGetDni
.
close
();
}
if
(
psInsertarPersona
!=
null
)
{
psInsertarPersona
.
close
();
}
if
(
connection
!=
null
)
{
connection
.
close
();
}
return
jsonReturn
;
}
catch
(
SQLException
e
)
{
System
.
err
.
println
(
"Error: ha ocurrido un error al intentar cerrar las conexiones y/o liberacion de recursos -> "
+
e
.
getMessage
());
}
}
}
@Override
jsonReturn
=
new
JSONObject
(
response
);
public
JSONObject
cambiarEstado
(
JSONObject
datos
)
{
return
jsonReturn
;
System
.
out
.
println
(
datos
);
}
JSONObject
jsonReturn
=
new
JSONObject
();
String
base
=
"demojsoncrud"
;
@Override
Connection
connection
=
null
;
public
JSONObject
editarPersona
(
JSONObject
datos
)
{
PreparedStatement
psEditarPersona
=
null
;
JSONObject
jsonReturn
=
null
;
int
resultPersona
=
0
;
String
base
=
"demojsoncrud"
;
ResponseHelper
response
=
new
ResponseHelper
();
Connection
connection
=
null
;
PreparedStatement
psEditarPersona
=
null
;
try
{
int
resultPersona
=
0
;
connection
=
SqlserverDAOFactory
.
obtenerConexion
(
base
);
ResponseHelper
response
=
new
ResponseHelper
();
String
sql
=
"UPDATE persona "
try
{
+
" SET estado = ? "
connection
=
SqlserverDAOFactory
.
obtenerConexion
(
base
);
+
"WHERE codigo = ?;"
;
String
sql
=
"update persona "
psEditarPersona
=
connection
.
prepareStatement
(
sql
);
+
"set apellidos = RTRIM(UPPER(?)), "
psEditarPersona
.
setString
(
1
,
datos
.
getString
(
"estado"
).
equals
(
"1"
)
?
"0"
:
"1"
);
+
"nombres = RTRIM(UPPER(?)), "
psEditarPersona
.
setString
(
2
,
datos
.
getString
(
"codigo"
));
+
"edad = ?, "
resultPersona
=
psEditarPersona
.
executeUpdate
();
+
"correo = RTRIM(UPPER(?)) "
+
"where codigo = ?"
;
if
(
resultPersona
==
1
)
{
response
.
setMessage
(
"Se actualizo correctamente"
);
psEditarPersona
=
connection
.
prepareStatement
(
sql
);
response
.
setStatus
(
true
);
// ACTUALIZO
int
q
=
1
;
// CONTADOR PARA LAS COLUMNAS
}
else
{
psEditarPersona
.
setString
(
q
++,
datos
.
getString
(
"apellidos"
));
response
.
setMessage
(
"Error al actualizar"
);
psEditarPersona
.
setString
(
q
++,
datos
.
getString
(
"nombres"
));
response
.
setStatus
(
false
);
// ERROR
psEditarPersona
.
setString
(
q
++,
datos
.
getString
(
"edad"
));
}
psEditarPersona
.
setString
(
q
++,
datos
.
getString
(
"correo"
));
psEditarPersona
.
setString
(
q
++,
datos
.
getString
(
"codigo"
));
}
catch
(
SQLException
e
)
{
resultPersona
=
psEditarPersona
.
executeUpdate
();
response
.
setStatus
(
false
);
response
.
setMessage
(
"Error al actualizar -> "
+
e
.
getMessage
()
+
"["
+
e
.
getErrorCode
()
+
"]"
);
if
(
resultPersona
==
1
)
{
}
finally
{
response
.
setMessage
(
"Se actualizo correctamente"
);
try
{
response
.
setStatus
(
true
);
// ACTUALIZO
if
(
connection
!=
null
)
{
}
else
{
connection
.
close
();
response
.
setMessage
(
"Error al actualizar"
);
}
response
.
setStatus
(
false
);
// ERROR
if
(
psEditarPersona
!=
null
)
{
}
psEditarPersona
.
close
();
}
}
catch
(
SQLException
e
)
{
}
catch
(
SQLException
e
)
{
response
.
setStatus
(
false
);
response
.
setStatus
(
false
);
response
.
setMessage
(
"Error al actualizar -> "
+
e
.
getMessage
()
+
"["
+
e
.
getErrorCode
()
+
"]"
);
response
.
setMessage
(
"Error al actualizar -> "
+
e
.
getMessage
()
+
"["
+
e
.
getErrorCode
()
+
"]"
);
System
.
err
.
println
(
"Error al registrar -> "
+
e
.
getMessage
()
+
"["
+
e
.
getErrorCode
()
+
"]"
);
}
}
finally
{
try
{
if
(
connection
!=
null
)
{
connection
.
close
();
}
if
(
psEditarPersona
!=
null
)
{
psEditarPersona
.
close
();
}
}
}
catch
(
SQLException
e
)
{
System
.
err
.
println
(
"Error: ha ocurrido un error al intentar cerrar las conexiones y/o liberacion de recursos -> "
+
e
.
getMessage
());
}
}
jsonReturn
=
new
JSONObject
(
response
);
jsonReturn
=
new
JSONObject
(
response
);
return
jsonReturn
;
return
jsonReturn
;
}
@Override
public
JSONObject
cambiarEstado
(
JSONObject
datos
)
{
System
.
out
.
println
(
datos
);
JSONObject
jsonReturn
=
null
;
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
()
+
"]"
);
System
.
err
.
println
(
"Error al registrar -> "
+
e
.
getMessage
()
+
"["
+
e
.
getErrorCode
()
+
"]"
);
}
finally
{
try
{
if
(
psEditarPersona
!=
null
)
{
psEditarPersona
.
close
();
}
if
(
connection
!=
null
)
{
connection
.
close
();
}
}
catch
(
SQLException
e
)
{
System
.
err
.
println
(
"Error: ha ocurrido un error al intentar cerrar las conexiones y/o liberacion de recursos -> "
+
e
.
getMessage
());
}
}
}
jsonReturn
=
new
JSONObject
(
response
);
return
jsonReturn
;
}
}
}
src/java/demojsoncrud/sqlserverdao/UbigeoSqlserverDAO.java
View file @
94d0c27c
/*
* 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,201 +10,193 @@ import java.sql.SQLException;
...
@@ -15,201 +10,193 @@ 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
;
Connection
connection
=
null
;
Connection
connection
=
null
;
String
base
=
"ubigeo"
;
String
base
=
"ubigeo"
;
ResponseHelper
response
=
new
ResponseHelper
();
ResponseHelper
response
=
new
ResponseHelper
();
try
{
try
{
connection
=
SqlserverDAOFactory
.
obtenerConexion
(
base
);
connection
=
SqlserverDAOFactory
.
obtenerConexion
(
base
);
String
sql
String
sql
=
"select "
=
"select "
+
"codDepartamento as codigoDepartamento, "
+
"codDepartamento as codigoDepartamento, "
+
"nombreDepartamento as nombreDepartamento "
+
"nombreDepartamento as nombreDepartamento "
+
"from ubigeo "
+
"from ubigeo "
+
"group by codDepartamento, nombreDepartamento "
+
"group by codDepartamento, nombreDepartamento "
+
"order by nombreDepartamento"
;
+
"order by nombreDepartamento"
;
psListarDepartamento
=
connection
.
prepareStatement
(
sql
);
psListarDepartamento
=
connection
.
prepareStatement
(
sql
);
rsListarDepartamento
=
psListarDepartamento
.
executeQuery
();
rsListarDepartamento
=
psListarDepartamento
.
executeQuery
();
while
(
rsListarDepartamento
.
next
())
{
while
(
rsListarDepartamento
.
next
())
{
UbigeoBean
ubigeoBean
=
new
UbigeoBean
();
UbigeoBean
ubigeoBean
=
new
UbigeoBean
();
ubigeoBean
.
setCodigoDepartamento
(
rsListarDepartamento
.
getString
(
"codigoDepartamento"
));
ubigeoBean
.
setCodigoDepartamento
(
rsListarDepartamento
.
getString
(
"codigoDepartamento"
));
ubigeoBean
.
setNombreDepartamento
(
rsListarDepartamento
.
getString
(
"nombreDepartamento"
));
ubigeoBean
.
setNombreDepartamento
(
rsListarDepartamento
.
getString
(
"nombreDepartamento"
));
JSONObject
obj
=
new
JSONObject
(
ubigeoBean
);
JSONObject
obj
=
new
JSONObject
(
ubigeoBean
);
data
.
put
(
obj
);
data
.
put
(
obj
);
}
}
JSONObject
jsonData
=
new
JSONObject
();
JSONObject
jsonData
=
new
JSONObject
();
jsonData
.
put
(
"departamentos"
,
data
);
jsonData
.
put
(
"departamentos"
,
data
);
response
.
setData
(
jsonData
);
response
.
setData
(
jsonData
);
response
.
setMessage
(
"Listado correctamente!"
);
response
.
setMessage
(
"Listado correctamente!"
);
response
.
setStatus
(
true
);
response
.
setStatus
(
true
);
}
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
);
}
finally
{
System
.
err
.
println
(
"Error al registrar -> "
+
e
.
getMessage
()
+
"["
+
e
.
getErrorCode
()
+
"]"
);
try
{
}
finally
{
if
(
rsListarDepartamento
!=
null
)
{
try
{
rsListarDepartamento
.
close
();
if
(
rsListarDepartamento
!=
null
)
{
}
rsListarDepartamento
.
close
();
if
(
psListarDepartamento
!=
null
)
{
psListarDepartamento
.
close
();
}
if
(
connection
!=
null
)
{
connection
.
close
();
}
}
catch
(
SQLException
e
)
{
response
.
setMessage
(
"Error al listarDepartamento -> "
+
e
.
getMessage
()
+
" Error code ["
+
e
.
getErrorCode
()
+
"]."
);
response
.
setStatus
(
false
);
}
}
}
if
(
psListarDepartamento
!=
null
)
{
psListarDepartamento
.
close
();
}
if
(
connection
!=
null
)
{
connection
.
close
();
}
}
catch
(
SQLException
e
)
{
System
.
err
.
println
(
"Error: ha ocurrido un error al intentar cerrar las conexiones y/o liberacion de recursos -> "
+
e
.
getMessage
());
}
jsonReturn
=
new
JSONObject
(
response
);
return
jsonReturn
;
}
}
@Override
jsonReturn
=
new
JSONObject
(
response
);
public
JSONObject
listarProvincia
(
JSONObject
datos
)
{
return
jsonReturn
;
System
.
out
.
println
(
datos
);
}
JSONObject
jsonReturn
=
new
JSONObject
();
JSONArray
data
=
new
JSONArray
();
@Override
PreparedStatement
psListarProvincia
=
null
;
public
JSONObject
listarProvincia
(
JSONObject
datos
)
{
ResultSet
rsListarProvincia
=
null
;
System
.
out
.
println
(
datos
);
Connection
connection
=
null
;
JSONObject
jsonReturn
=
null
;
String
base
=
"ubigeo"
;
JSONArray
data
=
new
JSONArray
();
ResponseHelper
response
=
new
ResponseHelper
();
PreparedStatement
psListarProvincia
=
null
;
ResultSet
rsListarProvincia
=
null
;
try
{
Connection
connection
=
null
;
connection
=
SqlserverDAOFactory
.
obtenerConexion
(
base
);
String
base
=
"ubigeo"
;
String
sql
ResponseHelper
response
=
new
ResponseHelper
();
=
"select "
+
"codProvincia as codigoProvincia, "
try
{
+
"nombreProvincia as nombreProvincia "
connection
=
SqlserverDAOFactory
.
obtenerConexion
(
base
);
+
"from ubigeo "
String
sql
+
"where codDepartamento = ? "
=
"select "
+
"group by codProvincia, nombreProvincia "
+
"codProvincia as codigoProvincia, "
+
"order by nombreProvincia"
;
+
"nombreProvincia as nombreProvincia "
psListarProvincia
=
connection
.
prepareStatement
(
sql
);
+
"from ubigeo "
psListarProvincia
.
setInt
(
1
,
datos
.
getInt
(
"codigoDepartamento"
));
+
"where codDepartamento = ? "
rsListarProvincia
=
psListarProvincia
.
executeQuery
();
+
"group by codProvincia, nombreProvincia "
while
(
rsListarProvincia
.
next
())
{
+
"order by nombreProvincia"
;
UbigeoBean
ubigeoBean
=
new
UbigeoBean
();
psListarProvincia
=
connection
.
prepareStatement
(
sql
);
ubigeoBean
.
setCodigoProvincia
(
rsListarProvincia
.
getString
(
"codigoProvincia"
));
psListarProvincia
.
setInt
(
1
,
datos
.
getInt
(
"codigoDepartamento"
));
ubigeoBean
.
setNombreProvincia
(
rsListarProvincia
.
getString
(
"nombreProvincia"
));
rsListarProvincia
=
psListarProvincia
.
executeQuery
();
JSONObject
obj
=
new
JSONObject
(
ubigeoBean
);
while
(
rsListarProvincia
.
next
())
{
data
.
put
(
obj
);
UbigeoBean
ubigeoBean
=
new
UbigeoBean
();
}
ubigeoBean
.
setCodigoProvincia
(
rsListarProvincia
.
getString
(
"codigoProvincia"
));
ubigeoBean
.
setNombreProvincia
(
rsListarProvincia
.
getString
(
"nombreProvincia"
));
JSONObject
jsonData
=
new
JSONObject
();
JSONObject
obj
=
new
JSONObject
(
ubigeoBean
);
jsonData
.
put
(
"provincias"
,
data
);
data
.
put
(
obj
);
}
response
.
setData
(
jsonData
);
response
.
setMessage
(
"Listado correctamente!"
);
JSONObject
jsonData
=
new
JSONObject
();
response
.
setStatus
(
true
);
jsonData
.
put
(
"provincias"
,
data
);
}
catch
(
SQLException
e
)
{
response
.
setData
(
jsonData
);
response
.
setMessage
(
"Error al listarProvincia -> "
+
e
.
getMessage
()
+
" Error code ["
+
e
.
getErrorCode
()
+
"]."
);
response
.
setMessage
(
"Listado correctamente!"
);
response
.
setStatus
(
false
);
response
.
setStatus
(
true
);
}
finally
{
try
{
}
catch
(
SQLException
e
)
{
if
(
rsListarProvincia
!=
null
)
{
response
.
setMessage
(
"Error al listarProvincia -> "
+
e
.
getMessage
()
+
" Error code ["
+
e
.
getErrorCode
()
+
"]."
);
rsListarProvincia
.
close
();
response
.
setStatus
(
false
);
}
System
.
err
.
println
(
"Error al registrar -> "
+
e
.
getMessage
()
+
"["
+
e
.
getErrorCode
()
+
"]"
);
if
(
psListarProvincia
!=
null
)
{
}
finally
{
psListarProvincia
.
close
();
try
{
}
if
(
rsListarProvincia
!=
null
)
{
if
(
connection
!=
null
)
{
rsListarProvincia
.
close
();
connection
.
close
();
}
}
catch
(
SQLException
e
)
{
response
.
setMessage
(
"Error al listarProvincia -> "
+
e
.
getMessage
()
+
" Error code ["
+
e
.
getErrorCode
()
+
"]."
);
response
.
setStatus
(
false
);
}
}
}
if
(
psListarProvincia
!=
null
)
{
psListarProvincia
.
close
();
}
if
(
connection
!=
null
)
{
connection
.
close
();
}
}
catch
(
SQLException
e
)
{
System
.
err
.
println
(
"Error: ha ocurrido un error al intentar cerrar las conexiones y/o liberacion de recursos -> "
+
e
.
getMessage
());
}
jsonReturn
=
new
JSONObject
(
response
);
return
jsonReturn
;
}
}
@Override
jsonReturn
=
new
JSONObject
(
response
);
public
JSONObject
listarDistrito
(
JSONObject
datos
)
{
return
jsonReturn
;
JSONObject
jsonReturn
=
new
JSONObject
();
}
JSONArray
data
=
new
JSONArray
();
PreparedStatement
psListarDistrito
=
null
;
@Override
ResultSet
rsListarDistrito
=
null
;
public
JSONObject
listarDistrito
(
JSONObject
datos
)
{
Connection
connection
=
null
;
JSONObject
jsonReturn
=
null
;
String
base
=
"ubigeo"
;
JSONArray
data
=
new
JSONArray
();
ResponseHelper
response
=
new
ResponseHelper
();
PreparedStatement
psListarDistrito
=
null
;
ResultSet
rsListarDistrito
=
null
;
try
{
Connection
connection
=
null
;
connection
=
SqlserverDAOFactory
.
obtenerConexion
(
base
);
String
base
=
"ubigeo"
;
String
sql
ResponseHelper
response
=
new
ResponseHelper
();
=
"select "
+
"codDistrito as codigoDistrito, "
try
{
+
"nombreDistrito as nombreDistrito "
connection
=
SqlserverDAOFactory
.
obtenerConexion
(
base
);
+
"from ubigeo "
String
sql
+
"where codProvincia = ? and codDepartamento = ? "
=
"select "
+
"order by nombreDistrito"
;
+
"codDistrito as codigoDistrito, "
psListarDistrito
=
connection
.
prepareStatement
(
sql
);
+
"nombreDistrito as nombreDistrito "
psListarDistrito
.
setInt
(
1
,
datos
.
getInt
(
"codigoProvincia"
));
+
"from ubigeo "
psListarDistrito
.
setInt
(
2
,
datos
.
getInt
(
"codigoDepartamento"
));
+
"where codProvincia = ? and codDepartamento = ? "
rsListarDistrito
=
psListarDistrito
.
executeQuery
();
+
"order by nombreDistrito"
;
while
(
rsListarDistrito
.
next
())
{
psListarDistrito
=
connection
.
prepareStatement
(
sql
);
UbigeoBean
ubigeoBean
=
new
UbigeoBean
();
psListarDistrito
.
setInt
(
1
,
datos
.
getInt
(
"codigoProvincia"
));
ubigeoBean
.
setCodigoDistrito
(
rsListarDistrito
.
getString
(
"codigoDistrito"
));
psListarDistrito
.
setInt
(
2
,
datos
.
getInt
(
"codigoDepartamento"
));
ubigeoBean
.
setNombreDistrito
(
rsListarDistrito
.
getString
(
"nombreDistrito"
));
rsListarDistrito
=
psListarDistrito
.
executeQuery
();
JSONObject
obj
=
new
JSONObject
(
ubigeoBean
);
while
(
rsListarDistrito
.
next
())
{
data
.
put
(
obj
);
UbigeoBean
ubigeoBean
=
new
UbigeoBean
();
}
ubigeoBean
.
setCodigoDistrito
(
rsListarDistrito
.
getString
(
"codigoDistrito"
));
ubigeoBean
.
setNombreDistrito
(
rsListarDistrito
.
getString
(
"nombreDistrito"
));
JSONObject
jsonData
=
new
JSONObject
();
JSONObject
obj
=
new
JSONObject
(
ubigeoBean
);
jsonData
.
put
(
"distritos"
,
data
);
data
.
put
(
obj
);
}
response
.
setData
(
jsonData
);
response
.
setMessage
(
"Listado correctamente!"
);
JSONObject
jsonData
=
new
JSONObject
();
response
.
setStatus
(
true
);
jsonData
.
put
(
"distritos"
,
data
);
}
catch
(
SQLException
e
)
{
response
.
setData
(
jsonData
);
response
.
setMessage
(
"Error al listarDistrito -> "
+
e
.
getMessage
()
+
" Error code ["
+
e
.
getErrorCode
()
+
"]."
);
response
.
setMessage
(
"Listado correctamente!"
);
response
.
setStatus
(
false
);
response
.
setStatus
(
true
);
}
finally
{
try
{
}
catch
(
SQLException
e
)
{
if
(
rsListarDistrito
!=
null
)
{
response
.
setMessage
(
"Error al listarDistrito -> "
+
e
.
getMessage
()
+
" Error code ["
+
e
.
getErrorCode
()
+
"]."
);
rsListarDistrito
.
close
();
response
.
setStatus
(
false
);
}
System
.
err
.
println
(
"Error al registrar -> "
+
e
.
getMessage
()
+
"["
+
e
.
getErrorCode
()
+
"]"
);
if
(
psListarDistrito
!=
null
)
{
}
finally
{
psListarDistrito
.
close
();
try
{
}
if
(
rsListarDistrito
!=
null
)
{
if
(
connection
!=
null
)
{
rsListarDistrito
.
close
();
connection
.
close
();
}
}
catch
(
SQLException
e
)
{
response
.
setMessage
(
"Error al listarDistrito -> "
+
e
.
getMessage
()
+
" Error code ["
+
e
.
getErrorCode
()
+
"]."
);
response
.
setStatus
(
false
);
}
}
}
if
(
psListarDistrito
!=
null
)
{
jsonReturn
=
new
JSONObject
(
response
);
psListarDistrito
.
close
();
}
return
jsonReturn
;
if
(
connection
!=
null
)
{
connection
.
close
();
}
}
catch
(
SQLException
e
)
{
System
.
err
.
println
(
"Error: ha ocurrido un error al intentar cerrar las conexiones y/o liberacion de recursos -> "
+
e
.
getMessage
());
}
}
}
jsonReturn
=
new
JSONObject
(
response
);
return
jsonReturn
;
}
}
}
src/java/demojsoncrud/utilities/CustomHttpServletRequest.java
deleted
100644 → 0
View file @
ba1776aa
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
;
}
}
src/java/demojsoncrud/utilities/ResponseHelper.java
View file @
94d0c27c
...
@@ -5,36 +5,41 @@ import org.json.JSONObject;
...
@@ -5,36 +5,41 @@ import org.json.JSONObject;
public
class
ResponseHelper
implements
Serializable
{
public
class
ResponseHelper
implements
Serializable
{
private
JSONObject
data
;
private
JSONObject
data
;
private
boolean
status
;
private
boolean
status
;
private
String
message
;
private
String
message
;
public
ResponseHelper
()
{
public
ResponseHelper
()
{
}
}
public
ResponseHelper
(
JSONObject
data
,
boolean
status
,
String
message
)
{
public
JSONObject
getData
()
{
this
.
data
=
data
;
return
data
;
this
.
status
=
status
;
}
this
.
message
=
message
;
}
public
boolean
isStatus
()
{
return
status
;
public
JSONObject
getData
()
{
}
return
data
;
}
public
String
getMessage
()
{
return
message
;
public
boolean
isStatus
()
{
}
return
status
;
}
public
void
setData
(
JSONObject
data
)
{
this
.
data
=
data
;
public
String
getMessage
()
{
}
return
message
;
}
public
void
setStatus
(
boolean
status
)
{
this
.
status
=
status
;
public
void
setData
(
JSONObject
data
)
{
}
this
.
data
=
data
;
}
public
void
setMessage
(
String
message
)
{
this
.
message
=
message
;
public
void
setStatus
(
boolean
status
)
{
}
this
.
status
=
status
;
}
public
void
setMessage
(
String
message
)
{
this
.
message
=
message
;
}
}
}
web/js/general.js
View file @
94d0c27c
/**
/**
* 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
...
@@ -44,32 +7,32 @@ let fetchSo = (url, object) => {
...
@@ -44,32 +7,32 @@ let fetchSo = (url, object) => {
* @returns {Promise} Promise retorna status, msg
* @returns {Promise} Promise retorna status, msg
*/
*/
let
createSelectOptions
=
(
obj
,
valueName
,
textName
)
=>
{
let
createSelectOptions
=
(
obj
,
valueName
,
textName
)
=>
{
let
options
=
''
let
options
=
''
obj
.
forEach
((
data
)
=>
{
obj
.
forEach
((
data
)
=>
{
options
+=
`<option value="
${
data
[
valueName
]}
">
${
data
[
textName
]}
</option>`
options
+=
`<option value="
${
data
[
valueName
]}
">
${
data
[
textName
]}
</option>`
})
})
return
options
return
options
}
}
let
customSwal
=
{
let
customSwal
=
{
alert
(
title
,
text
,
type
)
{
alert
(
title
,
text
,
type
)
{
let
colors
=
{
let
colors
=
{
success
:
'#66BB6A'
,
success
:
'#66BB6A'
,
error
:
'#EF5350'
,
error
:
'#EF5350'
,
warning
:
'#FF7043'
warning
:
'#FF7043'
}
let
btnColor
=
colors
[
type
]
return
new
Promise
((
resolve
,
reject
)
=>
{
swal
({
title
:
title
,
text
:
text
,
confirmButtonColor
:
btnColor
,
type
:
type
},
(
isConfirm
)
=>
{
resolve
()
})
})
}
}
let
btnColor
=
colors
[
type
]
return
new
Promise
((
resolve
,
reject
)
=>
{
swal
({
title
:
title
,
text
:
text
,
confirmButtonColor
:
btnColor
,
type
:
type
},
(
isConfirm
)
=>
{
resolve
()
})
})
}
}
}
/**
/**
...
@@ -81,46 +44,26 @@ let customSwal = {
...
@@ -81,46 +44,26 @@ let customSwal = {
*/
*/
let
getClosest
=
(
elem
,
selector
)
=>
{
let
getClosest
=
(
elem
,
selector
)
=>
{
// Element.matches() polyfill
// Element.matches() polyfill
if
(
!
Element
.
prototype
.
matches
)
{
if
(
!
Element
.
prototype
.
matches
)
{
Element
.
prototype
.
matches
=
Element
.
prototype
.
matches
=
Element
.
prototype
.
matchesSelector
||
Element
.
prototype
.
matchesSelector
||
Element
.
prototype
.
mozMatchesSelector
||
Element
.
prototype
.
mozMatchesSelector
||
Element
.
prototype
.
msMatchesSelector
||
Element
.
prototype
.
msMatchesSelector
||
Element
.
prototype
.
oMatchesSelector
||
Element
.
prototype
.
oMatchesSelector
||
Element
.
prototype
.
webkitMatchesSelector
||
Element
.
prototype
.
webkitMatchesSelector
||
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
}
for
(;
elem
&&
elem
!==
document
;
elem
=
elem
.
parentNode
)
{
// Get closest match
if
(
elem
.
matches
(
selector
))
return
elem
for
(;
elem
&&
elem
!==
document
;
elem
=
elem
.
parentNode
)
{
}
if
(
elem
.
matches
(
selector
))
return
null
return
elem
}
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
web/js/pages/persona.js
View file @
94d0c27c
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
(
/ | /gi
,
' '
)
return
value
.
replace
(
/<.
[^
<>
]
*
?
>/g
,
' '
).
replace
(
/ | /gi
,
' '
)
// remove numbers and punctuation
// remove numbers and punctuation
.
replace
(
/
[
0-9.(),;:!?%#$'"_+=
\/
-
]
*/g
,
''
)
.
replace
(
/
[
0-9.(),;:!?%#$'"_+=
\/
-
]
*/g
,
''
)
}
}
jQuery
.
validator
.
addMethod
(
'lettersonly'
,
function
(
value
,
element
)
{
jQuery
.
validator
.
addMethod
(
'lettersonly'
,
function
(
value
,
element
)
{
return
this
.
optional
(
element
)
||
/^
[
a-z
\s]
+$/i
.
test
(
value
)
return
this
.
optional
(
element
)
||
/^
[
a-z
\s]
+$/i
.
test
(
value
)
},
'Solo letras.'
)
},
'Solo letras.'
)
jQuery
.
validator
.
setDefaults
({
jQuery
.
validator
.
setDefaults
({
debug
:
true
,
debug
:
true
,
ignore
:
'input[type=hidden], .select2-search__field'
,
// ignore hidden fields
ignore
:
'input[type=hidden], .select2-search__field'
,
// ignore hidden fields
errorClass
:
'validation-error-label'
,
errorClass
:
'validation-error-label'
,
successClass
:
'validation-valid-label'
,
successClass
:
'validation-valid-label'
,
highlight
:
function
(
element
,
errorClass
)
{
highlight
:
function
(
element
,
errorClass
)
{
$
(
element
).
removeClass
(
errorClass
)
$
(
element
).
removeClass
(
errorClass
)
},
},
unhighlight
:
function
(
element
,
errorClass
)
{
unhighlight
:
function
(
element
,
errorClass
)
{
$
(
element
).
removeClass
(
errorClass
)
$
(
element
).
removeClass
(
errorClass
)
},
},
// Different components require proper error label placement
// Different components require proper error label placement
errorPlacement
:
function
(
error
,
element
)
{
errorPlacement
:
function
(
error
,
element
)
{
// Styled checkboxes, radios, bootstrap switch
// Styled checkboxes, radios, bootstrap switch
if
(
element
.
parents
(
'div'
).
hasClass
(
'checker'
)
||
element
.
parents
(
'div'
).
hasClass
(
'choice'
)
||
element
.
parent
().
hasClass
(
'bootstrap-switch-container'
))
{
if
(
element
.
parents
(
'div'
).
hasClass
(
'checker'
)
||
element
.
parents
(
'div'
).
hasClass
(
'choice'
)
||
element
.
parent
().
hasClass
(
'bootstrap-switch-container'
))
{
if
(
element
.
parents
(
'label'
).
hasClass
(
'checkbox-inline'
)
||
element
.
parents
(
'label'
).
hasClass
(
'radio-inline'
))
{
if
(
element
.
parents
(
'label'
).
hasClass
(
'checkbox-inline'
)
||
element
.
parents
(
'label'
).
hasClass
(
'radio-inline'
))
{
error
.
appendTo
(
element
.
parent
().
parent
().
parent
().
parent
())
error
.
appendTo
(
element
.
parent
().
parent
().
parent
().
parent
())
}
else
{
}
else
{
error
.
appendTo
(
element
.
parent
().
parent
().
parent
().
parent
().
parent
())
error
.
appendTo
(
element
.
parent
().
parent
().
parent
().
parent
().
parent
())
}
}
}
}
// Unstyled checkboxes, radios
// Unstyled checkboxes, radios
else
if
(
element
.
parents
(
'div'
).
hasClass
(
'checkbox'
)
||
element
.
parents
(
'div'
).
hasClass
(
'radio'
))
{
else
if
(
element
.
parents
(
'div'
).
hasClass
(
'checkbox'
)
||
element
.
parents
(
'div'
).
hasClass
(
'radio'
))
{
error
.
appendTo
(
element
.
parent
().
parent
().
parent
())
error
.
appendTo
(
element
.
parent
().
parent
().
parent
())
}
}
// Input with icons and Select2
// Input with icons and Select2
else
if
(
element
.
parents
(
'div'
).
hasClass
(
'has-feedback'
)
||
element
.
hasClass
(
'select2-hidden-accessible'
))
{
else
if
(
element
.
parents
(
'div'
).
hasClass
(
'has-feedback'
)
||
element
.
hasClass
(
'select2-hidden-accessible'
))
{
error
.
appendTo
(
element
.
parent
())
error
.
appendTo
(
element
.
parent
())
}
}
// Inline checkboxes, radios
// Inline checkboxes, radios
else
if
(
element
.
parents
(
'label'
).
hasClass
(
'checkbox-inline'
)
||
element
.
parents
(
'label'
).
hasClass
(
'radio-inline'
))
{
else
if
(
element
.
parents
(
'label'
).
hasClass
(
'checkbox-inline'
)
||
element
.
parents
(
'label'
).
hasClass
(
'radio-inline'
))
{
error
.
appendTo
(
element
.
parent
().
parent
())
error
.
appendTo
(
element
.
parent
().
parent
())
}
}
// Input group, styled file input
// Input group, styled file input
else
if
(
element
.
parent
().
hasClass
(
'uploader'
)
||
element
.
parents
().
hasClass
(
'input-group'
))
{
else
if
(
element
.
parent
().
hasClass
(
'uploader'
)
||
element
.
parents
().
hasClass
(
'input-group'
))
{
error
.
appendTo
(
element
.
parent
().
parent
())
error
.
appendTo
(
element
.
parent
().
parent
())
}
else
{
}
else
{
error
.
insertAfter
(
element
)
error
.
insertAfter
(
element
)
}
}
}
}
})
})
}
}
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
()
$
(
'#tbl-persona'
).
DataTable
({
$
(
'#tbl-persona'
).
DataTable
({
language
:
{
language
:
{
paginate
:
{
'next'
:
'→ '
,
'previous'
:
' ←'
},
paginate
:
{
'next'
:
'→ '
,
'previous'
:
' ←'
},
emptyTable
:
'No hay registros'
emptyTable
:
'No hay registros'
},
},
ajax
:
{
ajax
:
{
url
:
'../PersonaServlet?accion=listarPersona'
,
url
:
'../PersonaServlet?accion=listarPersona'
,
type
:
'POST'
,
type
:
'POST'
,
dataType
:
'json'
,
dataType
:
'json'
,
data
:
{
search
:
txtCriterioBusqueda
}
data
:
{
search
:
txtCriterioBusqueda
}
},
},
columnDefs
:
[
columnDefs
:
[
{
targets
:
0
,
orderable
:
false
,
width
:
'5%'
,
className
:
'centrado'
},
{
targets
:
0
,
orderable
:
false
,
width
:
'5%'
,
className
:
'centrado'
},
{
targets
:
1
,
orderable
:
false
,
width
:
'15%'
,
className
:
'centrado'
},
{
targets
:
1
,
orderable
:
false
,
width
:
'15%'
,
className
:
'centrado'
},
{
targets
:
2
,
orderable
:
false
,
width
:
'30%'
,
className
:
'centrado'
},
{
targets
:
2
,
orderable
:
false
,
width
:
'30%'
,
className
:
'centrado'
},
{
targets
:
3
,
orderable
:
false
,
width
:
'5%'
,
className
:
'centrado'
},
{
targets
:
3
,
orderable
:
false
,
width
:
'5%'
,
className
:
'centrado'
},
{
targets
:
4
,
orderable
:
false
,
width
:
'25%'
,
className
:
'centrado'
},
{
targets
:
4
,
orderable
:
false
,
width
:
'25%'
,
className
:
'centrado'
},
{
targets
:
5
,
orderable
:
false
,
width
:
'10%'
,
className
:
'centrado'
},
{
targets
:
5
,
orderable
:
false
,
width
:
'10%'
,
className
:
'centrado'
},
{
targets
:
6
,
orderable
:
false
,
width
:
'10%'
,
className
:
'centrado'
}
{
targets
:
6
,
orderable
:
false
,
width
:
'10%'
,
className
:
'centrado'
}
],
],
dom
:
'<"datatable-scroll"t><"datatable-footer"ip>'
,
dom
:
'<"datatable-scroll"t><"datatable-footer"ip>'
,
fixedColumns
:
true
,
fixedColumns
:
true
,
processing
:
true
,
processing
:
true
,
serverSide
:
true
,
serverSide
:
true
,
responsive
:
true
,
responsive
:
true
,
iDisplayLength
:
10
,
iDisplayLength
:
10
,
bSort
:
false
,
bSort
:
false
,
bInfo
:
false
,
bInfo
:
false
,
searching
:
false
,
searching
:
false
,
bLengthChange
:
false
,
bLengthChange
:
false
,
bFilter
:
false
,
bFilter
:
false
,
aaSorting
:
[],
aaSorting
:
[],
ordering
:
false
,
ordering
:
false
,
scrollX
:
false
,
scrollX
:
false
,
ScrollCollapse
:
true
,
ScrollCollapse
:
true
,
columns
:
[
columns
:
[
{
data
:
'codigo'
},
{
data
:
'codigo'
},
{
data
:
'dni'
},
{
data
:
'dni'
},
{
{
data
:
null
,
data
:
null
,
render
:
function
(
data
,
type
,
row
)
{
render
:
function
(
data
,
type
,
row
)
{
return
data
.
apellidos
+
', '
+
data
.
nombres
return
data
.
apellidos
+
', '
+
data
.
nombres
}
}
},
},
{
data
:
'edad'
,
className
:
'text-center'
},
{
data
:
'edad'
,
className
:
'text-center'
},
{
data
:
'correo'
},
{
data
:
'correo'
},
{
{
data
:
'estado'
,
data
:
'estado'
,
className
:
'text-center'
,
className
:
'text-center'
,
render
:
function
(
data
,
type
,
row
)
{
render
:
function
(
data
,
type
,
row
)
{
return
data
===
'1'
?
'<span class="label label-success">activo</span>'
:
'<span class="label label-danger">inactivo</span>'
return
data
===
'1'
?
'<span class="label label-success">activo</span>'
:
'<span class="label label-danger">inactivo</span>'
}
}
},
},
{
{
data
:
null
,
data
:
null
,
className
:
'text-center'
,
className
:
'text-center'
,
render
:
function
(
data
,
type
,
row
)
{
render
:
function
(
data
,
type
,
row
)
{
// debugger
// debugger
return
`<ul class="icons-list">
return
`<ul class="icons-list">
<li title="Editar" data-toggle="modal" data-target="#modal_editar_persona" class="text-primary-600">
<li title="Editar" data-toggle="modal" data-target="#modal_editar_persona" class="text-primary-600">
<a href="#" class="editarPersona"><i class="icon-pencil7"></i></a>
<a href="#" class="editarPersona"><i class="icon-pencil7"></i></a>
</li>
</li>
...
@@ -127,241 +127,273 @@ let persona = {
...
@@ -127,241 +127,273 @@ let persona = {
<a href="#" class="estado"><i class="icon-switch2"></i></a>
<a href="#" class="estado"><i class="icon-switch2"></i></a>
</li>
</li>
</ul>`
</ul>`
}
}
}
}
],
],
'fnInitComplete'
:
function
(
oSettings
,
json
)
{
'fnInitComplete'
:
function
(
oSettings
,
json
)
{
resolve
(
json
)
resolve
(
json
)
}
})
})
},
cambiarEstado
(
datos
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
fetchSo
(
'../PersonaServlet?accion=cambiarEstado'
,
datos
)
.
then
((
data
)
=>
{
resolve
(
data
)
})
.
catch
((
data
)
=>
{
reject
(
data
)
})
})
},
registrar
()
{
let
json
=
{
dni
:
$
(
'#txt_numero_documento'
).
val
().
trim
(),
apellidos
:
$
(
'#txt_apellidos'
).
val
().
trim
(),
nombres
:
$
(
'#txt_nombres'
).
val
().
trim
(),
edad
:
$
(
'#txt_edad'
).
val
().
trim
(),
correo
:
$
(
'#txt_correo'
).
val
().
trim
()
}
}
return
new
Promise
((
resolve
,
reject
)
=>
{
})
fetchSo
(
'../PersonaServlet?accion=registrarPersona'
,
json
)
})
.
then
((
data
)
=>
{
},
resolve
(
data
)
cambiarEstado
(
datos
)
{
})
return
new
Promise
((
resolve
,
reject
)
=>
{
.
catch
((
data
)
=>
{
$
.
ajax
({
reject
(
data
)
url
:
'../PersonaServlet'
,
})
dataType
:
'json'
,
})
type
:
'POST'
,
},
data
:
{
editar
()
{
accion
:
'cambiarEstado'
,
let
json
=
{
json
:
JSON
.
stringify
(
datos
)
apellidos
:
$
(
'#txt_apellidos_editar'
).
val
().
trim
(),
},
beforeSend
:
function
(
xhr
)
{
nombres
:
$
(
'#txt_nombres_editar'
).
val
().
trim
(),
edad
:
$
(
'#txt_edad_editar'
).
val
().
trim
(),
},
success
:
function
(
data
,
textStatus
,
jqXHR
)
{
correo
:
$
(
'#txt_correo_editar'
).
val
().
trim
(),
resolve
(
data
)
codigo
:
localStorage
.
getItem
(
'codigoPersona'
)
},
error
:
function
(
jqXHR
,
textStatus
,
errorThrown
)
{
reject
(
"Error al cambiar estado"
)
}
}
return
new
Promise
((
resolve
,
reject
)
=>
{
})
fetchSo
(
'../PersonaServlet?accion=editarPersona'
,
json
)
})
.
then
((
data
)
=>
{
},
resolve
(
data
)
registrar
()
{
})
let
json
=
{
.
catch
((
data
)
=>
{
dni
:
$
(
'#txt_numero_documento'
).
val
().
trim
(),
reject
(
data
)
apellidos
:
$
(
'#txt_apellidos'
).
val
().
trim
(),
})
nombres
:
$
(
'#txt_nombres'
).
val
().
trim
(),
edad
:
$
(
'#txt_edad'
).
val
().
trim
(),
correo
:
$
(
'#txt_correo'
).
val
().
trim
()
}
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
)
.
then
((
data
)
=>
{
resolve
(
data
)
})
.
catch
((
data
)
=>
{
reject
(
data
)
})
})
})
},
editar
()
{
let
json
=
{
apellidos
:
$
(
'#txt_apellidos_editar'
).
val
().
trim
(),
nombres
:
$
(
'#txt_nombres_editar'
).
val
().
trim
(),
edad
:
$
(
'#txt_edad_editar'
).
val
().
trim
(),
correo
:
$
(
'#txt_correo_editar'
).
val
().
trim
(),
codigo
:
localStorage
.
getItem
(
'codigoPersona'
)
}
}
return
new
Promise
((
resolve
,
reject
)
=>
{
$
.
ajax
({
url
:
'../PersonaServlet'
,
dataType
:
'json'
,
type
:
'POST'
,
data
:
{
accion
:
'editarPersona'
,
json
:
JSON
.
stringify
(
json
)
},
beforeSend
:
function
(
xhr
)
{
},
success
:
function
(
data
,
textStatus
,
jqXHR
)
{
resolve
(
data
)
},
error
:
function
(
jqXHR
,
textStatus
,
errorThrown
)
{
reject
(
"Error al editar persona"
)
}
})
})
}
}
}
let
obtenerDatosTabla
=
(
fila
)
=>
{
let
obtenerDatosTabla
=
(
fila
)
=>
{
return
$
(
'#tbl-persona'
).
DataTable
().
row
(
fila
).
data
()
return
$
(
'#tbl-persona'
).
DataTable
().
row
(
fila
).
data
()
}
}
let
limpiarCajasTexto
=
()
=>
{
let
limpiarCajasTexto
=
()
=>
{
$
(
'#btn_agregar_persona'
).
on
(
'click'
,
function
()
{
$
(
'#btn_agregar_persona'
).
on
(
'click'
,
function
()
{
$
(
'#txt_numero_documento'
).
attr
({
$
(
'#txt_numero_documento'
).
attr
({
autofocus
:
true
autofocus
:
true
}).
val
(
''
)
}).
val
(
''
)
$
(
'#txt_apellidos'
).
val
(
''
)
$
(
'#txt_apellidos'
).
val
(
''
)
$
(
'#txt_nombres'
).
val
(
''
)
$
(
'#txt_nombres'
).
val
(
''
)
$
(
'#txt_edad'
).
val
(
''
)
$
(
'#txt_edad'
).
val
(
''
)
$
(
'#txt_correo'
).
val
(
''
)
$
(
'#txt_correo'
).
val
(
''
)
})
})
}
}
let
asignarEventos
=
()
=>
{
let
asignarEventos
=
()
=>
{
$
(
'#tbl-persona tbody'
).
on
(
'click'
,
'.estado'
,
(
event
)
=>
{
$
(
'#tbl-persona tbody'
).
on
(
'click'
,
'.estado'
,
(
event
)
=>
{
let
rowData
=
obtenerDatosTabla
(
$
(
event
.
currentTarget
).
parents
(
'tr'
))
let
rowData
=
obtenerDatosTabla
(
$
(
event
.
currentTarget
).
parents
(
'tr'
))
swal
({
swal
({
title
:
'¿Estás seguro?'
,
title
:
'¿Estás seguro?'
,
text
:
`¿Desea
${
rowData
.
estado
===
'1'
?
'DESACTIVAR'
:
'ACTIVAR'
}
a
${
rowData
.
apellidos
}
${
rowData
.
nombres
}
?`
,
text
:
`¿Desea
${
rowData
.
estado
===
'1'
?
'DESACTIVAR'
:
'ACTIVAR'
}
a
${
rowData
.
apellidos
}
${
rowData
.
nombres
}
?`
,
type
:
'warning'
,
type
:
'warning'
,
showCancelButton
:
true
,
showCancelButton
:
true
,
confirmButtonColor
:
'#EF5350'
,
confirmButtonColor
:
'#EF5350'
,
confirmButtonText
:
'Si'
,
confirmButtonText
:
'Si'
,
cancelButtonText
:
'No'
,
cancelButtonText
:
'No'
,
closeOnConfirm
:
false
,
closeOnConfirm
:
false
,
closeOnCancel
:
true
closeOnCancel
:
true
},
(
isConfirm
)
=>
{
},
(
isConfirm
)
=>
{
if
(
isConfirm
)
{
if
(
isConfirm
)
{
persona
.
cambiarEstado
(
rowData
)
persona
.
cambiarEstado
(
rowData
)
.
then
((
data
)
=>
{
.
then
((
data
)
=>
{
customSwal
customSwal
.
alert
(
'¡Exito!'
,
'Se actualizó con exito.'
,
'success'
)
.
alert
(
'¡Exito!'
,
'Se actualizó con exito.'
,
'success'
)
.
then
(()
=>
{
.
then
(()
=>
{
persona
.
listar
()
persona
.
listar
()
})
})
})
})
.
catch
((
data
)
=>
{
.
catch
((
data
)
=>
{
customSwal
.
alert
(
'¡Error!'
,
'No se registró con exito.'
,
'error'
)
customSwal
.
alert
(
'¡Error!'
,
'No se registró con exito.'
,
'error'
)
})
})
}
}
})
})
})
})
document
.
querySelectorAll
(
'.estado'
).
forEach
((
e
)
=>
{
document
.
querySelectorAll
(
'.estado'
).
forEach
((
e
)
=>
{
e
.
addEventListener
(
'click'
,
(
ev
)
=>
{
e
.
addEventListener
(
'click'
,
(
ev
)
=>
{
console
.
log
(
getClosest
(
ev
.
target
,
'tr'
).
rowIndex
-
1
)
console
.
log
(
getClosest
(
ev
.
target
,
'tr'
).
rowIndex
-
1
)
})
})
})
})
$
(
'#tbl-persona tbody'
).
on
(
'click'
,
'.editarPersona'
,
(
event
)
=>
{
$
(
'#tbl-persona tbody'
).
on
(
'click'
,
'.editarPersona'
,
(
event
)
=>
{
let
rowData
=
obtenerDatosTabla
(
$
(
event
.
currentTarget
).
parents
(
'tr'
))
let
rowData
=
obtenerDatosTabla
(
$
(
event
.
currentTarget
).
parents
(
'tr'
))
$
(
'#txt_numero_documento_editar'
).
val
(
rowData
.
dni
)
$
(
'#txt_numero_documento_editar'
).
val
(
rowData
.
dni
)
$
(
'#txt_apellidos_editar'
).
val
(
rowData
.
apellidos
)
$
(
'#txt_apellidos_editar'
).
val
(
rowData
.
apellidos
)
$
(
'#txt_nombres_editar'
).
val
(
rowData
.
nombres
)
$
(
'#txt_nombres_editar'
).
val
(
rowData
.
nombres
)
$
(
'#txt_edad_editar'
).
val
(
rowData
.
edad
)
$
(
'#txt_edad_editar'
).
val
(
rowData
.
edad
)
$
(
'#txt_correo_editar'
).
val
(
rowData
.
correo
)
$
(
'#txt_correo_editar'
).
val
(
rowData
.
correo
)
localStorage
.
setItem
(
'codigoPersona'
,
rowData
.
codigo
)
localStorage
.
setItem
(
'codigoPersona'
,
rowData
.
codigo
)
})
})
$
(
'#btn_registrar_persona'
).
on
(
'click'
,
function
()
{
$
(
'#btn_registrar_persona'
).
on
(
'click'
,
function
()
{
let
validator
=
$
(
'#form_registrar_persona'
).
validate
({
let
validator
=
$
(
'#form_registrar_persona'
).
validate
({
rules
:
{
rules
:
{
txt_numero_documento
:
{
required
:
true
,
digits
:
true
,
minlength
:
8
,
maxlength
:
8
},
txt_numero_documento
:
{
required
:
true
,
digits
:
true
,
minlength
:
8
,
maxlength
:
8
},
txt_apellidos
:
{
required
:
true
,
lettersonly
:
true
,
maxlength
:
50
},
txt_apellidos
:
{
required
:
true
,
lettersonly
:
true
,
maxlength
:
50
},
txt_nombres
:
{
required
:
true
,
lettersonly
:
true
,
maxlength
:
50
},
txt_nombres
:
{
required
:
true
,
lettersonly
:
true
,
maxlength
:
50
},
txt_edad
:
{
required
:
true
,
digits
:
true
,
maxlength
:
2
},
txt_edad
:
{
required
:
true
,
digits
:
true
,
maxlength
:
2
},
txt_correo
:
{
required
:
true
,
email
:
true
,
maxlength
:
50
}
txt_correo
:
{
required
:
true
,
email
:
true
,
maxlength
:
50
}
},
},
messages
:
{
messages
:
{
txt_numero_documento
:
{
txt_numero_documento
:
{
required
:
'Ingresar número de documento.'
,
required
:
'Ingresar número de documento.'
,
minlength
:
'Ingresar 8 dígitos.'
,
minlength
:
'Ingresar 8 dígitos.'
,
maxlength
:
'Ingresar 8 dígitos.'
,
maxlength
:
'Ingresar 8 dígitos.'
,
digits
:
'Ingresar solo números.'
digits
:
'Ingresar solo números.'
},
},
txt_apellidos
:
{
txt_apellidos
:
{
required
:
'Ingresar apellidos.'
,
required
:
'Ingresar apellidos.'
,
maxlength
:
'Ingresar 50 letras como máximo.'
,
maxlength
:
'Ingresar 50 letras como máximo.'
,
lettersonly
:
'Ingresar solo letras'
lettersonly
:
'Ingresar solo letras'
},
},
txt_nombres
:
{
txt_nombres
:
{
required
:
'Ingresar nombres.'
,
required
:
'Ingresar nombres.'
,
maxlength
:
'Ingresar 50 letras como máximo.'
,
maxlength
:
'Ingresar 50 letras como máximo.'
,
lettersonly
:
'Ingresar solo letras'
lettersonly
:
'Ingresar solo letras'
},
},
txt_edad
:
{
txt_edad
:
{
required
:
'Ingresar edad.'
,
required
:
'Ingresar edad.'
,
maxlength
:
'Ingresar 2 dígitos como máximo'
,
maxlength
:
'Ingresar 2 dígitos como máximo'
,
digits
:
'Ingresar solo números'
digits
:
'Ingresar solo números'
},
},
txt_correo
:
{
txt_correo
:
{
required
:
'Ingresar correo.'
,
required
:
'Ingresar correo.'
,
maxlength
:
'Ingresar 50 letras como máximo.'
,
maxlength
:
'Ingresar 50 letras como máximo.'
,
email
:
'Ingresar un correo válido'
email
:
'Ingresar un correo válido'
}
}
})
let
estado
=
validator
.
form
()
if
(
estado
)
{
persona
.
registrar
()
.
then
((
data
)
=>
{
customSwal
.
alert
(
'¡Exito!'
,
data
.
message
,
'success'
)
.
then
(()
=>
{
$
(
'#modal_registrar_persona'
).
modal
(
'hide'
)
})
.
then
(()
=>
{
persona
.
listar
()
})
})
.
catch
((
data
)
=>
{
customSwal
.
alert
(
'¡Error!'
,
data
.
message
,
'error'
)
})
}
}
}
})
})
let
estado
=
validator
.
form
()
$
(
'#btn_editar_persona'
).
on
(
'click'
,
function
()
{
if
(
estado
)
{
let
validator
=
$
(
'#form_editar_persona'
).
validate
({
persona
rules
:
{
.
registrar
()
txt_apellidos_editar
:
{
required
:
true
,
lettersonly
:
true
,
maxlength
:
50
},
.
then
((
data
)
=>
{
txt_nombres_editar
:
{
required
:
true
,
lettersonly
:
true
,
maxlength
:
50
},
customSwal
txt_edad_editar
:
{
required
:
true
,
digits
:
true
,
maxlength
:
2
},
.
alert
(
'¡Exito!'
,
data
.
message
,
'success'
)
txt_correo_editar
:
{
required
:
true
,
email
:
true
,
maxlength
:
50
}
.
then
(()
=>
{
},
$
(
'#modal_registrar_persona'
).
modal
(
'hide'
)
messages
:
{
})
txt_apellidos_editar
:
{
.
then
(()
=>
{
required
:
'Ingresar apellidos.'
,
persona
.
listar
()
maxlength
:
'Ingresar 50 letras como máximo.'
,
})
lettersonly
:
'Ingresar solo letras'
},
txt_nombres_editar
:
{
required
:
'Ingresar nombres.'
,
maxlength
:
'Ingresar 50 letras como máximo.'
,
lettersonly
:
'Ingresar solo letras'
},
txt_edad_editar
:
{
required
:
'Ingresar edad.'
,
maxlength
:
'Ingresar 2 dígitos como máximo'
,
digits
:
'Ingresar solo números'
},
txt_correo_editar
:
{
required
:
'Ingresar correo.'
,
maxlength
:
'Ingresar 50 letras como máximo.'
,
email
:
'Ingresar un correo válido'
}
}
})
})
let
estado
=
validator
.
form
()
.
catch
((
data
)
=>
{
if
(
estado
)
{
customSwal
.
alert
(
'¡Error!'
,
data
.
message
,
'error'
)
persona
})
.
editar
()
}
.
then
((
data
)
=>
{
})
customSwal
.
alert
(
'¡Exito!'
,
data
.
message
,
'success'
)
$
(
'#btn_editar_persona'
).
on
(
'click'
,
function
()
{
.
then
(()
=>
{
let
validator
=
$
(
'#form_editar_persona'
).
validate
({
$
(
'#modal_editar_persona'
).
modal
(
'hide'
)
rules
:
{
})
txt_apellidos_editar
:
{
required
:
true
,
lettersonly
:
true
,
maxlength
:
50
},
.
then
(()
=>
{
txt_nombres_editar
:
{
required
:
true
,
lettersonly
:
true
,
maxlength
:
50
},
persona
.
listar
()
txt_edad_editar
:
{
required
:
true
,
digits
:
true
,
maxlength
:
2
},
})
txt_correo_editar
:
{
required
:
true
,
email
:
true
,
maxlength
:
50
}
})
},
.
catch
((
data
)
=>
{
messages
:
{
customSwal
.
alert
(
'¡Error!'
,
data
.
message
,
'error'
)
txt_apellidos_editar
:
{
})
required
:
'Ingresar apellidos.'
,
maxlength
:
'Ingresar 50 letras como máximo.'
,
lettersonly
:
'Ingresar solo letras'
},
txt_nombres_editar
:
{
required
:
'Ingresar nombres.'
,
maxlength
:
'Ingresar 50 letras como máximo.'
,
lettersonly
:
'Ingresar solo letras'
},
txt_edad_editar
:
{
required
:
'Ingresar edad.'
,
maxlength
:
'Ingresar 2 dígitos como máximo'
,
digits
:
'Ingresar solo números'
},
txt_correo_editar
:
{
required
:
'Ingresar correo.'
,
maxlength
:
'Ingresar 50 letras como máximo.'
,
email
:
'Ingresar un correo válido'
}
}
}
})
})
let
estado
=
validator
.
form
()
if
(
estado
)
{
persona
.
editar
()
.
then
((
data
)
=>
{
customSwal
.
alert
(
'¡Exito!'
,
data
.
message
,
'success'
)
.
then
(()
=>
{
$
(
'#modal_editar_persona'
).
modal
(
'hide'
)
})
.
then
(()
=>
{
persona
.
listar
()
})
})
.
catch
((
data
)
=>
{
customSwal
.
alert
(
'¡Error!'
,
data
.
message
,
'error'
)
})
}
})
$
(
'#btn_buscar_criterio'
).
on
(
'click'
,
function
()
{
$
(
'#btn_buscar_criterio'
).
on
(
'click'
,
function
()
{
persona
.
listar
()
persona
.
listar
()
})
})
$
(
'#txt-busqueda'
).
keyup
(
function
(
e
)
{
$
(
'#txt-busqueda'
).
keyup
(
function
(
e
)
{
if
(
e
.
keyCode
===
13
)
{
if
(
e
.
keyCode
===
13
)
{
persona
.
listar
()
persona
.
listar
()
}
}
})
})
}
}
jqueryValidateConfig
()
jqueryValidateConfig
()
...
...
web/js/pages/ubigeo.js
View file @
94d0c27c
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'
,
resolve
(
data
)
dataType
:
'json'
,
})
type
:
'POST'
,
.
catch
((
data
)
=>
{
data
:
{
reject
(
data
)
accion
:
'listarDepartamento'
})
},
beforeSend
:
function
(
xhr
)
{
})
},
},
success
:
function
(
data
,
textStatus
,
jqXHR
)
{
consultarProvincia
(
codigoDepartamento
)
{
resolve
(
data
)
let
json
=
{
},
error
:
function
(
jqXHR
,
textStatus
,
errorThrown
)
{
codigoDepartamento
:
codigoDepartamento
reject
(
"Error al listar departamentos"
)
}
}
return
new
Promise
((
resolve
,
reject
)
=>
{
})
fetchSo
(
'../UbigeoServlet?accion=listarProvincia'
,
json
)
})
.
then
((
data
)
=>
{
},
resolve
(
data
)
consultarProvincia
(
codigoDepartamento
)
{
})
let
json
=
{
.
catch
((
data
)
=>
{
codigoDepartamento
:
codigoDepartamento
reject
(
data
)
}
})
return
new
Promise
((
resolve
,
reject
)
=>
{
})
$
.
ajax
({
},
url
:
'../UbigeoServlet'
,
consultarDistrito
(
codigoDepartamento
,
codigoProvincia
)
{
dataType
:
'json'
,
let
json
=
{
type
:
'POST'
,
codigoProvincia
:
codigoProvincia
,
data
:
{
codigoDepartamento
:
codigoDepartamento
accion
:
'listarProvincia'
,
json
:
JSON
.
stringify
(
json
)
},
beforeSend
:
function
(
xhr
)
{
},
success
:
function
(
data
,
textStatus
,
jqXHR
)
{
resolve
(
data
)
},
error
:
function
(
jqXHR
,
textStatus
,
errorThrown
)
{
reject
(
"Error al listar provincias"
)
}
}
return
new
Promise
((
resolve
,
reject
)
=>
{
})
fetchSo
(
'../UbigeoServlet?accion=listarDistrito'
,
json
)
})
.
then
((
data
)
=>
{
},
resolve
(
data
)
consultarDistrito
(
codigoDepartamento
,
codigoProvincia
)
{
})
let
json
=
{
.
catch
((
data
)
=>
{
codigoProvincia
:
codigoProvincia
,
reject
(
data
)
codigoDepartamento
:
codigoDepartamento
})
})
}
}
return
new
Promise
((
resolve
,
reject
)
=>
{
$
.
ajax
({
url
:
'../UbigeoServlet'
,
dataType
:
'json'
,
type
:
'POST'
,
data
:
{
accion
:
'listarDistrito'
,
json
:
JSON
.
stringify
(
json
)
},
beforeSend
:
function
(
xhr
)
{
},
success
:
function
(
data
,
textStatus
,
jqXHR
)
{
resolve
(
data
)
},
error
:
function
(
jqXHR
,
textStatus
,
errorThrown
)
{
reject
(
"Error al listar distritos"
)
}
})
})
}
}
}
let
asignarEventos
=
()
=>
{
let
asignarEventos
=
()
=>
{
let
slDepartamento
=
document
.
querySelector
(
'#select_departamento'
)
let
slDepartamento
=
document
.
querySelector
(
'#select_departamento'
)
let
slProvincia
=
document
.
querySelector
(
'#select_provincia'
)
let
slProvincia
=
document
.
querySelector
(
'#select_provincia'
)
let
slDistrito
=
document
.
querySelector
(
'#select_distrito'
)
let
slDistrito
=
document
.
querySelector
(
'#select_distrito'
)
slDepartamento
.
addEventListener
(
'change'
,
(
e
)
=>
{
slDepartamento
.
addEventListener
(
'change'
,
(
e
)
=>
{
let
codigoDepartamento
=
parseInt
(
e
.
target
.
value
)
let
codigoDepartamento
=
parseInt
(
e
.
target
.
value
)
ubigeo
.
consultarProvincia
(
codigoDepartamento
)
ubigeo
.
consultarProvincia
(
codigoDepartamento
)
.
then
((
data
)
=>
{
.
then
((
data
)
=>
{
let
opt
=
'<option value="0">[ SELECCIONE ]</option>'
let
opt
=
'<option value="0">[ SELECCIONE ]</option>'
opt
+=
createSelectOptions
(
data
.
data
.
provincias
,
'codigoProvincia'
,
'nombreProvincia'
)
opt
+=
createSelectOptions
(
data
.
data
.
provincias
,
'codigoProvincia'
,
'nombreProvincia'
)
slProvincia
.
innerHTML
=
opt
slProvincia
.
innerHTML
=
opt
})
})
.
then
(()
=>
{
.
then
(()
=>
{
$
(
'#select_provincia'
).
selectpicker
(
'refresh'
)
slDistrito
.
innerHTML
=
'<option value="0">[ SELECCIONE ]</option>'
})
$
(
'#select_distrito'
).
selectpicker
(
'refresh'
)
})
slProvincia
.
addEventListener
(
'change'
,
(
e
)
=>
{
let
codigoDepartamento
=
parseInt
(
slDepartamento
.
value
)
let
codigoProvincia
=
parseInt
(
e
.
target
.
value
)
ubigeo
.
consultarDistrito
(
codigoDepartamento
,
codigoProvincia
)
.
then
((
data
)
=>
{
let
opt
=
'<option value="0">[ SELECCIONE ]</option>'
opt
+=
createSelectOptions
(
data
.
data
.
distritos
,
'codigoDistrito'
,
'nombreDistrito'
)
slDistrito
.
innerHTML
=
opt
})
.
then
(()
=>
{
$
(
'#select_distrito'
).
selectpicker
(
'refresh'
)
})
})
slDepartamento
.
addEventListener
(
'change'
,
(
e
)
=>
{
let
departamento
=
parseInt
(
e
.
target
.
value
)
if
(
departamento
===
0
)
{
slProvincia
.
innerHTML
=
'<option value="0">[ SELECCIONE ]</option>'
slDistrito
.
innerHTML
=
'<option value="0">[ SELECCIONE ]</option>'
}
else
{
slProvincia
.
value
=
0
slDistrito
.
value
=
0
}
$
(
'#select_provincia'
).
selectpicker
(
'refresh'
)
$
(
'#select_provincia'
).
selectpicker
(
'refresh'
)
})
})
slProvincia
.
addEventListener
(
'change'
,
(
e
)
=>
{
let
codigoDepartamento
=
parseInt
(
slDepartamento
.
value
)
let
codigoProvincia
=
parseInt
(
e
.
target
.
value
)
ubigeo
.
consultarDistrito
(
codigoDepartamento
,
codigoProvincia
)
.
then
((
data
)
=>
{
let
opt
=
'<option value="0">[ SELECCIONE ]</option>'
opt
+=
createSelectOptions
(
data
.
data
.
distritos
,
'codigoDistrito'
,
'nombreDistrito'
)
slDistrito
.
innerHTML
=
opt
})
.
then
(()
=>
{
$
(
'#select_distrito'
).
selectpicker
(
'refresh'
)
$
(
'#select_distrito'
).
selectpicker
(
'refresh'
)
})
})
})
slDepartamento
.
addEventListener
(
'change'
,
(
e
)
=>
{
let
departamento
=
parseInt
(
e
.
target
.
value
)
if
(
departamento
===
0
)
{
slProvincia
.
innerHTML
=
'<option value="0">[ SELECCIONE ]</option>'
slDistrito
.
innerHTML
=
'<option value="0">[ SELECCIONE ]</option>'
}
else
{
slProvincia
.
value
=
0
slDistrito
.
value
=
0
}
$
(
'#select_provincia'
).
selectpicker
(
'refresh'
)
$
(
'#select_distrito'
).
selectpicker
(
'refresh'
)
})
}
}
ubigeo
ubigeo
.
consultarDepartamento
()
.
consultarDepartamento
()
.
then
((
data
)
=>
{
.
then
((
data
)
=>
{
let
element
=
document
.
querySelector
(
'#select_departamento'
)
let
element
=
document
.
querySelector
(
'#select_departamento'
)
let
opt
=
'<option value="0">[ SELECCIONE ]</option>'
let
opt
=
'<option value="0">[ SELECCIONE ]</option>'
opt
+=
createSelectOptions
(
data
.
data
.
departamentos
,
'codigoDepartamento'
,
'nombreDepartamento'
)
opt
+=
createSelectOptions
(
data
.
data
.
departamentos
,
'codigoDepartamento'
,
'nombreDepartamento'
)
element
.
innerHTML
=
opt
element
.
innerHTML
=
opt
})
})
.
then
(()
=>
{
.
then
(()
=>
{
$
(
'#select_departamento'
).
selectpicker
(
'refresh'
)
$
(
'#select_departamento'
).
selectpicker
(
'refresh'
)
})
})
asignarEventos
()
asignarEventos
()
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment