DatabaseUtils.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package db;
  2. import java.lang.annotation.Annotation;
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.InvocationTargetException;
  5. import db.annotation.DbField;
  6. import db.annotation.DbId;
  7. import db.annotation.DbLink;
  8. public class DatabaseUtils {
  9. public static boolean isPersistable(Class<?> clazz) {
  10. for(Class<?> interfaces : clazz.getInterfaces()) {
  11. if (interfaces.getName().endsWith("Persistable")) {
  12. return true;
  13. }
  14. }
  15. return false;
  16. }
  17. public static void checkIfPersistable(Field f) {
  18. if (!isPersistable(f.getType())) {
  19. throw new IllegalStateException("Field object don't implement Persistable");
  20. }
  21. }
  22. public static boolean isDbId(Field f) {
  23. return isAnnotation(f, DbId.class);
  24. }
  25. public static boolean isDbField(Field f) {
  26. return isAnnotation(f, DbField.class);
  27. }
  28. public static String getDbField(Field f) {
  29. if (!isDbField(f)) {
  30. return null;
  31. }
  32. return f.getAnnotation(DbField.class).value();
  33. }
  34. public static boolean isDbLink(Field f) {
  35. return isAnnotation(f, DbLink.class);
  36. }
  37. public static String getDbLink(Field f) {
  38. if (!isDbLink(f)) {
  39. return null;
  40. }
  41. String val = f.getAnnotation(DbLink.class).value();
  42. if ("#PROPERTIES#".equals(val)) {
  43. val = DatabaseProperties.get("dblink");
  44. }
  45. return val;
  46. }
  47. public static Field getIdField(Class<?> clazz) {
  48. if (!isPersistable(clazz)) {
  49. return null;
  50. }
  51. for(Field f : clazz.getFields()) {
  52. if (isDbField(f) && isDbId(f)) {
  53. return f;
  54. }
  55. }
  56. return null;
  57. }
  58. public static <T extends Persistable> Persistable getDbLinkObject(Field f, T obj) {
  59. if (!isDbLink(f)) {
  60. return null;
  61. }
  62. checkIfPersistable(f);
  63. try {
  64. return (Persistable) f.get(obj);
  65. } catch (IllegalArgumentException | IllegalAccessException e) {
  66. return null;
  67. }
  68. }
  69. public static DatabaseTable<?> getDatabaseTable(Field f) {
  70. if (!(isDbLink(f) && isPersistable(f.getType()))) {
  71. return null;
  72. }
  73. String pckg = getDbLink(f);
  74. String[] split = f.getType().getName().split("\\.");
  75. String className = split[split.length - 1];
  76. return getDatabaseTable(pckg, className);
  77. }
  78. public static DatabaseTable<?> getDatabaseTable(String pckg, String className) {
  79. Class<?> clazz;
  80. try {
  81. clazz = Class.forName(pckg + "." + className + "Table");
  82. return (DatabaseTable<?>) clazz.getMethod("getInstance").invoke(null);
  83. } catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
  84. throw new IllegalStateException("Unable to load " + className + "Table in the package " + pckg, e);
  85. }
  86. }
  87. private static boolean isAnnotation(Field f, Class<? extends Annotation> clazz) {
  88. return f.isAnnotationPresent(clazz);
  89. }
  90. }