Created
December 17, 2012 20:23
-
-
Save Isxida/4321820 to your computer and use it in GitHub Desktop.
Disclaimer: todo el codigo no lo he hecho yo (yo no comento con uppercase letters) . Mirar la parte de extraccion y envio al JasperReports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pe.com.tdp.perifericos.averias.servlet; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import java.io.PrintWriter; | |
import java.math.BigInteger; | |
import java.sql.SQLException; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Date; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.SortedMap; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import jxl.write.DateTime; | |
import net.sf.jasperreports.engine.JRException; | |
import net.sf.jasperreports.engine.JasperCompileManager; | |
import net.sf.jasperreports.engine.JasperExportManager; | |
import net.sf.jasperreports.engine.JasperFillManager; | |
import net.sf.jasperreports.engine.JasperPrint; | |
import net.sf.jasperreports.engine.JasperReport; | |
import org.apache.commons.beanutils.BeanMap; | |
import org.apache.log4j.Logger; | |
import bsh.util.Util; | |
import com.google.gson.Gson; | |
import com.ibatis.sqlmap.client.SqlMapClient; | |
import com.ibm.keymanager.audit.m; | |
import pe.com.tdp.perifericos.averias.bean.ActxTecnico; | |
import pe.com.tdp.perifericos.averias.service.AveriaServices; | |
import pe.com.tdp.perifericos.averias.serviceImpl.AveriaServicesImpl; | |
import pe.com.tdp.perifericos.comun.bean.JqGridJson; | |
import pe.com.tdp.perifericos.comun.bean.JqGridRow; | |
import pe.com.tdp.perifericos.comun.util.DataSourceListReport; | |
import pe.com.tdp.perifericos.comun.util.JqueryUtil; | |
import pe.com.tdp.perifericos.comun.util.Utils; | |
import pe.com.tdp.perifericos.seguridad.DAOImpl.EventoDAOImpl; | |
import pe.com.tdp.perifericos.seguridad.bean.Usuario; | |
/** | |
* Servlet implementation class RpteBoletaServlet | |
*/ | |
public class RpteActXTecnicoServlet extends HttpServlet { | |
private static final long serialVersionUID = 1L; | |
private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(RpteActXTecnicoServlet.class); | |
final int SEC_PER_DAY = 1000; | |
SqlMapClient objSqlMapClient = null; | |
public Usuario beanUsuario = null ; | |
private String accion = ""; | |
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { | |
/*LLAMADA DE METODO*/ | |
AveriaServices averiaServices = new AveriaServicesImpl(); | |
/*UTIL OUT JSON*/ | |
beanUsuario = (Usuario) req.getSession().getAttribute("seguridad.usuario"); | |
String listaEstaciones = req.getParameter("hidEstaciones"); | |
String listaServicios = req.getParameter("hidServicios"); | |
String usuarioNombre = | |
(beanUsuario.getUsuNomDs().trim() + " " | |
+ beanUsuario.getUsuApeDs().trim()) == null ? "" : (beanUsuario.getUsuNomDs().trim() + " " | |
+ beanUsuario.getUsuApeDs().trim()); | |
try { | |
accion = req.getParameter("accion")==null?"":req.getParameter("accion"); | |
logger.info("TIPO DE PARAMETRO :" + accion); | |
HashMap<String, Object> map = new HashMap<String, Object>(); | |
Map<String, Object> mapTec = new HashMap<String, Object>(); | |
/* OBJETO LIST FOR ESTACION/SERVICIOS */ | |
String rqLstEstaciones = req.getParameter("hidEstaciones"); | |
String rqLstServicios = req.getParameter("hidServicios"); | |
/* OBJETO FOR PARAMETROS */ | |
String rqRzClient=req.getParameter("txtCliente")==null?"":req.getParameter("txtCliente"); | |
String rqFecIni=req.getParameter("dtFechaI")==null?"":req.getParameter("dtFechaI"); | |
String rqFecFin=req.getParameter("dtFechaF")==null?"":req.getParameter("dtFechaF"); | |
String rqIdMasiva=req.getParameter("cboMasiva")==null?"":req.getParameter("cboMasiva"); | |
String rqIdInstalaciones=req.getParameter("cbxInst")==null?"":req.getParameter("cbxInst"); | |
String rqIdGrupo=req.getParameter("cboGrupo")==null?"":req.getParameter("cboGrupo"); | |
/*OBJETO MAP ALL */ | |
map.put("mapFecIni",rqFecIni.toString()); | |
map.put("mapFecFin",rqFecFin.toString()); | |
map.put("mapRzClient",rqRzClient.toString().toUpperCase()); | |
map.put("mapIdMasiva",rqIdMasiva.toString()); | |
map.put("mapIdGrupo",rqIdGrupo.toString()); | |
map.put("mapLstEstaciones",rqLstEstaciones.toString()); | |
map.put("mapLstServicios",rqLstServicios.toString()); | |
int npage = Integer | |
.parseInt((req.getParameter("page") == null ? "1" : req | |
.getParameter("page"))); | |
int nrows = Integer | |
.parseInt((req.getParameter("rows") == null ? "1" : req | |
.getParameter("rows"))); | |
int records = averiaServices.totalRegistros(map, "resultActXTecnicoAverias"); | |
int nstart = (nrows*npage - nrows); | |
map.put("nstart",nstart); | |
map.put("nend", records); | |
List<ActxTecnico> lista = averiaServices.rpteActuXTecnico(map); | |
int cont = 1; | |
int totHoras = 0; | |
int totMin = 0; | |
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy hh:mm"); | |
mapTec.put("codAveria",""); | |
mapTec.put("codPrefijo",""); | |
mapTec.put("codTecnico",""); | |
mapTec.put("tiempoAnt", ""); | |
for (ActxTecnico actxTecnico : lista) { | |
if ( | |
mapTec.get("codAveria").toString().equals(actxTecnico.getCodAveria().trim().toString()) && | |
mapTec.get("codPrefijo").toString().equals(actxTecnico.getCodPrefijo().trim().toString()) && | |
mapTec.get("codTecnico").toString().equals(actxTecnico.getCodTecnico().trim().toString()) && | |
mapTec.get("tiempoAnt") != null | |
){ | |
//nro de actuaciones | |
cont++; | |
actxTecnico.setNroActuaciones(""+cont); | |
//Tiempo Medio | |
Date tiempoAnterior = dateFormat.parse((mapTec.get("tiempoAnt").toString())); | |
Date tiempoActual = dateFormat.parse(actxTecnico.getTiempo().toString()); | |
long secs = (tiempoActual.getTime() - tiempoAnterior.getTime()) /SEC_PER_DAY; | |
// Arreglar Horas. | |
// Mas horas. | |
int hours = (int) (secs/365/3600); | |
secs = secs % 3600; | |
int mins = (int) (secs / 60); | |
secs = secs % 60; | |
totHoras+=hours; | |
totMin+=mins; | |
if(totMin >=60){ | |
totMin-=60; | |
totHoras++; | |
} | |
actxTecnico.setTiempoMedio(totHoras +" : "+ totMin); | |
}else { | |
cont = 1; | |
totHoras = 0; | |
totMin = 00; | |
if( actxTecnico.getTiempo() != null){ | |
actxTecnico.setTiempoMedio(totHoras+" : "+totMin); | |
} | |
} | |
mapTec.put("codAveria", actxTecnico.getCodAveria().trim()); | |
mapTec.put("codPrefijo",actxTecnico.getCodPrefijo().trim()); | |
mapTec.put("codTecnico", actxTecnico.getCodTecnico().trim()); | |
mapTec.put("tiempoAnt", actxTecnico.getTiempo()); | |
} | |
logger.debug("ENTRO "); | |
Map<String, ActxTecnico> mapToPdf = new HashMap<String, ActxTecnico>(); | |
for (ActxTecnico i : lista) mapToPdf.put(i.getCodTecnico() ,i); | |
if (accion.equals("pdf")){ | |
logger.debug("pdf"); | |
//Cabecera | |
String camposActTec[] = {"nomCliente", | |
"FecInicio", | |
"FecFin", | |
"nomUsuario", | |
"nomEstacion", | |
"nomGrupo" | |
}; | |
ArrayList listaCampos = new ArrayList(Arrays.asList(camposActTec)); | |
DataSourceListReport datasource = new DataSourceListReport(); | |
//Cabecera,data columnas | |
Map<String,Object> paramToPdf = new HashMap<String,Object>(); | |
paramToPdf.put("nomCliente",rqRzClient); | |
paramToPdf.put("FecInicio",rqFecIni); | |
paramToPdf.put("FecFin",rqFecFin); | |
paramToPdf.put("nomUsuario", usuarioNombre.toString() ); | |
paramToPdf.put("nomEstacion", listaEstaciones.toString()); | |
paramToPdf.put("nomGrupo", listaServicios.toString()); | |
HashMap<String, Object> rMap = new HashMap<String, Object>(); | |
datasource.setLista(listaCampos, lista); | |
String rutaReporte = req | |
.getSession() | |
.getServletContext() | |
.getRealPath( | |
"/WEB-INF/classes/pe/com/tdp/perifericos/averias/reportes/RepActXTecnico" | |
+ ".jrxml"); | |
Map<String, Object> parameters = new HashMap<String, Object>(); | |
JasperReport reporte = JasperCompileManager.compileReport(rutaReporte); | |
JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, parameters, datasource); | |
res.setContentType("application/pdf"); | |
OutputStream out = res.getOutputStream(); | |
JasperExportManager.exportReportToPdfStream(jasperPrint, out); | |
out.close(); | |
}if(accion.equals("busq")) { | |
logger.debug("BUSQUEDA "); | |
res.setContentType("text/html"); | |
PrintWriter outi = res.getWriter(); | |
JqueryUtil util = new JqueryUtil(); | |
Gson gson = new Gson(); | |
List<JqGridRow> rows = new ArrayList<JqGridRow>(); | |
for (Object axt : lista) { | |
ActxTecnico act = (ActxTecnico) axt; | |
HashMap<String, Object> resMap = new HashMap<String, Object>(); | |
resMap.put("codAveria", (act.getCodAveria() == null ? "" : act.getCodAveria().toString())); | |
resMap.put("codPrefijo", (act.getCodPrefijo() == null ? "" : act.getCodPrefijo().toString())); | |
resMap.put("codTecnico", (act.getCodTecnico() == null || act.getCodTecnico().trim() == "" ? "Vacio" : act.getCodTecnico().toString())); | |
resMap.put("nomTecnico", (act.getNomTecnico() == null || act.getNomTecnico().trim() == "" ? "Vacio" : act.getNomTecnico().toString())); | |
resMap.put("nroActuaciones", (act.getNroActuaciones() == null ? "1" : act.getNroActuaciones().toString())); | |
resMap.put("tiempo", (act.getTiempo() == null ? "No time" : act.getTiempo().toString())); | |
resMap.put("tiempoMedio", (act.getTiempoMedio() == null ? "No medium time " : act.getTiempoMedio().toString() ) ); | |
System.out.println("map: "+resMap); | |
JqGridRow grow = new JqGridRow(); | |
grow.setId(resMap.get("codTecnico").toString()); | |
List listOfResult = new ArrayList(resMap.values()); | |
//grow.setCell(Utils.hashMapToList(resMap,"Averia","resultActtXTecnicoAverias")); | |
grow.setCell(listOfResult); | |
rows.add(grow); | |
} | |
JqGridJson jsonOrOrden = new JqGridJson(); | |
jsonOrOrden.setPage(npage); | |
jsonOrOrden.setRecords(records); | |
int total = (int) (Math.ceil((double) records/ (double)nrows)); | |
jsonOrOrden.setTotal(total==0 ? 1 : total); | |
jsonOrOrden.setGrows(rows); | |
String json = gson.toJson(jsonOrOrden); | |
//end | |
outi.println(json); | |
} | |
} catch (NullPointerException e){ | |
e.printStackTrace(); | |
} catch (SecurityException e) { | |
e.printStackTrace(); | |
} catch (IllegalArgumentException e) { | |
e.printStackTrace(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} catch (JRException e) { | |
e.printStackTrace(); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment