diff --git a/pom.xml b/pom.xml
index 3af0e32..306cef9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -2,7 +2,7 @@
4.0.0
kangaroo-and-rabbit
archidata
- 0.1.5
+ 0.2.0
2.1
2.32
diff --git a/src/org/kar/archidata/SqlWrapper.java b/src/org/kar/archidata/SqlWrapper.java
index 871517a..8b50c8a 100644
--- a/src/org/kar/archidata/SqlWrapper.java
+++ b/src/org/kar/archidata/SqlWrapper.java
@@ -552,139 +552,72 @@ public class SqlWrapper {
throw new Exception("Not manage type ==> need to add it ...");
}
}
-
+ @Deprecated
public static T getWith(Class clazz, String key, String value) throws Exception {
- return getWhere(clazz, key, "=", value);
+ return getWhere(clazz, List.of(new WhereCondition(key, "=", value)), false);
}
+ @Deprecated
public static T getWhere(Class clazz, String key, String operator, Object value, boolean full ) throws Exception {
- return getWhere(clazz, key, operator, value, null, null, null, full);
+ return getWhere(clazz, List.of(new WhereCondition(key, operator, value)), full);
}
+ @Deprecated
public static T getWhere(Class clazz, String key, String operator, Object value ) throws Exception {
- return getWhere(clazz, key, operator, value, null, null, null, false);
+ return getWhere(clazz, List.of(new WhereCondition(key, operator, value)), false);
}
+ @Deprecated
public static T getWhere(Class clazz, String key, String operator, Object value, String key2, String operator2, Object value2 ) throws Exception {
- return getWhere(clazz, key, operator, value, key2, operator2, value2, false);
+ return getWhere(clazz,
+ List.of(
+ new WhereCondition(key, operator, value),
+ new WhereCondition(key2, operator2, value2)
+ ), false);
}
+ @Deprecated
public static T getWhere(Class clazz, String key, String operator, Object value, String key2, String operator2, Object value2, boolean full ) throws Exception {
- DBEntry entry = new DBEntry(GlobalConfiguration.dbConfig);
+ return getWhere(clazz,
+ List.of(
+ new WhereCondition(key, operator, value),
+ new WhereCondition(key2, operator2, value2)
+ ), full);
+ }
+
+ public static T getWhere(Class clazz, List condition, boolean full ) throws Exception {
+ DBEntry entry = new DBEntry(GlobalConfiguration.dbConfig);
T out = null;
// real add in the BDD:
- try {
- String tableName = getTableName(clazz);
- boolean createIfNotExist = clazz.getDeclaredAnnotationsByType(SQLIfNotExists.class).length != 0;
- StringBuilder query = new StringBuilder();
- query.append("SELECT ");
- //query.append(tableName);
- //query.append(" SET ");
-
- boolean firstField = true;
- int count = 0;
- for (Field elem : clazz.getFields()) {
- ModelLink linkGeneric = getLinkMode(elem);
- if (linkGeneric != ModelLink.NONE) {
- continue;
- }
- boolean createTime = elem.getDeclaredAnnotationsByType(SQLCreateTime.class).length != 0;
- if (!full && createTime) {
- continue;
- }
- String name = elem.getName();
- boolean updateTime = elem.getDeclaredAnnotationsByType(SQLUpdateTime.class).length != 0;
- if (!full && updateTime) {
- continue;
- }
- count++;
- if (firstField) {
- firstField = false;
- } else {
- query.append(",");
- }
- query.append(" ");
- query.append(tableName);
- query.append(".");
-
- query.append(name);
- }
- query.append(" FROM `");
- query.append(tableName);
- query.append("` ");
- query.append(" WHERE ");
- query.append(tableName);
- query.append(".");
- query.append(key);
- query.append(" ");
- query.append(operator);
- query.append(" ?");
- if (key2 != null) {
- query.append(" AND ");
- query.append(tableName);
- query.append(".");
- query.append(key2);
- query.append(" ");
- query.append(operator2);
- query.append(" ?");
- }
- /*
- query.append(" AND ");
- query.append(tableName);
- query.append(".deleted = false ");
- */
- firstField = true;
- //System.out.println("generate the querry: '" + query.toString() + "'");
- // prepare the request:
- PreparedStatement ps = entry.connection.prepareStatement(query.toString(), Statement.RETURN_GENERATED_KEYS);
- int iii = 1;
- addElement(ps, value, iii++);
- if (key2 != null) {
- addElement(ps, value2, iii++);
- }
- // execute the request
- ResultSet rs = ps.executeQuery();
- while (rs.next()) {
- Object data = clazz.getConstructors()[0].newInstance();
- count = 1;
- for (Field elem : clazz.getFields()) {
- ModelLink linkGeneric = getLinkMode(elem);
- if (linkGeneric != ModelLink.NONE) {
- continue;
- }
- boolean createTime = elem.getDeclaredAnnotationsByType(SQLCreateTime.class).length != 0;
- if (!full && createTime) {
- continue;
- }
- String name = elem.getName();
- boolean updateTime = elem.getDeclaredAnnotationsByType(SQLUpdateTime.class).length != 0;
- if (!full && updateTime) {
- continue;
- }
- //this.name = rs.getString(iii++);
- //this.description = rs.getString(iii++);
- //this.covers = getListOfIds(rs, iii++);
- setValueFromDb(elem.getType(), data, count, elem, rs);
- count++;
- }
- out = (T)data;
- }
-
- } catch (SQLException ex) {
- ex.printStackTrace();
- } finally {
- entry.close();
- entry = null;
+
+ List values = getsWhere(clazz, condition, full, 1);
+ if (values.size() == 0) {
+ return null;
}
- return out;
+ return values.get(0);
}
+ @Deprecated
public static List getsWhere(Class clazz, String key, String operator, Object value ) throws Exception {
- return getsWhere(clazz, key, operator, value, false);
+ return getsWhere(clazz, List.of(new WhereCondition(key, operator, value)), null, false, null);
}
+ @Deprecated
public static List getsWhere(Class clazz, String key, String operator, Object value, boolean full ) throws Exception {
+ return getsWhere(clazz, List.of(new WhereCondition(key, operator, value)), null, full, null);
+ }
+
+ public static List getsWhere(Class clazz, List condition) throws Exception {
+ return getsWhere(clazz, condition, null, false, null);
+ }
+ public static List getsWhere(Class clazz, List condition, boolean full ) throws Exception {
+ return getsWhere(clazz, condition, null, full, null);
+ }
+ public static List getsWhere(Class clazz, List condition, boolean full, Integer linit) throws Exception {
+ return getsWhere(clazz, condition, null, full, linit);
+ }
+ public static List getsWhere(Class clazz, List condition, String orderBy, boolean full, Integer linit) throws Exception {
DBEntry entry = new DBEntry(GlobalConfiguration.dbConfig);
List outs = new ArrayList<>();
// real add in the BDD:
try {
String tableName = getTableName(clazz);
- boolean createIfNotExist = clazz.getDeclaredAnnotationsByType(SQLIfNotExists.class).length != 0;
+ //boolean createIfNotExist = clazz.getDeclaredAnnotationsByType(SQLIfNotExists.class).length != 0;
StringBuilder query = new StringBuilder();
query.append("SELECT ");
//query.append(tableName);
@@ -722,23 +655,45 @@ public class SqlWrapper {
query.append(tableName);
query.append("` ");
query.append(" WHERE ");
- query.append(tableName);
- query.append(".");
- query.append(key);
- query.append(" ");
- query.append(operator);
- query.append(" ?");
+ if (condition.size() == 0) {
+ throw new ExceptionDBInterface(4575643, tableName + " ==> request where without parameters");
+ }
+ boolean first = true;
+ for (WhereCondition elem : condition) {
+ if (first) {
+ first = false;
+ } else {
+ query.append(" AND ");
+ }
+ query.append(tableName);
+ query.append(".");
+ query.append(elem.key());
+ query.append(" ");
+ query.append(elem.comparator());
+ query.append(" ?");
+ }
+ if (orderBy != null && orderBy.length() >= 1) {
+ query.append(" ORDER BY ");
+ //query.append(tableName);
+ //query.append(".");
+ query.append(orderBy);
+ }
+ if (linit != null && linit >= 1) {
+ query.append(" LIMIT " + linit);
+ }
/*
query.append(" AND ");
query.append(tableName);
query.append(".deleted = false ");
*/
firstField = true;
- System.out.println("generate the querry: '" + query.toString() + "'");
+ System.out.println("generate the query: '" + query.toString() + "'");
// prepare the request:
PreparedStatement ps = entry.connection.prepareStatement(query.toString(), Statement.RETURN_GENERATED_KEYS);
int iii = 1;
- addElement(ps, value, iii++);
+ for (WhereCondition elem : condition) {
+ addElement(ps, elem.Value(), iii++);
+ }
// execute the request
ResultSet rs = ps.executeQuery();
while (rs.next()) {
@@ -758,18 +713,17 @@ public class SqlWrapper {
if (!full && updateTime) {
continue;
}
- //this.name = rs.getString(iii++);
- //this.description = rs.getString(iii++);
- //this.covers = getListOfIds(rs, iii++);
setValueFromDb(elem.getType(), data, count, elem, rs);
count++;
}
T out = (T)data;
outs.add(out);
}
-
+
} catch (SQLException ex) {
ex.printStackTrace();
+ } catch (Exception ex) {
+ ex.printStackTrace();
} finally {
entry.close();
entry = null;
diff --git a/src/org/kar/archidata/WhereCondition.java b/src/org/kar/archidata/WhereCondition.java
new file mode 100644
index 0000000..5c325d7
--- /dev/null
+++ b/src/org/kar/archidata/WhereCondition.java
@@ -0,0 +1,8 @@
+package org.kar.archidata;
+
+public record WhereCondition(
+ String key,
+ String comparator,
+ Object Value) {
+
+}
diff --git a/src/org/kar/archidata/catcher/ExceptionCatcher.java b/src/org/kar/archidata/catcher/ExceptionCatcher.java
new file mode 100644
index 0000000..87ed1ac
--- /dev/null
+++ b/src/org/kar/archidata/catcher/ExceptionCatcher.java
@@ -0,0 +1,26 @@
+package org.kar.archidata.catcher;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+
+
+public class ExceptionCatcher
+implements ExceptionMapper {
+ @Override
+ public Response toResponse(Exception exception) {
+ System.out.println("Catch exception (not managed...):");
+ RestErrorResponse ret = build(exception);
+ System.out.println("Error UUID=" + ret.uuid);
+ exception.printStackTrace();
+ return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
+ .entity(ret)
+ .type(MediaType.APPLICATION_JSON)
+ .build();
+ }
+
+ private RestErrorResponse build(Exception exception) {
+ return new RestErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, "Catch Unknown Exception", exception.getMessage());
+ }
+
+}
diff --git a/src/org/kar/archidata/catcher/FailExceptionCatcher.java b/src/org/kar/archidata/catcher/FailExceptionCatcher.java
new file mode 100644
index 0000000..dc7b529
--- /dev/null
+++ b/src/org/kar/archidata/catcher/FailExceptionCatcher.java
@@ -0,0 +1,28 @@
+package org.kar.archidata.catcher;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+
+import org.kar.archidata.exception.FailException;
+
+
+public class FailExceptionCatcher
+implements ExceptionMapper {
+ @Override
+ public Response toResponse(FailException exception) {
+ RestErrorResponse ret = build(exception);
+ System.out.println("Error UUID=" + ret.uuid);
+ // Not display backtrace ==> this may be a normal case ...
+ //exception.printStackTrace();
+ return Response.status(exception.status)
+ .entity(ret)
+ .type(MediaType.APPLICATION_JSON)
+ .build();
+ }
+
+ private RestErrorResponse build(FailException exception) {
+ return new RestErrorResponse(exception.status, "Request Fail", exception.getMessage());
+ }
+
+}
diff --git a/src/org/kar/archidata/catcher/InputExceptionCatcher.java b/src/org/kar/archidata/catcher/InputExceptionCatcher.java
new file mode 100644
index 0000000..ff931c2
--- /dev/null
+++ b/src/org/kar/archidata/catcher/InputExceptionCatcher.java
@@ -0,0 +1,27 @@
+package org.kar.archidata.catcher;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+
+import org.kar.archidata.exception.InputException;
+
+
+public class InputExceptionCatcher
+implements ExceptionMapper {
+ @Override
+ public Response toResponse(InputException exception) {
+ RestErrorResponse ret = build(exception);
+ System.out.println("Error UUID=" + ret.uuid);
+ exception.printStackTrace();
+ return Response.status(exception.status)
+ .entity(ret)
+ .type(MediaType.APPLICATION_JSON)
+ .build();
+ }
+
+ private RestErrorResponse build(InputException exception) {
+ return new RestErrorResponse(exception.status, "Error on input='" + exception.missingVariable + "'" , exception.getMessage());
+ }
+
+}
diff --git a/src/org/kar/archidata/catcher/RestErrorResponse.java b/src/org/kar/archidata/catcher/RestErrorResponse.java
new file mode 100644
index 0000000..8d3636f
--- /dev/null
+++ b/src/org/kar/archidata/catcher/RestErrorResponse.java
@@ -0,0 +1,35 @@
+package org.kar.archidata.catcher;
+
+import java.time.Instant;
+import java.util.UUID;
+
+import javax.ws.rs.core.Response;
+
+public class RestErrorResponse {
+ public UUID uuid = UUID.randomUUID();
+ public String time;
+ public String error;
+ public String message;
+ final public int status;
+ final public String statusMessage;
+ public RestErrorResponse(Response.Status status, String time, String error, String message) {
+ this.time = time;
+ this.error = error;
+ this.message = message;
+ this.status = status.getStatusCode();
+ this.statusMessage = status.getReasonPhrase();
+ }
+ public RestErrorResponse(Response.Status status, String error, String message) {
+ this.time = Instant.now().toString();
+ this.error = error;
+ this.message = message;
+ this.status = status.getStatusCode();
+ this.statusMessage = status.getReasonPhrase();
+ }
+ public RestErrorResponse(Response.Status status) {
+ this.time = Instant.now().toString();
+ this.status = status.getStatusCode();
+ this.statusMessage = status.getReasonPhrase();
+ }
+
+}
diff --git a/src/org/kar/archidata/catcher/SystemExceptionCatcher.java b/src/org/kar/archidata/catcher/SystemExceptionCatcher.java
new file mode 100644
index 0000000..91720b9
--- /dev/null
+++ b/src/org/kar/archidata/catcher/SystemExceptionCatcher.java
@@ -0,0 +1,28 @@
+package org.kar.archidata.catcher;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+
+import org.kar.archidata.exception.InputException;
+import org.kar.archidata.exception.SystemException;
+
+
+public class SystemExceptionCatcher
+implements ExceptionMapper {
+ @Override
+ public Response toResponse(SystemException exception) {
+ RestErrorResponse ret = build(exception);
+ System.out.println("Error UUID=" + ret.uuid);
+ exception.printStackTrace();
+ return Response.status(exception.status)
+ .entity(ret)
+ .type(MediaType.APPLICATION_JSON)
+ .build();
+ }
+
+ private RestErrorResponse build(SystemException exception) {
+ return new RestErrorResponse(exception.status, "System error", exception.getMessage());
+ }
+
+}
diff --git a/src/org/kar/archidata/exception/FailException.java b/src/org/kar/archidata/exception/FailException.java
new file mode 100644
index 0000000..4309fa9
--- /dev/null
+++ b/src/org/kar/archidata/exception/FailException.java
@@ -0,0 +1,17 @@
+package org.kar.archidata.exception;
+
+import javax.ws.rs.core.Response;
+
+public class FailException extends Exception {
+ private static final long serialVersionUID = 1L;
+ public final Response.Status status;
+ public FailException(Response.Status status, String message) {
+ super(message);
+ this.status = status;
+ }
+ public FailException(String message) {
+ super(message);
+ this.status = Response.Status.BAD_REQUEST;
+
+ }
+}
diff --git a/src/org/kar/archidata/exception/InputException.java b/src/org/kar/archidata/exception/InputException.java
new file mode 100644
index 0000000..c52b81e
--- /dev/null
+++ b/src/org/kar/archidata/exception/InputException.java
@@ -0,0 +1,19 @@
+package org.kar.archidata.exception;
+
+import javax.ws.rs.core.Response;
+
+public class InputException extends Exception {
+ private static final long serialVersionUID = 1L;
+ public final String missingVariable;
+ public final Response.Status status;
+ public InputException(Response.Status status, String variable, String message) {
+ super(message);
+ this.missingVariable = variable;
+ this.status = status;
+ }
+ public InputException(String variable, String message) {
+ super(message);
+ this.missingVariable = variable;
+ this.status = Response.Status.NOT_ACCEPTABLE;
+ }
+}
diff --git a/src/org/kar/archidata/exception/SystemException.java b/src/org/kar/archidata/exception/SystemException.java
new file mode 100644
index 0000000..17ad2b4
--- /dev/null
+++ b/src/org/kar/archidata/exception/SystemException.java
@@ -0,0 +1,16 @@
+package org.kar.archidata.exception;
+
+import javax.ws.rs.core.Response;
+
+public class SystemException extends Exception {
+ private static final long serialVersionUID = 1L;
+ public final Response.Status status;
+ public SystemException(Response.Status status, String message) {
+ super(message);
+ this.status = status;
+ }
+ public SystemException(String message) {
+ super(message);
+ this.status = Response.Status.INTERNAL_SERVER_ERROR;
+ }
+}