Alejandro Acuña
2024-11-19 72a8eee716de93344f977ab79be7d3bfeb341462
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
package art.servers.gost.access.controller;
 
import art.library.interop.InteropParameters;
import art.library.interop.InteropResponse;
import art.library.interop.serialization.Serialization;
import art.library.interop.serialization.SerializationException;
import art.library.model.devices.Device;
import art.library.model.devices.gost.access.types.AccessEnforcement_Detection;
import art.library.model.devices.gost.access.types.AccessEnforcement_Detection_State;
import art.library.model.devices.gost.access.types.AccessEnforcement_Detection_State_Discarded;
import art.library.model.devices.gost.types.atex5.PersonATEX5;
import art.library.model.devices.gost.types.atex5.VehicleATEX5;
import art.library.model.devices.gost.types.permissions.List_Detections;
import art.library.model.devices.user.UserPermission;
import art.library.utils.synchro.Mutex;
import art.servers.ServerException;
import art.servers.gost.access.Shared;
import art.servers.gost.access.reports.ReportAccess;
import art.servers.gost.access.reports.ReportAccessVPV;
import art.servers.gost.access.reports.ReportAccessZPR;
import art.servers.gost.atex5.ATEX5;
import art.servers.types.HttpAuthentication;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
 
public class ListenerImplementation extends art.servers.controller.ListenerImplementation
{
    
    private static Mutex mutex = new Mutex();
 
    public InteropResponse sendCommands(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
        throw new SerializationException(Shared.getMessage(language, "Not available"));
    }
 
    
    
    
    
