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.information.AccessEnforcementInformation_ZPR;
|
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_ZPR 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_ZPR(DatabasePoolConnection database)
|
{
|
super(database);
|
this.database = database;
|
this.name = Shared.getMessage("Controller detections ZPR");
|
this.setName(this.name);
|
this.configuration = ((Configuration)Shared.configuration).detail;
|
}
|
|
public String[] getDEN(AccessEnforcement_Detection[] detections) throws Exception
|
{
|
List<String> lRecords = new ArrayList();
|
|
try
|
{
|
for (AccessEnforcement_Detection detection : detections)
|
{
|
//MOCK REMOVEME
|
detection.device.zpr = new AccessEnforcementInformation_ZPR();
|
{
|
detection.device.zpr.legislationCode = "MOCK";
|
detection.device.zpr.placeInformation = "MOCK";
|
detection.device.zpr.streetAcronyms = "MOCK";
|
detection.device.zpr.streetCode = "MOCK";
|
detection.device.zpr.streetName = "MOCK";
|
detection.device.zpr.streetNumber = "MOCK";
|
}
|
|
if(detection.getLastState().violation.regulation == null)
|
detection.getLastState().violation.regulation = "NO OBECER SEÑAL DE BLABLABLA";
|
//
|
|
|
String result = "CORUÑA ";
|
result += detection.getLastState().violation.record;
|
result += filterDEN.format(new Date(detection.timestamp));
|
result += " " + detection.device.zpr.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.zpr.streetCode.length(); i < 5; i++)
|
{
|
result += "0";
|
}
|
result += detection.device.zpr.streetCode;
|
result += detection.device.zpr.streetAcronyms;
|
for (int i = detection.device.zpr.streetAcronyms.length(); i < 5; i++)
|
{
|
result += " ";
|
}
|
result += detection.device.zpr.streetName;
|
for (int i = detection.device.zpr.streetName.length(); i < 50; i++)
|
{
|
result += " ";
|
}
|
for (int i = detection.device.zpr.streetNumber.length(); i < 4; i++)
|
{
|
result += "0";
|
}
|
result += detection.device.zpr.streetNumber;
|
result += " ";
|
result += detection.device.zpr.placeInformation;
|
for (int i = detection.device.zpr.streetNumber.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; //TODO POLICE LICENSE??
|
for (int i = detection.getLastState().validation.policeLicense.length(); i < 8; i++) //POLICE LICENSE???
|
{
|
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;
|
|
Exception throwException = 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_VIOLATION_PROCESSED
|
|
if (nextDetectionState.state == AccessEnforcement_Detection_State.STATE_VIOLATION_PROCESSED)
|
{
|
if (isDuplicatedEvidence(detection))
|
{
|
nextDetectionState.state = AccessEnforcement_Detection_State.STATE_DISCARDED;
|
nextDetectionState.violation = null;
|
nextDetectionState.discarded = new AccessEnforcement_Detection_State_Discarded();
|
{
|
nextDetectionState.discarded.code = AccessEnforcement_Detection_State_Discarded.DISCARDED_DUPLICATED_EVIDENCE;
|
}
|
|
throwException = new ServerException(Shared.getMessage(language, "Evidence discarded, this vehicle has another infraction processed for current day"));
|
}
|
}
|
|
|
// 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);
|
}
|
}
|
|
if(throwException != null)
|
throw throwException;
|
|
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 boolean isDuplicatedEvidence(AccessEnforcement_Detection detection) throws ServerException
|
{
|
try
|
{
|
Connection connection = null;
|
PreparedStatement statement = null;
|
ResultSet resultset = null;
|
|
try
|
{
|
connection = database.getConnection(true);
|
|
Calendar calFrom = Calendar.getInstance();
|
{
|
calFrom.setTimeInMillis(detection.timestamp);
|
calFrom.set(Calendar.HOUR_OF_DAY, 0);
|
calFrom.set(Calendar.MINUTE, 0);
|
calFrom.set(Calendar.SECOND, 0);
|
calFrom.set(Calendar.MILLISECOND, 0);
|
}
|
|
Calendar calTo = Calendar.getInstance();
|
{
|
calTo.setTimeInMillis(calFrom.getTimeInMillis());
|
calTo.set(Calendar.HOUR_OF_DAY, 23);
|
calTo.set(Calendar.MINUTE, 59);
|
calTo.set(Calendar.SECOND, 59);
|
calTo.set(Calendar.MILLISECOND, 999);
|
}
|
|
statement = connection.prepareStatement("SELECT value FROM detections WHERE number != ? AND plate = ? AND state = ? AND datetime >= ? AND datetime <= ?");
|
{
|
statement.setLong(1, detection.number);
|
statement.setString(2, detection.getLastState().vehicle.plate.toUpperCase());
|
statement.setInt(3, AccessEnforcement_Detection_State.STATE_VIOLATION_PROCESSED);
|
statement.setTimestamp(4, new java.sql.Timestamp(calFrom.getTimeInMillis()));
|
statement.setTimestamp(5, new java.sql.Timestamp(calTo.getTimeInMillis()));
|
}
|
|
resultset = statement.executeQuery();
|
|
return resultset.next();
|
}
|
catch (Exception e)
|
{
|
throw new ServerException(e.toString());
|
}
|
finally
|
{
|
database.releaseConnection(connection, statement, resultset);
|
}
|
}
|
catch (Exception e)
|
{
|
throw new ServerException(e.toString());
|
}
|
}
|
|
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, "ZPR");
|
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, "ZPR");
|
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, "ZPR");
|
statement.setInt(3, cal.get(Calendar.YEAR));
|
|
statement.executeUpdate();
|
}
|
|
return cal.get(Calendar.YEAR) + "-P-" + 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();
|
}
|
}
|
|
//<editor-fold defaultstate="collapsed" desc="Private functions">
|
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<matricula.length(); i++)
|
{
|
if ((matricula.charAt(i) >= 48) && (matricula.charAt(i) <= 57)) return(i);
|
}
|
|
return(-1);
|
}
|
//</editor-fold>
|
|
}
|