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
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 ReportAccessZPR extends ReportAccess
{
    public ReportAccessZPR(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-ZPR.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.zpr != null) 
            {
                if(detection.zpr.entryLocation != null)
                    getXSSFCell(xssfsheet, "B7").setCellValue(nonull(detection.zpr.entryLocation));
                if(detection.zpr.entryTimestamp != null )
                    getXSSFCell(xssfsheet, "B8").setCellValue(formato1.format(detection.zpr.entryTimestamp) + " " + formato2.format(detection.zpr.entryTimestamp));
                
                if(detection.zpr.exitLocation != null)
                    getXSSFCell(xssfsheet, "B9").setCellValue(nonull(detection.zpr.exitLocation));
                if(detection.zpr.exitTimestamp != null)
                    getXSSFCell(xssfsheet, "B10").setCellValue(formato1.format(detection.zpr.exitTimestamp) + " " + formato2.format(detection.zpr.exitTimestamp));
            }
            if(detectionState.violation != null)
            {
                try
                {
                    float amount = Float.parseFloat(detectionState.violation.amount);
                    float bonus = Float.parseFloat(detectionState.violation.bonus);
                    int points = Integer.parseInt(detectionState.violation.points);
 
                    getXSSFCell(xssfsheet, "E13").setCellValue(nonull(String.format("%.2f", amount)));
                    getXSSFCell(xssfsheet, "I13").setCellValue(nonull(String.format("%.2f", bonus)));
                    getXSSFCell(xssfsheet, "N13").setCellValue(nonull(String.valueOf(points)));
                }
                catch (Exception ex){}
 
                getXSSFCell(xssfsheet, "M2").setCellValue(nonull(detectionState.violation.record));
                getXSSFCell(xssfsheet, "A14").setCellValue(nonull(detectionState.violation.description));
                getXSSFCell(xssfsheet, "A13").setCellValue(nonull(detectionState.violation.LSV));
                getXSSFCell(xssfsheet, "C13").setCellValue(nonull(detectionState.violation.RGC));
                getXSSFCell(xssfsheet, "D13").setCellValue(nonull(detectionState.violation.type));
                getXSSFCell(xssfsheet, "O13").setCellValue(nonull(detectionState.violation.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, "B18").setCellValue(nonull(detectionState.vehicle.getPlate()));
        }
        catch (Exception exception)
        {
        }
        
        // Signature
        
        // Pictures
        
        try
        {
            List<AccessEnforcement_Detection_Image> images = detection.images;
            
            Optional<byte[]> image1 = getImage(AccessEnforcement_Detection_Image.NAME_PLATE); 
            Optional<byte[]> image2 = getImage(AccessEnforcement_Detection_Image.NAME_OVERVIEW);
            
            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(6);
                anchor.setCol2(38);
                anchor.setRow2(14);
 
                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(16);
                anchor.setCol2(38);
                anchor.setRow2(24);
 
                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();
    }
}