Alejandro Acuña
2024-12-18 44b33e24b644459038edd956cfce7345ce3236c1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
 
public class ReportAccessVPV extends ReportAccess
{
    public ReportAccessVPV(String language, String format, AccessEnforcement_Detection detection) throws Exception
    {
        super(language, format, detection);
    }
    
    @Override
     public void loadTemplate(String language1) throws ServerException, IOException
    {
        String filename = "data/" + Shared.getApplicationName() + "/templates/" + language1 + "/art.library.model.devices.gost.access.AccessEnforcement-VPV.xlsx";
        File file = new File(filename);
        if (file.exists() == false) file = Resources.getResourceFile(filename);
        if (file.exists() == false)
        {
            throw new ServerException(Shared.getMessage(language1, "Template not found"));
        }
        workbook = new XSSFWorkbook(new FileInputStream(new File(filename)));
    }
    
    @Override
    protected void fill() throws Exception
    {
        AccessEnforcement_Detection_State detectionState = detection.getLastState();
        SimpleDateFormat formato1 = new SimpleDateFormat("dd/MM/yyyy");
        SimpleDateFormat formato2 = new SimpleDateFormat(Shared.getMessage(language, "HH:mm:ss"));
        XSSFSheet xssfsheet = workbook.getSheetAt(0);
 
        // Records
        
        try
        {
            if (detection.vpv != null) 
            {
//                getXSSFCell(xssfsheet, "M2").setCellValue(nonull(detection.vpv.record)); //TODO RECORD WHERE?
                getXSSFCell(xssfsheet, "A12").setCellValue(nonull(detection.vpv.description));
                getXSSFCell(xssfsheet, "A11").setCellValue(nonull(detection.vpv.LSV));
                getXSSFCell(xssfsheet, "C11").setCellValue(nonull(detection.vpv.RGC));
                getXSSFCell(xssfsheet, "D11").setCellValue(nonull(detection.vpv.type));
                getXSSFCell(xssfsheet, "E11").setCellValue(nonull(detection.vpv.amount));
                getXSSFCell(xssfsheet, "I11").setCellValue(nonull(detection.vpv.bonus));
                getXSSFCell(xssfsheet, "N11").setCellValue(nonull(detection.vpv.points));
                getXSSFCell(xssfsheet, "O11").setCellValue(nonull(detection.vpv.key));
            }
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
        }
        
       
        // Location
        
        try
        {
            getXSSFCell(xssfsheet, "A5").setCellValue(formato1.format(detection.timestamp));
            getXSSFCell(xssfsheet, "F5").setCellValue(formato2.format(detection.timestamp));
            getXSSFCell(xssfsheet, "B7").setCellValue(nonull(detection.device.location));
            getXSSFCell(xssfsheet, "B8").setCellValue(nonull(detection.device.direction));
            getXSSFCell(xssfsheet, "B16").setCellValue(nonull(detectionState.vehicle.getPlate()));
        }
        catch (Exception exception)
        {
        }
        
        // Signature
        
        try
        {
            
            String policeLicense = getPoliceLicense();
            
            getXSSFCell(xssfsheet, "D22").setCellValue(nonull(policeLicense));
            
            //TODO SIGNATURE?
//                    
//            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;
            
            Optional<byte[]> image1 = getImage(AccessEnforcement_Detection_Image.NAME_PLATE); //TODO GET IMAGE NAMES
            Optional<byte[]> image2 = getImage(AccessEnforcement_Detection_Image.NAME_PLATE); //TODO  GET IMAGE NAMES
            
            if (image1.isPresent())
            {
                int pictureIndex = workbook.addPicture(image1.get(), Workbook.PICTURE_TYPE_PNG);
 
                XSSFDrawing drawing = (XSSFDrawing) xssfsheet.createDrawingPatriarch();
 
                XSSFClientAnchor anchor = new XSSFClientAnchor();
                anchor.setCol1(17);
                anchor.setRow1(1);
                anchor.setCol2(39);
                anchor.setRow2(11);
 
                drawing.createPicture(anchor, pictureIndex);
            }
        
            if (image2.isPresent())
            {
                int pictureIndex = workbook.addPicture(image2.get(), Workbook.PICTURE_TYPE_PNG);
 
                XSSFDrawing drawing = (XSSFDrawing) xssfsheet.createDrawingPatriarch();
 
                XSSFClientAnchor anchor = new XSSFClientAnchor();
                anchor.setCol1(17);
                anchor.setRow1(12);
                anchor.setCol2(39);
                anchor.setRow2(22);
 
                drawing.createPicture(anchor, pictureIndex);
            }
 
        }
        catch (Exception exception)
        {
        }
    }
    
    
    
    
    private Optional<byte[]> getImage(String imageName)
    {
        return detection.images.stream()
                .filter(Objects::nonNull)
                .filter(image -> image.name != null)
                .filter(image -> image.name.equals(imageName))
                .map(image -> image.data)
                .findAny();
    }
}