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<AccessEnforcement_Detection_Image> images = detection.images;
|
|
int pictureNumber = 0;
|
|
for (int i=0; i<images.size(); i++)
|
{
|
if (images.get(i).name.indexOf(AccessEnforcement_Detection_Image.NAME_OVERVIEW) == 0)
|
{
|
XSSFDrawing drawing = xssfsheet.createDrawingPatriarch();
|
XSSFClientAnchor anchor = getAnchour(pictureNumber, images.size());
|
if (anchor != null)
|
{
|
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(images.get(i).data, Workbook.PICTURE_TYPE_JPEG));
|
pictureNumber = pictureNumber + 1;
|
}
|
}
|
}
|
|
for (int i=0; i<images.size(); i++)
|
{
|
if (images.get(i).name.indexOf(AccessEnforcement_Detection_Image.NAME_PLATE) == 0)
|
{
|
XSSFDrawing drawing = xssfsheet.createDrawingPatriarch();
|
XSSFClientAnchor anchor = getAnchour(pictureNumber, images.size());
|
if (anchor != null)
|
{
|
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(images.get(i).data, Workbook.PICTURE_TYPE_JPEG));
|
pictureNumber = pictureNumber + 1;
|
}
|
}
|
}
|
}
|
catch (Exception exception)
|
{
|
}
|
}
|
|
|
|
|
|
private XSSFClientAnchor getAnchour(int number, int total)
|
{
|
if (total == 1)
|
{
|
return new XSSFClientAnchor(0, 0, 0, 0, 1, 37, 19, 73);
|
}
|
else if (total <= 4)
|
{
|
switch (number)
|
{
|
case 0 : return new XSSFClientAnchor(0, 0, 0, 0, 1, 37, 12, 55);
|
case 1 : return new XSSFClientAnchor(0, 0, 0, 0, 12, 37, 19, 55);
|
case 2 : return new XSSFClientAnchor(0, 0, 0, 0, 1, 55, 12, 73);
|
case 3: return new XSSFClientAnchor(0, 0, 0, 0, 12, 55, 19, 73);
|
}
|
}
|
else
|
{
|
switch (number)
|
{
|
case 0 : return new XSSFClientAnchor(0, 0, 0, 0, 1, 37, 9, 49);
|
case 1 : return new XSSFClientAnchor(0, 0, 0, 0, 9, 37, 15, 49);
|
case 2 : return new XSSFClientAnchor(0, 0, 0, 0, 15, 37, 19, 49);
|
|
case 3 : return new XSSFClientAnchor(0, 0, 0, 0, 1, 49, 9, 61);
|
case 4 : return new XSSFClientAnchor(0, 0, 0, 0, 9, 49, 15, 61);
|
case 5 : return new XSSFClientAnchor(0, 0, 0, 0, 15, 49, 19, 61);
|
|
case 6 : return new XSSFClientAnchor(0, 0, 0, 0, 1, 61, 9, 73);
|
case 7 : return new XSSFClientAnchor(0, 0, 0, 0, 9, 61, 15, 73);
|
case 8 : return new XSSFClientAnchor(0, 0, 0, 0, 15, 61, 19, 73);
|
}
|
}
|
|
|
|
return null;
|
}
|
|
|
|
|
private String getPoliceLicense()
|
{
|
try
|
{
|
for (int i=detection.states.size()-1; 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;
|
}
|
|
|
}
|