    public InteropResponse listDetections(InteropParameters parameters) throws SerializationException
    {
        Shared.println("Listener", "1.listDetections");
        String language = (String)parameters.getParameterValue("language");
        Shared.println("Listener", "2.listDetections: " + language);
 
        try
        {
            HttpAuthentication authentication = getHttpAuthentication(parameters, language);
            Shared.println("Listener", "3.listDetections: " + authentication);
 
            List_Detections result = new List_Detections();
            result.filter = (parameters.hasParameter("filter") == true) ? (String)parameters.getParameterValue("filter") : null;
            result.order = (parameters.hasParameter("order") == true) ? (String)parameters.getParameterValue("order") : null;
            result.offset = (parameters.hasParameter("offset") == true) ? Integer.parseInt((String)parameters.getParameterValue("offset")) : 0;
            result.limit = (parameters.hasParameter("limit") == true) ? Integer.parseInt((String)parameters.getParameterValue("limit")) : 10000;
            // Shared.println("Listener", "4.listDetections: " + Serialization.toString(result));
 
            Shared.controllerDetections.listDetections(authentication, language, result);
            Shared.println("Listener", "5.listDetections: " + result.total);
            InteropResponse response = new InteropResponse(result, "application/json", false);
            return response;
        }
        catch (SerializationException exception)
        {
            Shared.printstack("Listener", exception);
            throw exception;
        }
        catch (Exception exception)
        {
            Shared.printstack("Listener", exception);
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }
 
 
    
    public InteropResponse getDetections(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
        
        try
        {
            HttpAuthentication authentication = getHttpAuthentication(parameters, language);
 
            List_Detections result = new List_Detections();
            result.filter = (parameters.hasParameter("filter") == true) ? (String)parameters.getParameterValue("filter") : null;
            result.order = (parameters.hasParameter("order") == true) ? (String)parameters.getParameterValue("order") : null;
            result.offset = (parameters.hasParameter("offset") == true) ? Integer.parseInt((String)parameters.getParameterValue("offset")) : 0;
            result.limit = (parameters.hasParameter("limit") == true) ? Integer.parseInt((String)parameters.getParameterValue("limit")) : 10000;
 
            Shared.controllerDetections.getDetections(authentication, language, result);
            InteropResponse response = new InteropResponse(result, "application/json", false);
            return response;
        }
        catch (SerializationException exception)
        {
            throw exception;
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }
 
 
 
    
    public InteropResponse getDetection(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
 
        try
        {
            HttpAuthentication authentication = getHttpAuthentication(parameters, language);
            
            boolean images = true;
            
            if (parameters.hasParameter("images") == true)
            {
                images = Boolean.parseBoolean((String)parameters.getParameterValue("images"));
            }
 
            Shared.println("Listener", "1.GetDetection");
            long identifier = Long.parseLong((String)parameters.getParameterValue("identifier"));
            Shared.println("Listener", "2.GetDetection: " + identifier);
            AccessEnforcement_Detection detection = Shared.controllerDetections.getDetection(identifier, images);
            Shared.println("Listener", "3.GetDetection: " + detection);
            if (detection == null) throw new SerializationException(Shared.getMessage(language, "Invalid discard detection"));
            Shared.println("Listener", "4.GetDetection: " + new Date(detection.timestamp).toString());
 
            UserPermission permission = Shared.controllerUserPermissions.getUserPermission(authentication.profile, authentication.username);
 
            if (permission.hasPermissionsDevice(detection.device.getIdentifier()) == false)
            {
                throw new SerializationException(Shared.getMessage(language, "Not allowed"));
            }
 
            Shared.println("Listener", "5.GetDetection: " + new Date(detection.timestamp).toString());
            InteropResponse response = new InteropResponse(detection, "application/json", true);
            Shared.println("Listener", "6.GetDetection: " + response);
            return(response);
        }
        catch (ServerException exception)
        {
            Shared.printstack("Listener", exception);
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()));
        }
        catch (Exception exception)
        {
            Shared.printstack("Listener", exception);
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }    
 
    
 
    
    public InteropResponse setDetection(InteropParameters parameters) throws SerializationException
    {
        mutex.lockWrite();
        
        String language = (String)parameters.getParameterValue("language");
 
        try
        {
            HttpAuthentication authentication = getHttpAuthentication(parameters, language);
            
            // Get detection 
            
            AccessEnforcement_Detection detection = Shared.controllerDetections.getDetection(Long.parseLong((String)parameters.getParameterValue("number")), false);
            AccessEnforcement_Detection_State detectionState = (AccessEnforcement_Detection_State)Serialization.deserialize(AccessEnforcement_Detection_State.class, (String)parameters.getParameterValue("body-content"));
 
            // Permissions
 
            UserPermission permission = Shared.controllerUserPermissions.getUserPermission(authentication.profile, authentication.username);
 
            if ((permission.hasPermissionsDevice(detection.device.getIdentifier()) == false) || (permission.hasPermissionsParameters(Shared.getApplicationName(), parameters) == false))
            {
                throw new SerializationException(Shared.getMessage(language, "Not allowed"));
            }
            
            byte[] selectedImage = parameters.getBodyContent();
            return (new InteropResponse(Shared.controllerDetections.setDetection(authentication, detection, detectionState, selectedImage, language)));
            
        }
        catch (ServerException exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()));
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
        finally
        {
            mutex.releaseWrite();
        }
    }
    
    
    public InteropResponse getExport(InteropParameters parameters) throws SerializationException
    {
        String language = (String) parameters.getParameterValue("language");
 
        try
        {
            HttpAuthentication authentication = getHttpAuthentication(parameters, language);
 
            Device device = Shared.model.getDevice(parameters.getParameterValue("device"));
            Long timestamp = Long.parseLong(parameters.getParameterValue("timestamp"));
 
            // Permissions
            UserPermission permission = Shared.controllerUserPermissions.getUserPermission(authentication.profile, authentication.username);
 
            if ((permission.hasPermissionsDevice(device.getIdentifier()) == false) || (permission.hasPermissionsParameters(Shared.getApplicationName(), parameters) == false))
            {
                throw new SerializationException(Shared.getMessage(language, "Not allowed"));
            }
            
            Calendar from = Calendar.getInstance();
            {
                from.setTimeInMillis(timestamp);
                from.set(Calendar.HOUR_OF_DAY, 0);
                from.set(Calendar.MINUTE, 0);
                from.set(Calendar.SECOND, 0);
                from.set(Calendar.MILLISECOND, 0);
            }
            
            Calendar to = Calendar.getInstance();
            {
                to.setTimeInMillis(from.getTimeInMillis());
                to.set(Calendar.HOUR_OF_DAY, 23);
                to.set(Calendar.MINUTE, 59);
                to.set(Calendar.SECOND, 59);
                to.set(Calendar.MILLISECOND, 999);
            }
            
            Controller_Detections_Coruna controllerDetections = (Controller_Detections_Coruna)Shared.controllerDetections;
            
            AccessEnforcement_Detection[] detections = controllerDetections.getDetections(authentication, language, device, from.getTimeInMillis(), to.getTimeInMillis());
            
            List<Object> responseData = new ArrayList();
            
            SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
            
            for(AccessEnforcement_Detection detection : detections)
            {
                InteropParameters cloneParameters = Serialization.clone(parameters);
                {
                    cloneParameters.addParameter("number", String.valueOf(detection.number));
                    cloneParameters.addParameter("format", "xlsx");
                }
                
                InteropResponse response = printDetection(cloneParameters);
                {
                    response.name = detection.device.name + " " + detection.getLastState().violation.record + " " + detection.number + " " + format.format(detection.timestamp) + ".xlsx";
                 }
                responseData.add(response);
            }
            
            if(Shared.controllerDetections instanceof Controller_Detections_ZPR)
                responseData.add(Controller_Detections_ZPR.class.cast(Shared.controllerDetections).getDEN(detections));
            else if(Shared.controllerDetections instanceof Controller_Detections_VPV)
                responseData.add(Controller_Detections_VPV.class.cast(Shared.controllerDetections).getDEN(detections));
 
            InteropResponse response = new InteropResponse(responseData);
            
            controllerDetections.addExport(device, from.getTimeInMillis());
 
            return response;
        } 
       catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }
    
