[DEV] add many tools to simplify usages
This commit is contained in:
parent
15c9ab83db
commit
1a6135f2cf
2
pom.xml
2
pom.xml
@ -2,7 +2,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>kangaroo-and-rabbit</groupId>
|
||||
<artifactId>archidata</artifactId>
|
||||
<version>0.1.5</version>
|
||||
<version>0.2.0</version>
|
||||
<properties>
|
||||
<jaxrs.version>2.1</jaxrs.version>
|
||||
<jersey.version>2.32</jersey.version>
|
||||
|
@ -552,139 +552,72 @@ public class SqlWrapper {
|
||||
throw new Exception("Not manage type ==> need to add it ...");
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static <T> T getWith(Class<T> 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> T getWhere(Class<T> 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> T getWhere(Class<T> 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> T getWhere(Class<T> 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> T getWhere(Class<T> clazz, String key, String operator, Object value, String key2, String operator2, Object value2, boolean full ) throws Exception {
|
||||
return getWhere(clazz,
|
||||
List.of(
|
||||
new WhereCondition(key, operator, value),
|
||||
new WhereCondition(key2, operator2, value2)
|
||||
), full);
|
||||
}
|
||||
|
||||
public static <T> T getWhere(Class<T> clazz, List<WhereCondition> 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;
|
||||
List<T> values = getsWhere(clazz, condition, full, 1);
|
||||
if (values.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
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;
|
||||
}
|
||||
return out;
|
||||
return values.get(0);
|
||||
}
|
||||
@Deprecated
|
||||
public static <T> List<T> getsWhere(Class<T> 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 <T> List<T> getsWhere(Class<T> 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 <T> List<T> getsWhere(Class<T> clazz, List<WhereCondition> condition) throws Exception {
|
||||
return getsWhere(clazz, condition, null, false, null);
|
||||
}
|
||||
public static <T> List<T> getsWhere(Class<T> clazz, List<WhereCondition> condition, boolean full ) throws Exception {
|
||||
return getsWhere(clazz, condition, null, full, null);
|
||||
}
|
||||
public static <T> List<T> getsWhere(Class<T> clazz, List<WhereCondition> condition, boolean full, Integer linit) throws Exception {
|
||||
return getsWhere(clazz, condition, null, full, linit);
|
||||
}
|
||||
public static <T> List<T> getsWhere(Class<T> clazz, List<WhereCondition> condition, String orderBy, boolean full, Integer linit) throws Exception {
|
||||
DBEntry entry = new DBEntry(GlobalConfiguration.dbConfig);
|
||||
List<T> 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 ");
|
||||
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(key);
|
||||
query.append(elem.key());
|
||||
query.append(" ");
|
||||
query.append(operator);
|
||||
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,9 +713,6 @@ 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++;
|
||||
}
|
||||
@ -770,6 +722,8 @@ public class SqlWrapper {
|
||||
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
entry.close();
|
||||
entry = null;
|
||||
|
8
src/org/kar/archidata/WhereCondition.java
Normal file
8
src/org/kar/archidata/WhereCondition.java
Normal file
@ -0,0 +1,8 @@
|
||||
package org.kar.archidata;
|
||||
|
||||
public record WhereCondition(
|
||||
String key,
|
||||
String comparator,
|
||||
Object Value) {
|
||||
|
||||
}
|
26
src/org/kar/archidata/catcher/ExceptionCatcher.java
Normal file
26
src/org/kar/archidata/catcher/ExceptionCatcher.java
Normal file
@ -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<Exception> {
|
||||
@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());
|
||||
}
|
||||
|
||||
}
|
28
src/org/kar/archidata/catcher/FailExceptionCatcher.java
Normal file
28
src/org/kar/archidata/catcher/FailExceptionCatcher.java
Normal file
@ -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<FailException> {
|
||||
@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());
|
||||
}
|
||||
|
||||
}
|
27
src/org/kar/archidata/catcher/InputExceptionCatcher.java
Normal file
27
src/org/kar/archidata/catcher/InputExceptionCatcher.java
Normal file
@ -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<InputException> {
|
||||
@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());
|
||||
}
|
||||
|
||||
}
|
35
src/org/kar/archidata/catcher/RestErrorResponse.java
Normal file
35
src/org/kar/archidata/catcher/RestErrorResponse.java
Normal file
@ -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();
|
||||
}
|
||||
|
||||
}
|
28
src/org/kar/archidata/catcher/SystemExceptionCatcher.java
Normal file
28
src/org/kar/archidata/catcher/SystemExceptionCatcher.java
Normal file
@ -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<SystemException> {
|
||||
@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());
|
||||
}
|
||||
|
||||
}
|
17
src/org/kar/archidata/exception/FailException.java
Normal file
17
src/org/kar/archidata/exception/FailException.java
Normal file
@ -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;
|
||||
|
||||
}
|
||||
}
|
19
src/org/kar/archidata/exception/InputException.java
Normal file
19
src/org/kar/archidata/exception/InputException.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
16
src/org/kar/archidata/exception/SystemException.java
Normal file
16
src/org/kar/archidata/exception/SystemException.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user