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 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 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); } } // 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(); } // }