    public InteropResponse listExports(InteropParameters parameters) throws SerializationException
    {
        String language = (String) parameters.getParameterValue("language");
 
        try
        {
            HttpAuthentication authentication = getHttpAuthentication(parameters, language);
 
            Device device = Shared.model.getDevice(parameters.getParameterValue("device"));
            int year = Integer.parseInt(parameters.getParameterValue("year"));
 
            // Permissions
            UserPermission permission = Shared.controllerUserPermissions.getUserPermission(authentication.profile, authentication.username);
 
            if ((permission.hasPermissionsDevice(device.getIdentifier()) == false) || (permission.hasPermissionsParameters(Shared.getApplicationName(), parameters) == false))
            {
                throw new SerializationException(Shared.getMessage(language, "Not allowed"));
            }
 
            Long[] exports = Controller_Detections_Coruna.class.cast(Shared.controllerDetections).listExports(device, year);
            
            InteropResponse response = new InteropResponse(exports);
            
            return response;
        } 
       catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }
 
    public InteropResponse printDetection(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
        String format = (String)parameters.getParameterValue("format");
 
        try
        {
            HttpAuthentication authentication = getHttpAuthentication(parameters, language);
            
            // Get detection 
            
            AccessEnforcement_Detection detection = Shared.controllerDetections.getDetection(Long.parseLong((String)parameters.getParameterValue("number")), true);
 
           
            // Permissions
 
            UserPermission permission = Shared.controllerUserPermissions.getUserPermission(authentication.profile, authentication.username);
 
            if ((permission.hasPermissionsDevice(detection.device.getIdentifier()) == false) || (permission.hasPermissionsParameters(Shared.getApplicationName(), parameters) == false))
            {
                throw new SerializationException(Shared.getMessage(language, "Not allowed"));
            }
            
            ReportAccess report = null;
            
             if(detection.device.type.equals("VPV") || detection.device.installation.equals("VPV"))
             {
                 report = new ReportAccessVPV(language, format, detection);
             }
             else  if(detection.device.type.equals("ZPR") || detection.device.installation.equals("ZPR"))
             {
                 report = new ReportAccessZPR(language, format, detection);
             }
             else
             {
                 report = new ReportAccess(language, format, detection);
             }
             
            byte[] data = report.generate();
            
            InteropResponse response = new InteropResponse(data);
            if (format.equalsIgnoreCase("xlsx")) response.mime = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            if (format.equalsIgnoreCase("pdf")) response.mime = "application/pdf";
            response.name = getDetectionName(detection) + "." + format;
            report.close();
            return response;
        }
        catch (ServerException exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()));
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }
 
 
    public InteropResponse getPermission(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
 
        try
        {
            HttpAuthentication authentication = getHttpAuthentication(parameters, language);
            
            long timestamp = getTimestampSeconds((String)parameters.getParameterValue("timestamp"));
            String plate = ((String)parameters.getParameterValue("plate")).toUpperCase();
            String device = (String)parameters.getParameterValue("device");
            
            // Permissions
 
            UserPermission permission = Shared.controllerUserPermissions.getUserPermission(authentication.profile, authentication.username);
 
            if ((permission.hasPermissionsDevice(device) == false) || (permission.hasPermissionsParameters(Shared.getApplicationName(), parameters) == false))
            {
                throw new SerializationException(Shared.getMessage(language, "Not allowed"));
            }
            
            if (Shared.isKielce == true)
            {
                return (new InteropResponse(new Boolean(true), true));    
            }
            else
            {
                return (new InteropResponse(new Boolean(Shared.controllerDetections.hasPermission(timestamp, device, plate.replaceAll("\\s+","")) != null), true));    
            }
        }
        catch (ServerException exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()));
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }
 
        
 
    
    
    
    
    public InteropResponse getVehicleATEX5(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
 
        try
        {
            HttpAuthentication authentication = getHttpAuthentication(parameters, language);
            
            ATEX5 atex5 = new ATEX5();
            String plate = (String)parameters.getParameterValue("plate");
            
            if (parameters.hasParameter("timestamp"))
            {
                long timestamp = Long.parseLong((String)parameters.getParameterValue("timestamp"));
                
                VehicleATEX5 vehicle = atex5.solicitudConsultaVehiculoAtex(plate);
                
                if ((vehicle.datosTramites != null) && (vehicle.datosTramites.listaTransferencias != null))
                {
                    for (int i=(vehicle.datosTramites.listaTransferencias.transferencia.size()-1); i>=0; i--)
                    {
                        long transferenciaTimestamp = vehicle.datosTramites.listaTransferencias.transferencia.get(i).getFechaTransferencia();
 
                        if (timestamp < transferenciaTimestamp)
                        {
                            String doi = vehicle.datosTramites.listaTransferencias.transferencia.get(i).idDocumentoAnterior;
                            PersonATEX5 person = atex5.solicitudConsultaPersonaAtex(doi);  
                            vehicle.datosGenerales.titular.datosPersona = person.datosPersona;
                            vehicle.datosGenerales.titular.domicilio = person.datosGenerales.domicilio;
                            vehicle.datosGenerales.titular.domicilioIne = person.datosGenerales.domicilioIne;
                            vehicle.datosGenerales.titular.hasta = transferenciaTimestamp;
                            if (i < (vehicle.datosTramites.listaTransferencias.transferencia.size()-1))
                            {
                                vehicle.datosGenerales.titular.desde = vehicle.datosTramites.listaTransferencias.transferencia.get(i+1).getFechaTransferencia();
                            }
    
                            break;
                        }
                    }
                }
                
                InteropResponse response = new InteropResponse(vehicle, "application/json", false);
                return response;
            }
            else
            {
                VehicleATEX5 vehicle = atex5.solicitudConsultaVehiculoAtex(plate);
                InteropResponse response = new InteropResponse(vehicle, "application/json", false);
                return response;
            }
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }
    
    
    
    
    
 
    public InteropResponse getDiscardReasons(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
 
        try
        {
            HttpAuthentication authentication = getHttpAuthentication(parameters, language);
            
            List<AccessEnforcement_Detection_State_Discarded> reasons = AccessEnforcement_Detection_State_Discarded.getDiscardings();
            
            for (AccessEnforcement_Detection_State_Discarded reason : reasons)
            {
                reason.description = Shared.getMessage(language, reason.description);
            }
            
            return (new InteropResponse(reasons));
        }
        catch (Exception exception)
        {
            throw new SerializationException(Shared.getMessage(language, exception.getMessage()), exception);
        }
    }
    
    
 
    
    // <editor-fold defaultstate="collapsed" desc="Private">
 
    
    private HttpAuthentication getHttpAuthentication (InteropParameters parameters, String language) throws SerializationException
    {
        HttpAuthentication authentication = super.getHttpAuthentication(parameters);
 
        UserPermission permission = Shared.controllerUserPermissions.getUserPermission(authentication.profile, authentication.username);
 
        if (permission.hasPermissionsParameters(Shared.getApplicationName(), parameters) == false)
        {
            throw new SerializationException(Shared.getMessage(language, "Not allowed"));
        }
        
        return authentication;
    }
    
 
 
    
    private String getDetectionName(AccessEnforcement_Detection detection)
    {
        // Records
        
        try
        {
            AccessEnforcement_Detection_State detectionState = detection.getLastState();
            
            if (detectionState.processed != null) 
            {
                return detectionState.processed.record;
            }
            else if (detectionState.violation != null) 
            {
               return detectionState.violation.record;
            }
        }
        catch (Exception exception)
        {
        }
        
        return "" + detection.number;
    }
 
 
    
    public static long getTimestampSeconds(String parameter) throws Exception
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.parse(parameter).getTime();
    }
 
     
    
    
    
 
     // </editor-fold>  
         
   
}