DbMapper.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package db.mapper;
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import db.Table;
  9. import db.annotation.DbField;
  10. import db.annotation.DbLink;
  11. import db.annotation.DbTable;
  12. public class DbMapper {
  13. public static <T> Mapper<T> objectMapper(DbTable dbTable) {
  14. return new Mapper<T>() {
  15. @Override
  16. public T map(ResultSet rs) {
  17. try {
  18. T obj = (T) dbTable.entity().getConstructor().newInstance();
  19. rs.next();
  20. for(Field field : dbTable.entity().getFields()) {
  21. if(!field.isAnnotationPresent(DbField.class)) {
  22. continue;
  23. }
  24. // Recup valeur dans le resultat
  25. Object value = rs.getObject(field.getAnnotation(DbField.class).value());
  26. // Si lien avec une autre entité
  27. if(value != null && field.isAnnotationPresent(DbLink.class)) {
  28. checkIfPersistable(field);
  29. Long otherId = (Long) value;
  30. if(otherId == 0) {
  31. value = null;
  32. } else {
  33. String[] split = field.getType().getName().split("\\.");
  34. String className = split[split.length - 1];
  35. // Chargement de la class Table qui gère l'entité
  36. Class<?> clazz = Class.forName(field.getAnnotation(DbLink.class).value() + "." + className + "Table");
  37. Table table = (Table) clazz.getMethod("getInstance").invoke(null);
  38. value = table.getById(otherId);
  39. }
  40. }
  41. field.set(obj, value);
  42. }
  43. return obj;
  44. } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | SQLException | ClassNotFoundException e) {
  45. throw new IllegalStateException("Unable to map value to the entity");
  46. }
  47. }
  48. };
  49. }
  50. public static <T> Mapper<List<T>> listMapper(DbTable dbTable) {
  51. return new Mapper<List<T>>() {
  52. @Override
  53. public List<T> map(ResultSet rs) {
  54. try {
  55. List<T> list = new ArrayList<>();
  56. while(rs.next()) {
  57. T obj = (T) dbTable.entity().getConstructor().newInstance();
  58. for(Field field : dbTable.entity().getFields()) {
  59. if(!field.isAnnotationPresent(DbField.class)) {
  60. continue;
  61. }
  62. // Recup valeur dans le resultat
  63. Object val = rs.getObject(field.getAnnotation(DbField.class).value());
  64. // Si lien avec une autre entité
  65. if(val != null && field.isAnnotationPresent(DbLink.class)) {
  66. checkIfPersistable(field);
  67. Long otherId = (Long) val;
  68. if(otherId == 0) {
  69. val = null;
  70. } else {
  71. String[] split = field.getType().getName().split("\\.");
  72. String className = split[split.length - 1];
  73. // Chargement de la class Table qui gère l'entité
  74. Class<?> clazz = Class.forName(field.getAnnotation(DbLink.class).value() + "." + className + "Table");
  75. Table table = (Table) clazz.getMethod("getInstance").invoke(null);
  76. val = table.getById(otherId);
  77. }
  78. }
  79. field.set(obj, val);
  80. }
  81. list.add(obj);
  82. }
  83. return list;
  84. } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | SQLException | ClassNotFoundException e) {
  85. throw new IllegalStateException("Unable to map value to the entity");
  86. }
  87. }
  88. };
  89. }
  90. private static void checkIfPersistable(Field f) {
  91. boolean ok = false;
  92. for(Class<?> clazz : f.getType().getInterfaces()) {
  93. ok |= clazz.getName().endsWith("Persistable");
  94. }
  95. if (!ok) {
  96. throw new IllegalStateException("Field object don't implement Persistable");
  97. }
  98. }
  99. }