package art.servers.gost.access.reports; import art.library.model.devices.gost.access.types.AccessEnforcement_Detection; import art.library.model.devices.gost.access.types.AccessEnforcement_Detection_Image; import art.library.model.devices.gost.access.types.AccessEnforcement_Detection_State; import art.library.utils.resources.Resources; import art.servers.ServerException; import art.servers.Shared; import java.io.File; import java.io.FileInputStream; import java.nio.file.Files; import java.text.SimpleDateFormat; import java.util.List; import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.util.Units; import org.apache.poi.xssf.usermodel.XSSFClientAnchor; import org.apache.poi.xssf.usermodel.XSSFDrawing; import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ReportAccess extends XLSX { private String language = null; private AccessEnforcement_Detection detection = null; private String format = null; public ReportAccess(String language, String format, AccessEnforcement_Detection detection) throws Exception { this.language = language; this.detection = detection; this.format = format; String filename = "data/" + Shared.getApplicationName() + "/templates/" + language + "/art.library.model.devices.gost.access.AccessEnforcement.xlsx"; File file = new File(filename); if (file.exists() == false) file = Resources.getResourceFile(filename); if (file.exists() == false) throw new ServerException(Shared.getMessage(language, "Template not found")); workbook = new XSSFWorkbook(new FileInputStream(new File(filename))); } public byte[] generate() throws Exception { fill(); workbook.setForceFormulaRecalculation(true); XSSFFormulaEvaluator.evaluateAllFormulaCells(workbook); workbook.setActiveSheet(0); workbook.setSelectedTab(0); return saveAndClose(format); } private void fill() throws Exception { AccessEnforcement_Detection_State detectionState = detection.getLastState(); SimpleDateFormat formato1 = new SimpleDateFormat(Shared.getMessage(language, "dd/MM/yyyy")); SimpleDateFormat formato2 = new SimpleDateFormat(Shared.getMessage(language, "HH:mm:ss")); XSSFSheet xssfsheet = workbook.getSheetAt(0); // Records try { if (detectionState.processed != null) { getXSSFCell(xssfsheet, "I2").setCellValue(nonull(detectionState.processed.record)); getXSSFCell(xssfsheet, "B37").setCellValue(nonull(detectionState.processed.record)); getXSSFCell(xssfsheet, "J6").setCellValue(nonull(detectionState.processed.amount)); getXSSFCell(xssfsheet, "L6").setCellValue(nonull(detectionState.processed.points)); getXSSFCell(xssfsheet, "B13").setCellValue(nonull(detectionState.processed.description)); } else if (detectionState.violation != null) { getXSSFCell(xssfsheet, "I2").setCellValue(nonull(detectionState.violation.record)); getXSSFCell(xssfsheet, "B37").setCellValue(nonull(detectionState.violation.record)); getXSSFCell(xssfsheet, "J6").setCellValue(nonull(detectionState.violation.amount)); getXSSFCell(xssfsheet, "L6").setCellValue(nonull(detectionState.violation.points)); getXSSFCell(xssfsheet, "B13").setCellValue(nonull(detectionState.violation.description)); } } catch (Exception exception) { exception.printStackTrace(); } // Location try { getXSSFCell(xssfsheet, "I3").setCellValue(formato1.format(detection.timestamp)); getXSSFCell(xssfsheet, "K3").setCellValue(formato2.format(detection.timestamp)); getXSSFCell(xssfsheet, "B10").setCellValue(nonull(detection.device.location)); getXSSFCell(xssfsheet, "H10").setCellValue(nonull(detection.device.kilometricPoint)); getXSSFCell(xssfsheet, "I10").setCellValue(nonull(detection.device.direction)); getXSSFCell(xssfsheet, "B17").setCellValue(nonull(detectionState.vehicle.getPlate())); } catch (Exception exception) { } // ATEX5 try { getXSSFCell(xssfsheet, "H17").setCellValue(nonull(detectionState.vehicle.atex5.datosGenerales.descripcionVehiculo.tipoVehiculo.descripcion)); getXSSFCell(xssfsheet, "I17").setCellValue(nonull(detectionState.vehicle.atex5.datosGenerales.descripcionVehiculo.marca.descripcion + " " + detectionState.vehicle.atex5.datosGenerales.descripcionVehiculo.modelo)); if (detectionState.vehicle.atex5.datosGenerales.titular.datosPersona.personaFisica.idDocumento != null) { getXSSFCell(xssfsheet, "B21").setCellValue(nonull(detectionState.vehicle.atex5.datosGenerales.titular.datosPersona.personaFisica.idDocumento)); String nombre = detectionState.vehicle.atex5.datosGenerales.titular.datosPersona.personaFisica.nombre; nombre = nombre + " " + detectionState.vehicle.atex5.datosGenerales.titular.datosPersona.personaFisica.apellido1; nombre = nombre + " " + detectionState.vehicle.atex5.datosGenerales.titular.datosPersona.personaFisica.apellido2; getXSSFCell(xssfsheet, "F21").setCellValue(nonull(nombre)); } else if (detectionState.vehicle.atex5.datosGenerales.titular.datosPersona.personaJuridica.cif != null) { getXSSFCell(xssfsheet, "B21").setCellValue(nonull(detectionState.vehicle.atex5.datosGenerales.titular.datosPersona.personaJuridica.cif)); getXSSFCell(xssfsheet, "F21").setCellValue(nonull(detectionState.vehicle.atex5.datosGenerales.titular.datosPersona.personaJuridica.razonSocial)); } getXSSFCell(xssfsheet, "D24").setCellValue(nonull(detectionState.vehicle.atex5.datosGenerales.titular.domicilio.municipio)); int codigoPostal = (int)detectionState.vehicle.atex5.datosGenerales.titular.domicilio.codPostal; if (codigoPostal < 0) codigoPostal = codigoPostal & 0xFFFF; String direccion = detectionState.vehicle.atex5.datosGenerales.titular.domicilio.calle.replaceAll("\\s{2,}", " ").replaceAll(" ,", ",").trim(); getXSSFCell(xssfsheet, "B23").setCellValue(nonull(direccion)); getXSSFCell(xssfsheet, "I24").setCellValue(nonull(String.format("%05d", codigoPostal))); getXSSFCell(xssfsheet, "K24").setCellValue(nonull(detectionState.vehicle.atex5.datosGenerales.titular.domicilio.provincia.descripcion)); } catch (Exception exception) { } // Signature try { String policeLicense = getPoliceLicense(); getXSSFCell(xssfsheet, "D33").setCellValue(nonull(policeLicense)); byte[] data = Files.readAllBytes(new File("data/" + Shared.getApplicationName() + "/templates/" + language + "/" + getPoliceLicense() + ".png").toPath()); XSSFDrawing drawing = xssfsheet.createDrawingPatriarch(); XSSFClientAnchor anchor = new XSSFClientAnchor(Units.EMU_PER_PIXEL,Units.EMU_PER_PIXEL, -Units.EMU_PER_PIXEL, -Units.EMU_PER_PIXEL, 1, 33, 5, 35); anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE); //0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells. drawing.createPicture(anchor, workbook.addPicture(data, Workbook.PICTURE_TYPE_JPEG)); } catch (Exception exception) { } // Pictures try { List images = detection.images; int pictureNumber = 0; for (int i=0; i=0; i--) { if ((detection.states.get(i).validation != null) && (detection.states.get(i).validation.policeLicense != null)) { return detection.states.get(i).validation.policeLicense; } } } catch (Exception exception) { } return null; } private String nonull(String value) { if (value == null) return ""; return value; } }