Commit 95a1d028 by Alonso Moreno Postigo

[ADD] UbigeoBackend

parent 69bf0e82
/*
* 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.beans;
import java.io.Serializable;
/**
*
* @author Alonso
*/
public class UbigeoBean implements Serializable {
private String codigoDepartamento;
private String nombreDepartamento;
private String codigoProvincia;
private String nombreProvincia;
private String codigoDistrito;
private String nombreDistrito;
public String getCodigoDepartamento() {
return codigoDepartamento;
}
public void setCodigoDepartamento(String codigoDepartamento) {
this.codigoDepartamento = codigoDepartamento;
}
public String getNombreDepartamento() {
return nombreDepartamento;
}
public void setNombreDepartamento(String nombreDepartamento) {
this.nombreDepartamento = nombreDepartamento;
}
public String getCodigoProvincia() {
return codigoProvincia;
}
public void setCodigoProvincia(String codigoProvincia) {
this.codigoProvincia = codigoProvincia;
}
public String getNombreProvincia() {
return nombreProvincia;
}
public void setNombreProvincia(String nombreProvincia) {
this.nombreProvincia = nombreProvincia;
}
public String getCodigoDistrito() {
return codigoDistrito;
}
public void setCodigoDistrito(String codigoDistrito) {
this.codigoDistrito = codigoDistrito;
}
public String getNombreDistrito() {
return nombreDistrito;
}
public void setNombreDistrito(String nombreDistrito) {
this.nombreDistrito = nombreDistrito;
}
}
/*
* 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;
import demojsoncrud.sqlserverdao.SqlserverDAOFactory;
/**
*
* @author Alonso
*/
public abstract class DAOFactory {
public static final int SQL_SERVER = 1;
......@@ -15,7 +24,9 @@ public abstract class DAOFactory {
return null;
}
}
public abstract PersonaDAO getPersonaDAO();
public abstract UbigeoDAO getUbigeoDAO();
}
/*
* 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;
import demojsoncrud.beans.UbigeoBean;
import org.json.JSONObject;
/**
*
* @author Alonso
*/
public interface UbigeoDAO {
public JSONObject listarDepartamento();
public JSONObject listarProvincia(UbigeoBean bean);
public JSONObject listarDistrito(UbigeoBean bean);
}
/*
* 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.services;
import demojsoncrud.beans.UbigeoBean;
import demojsoncrud.dao.DAOFactory;
import demojsoncrud.dao.UbigeoDAO;
import org.json.JSONObject;
/**
*
* @author Alonso
*/
public class UbigeoService {
DAOFactory daoFactory = DAOFactory.getDAOFactory(DAOFactory.SQL_SERVER);
UbigeoDAO service = daoFactory.getUbigeoDAO();
public JSONObject listarDepartamento() {
JSONObject jsonReturn = null;
try {
jsonReturn = service.listarDepartamento();
} catch (Exception e) {
}
return jsonReturn;
}
public JSONObject listarProvincia(UbigeoBean bean) {
JSONObject jsonReturn = null;
try {
jsonReturn = service.listarProvincia(bean);
} catch (Exception e) {
}
return jsonReturn;
}
public JSONObject listarDistrito(UbigeoBean bean) {
JSONObject jsonReturn = null;
try {
jsonReturn = service.listarDistrito(bean);
} catch (Exception e) {
}
return jsonReturn;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package demojsoncrud.servlets;
import demojsoncrud.beans.UbigeoBean;
import demojsoncrud.services.UbigeoService;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;
/**
*
* @author Alonso
*/
public class UbigeoServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String param = request.getParameter("accion");
if ("listarDepartamento".equals(param)) {
listarDepartamento(request, response);
} else if ("listarProvincia".equals(param)) {
listarProvincia(request, response);
} else if ("listarDistrito".equals(param)) {
listarDistrito(request, response);
}
}
private void listarDepartamento(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("application/json");
PrintWriter pw = response.getWriter();
UbigeoService service = new UbigeoService();
JSONObject json = service.listarDepartamento();
pw.print(json);
}
private void listarProvincia(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("application/json");
PrintWriter pw = response.getWriter();
UbigeoService service = new UbigeoService();
String json = request.getParameter("json");
JSONObject jsonObject = new JSONObject(json);
String codigoDepartamento = jsonObject.getString("codigoDepartamento");
UbigeoBean bean = new UbigeoBean();
bean.setCodigoDepartamento(codigoDepartamento);
JSONObject jsonReturn = service.listarProvincia(bean);
pw.print(jsonReturn);
}
private void listarDistrito(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("application/json");
PrintWriter pw = response.getWriter();
UbigeoService service = new UbigeoService();
String json = request.getParameter("json");
JSONObject jsonObject = new JSONObject(json);
String codigoDepartamento = jsonObject.getString("codigoDepartamento");
String codigoProvincia = jsonObject.getString("codigoProvincia");
UbigeoBean bean = new UbigeoBean();
bean.setCodigoDepartamento(codigoDepartamento);
bean.setCodigoProvincia(codigoProvincia);
JSONObject jsonReturn = service.listarDistrito(bean);
pw.print(jsonReturn);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package demojsoncrud.sqlserverdao;
import demojsoncrud.dao.DAOFactory;
import demojsoncrud.dao.PersonaDAO;
import demojsoncrud.dao.UbigeoDAO;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
*
* @author Alonso
*/
public class SqlserverDAOFactory extends DAOFactory {
public static Connection obtenerConexion(String base) {
Connection connection = null;
String user = "sa";
String pwd = "Saco1357$";
String urlBase = "jdbc:sqlserver://172.16.2.118:1433;databaseName=";
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "";
if (base.equalsIgnoreCase("demojsoncrud")) {
String user = "sa";
String pwd = "Saco1357$";
String url = "jdbc:sqlserver://172.16.2.118:1433;databaseName=demojsoncrud";
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, user, pwd);
} catch (ClassNotFoundException | SQLException e) {
System.out.println("Error al conectarse => " + e.getMessage());
}
url = urlBase + "demojsoncrud";
} else if (base.equalsIgnoreCase("ubigeo")) {
url = urlBase + "ubigeo";
} else {
url = "";
}
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, user, pwd);
} catch (ClassNotFoundException | SQLException e) {
System.out.println("Error al conectarse => " + e.getMessage());
}
return connection;
......@@ -34,4 +51,9 @@ public class SqlserverDAOFactory extends DAOFactory {
public PersonaDAO getPersonaDAO() {
return new PersonaSqlserverDAO();
}
@Override
public UbigeoDAO getUbigeoDAO() {
return new UbigeoSqlserverDAO();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package demojsoncrud.sqlserverdao;
import demojsoncrud.beans.UbigeoBean;
import demojsoncrud.dao.UbigeoDAO;
import demojsoncrud.utilities.ResponseHelper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.json.JSONArray;
import org.json.JSONObject;
/**
*
* @author Alonso
*/
public class UbigeoSqlserverDAO implements UbigeoDAO {
@Override
public JSONObject listarDepartamento() {
JSONObject jsonReturn = new JSONObject();
JSONArray data = new JSONArray();
PreparedStatement psListarDepartamento = null;
ResultSet rsListarDepartamento = null;
Connection connection = null;
String base = "ubigeo";
ResponseHelper response = new ResponseHelper();
try {
connection = SqlserverDAOFactory.obtenerConexion(base);
String sql
= "select "
+ "codDepartamento as codigoDepartamento, "
+ "nombreDepartamento as nombreDepartamento "
+ "from ubigeo "
+ "group by codDepartamento, nombreDepartamento "
+ "order by nombreDepartamento";
psListarDepartamento = connection.prepareStatement(sql);
rsListarDepartamento = psListarDepartamento.executeQuery();
while (rsListarDepartamento.next()) {
UbigeoBean ubigeoBean = new UbigeoBean();
ubigeoBean.setCodigoDepartamento(rsListarDepartamento.getString("codigoDepartamento"));
ubigeoBean.setNombreDepartamento(rsListarDepartamento.getString("nombreDepartamento"));
JSONObject obj = new JSONObject(ubigeoBean);
data.put(obj);
}
JSONObject jsonData = new JSONObject();
jsonData.put("departamentos", data);
response.setData(jsonData);
response.setMessage("Listado correctamente!");
response.setStatus(true);
} catch (SQLException e) {
response.setMessage("Error al listarDepartamento -> " + e.getMessage() + " Error code [" + e.getErrorCode() + "].");
response.setStatus(false);
} finally {
try {
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);
}
}
jsonReturn = new JSONObject(response);
return jsonReturn;
}
@Override
public JSONObject listarProvincia(UbigeoBean bean) {
JSONObject jsonReturn = new JSONObject();
JSONArray data = new JSONArray();
PreparedStatement psListarProvincia = null;
ResultSet rsListarProvincia = null;
Connection connection = null;
String base = "ubigeo";
ResponseHelper response = new ResponseHelper();
try {
connection = SqlserverDAOFactory.obtenerConexion(base);
String sql
= "select "
+ "codProvincia as codigoProvincia, "
+ "nombreProvincia as nombreProvincia "
+ "from ubigeo "
+ "where codDepartamento = ? "
+ "group by codProvincia, nombreProvincia "
+ "order by nombreProvincia";
psListarProvincia = connection.prepareStatement(sql);
psListarProvincia.setString(1, bean.getCodigoDepartamento());
rsListarProvincia = psListarProvincia.executeQuery();
while (rsListarProvincia.next()) {
UbigeoBean ubigeoBean = new UbigeoBean();
ubigeoBean.setCodigoProvincia(rsListarProvincia.getString("codigoProvincia"));
ubigeoBean.setNombreProvincia(rsListarProvincia.getString("nombreProvincia"));
JSONObject obj = new JSONObject(ubigeoBean);
data.put(obj);
}
JSONObject jsonData = new JSONObject();
jsonData.put("provincias", data);
response.setData(jsonData);
response.setMessage("Listado correctamente!");
response.setStatus(true);
} catch (SQLException e) {
response.setMessage("Error al listarProvincia -> " + e.getMessage() + " Error code [" + e.getErrorCode() + "].");
response.setStatus(false);
} finally {
try {
if (rsListarProvincia != null) {
rsListarProvincia.close();
}
if (psListarProvincia != null) {
psListarProvincia.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
response.setMessage("Error al listarProvincia -> " + e.getMessage() + " Error code [" + e.getErrorCode() + "].");
response.setStatus(false);
}
}
jsonReturn = new JSONObject(response);
return jsonReturn;
}
@Override
public JSONObject listarDistrito(UbigeoBean bean) {
JSONObject jsonReturn = new JSONObject();
JSONArray data = new JSONArray();
PreparedStatement psListarDistrito = null;
ResultSet rsListarDistrito = null;
Connection connection = null;
String base = "ubigeo";
ResponseHelper response = new ResponseHelper();
try {
connection = SqlserverDAOFactory.obtenerConexion(base);
String sql
= "select "
+ "codDistrito as codigoDistrito, "
+ "nombreDistrito as nombreDistrito "
+ "from ubigeo "
+ "where codProvincia = ? and codDepartamento = ? "
+ "order by nombreDistrito";
psListarDistrito = connection.prepareStatement(sql);
psListarDistrito.setString(1, bean.getCodigoProvincia());
psListarDistrito.setString(2, bean.getCodigoDepartamento());
rsListarDistrito = psListarDistrito.executeQuery();
while (rsListarDistrito.next()) {
UbigeoBean ubigeoBean = new UbigeoBean();
ubigeoBean.setCodigoDistrito(rsListarDistrito.getString("codigoDistrito"));
ubigeoBean.setNombreDistrito(rsListarDistrito.getString("nombreDistrito"));
JSONObject obj = new JSONObject(ubigeoBean);
data.put(obj);
}
JSONObject jsonData = new JSONObject();
jsonData.put("distritos", data);
response.setData(jsonData);
response.setMessage("Listado correctamente!");
response.setStatus(true);
} catch (SQLException e) {
response.setMessage("Error al listarDistrito -> " + e.getMessage() + " Error code [" + e.getErrorCode() + "].");
response.setStatus(false);
} finally {
try {
if (rsListarDistrito != null) {
rsListarDistrito.close();
}
if (psListarDistrito != null) {
psListarDistrito.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
response.setMessage("Error al listarDistrito -> " + e.getMessage() + " Error code [" + e.getErrorCode() + "].");
response.setStatus(false);
}
}
jsonReturn = new JSONObject(response);
return jsonReturn;
}
}
......@@ -4,10 +4,18 @@
<servlet-name>PersonaServlet</servlet-name>
<servlet-class>demojsoncrud.servlets.PersonaServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>UbigeoServlet</servlet-name>
<servlet-class>demojsoncrud.servlets.UbigeoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PersonaServlet</servlet-name>
<url-pattern>/PersonaServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UbigeoServlet</servlet-name>
<url-pattern>/UbigeoServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment