[DEV] rework to finalize API
This commit is contained in:
parent
5fc45a23d3
commit
d9fcacc812
@ -15,6 +15,9 @@ import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Pattern;
|
||||
import jakarta.validation.constraints.Size;
|
||||
|
||||
public class AnnotationTools {
|
||||
static final Logger LOGGER = LoggerFactory.getLogger(AnnotationTools.class);
|
||||
@ -79,6 +82,28 @@ public class AnnotationTools {
|
||||
return length <= 0 ? null : length;
|
||||
}
|
||||
|
||||
public static Size getConstraintsSize(final Field element) throws Exception {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Size.class);
|
||||
if (annotation.length == 0) {
|
||||
return null;
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception("Must not have more than 1 element @Size on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
return (Size) annotation[0];
|
||||
}
|
||||
|
||||
public static String getConstraintsPattern(final Field element) throws Exception {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Pattern.class);
|
||||
if (annotation.length == 0) {
|
||||
return null;
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception("Must not have more than 1 element @Pattern on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
return ((Pattern) annotation[0]).regexp();
|
||||
}
|
||||
|
||||
public static boolean isAnnotationGroup(final Field field, final Class<?> annotationType) {
|
||||
try {
|
||||
final Annotation[] anns = field.getAnnotations();
|
||||
@ -117,7 +142,7 @@ public class AnnotationTools {
|
||||
return name;
|
||||
}
|
||||
|
||||
public static boolean getNotNull(final Field element) throws Exception {
|
||||
public static boolean getColumnNotNull(final Field element) throws Exception {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Column.class);
|
||||
if (annotation.length == 0) {
|
||||
return false;
|
||||
@ -128,6 +153,17 @@ public class AnnotationTools {
|
||||
return !((Column) annotation[0]).nullable();
|
||||
}
|
||||
|
||||
public static boolean getConstraintsNotNull(final Field element) throws Exception {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(NotNull.class);
|
||||
if (annotation.length == 0) {
|
||||
return false;
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception("Must not have more than 1 element @NotNull on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isPrimaryKey(final Field element) throws Exception {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Id.class);
|
||||
if (annotation.length == 0) {
|
||||
@ -135,6 +171,7 @@ public class AnnotationTools {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isUnique(final Field element) throws Exception {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Column.class);
|
||||
if (annotation.length == 0) {
|
||||
|
@ -10,17 +10,17 @@ import jakarta.ws.rs.ext.ExceptionMapper;
|
||||
|
||||
public class InputExceptionCatcher implements ExceptionMapper<InputException> {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(InputExceptionCatcher.class);
|
||||
|
||||
|
||||
@Override
|
||||
public Response toResponse(final InputException exception) {
|
||||
final RestErrorResponse ret = build(exception);
|
||||
LOGGER.error("Error UUID={} ==> '{}'=>'{}'", ret.uuid, exception.missingVariable, exception.getLocalizedMessage());
|
||||
//exception.printStackTrace();
|
||||
// exception.printStackTrace();
|
||||
return Response.status(exception.status).entity(ret).type(MediaType.APPLICATION_JSON).build();
|
||||
}
|
||||
|
||||
|
||||
private RestErrorResponse build(final InputException exception) {
|
||||
return new RestErrorResponse(exception.status, "Error on input='" + exception.missingVariable + "'", exception.getMessage());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ import org.kar.archidata.dataAccess.addOn.AddOnManyToOne;
|
||||
import org.kar.archidata.dataAccess.addOn.AddOnSQLTableExternalForeinKeyAsList;
|
||||
import org.kar.archidata.dataAccess.options.AccessDeletedItems;
|
||||
import org.kar.archidata.dataAccess.options.CheckFunction;
|
||||
import org.kar.archidata.dataAccess.options.Limit;
|
||||
import org.kar.archidata.dataAccess.options.Condition;
|
||||
import org.kar.archidata.db.DBEntry;
|
||||
import org.kar.archidata.exception.DataAccessException;
|
||||
import org.kar.archidata.tools.ConfigBaseVariable;
|
||||
@ -544,6 +544,10 @@ public class DataAccess {
|
||||
query.append("?");
|
||||
}
|
||||
query.append(")");
|
||||
final OrderBy orders = options.get(OrderBy.class);
|
||||
if (orders != null) {
|
||||
orders.generateQuerry(query, tableName);
|
||||
}
|
||||
LOGGER.warn("generate the query: '{}'", query.toString());
|
||||
// prepare the request:
|
||||
final PreparedStatement ps = entry.connection.prepareStatement(query.toString(), Statement.RETURN_GENERATED_KEYS);
|
||||
@ -628,7 +632,6 @@ public class DataAccess {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
// parse the object to be sure the data are valid:
|
||||
final T data = mapper.readValue(jsonData, clazz);
|
||||
|
||||
return insert(data);
|
||||
}
|
||||
|
||||
@ -746,6 +749,11 @@ public class DataAccess {
|
||||
query.append("` = ? ");
|
||||
}
|
||||
query.append(" ");
|
||||
final OrderBy orders = options.get(OrderBy.class);
|
||||
if (orders != null) {
|
||||
orders.generateQuerry(query, tableName);
|
||||
}
|
||||
query.append(" ");
|
||||
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
|
||||
whereAppendQuery(query, tableName, condition, null, deletedFieldName);
|
||||
firstField = true;
|
||||
@ -855,10 +863,13 @@ public class DataAccess {
|
||||
}
|
||||
}
|
||||
|
||||
public static void whereInjectValue(final PreparedStatement ps, final QueryItem condition, final CountInOut iii) throws Exception {
|
||||
public static void whereInjectValue(final PreparedStatement ps, final QueryOptions options, final CountInOut iii) throws Exception {
|
||||
// Check if we have a condition to generate
|
||||
if (condition != null) {
|
||||
condition.injectQuerry(ps, iii);
|
||||
if (options != null) {
|
||||
final Condition condition = options.get(Condition.class);
|
||||
if (condition != null) {
|
||||
condition.injectQuerry(ps, iii);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -882,16 +893,16 @@ public class DataAccess {
|
||||
return executeQuerry(query, false);
|
||||
}
|
||||
|
||||
public static <T> T getWhere(final Class<T> clazz, final QueryItem condition) throws Exception {
|
||||
return getWhere(clazz, condition, null);
|
||||
}
|
||||
|
||||
public static <T> T getWhere(final Class<T> clazz, final QueryItem condition, QueryOptions options) throws Exception {
|
||||
public static <T> T getWhere(final Class<T> clazz, QueryOptions options) throws Exception {
|
||||
return getWhere(clazz, options);
|
||||
if (options == null) {
|
||||
options = new QueryOptions();
|
||||
}
|
||||
options.add(new Limit(1));
|
||||
final List<T> values = getsWhere(clazz, condition, options);
|
||||
final Limit limit = options.get(Limit.class);
|
||||
if (limit != null) {
|
||||
options.add(new Limit(1));
|
||||
}
|
||||
final List<T> values = getsWhere(clazz, options);
|
||||
if (values.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
@ -899,11 +910,7 @@ public class DataAccess {
|
||||
}
|
||||
|
||||
public static <T> List<T> getsWhere(final Class<T> clazz, final QueryItem condition) throws Exception {
|
||||
return getsWhere(clazz, condition, null, null);
|
||||
}
|
||||
|
||||
public static <T> List<T> getsWhere(final Class<T> clazz, final QueryItem condition, final QueryOptions options) throws Exception {
|
||||
return getsWhere(clazz, condition, null, options);
|
||||
return getsWhere(clazz, condition, null);
|
||||
}
|
||||
|
||||
public static void generateSelectField(final StringBuilder querySelect, final StringBuilder query, final Class<?> clazz, final QueryOptions options, final CountInOut count) throws Exception {
|
||||
@ -943,7 +950,7 @@ public class DataAccess {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> List<T> getsWhere(final Class<T> clazz, final QueryItem condition, final String orderBy, final QueryOptions options) throws Exception {
|
||||
public static <T> List<T> getsWhere(final Class<T> clazz, final QueryItem condition, final QueryOptions options) throws Exception {
|
||||
final List<LazyGetter> lazyCall = new ArrayList<>();
|
||||
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
|
||||
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
|
||||
@ -963,22 +970,13 @@ public class DataAccess {
|
||||
querySelect.append(query.toString());
|
||||
query = querySelect;
|
||||
whereAppendQuery(query, tableName, condition, options, deletedFieldName);
|
||||
if (orderBy != null && orderBy.length() >= 1) {
|
||||
query.append(" ORDER BY ");
|
||||
query.append(orderBy);
|
||||
final OrderBy orders = options.get(OrderBy.class);
|
||||
if (orders != null) {
|
||||
orders.generateQuerry(query, tableName);
|
||||
}
|
||||
if (options != null) {
|
||||
final Limit limit = options.get(Limit.class);
|
||||
if (limit != null) {
|
||||
if (limit.getLimit() >= 1) {
|
||||
query.append(" LIMIT " + limit.getLimit());
|
||||
} else {
|
||||
LOGGER.warn("Limit is equal @ {}", limit.getLimit());
|
||||
entry.close();
|
||||
entry = null;
|
||||
return outs;
|
||||
}
|
||||
}
|
||||
final Limit limit = options.get(Limit.class);
|
||||
if (limit != null) {
|
||||
limit.generateQuerry(query, tableName);
|
||||
}
|
||||
LOGGER.warn("generate the query: '{}'", query.toString());
|
||||
// prepare the request:
|
||||
|
@ -37,8 +37,8 @@ public interface DataAccessAddOn {
|
||||
// Element can be retrieve with the specific mode
|
||||
boolean canRetrieve(final Field field);
|
||||
|
||||
void generateQuerry(@NotNull String tableName, @NotNull Field field, @NotNull final StringBuilder querrySelect, @NotNull final StringBuilder querry, @NotNull String name,
|
||||
@NotNull CountInOut count, QueryOptions options) throws Exception;
|
||||
void generateQuerry(@NotNull String tableName, @NotNull Field field, @NotNull final StringBuilder querySelect, @NotNull final StringBuilder query, @NotNull String name, @NotNull CountInOut count,
|
||||
QueryOptions options) throws Exception;
|
||||
|
||||
// Return the number of colomn read
|
||||
void fillFromQuerry(ResultSet rs, Field field, Object data, CountInOut count, QueryOptions options, final List<LazyGetter> lazyCall)
|
||||
|
@ -132,7 +132,7 @@ public class DataFactory {
|
||||
final boolean createIfNotExist, final boolean createDrop, final int fieldId, final Class<?> classModel) throws Exception {
|
||||
final String name = AnnotationTools.getFieldName(elem);
|
||||
final Integer limitSize = AnnotationTools.getLimitSize(elem);
|
||||
final boolean notNull = AnnotationTools.getNotNull(elem);
|
||||
final boolean notNull = AnnotationTools.getColumnNotNull(elem);
|
||||
|
||||
final boolean primaryKey = AnnotationTools.isPrimaryKey(elem);
|
||||
final GenerationType strategy = AnnotationTools.getStrategy(elem);
|
||||
|
20
src/org/kar/archidata/dataAccess/Limit.java
Normal file
20
src/org/kar/archidata/dataAccess/Limit.java
Normal file
@ -0,0 +1,20 @@
|
||||
package org.kar.archidata.dataAccess;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
|
||||
public class Limit extends QueryOption {
|
||||
protected final long limit;
|
||||
|
||||
public Limit(final long limit) {
|
||||
this.limit = limit;
|
||||
}
|
||||
|
||||
public void generateQuerry(final StringBuilder query, final String tableName) {
|
||||
query.append(" LIMIT ? ");
|
||||
}
|
||||
|
||||
public void injectQuerry(final PreparedStatement ps, final CountInOut iii) throws Exception {
|
||||
DataAccess.addElement(ps, this.limit, iii);
|
||||
iii.inc();
|
||||
}
|
||||
}
|
41
src/org/kar/archidata/dataAccess/OrderBy.java
Normal file
41
src/org/kar/archidata/dataAccess/OrderBy.java
Normal file
@ -0,0 +1,41 @@
|
||||
package org.kar.archidata.dataAccess;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.util.List;
|
||||
|
||||
public class OrderBy extends QueryOption {
|
||||
protected final List<OrderItem> childs;
|
||||
|
||||
public OrderBy(final List<OrderItem> childs) {
|
||||
this.childs = childs;
|
||||
}
|
||||
|
||||
public OrderBy(final OrderItem... childs) {
|
||||
this.childs = List.of(childs);
|
||||
}
|
||||
|
||||
public void generateQuerry(final StringBuilder query, final String tableName) {
|
||||
if (this.childs.size() >= 1) {
|
||||
query.append(" ORDER BY ");
|
||||
}
|
||||
boolean first = true;
|
||||
for (final OrderItem elem : this.childs) {
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
query.append(", ");
|
||||
}
|
||||
query.append("`");
|
||||
query.append(elem.value);
|
||||
query.append("` ");
|
||||
query.append(elem.order.toString());
|
||||
}
|
||||
if (this.childs.size() >= 1) {
|
||||
query.append(")");
|
||||
}
|
||||
}
|
||||
|
||||
public void injectQuerry(final PreparedStatement ps, final CountInOut iii) throws Exception {
|
||||
// nothing to add.
|
||||
}
|
||||
}
|
16
src/org/kar/archidata/dataAccess/OrderItem.java
Normal file
16
src/org/kar/archidata/dataAccess/OrderItem.java
Normal file
@ -0,0 +1,16 @@
|
||||
package org.kar.archidata.dataAccess;
|
||||
|
||||
public class OrderItem {
|
||||
public enum Order {
|
||||
ASC, DESC
|
||||
};
|
||||
|
||||
public final String value;
|
||||
public final Order order;
|
||||
|
||||
public OrderItem(final String value, final Order order) {
|
||||
this.value = value;
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
}
|
@ -18,21 +18,21 @@ public class QueryAnd implements QueryItem {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateQuerry(final StringBuilder querry, final String tableName) {
|
||||
public void generateQuerry(final StringBuilder query, final String tableName) {
|
||||
if (this.childs.size() >= 1) {
|
||||
querry.append(" (");
|
||||
query.append(" (");
|
||||
}
|
||||
boolean first = true;
|
||||
for (final QueryItem elem : this.childs) {
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
querry.append(" AND ");
|
||||
query.append(" AND ");
|
||||
}
|
||||
elem.generateQuerry(querry, tableName);
|
||||
elem.generateQuerry(query, tableName);
|
||||
}
|
||||
if (this.childs.size() >= 1) {
|
||||
querry.append(")");
|
||||
query.append(")");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,14 +14,13 @@ public class QueryCondition implements QueryItem {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateQuerry(final StringBuilder querry, final String tableName) {
|
||||
querry.append(tableName);
|
||||
querry.append(".");
|
||||
querry.append(this.key);
|
||||
querry.append(" ");
|
||||
querry.append(this.comparator);
|
||||
querry.append(" ?");
|
||||
|
||||
public void generateQuerry(final StringBuilder query, final String tableName) {
|
||||
query.append(tableName);
|
||||
query.append(".");
|
||||
query.append(this.key);
|
||||
query.append(" ");
|
||||
query.append(this.comparator);
|
||||
query.append(" ?");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -18,22 +18,26 @@ public class QueryInList<T> implements QueryItem {
|
||||
this(key, "IN", value);
|
||||
}
|
||||
|
||||
public QueryInList(final String key, final T... value) {
|
||||
this(key, "IN", List.of(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateQuerry(final StringBuilder querry, final String tableName) {
|
||||
querry.append(tableName);
|
||||
querry.append(".");
|
||||
querry.append(this.key);
|
||||
querry.append(" ");
|
||||
querry.append(this.comparator);
|
||||
querry.append(" (");
|
||||
public void generateQuerry(final StringBuilder query, final String tableName) {
|
||||
query.append(tableName);
|
||||
query.append(".");
|
||||
query.append(this.key);
|
||||
query.append(" ");
|
||||
query.append(this.comparator);
|
||||
query.append(" (");
|
||||
for (int iii = 0; iii < this.value.size(); iii++) {
|
||||
if (iii != 0) {
|
||||
querry.append(",?");
|
||||
query.append(",?");
|
||||
} else {
|
||||
querry.append("?");
|
||||
query.append("?");
|
||||
}
|
||||
}
|
||||
querry.append(")");
|
||||
query.append(")");
|
||||
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@ package org.kar.archidata.dataAccess;
|
||||
import java.sql.PreparedStatement;
|
||||
|
||||
public interface QueryItem {
|
||||
void generateQuerry(StringBuilder querry, String tableName);
|
||||
void generateQuerry(StringBuilder query, String tableName);
|
||||
|
||||
void injectQuerry(PreparedStatement ps, CountInOut iii) throws Exception;
|
||||
}
|
||||
|
@ -10,22 +10,26 @@ public class QueryOr implements QueryItem {
|
||||
this.childs = childs;
|
||||
}
|
||||
|
||||
public QueryOr(final QueryItem... childs) {
|
||||
this.childs = List.of(childs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateQuerry(final StringBuilder querry, final String tableName) {
|
||||
public void generateQuerry(final StringBuilder query, final String tableName) {
|
||||
if (this.childs.size() >= 1) {
|
||||
querry.append(" (");
|
||||
query.append(" (");
|
||||
}
|
||||
boolean first = true;
|
||||
for (final QueryItem elem : this.childs) {
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
querry.append(" OR ");
|
||||
query.append(" OR ");
|
||||
}
|
||||
elem.generateQuerry(querry, tableName);
|
||||
elem.generateQuerry(query, tableName);
|
||||
}
|
||||
if (this.childs.size() >= 1) {
|
||||
querry.append(")");
|
||||
query.append(")");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,6 +9,7 @@ import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.kar.archidata.annotation.AnnotationTools;
|
||||
import org.kar.archidata.dataAccess.DataAccess;
|
||||
@ -20,11 +21,13 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
import jakarta.validation.constraints.Size;
|
||||
|
||||
public class CheckJPA<T> implements CheckFunctionInterface {
|
||||
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CheckJPA.class);
|
||||
private final Class<?> clazz;
|
||||
|
||||
|
||||
/** By default some element are not read like createAt and UpdatedAt. This option permit to read it. */
|
||||
public interface CheckInterface<K> {
|
||||
/** This function implementation is design to check if the updated class is valid of not for insertion
|
||||
@ -33,10 +36,10 @@ public class CheckJPA<T> implements CheckFunctionInterface {
|
||||
* @throws Exception Exception is generate if the data are incorrect. */
|
||||
void check(K data) throws Exception;
|
||||
}
|
||||
|
||||
|
||||
private Map<String, List<CheckInterface<T>>> checking = null;
|
||||
|
||||
protected void add(String field, CheckInterface<T> checkFunction) {
|
||||
|
||||
protected void add(final String field, final CheckInterface<T> checkFunction) {
|
||||
List<CheckInterface<T>> actions = this.checking.get(field);
|
||||
if (actions == null) {
|
||||
actions = new ArrayList<>();
|
||||
@ -44,11 +47,11 @@ public class CheckJPA<T> implements CheckFunctionInterface {
|
||||
}
|
||||
actions.add(checkFunction);
|
||||
}
|
||||
|
||||
public CheckJPA(Class<T> clazz) {
|
||||
|
||||
public CheckJPA(final Class<T> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
|
||||
public void initialize() throws Exception {
|
||||
if (this.checking != null) {
|
||||
return;
|
||||
@ -58,101 +61,130 @@ public class CheckJPA<T> implements CheckFunctionInterface {
|
||||
// create Table:
|
||||
final List<String> primaryKeys = new ArrayList<>();
|
||||
for (final Field field : this.clazz.getFields()) {
|
||||
String fieldName = AnnotationTools.getFieldName(field);
|
||||
final String fieldName = AnnotationTools.getFieldName(field);
|
||||
if (AnnotationTools.isPrimaryKey(field)) {
|
||||
add(fieldName, (T data) -> {
|
||||
add(fieldName, (final T data) -> {
|
||||
throw new InputException(fieldName, "This is a '@Id' (primaryKey) ==> can not be change");
|
||||
});
|
||||
}
|
||||
if (AnnotationTools.getNotNull(field)) {
|
||||
add(fieldName, (T data) -> {
|
||||
if (AnnotationTools.getConstraintsNotNull(field)) {
|
||||
add(fieldName, (final T data) -> {
|
||||
if (field.get(data) == null) {
|
||||
throw new InputException(fieldName, "Can not be null");
|
||||
}
|
||||
});
|
||||
}
|
||||
if (AnnotationTools.isCreatedAtField(field) || AnnotationTools.isUpdateAtField(field)) {
|
||||
add(fieldName, (T data) -> {
|
||||
add(fieldName, (final T data) -> {
|
||||
throw new InputException(fieldName, "It is forbidden to change this field");
|
||||
});
|
||||
}
|
||||
|
||||
Class<?> type = field.getType();
|
||||
|
||||
final Class<?> type = field.getType();
|
||||
if (type == Long.class || type == long.class) {
|
||||
|
||||
|
||||
} else if (type == Integer.class || type == int.class) {
|
||||
|
||||
|
||||
} else if (type == Boolean.class || type == boolean.class) {
|
||||
|
||||
|
||||
} else if (type == Float.class || type == float.class) {
|
||||
|
||||
|
||||
} else if (type == Double.class || type == double.class) {
|
||||
|
||||
|
||||
} else if (type == Date.class || type == Timestamp.class) {
|
||||
|
||||
|
||||
} else if (type == LocalDate.class) {
|
||||
|
||||
|
||||
} else if (type == LocalTime.class) {
|
||||
|
||||
|
||||
} else if (type == String.class) {
|
||||
int maxSizeString = AnnotationTools.getLimitSize(field);
|
||||
|
||||
final int maxSizeString = AnnotationTools.getLimitSize(field);
|
||||
if (maxSizeString > 0) {
|
||||
add(fieldName, (T data) -> {
|
||||
Object elem = field.get(data);
|
||||
add(fieldName, (final T data) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
String elemTyped = (String) elem;
|
||||
final String elemTyped = (String) elem;
|
||||
if (elemTyped.length() > maxSizeString) {
|
||||
throw new InputException("name", "Too long size must be <= " + maxSizeString);
|
||||
throw new InputException(fieldName, "Too long size must be <= " + maxSizeString);
|
||||
}
|
||||
});
|
||||
}
|
||||
final Size limitSize = AnnotationTools.getConstraintsSize(field);
|
||||
if (limitSize != null) {
|
||||
add(fieldName, (final T data) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final String elemTyped = (String) elem;
|
||||
if (elemTyped.length() > limitSize.max()) {
|
||||
throw new InputException(fieldName, "Too long size (constraints) must be <= " + limitSize.max());
|
||||
}
|
||||
if (elemTyped.length() < limitSize.min()) {
|
||||
throw new InputException(fieldName, "Too small size (constraints) must be >= " + limitSize.max());
|
||||
}
|
||||
});
|
||||
}
|
||||
final String patternString = AnnotationTools.getConstraintsPattern(field);
|
||||
if (patternString != null) {
|
||||
final Pattern pattern = Pattern.compile(patternString);
|
||||
add(fieldName, (final T data) -> {
|
||||
final Object elem = field.get(data);
|
||||
if (elem == null) {
|
||||
return;
|
||||
}
|
||||
final String elemTyped = (String) elem;
|
||||
if (!pattern.matcher(elemTyped).find()) {
|
||||
throw new InputException(fieldName, "does not match the required pattern (constraints) must be '" + patternString + "'");
|
||||
}
|
||||
});
|
||||
}
|
||||
} else if (type == JsonValue.class) {
|
||||
|
||||
|
||||
} else if (type.isEnum()) {
|
||||
// nothing to do.
|
||||
}
|
||||
// keep this is last ==> take more time...
|
||||
if (AnnotationTools.isUnique(field)) {
|
||||
// Create the request ...
|
||||
add(fieldName, (T data) -> {
|
||||
add(fieldName, (final T data) -> {
|
||||
final Object other = DataAccess.getWhere(this.clazz, new QueryCondition(fieldName, "==", field.get(data)));
|
||||
if (other != null) {
|
||||
throw new InputException(fieldName, "Name already exist in the DB");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
} catch (final Exception ex) {
|
||||
this.checking = null;
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void check(Object data, List<String> filterValue) throws Exception {
|
||||
public void check(final Object data, final List<String> filterValue) throws Exception {
|
||||
initialize();
|
||||
if (!(this.clazz.isAssignableFrom(data.getClass()))) {
|
||||
throw new DataAccessException("Incompatatyble type of Object" + data.getClass().getCanonicalName());
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
T dataCasted = (T) data;
|
||||
for (String filter : filterValue) {
|
||||
List<CheckInterface<T>> actions = this.checking.get(filter);
|
||||
final T dataCasted = (T) data;
|
||||
for (final String filter : filterValue) {
|
||||
final List<CheckInterface<T>> actions = this.checking.get(filter);
|
||||
if (actions == null) {
|
||||
continue;
|
||||
}
|
||||
for (CheckInterface<T> action : actions) {
|
||||
for (final CheckInterface<T> action : actions) {
|
||||
action.check(dataCasted);
|
||||
}
|
||||
}
|
||||
checkTyped(dataCasted, filterValue);
|
||||
}
|
||||
|
||||
public void checkTyped(T data, List<String> filterValue) throws Exception {
|
||||
|
||||
public void checkTyped(final T data, final List<String> filterValue) throws Exception {
|
||||
// nothing to do ...
|
||||
}
|
||||
}
|
||||
|
28
src/org/kar/archidata/dataAccess/options/Condition.java
Normal file
28
src/org/kar/archidata/dataAccess/options/Condition.java
Normal file
@ -0,0 +1,28 @@
|
||||
package org.kar.archidata.dataAccess.options;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
|
||||
import org.kar.archidata.dataAccess.CountInOut;
|
||||
import org.kar.archidata.dataAccess.QueryItem;
|
||||
import org.kar.archidata.dataAccess.QueryOption;
|
||||
|
||||
/** By default some element are not read like createAt and UpdatedAt. This option permit to read it. */
|
||||
public class Condition extends QueryOption {
|
||||
public final QueryItem condition;
|
||||
|
||||
public Condition(final QueryItem items) {
|
||||
this.condition = items;
|
||||
}
|
||||
|
||||
public void generateQuerry(final StringBuilder query, final String tableName) {
|
||||
if (this.condition != null) {
|
||||
this.condition.generateQuerry(query, tableName);
|
||||
}
|
||||
}
|
||||
|
||||
public void injectQuerry(final PreparedStatement ps, final CountInOut iii) throws Exception {
|
||||
if (this.condition != null) {
|
||||
this.condition.injectQuerry(ps, iii);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package org.kar.archidata.dataAccess.options;
|
||||
|
||||
import org.kar.archidata.dataAccess.QueryOption;
|
||||
|
||||
/** Option that permit to access to a table structure with an other name that is define in the structure. Note: Internal use for link tables (see:
|
||||
* org.kar.archidata.dataAccess.addOn.model.LinkTable). */
|
||||
public class Limit extends QueryOption {
|
||||
private final int limit;
|
||||
|
||||
public Limit(final int limit) {
|
||||
this.limit = limit;
|
||||
}
|
||||
|
||||
public int getLimit() {
|
||||
return this.limit;
|
||||
}
|
||||
}
|
@ -26,15 +26,15 @@ public class RESTApi {
|
||||
final String baseUrl;
|
||||
private String token = null;
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
|
||||
public RESTApi(final String baseUrl) {
|
||||
this.baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
|
||||
public void setToken(final String token) {
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
|
||||
public <T> List<T> gets(final Class<T> clazz, final String urlOffset) throws RESTErrorResponseExeption, IOException, InterruptedException {
|
||||
final HttpClient client = HttpClient.newHttpClient();
|
||||
Builder requestBuilding = HttpRequest.newBuilder().version(Version.HTTP_1_1).uri(URI.create(this.baseUrl + urlOffset));
|
||||
@ -49,7 +49,7 @@ public class RESTApi {
|
||||
}
|
||||
return this.mapper.readValue(httpResponse.body(), new TypeReference<List<T>>() {});
|
||||
}
|
||||
|
||||
|
||||
public <T> T get(final Class<T> clazz, final String urlOffset) throws RESTErrorResponseExeption, IOException, InterruptedException {
|
||||
final HttpClient client = HttpClient.newHttpClient();
|
||||
Builder requestBuilding = HttpRequest.newBuilder().version(Version.HTTP_1_1).uri(URI.create(this.baseUrl + urlOffset));
|
||||
@ -74,7 +74,7 @@ public class RESTApi {
|
||||
}
|
||||
return this.mapper.readValue(httpResponse.body(), clazz);
|
||||
}
|
||||
|
||||
|
||||
public <T, U> T post(final Class<T> clazz, final String urlOffset, final U data) throws RESTErrorResponseExeption, IOException, InterruptedException {
|
||||
final HttpClient client = HttpClient.newHttpClient();
|
||||
final String body = this.mapper.writeValueAsString(data);
|
||||
@ -96,7 +96,7 @@ public class RESTApi {
|
||||
}
|
||||
return this.mapper.readValue(httpResponse.body(), clazz);
|
||||
}
|
||||
|
||||
|
||||
public <T> T postMap(final Class<T> clazz, final String urlOffset, final Map<String, Object> data) throws RESTErrorResponseExeption, IOException, InterruptedException {
|
||||
final HttpClient client = HttpClient.newHttpClient();
|
||||
final String body = this.mapper.writeValueAsString(data);
|
||||
@ -116,12 +116,12 @@ public class RESTApi {
|
||||
}
|
||||
return this.mapper.readValue(httpResponse.body(), clazz);
|
||||
}
|
||||
|
||||
|
||||
public <T, U> T put(final Class<T> clazz, final String urlOffset, final U data) throws RESTErrorResponseExeption, IOException, InterruptedException {
|
||||
final String body = this.mapper.writeValueAsString(data);
|
||||
return putJson(clazz, urlOffset, body);
|
||||
}
|
||||
|
||||
|
||||
public <T, U> T putJson(final Class<T> clazz, final String urlOffset, final String body) throws RESTErrorResponseExeption, IOException, InterruptedException {
|
||||
final HttpClient client = HttpClient.newHttpClient();
|
||||
Builder requestBuilding = HttpRequest.newBuilder().version(Version.HTTP_1_1).uri(URI.create(this.baseUrl + urlOffset));
|
||||
@ -142,7 +142,7 @@ public class RESTApi {
|
||||
}
|
||||
return this.mapper.readValue(httpResponse.body(), clazz);
|
||||
}
|
||||
|
||||
|
||||
public <T> T putMap(final Class<T> clazz, final String urlOffset, final Map<String, Object> data) throws RESTErrorResponseExeption, IOException, InterruptedException {
|
||||
final HttpClient client = HttpClient.newHttpClient();
|
||||
final String body = this.mapper.writeValueAsString(data);
|
||||
@ -162,7 +162,7 @@ public class RESTApi {
|
||||
}
|
||||
return this.mapper.readValue(httpResponse.body(), clazz);
|
||||
}
|
||||
|
||||
|
||||
public <T, U> T delete(final Class<T> clazz, final String urlOffset) throws RESTErrorResponseExeption, IOException, InterruptedException {
|
||||
final HttpClient client = HttpClient.newHttpClient();
|
||||
Builder requestBuilding = HttpRequest.newBuilder().version(Version.HTTP_1_1).uri(URI.create(this.baseUrl + urlOffset));
|
||||
|
Loading…
x
Reference in New Issue
Block a user