diff --git a/src/org/kar/archidata/dataAccess/options/CheckJPA.java b/src/org/kar/archidata/dataAccess/options/CheckJPA.java index 5363175..a5c7dc3 100644 --- a/src/org/kar/archidata/dataAccess/options/CheckJPA.java +++ b/src/org/kar/archidata/dataAccess/options/CheckJPA.java @@ -465,6 +465,22 @@ public class CheckJPA implements CheckFunctionInterface { } else if (type.isEnum()) { // nothing to do. } + final DataJson dataJson = AnnotationTools.getDataJson(field); + if (dataJson != null && dataJson.checker() != null) { + final CheckFunctionInterface checkerInstance = dataJson.checker().getDeclaredConstructor() + .newInstance(); + add(fieldName, + ( + final DBAccess ioDb, + final String baseName, + final T data, + final List modifiedValue, + final QueryOptions options) -> { + // get the field of the specific element + final Object tmpData = field.get(data); + checkerInstance.check(ioDb, baseName, tmpData, null, options); + }); + } // keep this is last ==> take more time... if (AnnotationTools.isUnique(field)) { // Create the request ... @@ -498,13 +514,21 @@ public class CheckJPA implements CheckFunctionInterface { } } + public void check(final Object data) throws Exception { + check(null, "", data, null, null); + } + + public void check(final String baseName, final Object data) throws Exception { + check(null, baseName, data, null, null); + } + public void check(final DBAccess ioDb, final String baseName, final Object data) throws Exception { check(ioDb, baseName, data, null, null); } - public void check(final DBAccess ioDb, final String baseName, final Object data, final List filterValue) + public void check(final DBAccess ioDb, final String baseName, final Object data, final List modifiedValue) throws Exception { - check(ioDb, baseName, data, filterValue, null); + check(ioDb, baseName, data, modifiedValue, null); } @Override @@ -512,11 +536,14 @@ public class CheckJPA implements CheckFunctionInterface { final DBAccess ioDb, final String baseName, final Object data, - final List modifiedValue, + List modifiedValue, final QueryOptions options) throws Exception { if (this.checking == null) { initialize(); } + if (modifiedValue == null) { + modifiedValue = AnnotationTools.getAllFieldsNames(this.clazz); + } if (!(this.clazz.isAssignableFrom(data.getClass()))) { throw new DataAccessException("Incompatatyble type of Object" + data.getClass().getCanonicalName()); } diff --git a/test/src/test/kar/archidata/dataAccess/TestDataJson.java b/test/src/test/kar/archidata/dataAccess/TestDataJson.java new file mode 100644 index 0000000..bb6750a --- /dev/null +++ b/test/src/test/kar/archidata/dataAccess/TestDataJson.java @@ -0,0 +1,44 @@ +package test.kar.archidata.dataAccess; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.kar.archidata.exception.InputException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import test.kar.archidata.StepwiseExtension; +import test.kar.archidata.dataAccess.model.DataInJson; +import test.kar.archidata.dataAccess.model.DataWithSubJson; +import test.kar.archidata.dataAccess.model.DataWithSubJson.DataWithSubJsonChecker; + +@ExtendWith(StepwiseExtension.class) +public class TestDataJson { + final static private Logger LOGGER = LoggerFactory.getLogger(TestDataJson.class); + + @Test + public void testCheckerDoesNotThrow() throws Exception { + final DataWithSubJson data = new DataWithSubJson(); + data.dataSerialized = new DataInJson(); + data.dataSerialized.data = "65454"; + final DataWithSubJsonChecker checker = new DataWithSubJsonChecker(); + Assertions.assertDoesNotThrow(() -> { + checker.check(null, "", data); + }); + } + + @Test + public void testCheckerDoesThrow() throws Exception { + final DataWithSubJson data = new DataWithSubJson(); + data.dataSerialized = new DataInJson(); + data.dataSerialized.data = "lqksjdflkjqsdf"; + final DataWithSubJsonChecker checker = new DataWithSubJsonChecker(); + final InputException res = Assertions.assertThrows(InputException.class, () -> { + checker.check(null, "", data); + }); + Assertions.assertEquals(res.getMessage(), + "does not match the required pattern (constraints) must be '^[0-9]+$'"); + + } + +} diff --git a/test/src/test/kar/archidata/dataAccess/model/DataInJson.java b/test/src/test/kar/archidata/dataAccess/model/DataInJson.java new file mode 100644 index 0000000..f300452 --- /dev/null +++ b/test/src/test/kar/archidata/dataAccess/model/DataInJson.java @@ -0,0 +1,20 @@ +package test.kar.archidata.dataAccess.model; + +import org.kar.archidata.dataAccess.options.CheckJPA; + +import jakarta.validation.constraints.Pattern; +import jakarta.validation.constraints.Size; + +public class DataInJson { + // Simple checker declaration + public static class DataInJsonChecker extends CheckJPA { + public DataInJsonChecker() { + super(DataInJson.class); + } + } + + // Simple data to verify if the checker is active + @Size(min = 3, max = 128) + @Pattern(regexp = "^[0-9]+$") + public String data; +} diff --git a/test/src/test/kar/archidata/dataAccess/model/DataWithSubJson.java b/test/src/test/kar/archidata/dataAccess/model/DataWithSubJson.java new file mode 100644 index 0000000..95b7070 --- /dev/null +++ b/test/src/test/kar/archidata/dataAccess/model/DataWithSubJson.java @@ -0,0 +1,16 @@ +package test.kar.archidata.dataAccess.model; + +import org.kar.archidata.annotation.DataJson; +import org.kar.archidata.dataAccess.options.CheckJPA; + +public class DataWithSubJson { + // Simple checker declaration + public static class DataWithSubJsonChecker extends CheckJPA { + public DataWithSubJsonChecker() { + super(DataWithSubJson.class); + } + } + + @DataJson(checker = DataInJson.DataInJsonChecker.class) + public DataInJson dataSerialized; +}