|
@@ -12,6 +12,7 @@ import java.util.Optional;
|
|
|
|
|
|
import db.annotation.DbField;
|
|
|
import db.annotation.DbId;
|
|
|
+import db.annotation.DbLink;
|
|
|
import db.annotation.DbTable;
|
|
|
|
|
|
public abstract class Table<T extends Persistable> {
|
|
@@ -141,6 +142,16 @@ public abstract class Table<T extends Persistable> {
|
|
|
return clazz.getAnnotation(DbTable.class);
|
|
|
}
|
|
|
|
|
|
+ private void checkIfPersistable(Field f) {
|
|
|
+ boolean ok = false;
|
|
|
+ for(Class<?> clazz : f.getType().getInterfaces()) {
|
|
|
+ ok |= clazz.getName().endsWith("Persistable");
|
|
|
+ }
|
|
|
+ if (!ok) {
|
|
|
+ throw new IllegalStateException("Field object don't implement Persistable");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private Optional<T> getFromDbById(long id) {
|
|
|
DbTable dbTable = getDbTableAnnotation();
|
|
|
// Cherche l'id
|
|
@@ -171,10 +182,22 @@ public abstract class Table<T extends Persistable> {
|
|
|
if(!field.isAnnotationPresent(DbField.class)) {
|
|
|
continue;
|
|
|
}
|
|
|
- obj.getClass().getField(field.getName()).set(obj, rs.getObject(field.getAnnotation(DbField.class).name()));
|
|
|
+ // Recup valeur dans le resultat
|
|
|
+ Object value = rs.getObject(field.getAnnotation(DbField.class).name());
|
|
|
+ // Si lien avec une autre entité
|
|
|
+ if(field.isAnnotationPresent(DbLink.class)) {
|
|
|
+ checkIfPersistable(field);
|
|
|
+ String[] split = field.getType().getName().split("\\.");
|
|
|
+ String className = split[split.length - 1];
|
|
|
+ // Chargement de la class Table qui gère l'entité
|
|
|
+ Class<?> clazz = Class.forName(field.getAnnotation(DbLink.class).pckg() + "." + className + "Table");
|
|
|
+ Table table = (Table) clazz.getMethod("getInstance").invoke(null);
|
|
|
+ value = table.getById((Long) value);
|
|
|
+ }
|
|
|
+ field.set(obj, value);
|
|
|
}
|
|
|
return obj;
|
|
|
- } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | NoSuchFieldException | SQLException e) {
|
|
|
+ } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | SQLException | ClassNotFoundException e) {
|
|
|
throw new IllegalStateException("Unable to map value to the entity");
|
|
|
}
|
|
|
});
|
|
@@ -197,12 +220,24 @@ public abstract class Table<T extends Persistable> {
|
|
|
if(!field.isAnnotationPresent(DbField.class)) {
|
|
|
continue;
|
|
|
}
|
|
|
- obj.getClass().getField(field.getName()).set(obj, rs.getObject(field.getAnnotation(DbField.class).name()));
|
|
|
+ // Recup valeur dans le resultat
|
|
|
+ Object val = rs.getObject(field.getAnnotation(DbField.class).name());
|
|
|
+ // Si lien avec une autre entité
|
|
|
+ if(field.isAnnotationPresent(DbLink.class)) {
|
|
|
+ checkIfPersistable(field);
|
|
|
+ String[] split = field.getType().getName().split("\\.");
|
|
|
+ String className = split[split.length - 1];
|
|
|
+ // Chargement de la class Table qui gère l'entité
|
|
|
+ Class<?> clazz = Class.forName(field.getAnnotation(DbLink.class).pckg() + "." + className + "Table");
|
|
|
+ Table table = (Table) clazz.getMethod("getInstance").invoke(null);
|
|
|
+ val = table.getById((Long) val);
|
|
|
+ }
|
|
|
+ field.set(obj, val);
|
|
|
}
|
|
|
list.add(obj);
|
|
|
}
|
|
|
return list;
|
|
|
- } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | NoSuchFieldException | SQLException e) {
|
|
|
+ } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | SQLException | ClassNotFoundException e) {
|
|
|
throw new IllegalStateException("Unable to map value to the entity");
|
|
|
}
|
|
|
});
|
|
@@ -229,12 +264,24 @@ public abstract class Table<T extends Persistable> {
|
|
|
if(!field.isAnnotationPresent(DbField.class)) {
|
|
|
continue;
|
|
|
}
|
|
|
- obj.getClass().getField(field.getName()).set(obj, rs.getObject(field.getAnnotation(DbField.class).name()));
|
|
|
+ // Recup valeur dans le resultat
|
|
|
+ Object value = rs.getObject(field.getAnnotation(DbField.class).name());
|
|
|
+ // Si lien avec une autre entité
|
|
|
+ if(field.isAnnotationPresent(DbLink.class)) {
|
|
|
+ checkIfPersistable(field);
|
|
|
+ String[] split = field.getType().getName().split("\\.");
|
|
|
+ String className = split[split.length - 1];
|
|
|
+ // Chargement de la class Table qui gère l'entité
|
|
|
+ Class<?> clazz = Class.forName(field.getAnnotation(DbLink.class).pckg() + "." + className + "Table");
|
|
|
+ Table table = (Table) clazz.getMethod("getInstance").invoke(null);
|
|
|
+ value = table.getById((Long) value);
|
|
|
+ }
|
|
|
+ field.set(obj, value);
|
|
|
}
|
|
|
list.add(obj);
|
|
|
}
|
|
|
return list;
|
|
|
- } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | NoSuchFieldException | SQLException e) {
|
|
|
+ } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | SQLException | ClassNotFoundException e) {
|
|
|
throw new IllegalStateException("Unable to map value to the entity");
|
|
|
}
|
|
|
});
|
|
@@ -258,12 +305,24 @@ public abstract class Table<T extends Persistable> {
|
|
|
if(!field.isAnnotationPresent(DbField.class)) {
|
|
|
continue;
|
|
|
}
|
|
|
- obj.getClass().getField(field.getName()).set(obj, rs.getObject(field.getAnnotation(DbField.class).name()));
|
|
|
+ // Recup valeur dans le resultat
|
|
|
+ Object value = rs.getObject(field.getAnnotation(DbField.class).name());
|
|
|
+ // Si lien avec une autre entité
|
|
|
+ if(field.isAnnotationPresent(DbLink.class)) {
|
|
|
+ checkIfPersistable(field);
|
|
|
+ String[] split = field.getType().getName().split("\\.");
|
|
|
+ String className = split[split.length - 1];
|
|
|
+ // Chargement de la class Table qui gère l'entité
|
|
|
+ Class<?> clazz = Class.forName(field.getAnnotation(DbLink.class).pckg() + "." + className + "Table");
|
|
|
+ Table table = (Table) clazz.getMethod("getInstance").invoke(null);
|
|
|
+ value = table.getById((Long) value);
|
|
|
+ }
|
|
|
+ field.set(obj, value);
|
|
|
}
|
|
|
list.add(obj);
|
|
|
}
|
|
|
return list;
|
|
|
- } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | NoSuchFieldException | SQLException e) {
|
|
|
+ } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | SQLException | ClassNotFoundException e) {
|
|
|
throw new IllegalStateException("Unable to map value to the entity");
|
|
|
}
|
|
|
});
|
|
@@ -302,7 +361,16 @@ public abstract class Table<T extends Persistable> {
|
|
|
}
|
|
|
sql.append(field.getAnnotation(DbField.class).name());
|
|
|
values.append("?");
|
|
|
- params.add(field.get(obj));
|
|
|
+ // Si il y a un lien avec une autre entité
|
|
|
+ if(field.isAnnotationPresent(DbLink.class)) {
|
|
|
+ checkIfPersistable(field);
|
|
|
+ Persistable persistable = (Persistable) field.get(obj);
|
|
|
+ params.add(persistable.getId());
|
|
|
+ }
|
|
|
+ // Sinon ajoute simplement le parametre
|
|
|
+ else {
|
|
|
+ params.add(field.get(obj));
|
|
|
+ }
|
|
|
first = false;
|
|
|
}
|
|
|
} catch (IllegalArgumentException | IllegalAccessException e) {
|
|
@@ -358,7 +426,16 @@ public abstract class Table<T extends Persistable> {
|
|
|
sql.append(",");
|
|
|
}
|
|
|
sql.append(field.getAnnotation(DbField.class).name() + " = ?");
|
|
|
- params.add(field.get(obj));
|
|
|
+ // Si il y a un lien avec une autre entité
|
|
|
+ if(field.isAnnotationPresent(DbLink.class)) {
|
|
|
+ checkIfPersistable(field);
|
|
|
+ Persistable persistable = (Persistable) field.get(obj);
|
|
|
+ params.add(persistable.getId());
|
|
|
+ }
|
|
|
+ // Sinon ajoute simplement le parametre
|
|
|
+ else {
|
|
|
+ params.add(field.get(obj));
|
|
|
+ }
|
|
|
first = false;
|
|
|
} catch (IllegalArgumentException | IllegalAccessException e) {
|
|
|
throw new IllegalStateException("Unable to access to the attribute", e);
|