package art.servers.gost.access.controller; import art.library.interop.serialization.Serialization; import art.library.model.devices.gost.access.AccessEnforcementInformation; 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.utils.licence.Licence; import art.library.utils.synchro.Mutex; import art.servers.ServerException; import art.servers.gost.access.Shared; import art.servers.gost.access.configuration.Configuration; import art.servers.gost.access.types.DatabasePoolConnection; import art.servers.gost.access.utils.ZIP; import art.servers.types.HttpAuthentication; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import org.postgresql.util.PGobject; public class Controller_Detections_VPV extends Controller_Detections_Coruna { private static SimpleDateFormat filter2 = new SimpleDateFormat("yyyy"); private static SimpleDateFormat filterDEN = new SimpleDateFormat("ddMMyyyyHHmmss"); private static Mutex mutex = new Mutex(); public Controller_Detections_VPV(DatabasePoolConnection database) { super(database); this.database = database; this.name = Shared.getMessage("Controller detections VPV"); this.setName(this.name); this.configuration = ((Configuration)Shared.configuration).detail; } public String[] getDEN(AccessEnforcement_Detection[] detections) throws Exception { List lRecords = new ArrayList(); try { for (AccessEnforcement_Detection detection : detections) { String result = "CORUÑA "; result += detection.getLastState().violation.record; result += filterDEN.format(new Date(detection.timestamp)); result += " " + detection.device.vpv.legislationCode; result += "5" + detection.getLastState().violation.key; result += " C"; result += " "; result += detection.getLastState().violation.regulation; for (int i = detection.getLastState().violation.regulation.length(); i < 340; i++) { result += " "; } result += "N"; result += "7 "; for (int i = detection.device.vpv.streetCode.length(); i < 5; i++) { result += "0"; } result += detection.device.vpv.streetCode; result += detection.device.vpv.streetAcronyms; for (int i = detection.device.vpv.streetAcronyms.length(); i < 5; i++) { result += " "; } result += detection.device.vpv.streetName; for (int i = detection.device.vpv.streetName.length(); i < 50; i++) { result += " "; } for (int i = detection.device.vpv.streetNumber.length(); i < 4; i++) { result += "0"; } result += detection.device.vpv.streetNumber; result += " "; result += detection.device.vpv.placeInformation; for (int i = detection.device.vpv.placeInformation.length(); i < 100; i++) { result += " "; } result += whiteSpaces(60); result += whiteSpaces(2); result += whiteSpaces(15); result += whiteSpaces(1); result += whiteSpaces(1); result += whiteSpaces(5); result += whiteSpaces(5); result += whiteSpaces(50); result += whiteSpaces(4); result += whiteSpaces(1); result += whiteSpaces(4); result += whiteSpaces(1); result += whiteSpaces(1); result += whiteSpaces(6); result += whiteSpaces(1); result += whiteSpaces(4); result += whiteSpaces(25); result += whiteSpaces(2); result += whiteSpaces(3); result += whiteSpaces(4); result += whiteSpaces(5); result += whiteSpaces(3); result += whiteSpaces(60); result += whiteSpaces(2); result += whiteSpaces(60); result += whiteSpaces(3); result += whiteSpaces(60); result += whiteSpaces(15); result += whiteSpaces(50); result += whiteSpaces(8); result += whiteSpaces(2); result += whiteSpaces(4); result += whiteSpaces(1); result += detection.getLastState().validation.policeLicense; for (int i = detection.getLastState().validation.policeLicense.length(); i < 8; i++) { result += " "; } result += whiteSpaces(8); result += whiteSpaces(6); result += whiteSpaces(6); result += "1"; result += getPlate(detection.getLastState().vehicle.plate); result += "1 "; result += whiteSpaces(20); result += whiteSpaces(20); result += whiteSpaces(6); result += whiteSpaces(6); result += whiteSpaces(100); result += filter2.format(new Date(detection.timestamp)); result += whiteSpaces(12); result += whiteSpaces(3); result += whiteSpaces(2); result += whiteSpaces(8); result += whiteSpaces(11); result += whiteSpaces(12); result += whiteSpaces(3); result += whiteSpaces(2); result += whiteSpaces(8); result += whiteSpaces(11); lRecords.add(result); } return(lRecords.toArray(new String[0])); } catch (Exception e) { throw e; } } public boolean setDetection(HttpAuthentication authentication, AccessEnforcement_Detection detection, AccessEnforcement_Detection_State nextDetectionState, byte[] selectedImage, String language) throws ServerException, Exception { Connection connection = null; PreparedStatement statement = null; // Update detection try { connection = database.getConnection(true); AccessEnforcementInformation information = getAccessEnforcementInformation(detection.device.getIdentifier()); AccessEnforcement_Detection_State currentDetectionState = detection.getLastState(); detection.images = null; if (detection == null) throw new Exception(Shared.getMessage(language, "Invalid detection")); // Check if state change is allowed if (Serialization.equals(currentDetectionState, nextDetectionState) == false) { boolean correct = ( (currentDetectionState.state == AccessEnforcement_Detection_State.STATE_REVISION_PENDING) && ((nextDetectionState.state == AccessEnforcement_Detection_State.STATE_VIOLATION_PROCESSED)) || (currentDetectionState.state == AccessEnforcement_Detection_State.STATE_REVISION_PENDING) && ((nextDetectionState.state == AccessEnforcement_Detection_State.STATE_DISCARDED)) || (currentDetectionState.state == AccessEnforcement_Detection_State.STATE_VIOLATION_PROCESSED) && ((nextDetectionState.state == AccessEnforcement_Detection_State.STATE_DISCARDED)) || (currentDetectionState.state == AccessEnforcement_Detection_State.STATE_VIOLATION_PROCESSED) && ((nextDetectionState.state == AccessEnforcement_Detection_State.STATE_VIOLATION_PROCESSED)) || (currentDetectionState.state == AccessEnforcement_Detection_State.STATE_DISCARDED) && ((nextDetectionState.state == AccessEnforcement_Detection_State.STATE_DISCARDED)) || (currentDetectionState.state == AccessEnforcement_Detection_State.STATE_DISCARDED) && ((nextDetectionState.state == AccessEnforcement_Detection_State.STATE_VIOLATION_PROCESSED)) ); if (correct == false) throw new ServerException(Shared.getMessage(language, "Invalid state")); if (authentication.username == null) throw new ServerException(Shared.getMessage(language, "Invalid username")); } else { return true; } // STATE_REVISION_PENDING -> STATE_REVISED if (nextDetectionState.state == AccessEnforcement_Detection_State.STATE_REVISED) { if (nextDetectionState.revision == null) { throw new ServerException(Shared.getMessage(language, "Invalid state")); } nextDetectionState.revision.user = authentication.username; nextDetectionState.revision.timestamp = System.currentTimeMillis(); } // STATE_REVISED -> STATE_VALIDATED if (nextDetectionState.state == AccessEnforcement_Detection_State.STATE_VALIDATED) { if (nextDetectionState.validation == null) { throw new ServerException(Shared.getMessage(language, "Invalid state")); } if (nextDetectionState.validation.policeLicense == null) { throw new ServerException(Shared.getMessage(language, "Invalid police license")); } nextDetectionState.validation.user = authentication.username; nextDetectionState.validation.timestamp = System.currentTimeMillis(); } // STATE_REVISION_PENDING -> STATE_DISCARDED // STATE_REVISED -> STATE_DISCARDED if (nextDetectionState.state == AccessEnforcement_Detection_State.STATE_DISCARDED) { if (nextDetectionState.discarded == null) { throw new ServerException(Shared.getMessage(language, "Invalid reason")); } nextDetectionState.discarded.codeName = AccessEnforcement_Detection_State_Discarded.getDescription(nextDetectionState.discarded.code); nextDetectionState.discarded.user = authentication.username; nextDetectionState.discarded.timestamp = System.currentTimeMillis(); } detection.states.add(nextDetectionState); if(detection.getLastState().state == AccessEnforcement_Detection_State.STATE_VIOLATION_PROCESSED) { if(detection.getLastState().violation != null && detection.getLastState().violation.record == null) { detection.getLastState().violation.record = getRecordNumber(detection); } } // Update detection if (nextDetectionState.vehicle.plate != null) nextDetectionState.vehicle.plate = nextDetectionState.vehicle.plate.replaceAll("\\s+",""); if (detection.getLastState().violation != null && detection.getLastState().violation.record != null) { statement = connection.prepareStatement("UPDATE detections SET plate = ?, state = ?, value = ?, record = ? WHERE number = ?"); statement.setString(1, nextDetectionState.vehicle.getPlate()); statement.setShort(2, nextDetectionState.state); PGobject jsonObject = new PGobject(); jsonObject.setType("json"); jsonObject.setValue(Serialization.toString(detection)); statement.setObject(3, jsonObject); statement.setString(4, detection.getLastState().violation.record); statement.setLong(5, detection.number); } else { statement = connection.prepareStatement("UPDATE detections SET plate = ?, state = ?, value = ? WHERE number = ?"); statement.setString(1, nextDetectionState.vehicle.getPlate()); statement.setShort(2, nextDetectionState.state); PGobject jsonObject = new PGobject(); jsonObject.setType("json"); jsonObject.setValue(Serialization.toString(detection)); statement.setObject(3, jsonObject); statement.setLong(4, detection.number); } statement.executeUpdate(); Shared.traceInformation(this.name, Shared.getMessage("Set detection"), statement.toString(), authentication, language); // Change selected image if ((selectedImage != null) && (selectedImage.length > 0)) { addImages(detection); if (detection.setImage("selected", selectedImage) == true) { // Save images ZIP.zip(detection, Licence.decrypt(information.storage.storagePassword), information.storage.storageFolder); } } return true; } catch (ServerException exception) { Shared.traceError(this.name, Shared.getMessage("Set detection"), exception); throw exception; } catch (Exception exception) { Shared.traceError(this.name, Shared.getMessage("Set detection"), exception); throw exception; } finally { database.releaseConnection(connection, statement); } } private String getRecordNumber(AccessEnforcement_Detection detection) throws Exception { mutex.lockWrite(); Connection connection = null; PreparedStatement statement = null; ResultSet resultset = null; try { Calendar cal = Calendar.getInstance(); { cal.setTimeInMillis(detection.timestamp); } connection = database.getConnection(false); statement = connection.prepareStatement("SELECT number FROM records WHERE type = ? AND year = ?"); statement.setString(1, "VPV"); statement.setInt(2, cal.get(Calendar.YEAR)); resultset = statement.executeQuery(); int recordNumber = 1; if (resultset.next() == true) { recordNumber = resultset.getInt(1) + 1; statement = connection.prepareStatement("UPDATE records SET number = ? WHERE type = ? AND year = ?"); statement.setInt(1, recordNumber); statement.setString(2, "VPV"); statement.setInt(3, cal.get(Calendar.YEAR)); statement.executeUpdate(); } else { statement = connection.prepareStatement("INSERT INTO records (type,year,number) VALUES (?,?,?)"); statement.setInt(1, recordNumber); statement.setString(2, "VPV"); statement.setInt(3, cal.get(Calendar.YEAR)); statement.executeUpdate(); } return cal.get(Calendar.YEAR) + "-C-" + String.format("%08d", recordNumber); } catch (Exception exception) { database.rollback(connection); Shared.traceError(this.name, Shared.getMessage("Set detection"), exception); throw exception; } finally { database.commitConnection(connection, statement, resultset); mutex.releaseWrite(); } } // private static String whiteSpaces(int spaces) { String result = ""; for (int i = 0; i < spaces; i++) { result += " "; } return (result); } private static String getPlate (String matricula) { String result = ""; if ((matricula.charAt(0) >= 48) && (matricula.charAt(0) <= 57)) { result += " -" + matricula.substring(0,4); result += " -" + matricula.substring(4) + " "; } else { int posicionNumero = getNumberPosition(matricula); String provincia = matricula.substring(0, posicionNumero); result += provincia; for (int i=provincia.length(); i<2; i++) result += " "; result += "-" + matricula.substring(posicionNumero, posicionNumero+4); String chars = matricula.substring(posicionNumero+4); result += " -" + chars; for (int i=chars.length(); i<4; i++) result += " "; } return(result); } private static int getNumberPosition (String matricula) { for (int i=0; i= 48) && (matricula.charAt(i) <= 57)) return(i); } return(-1); } // }