DatabaseMapper.java 3.6 KB

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