Guest User

Untitled

a guest
Jun 19th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. package com.project.service;
  2.  
  3. import com.google.common.collect.Sets;
  4. import org.apache.commons.collections.CollectionUtils;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.jetbrains.annotations.NotNull;
  7. import org.reflections.Reflections;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Service;
  10.  
  11. import javax.persistence.*;
  12. import java.lang.reflect.Field;
  13. import java.util.Arrays;
  14. import java.util.List;
  15. import java.util.Set;
  16.  
  17. import static org.reflections.ReflectionUtils.*;
  18.  
  19. /**
  20. * Common interface for all persistent objects.
  21. *
  22. * @author Rivaldo
  23. */
  24. interface MyEntity {
  25. Long getId();
  26. }
  27.  
  28. /**
  29. * Represents the existing relationship types.
  30. *
  31. * @author Rivaldo
  32. */
  33. enum Relationship {
  34.  
  35. ONE_TO_ONE(OneToOne.class),
  36. ONE_TO_MANY(OneToMany.class),
  37. MANY_TO_ONE(ManyToOne.class);
  38.  
  39. private Class relationshipAnnotation;
  40.  
  41. Relationship(Class relationshipAnnotation) {
  42. this.relationshipAnnotation = relationshipAnnotation;
  43. }
  44.  
  45. /**
  46. * Gets the relationship annotation
  47. *
  48. * @return the class that represents the relationship annotation
  49. */
  50. public Class getAnnotation() {
  51. return relationshipAnnotation;
  52. }
  53.  
  54. /**
  55. * Gets a query to verify the relationship.
  56. * The query lists all the lines in which the attribute is equal to the desired entity.
  57. * The entity must be added as a query's parameter using the 'entity' variable.
  58. *
  59. * @param table the table where the query will be performed
  60. * @param attribute the classe attribute (table column) where the entity is hold
  61. *
  62. * @return a query to list all the lines in which the entity is related
  63. */
  64. public String getQuery(@NotNull String table, @NotNull String attribute) {
  65. if (this == ONE_TO_MANY) {
  66. return inListQuery(table, attribute);
  67. } else {
  68. return singleObjectQuery(table, attribute);
  69. }
  70. }
  71.  
  72. @NotNull
  73. private String inListQuery(@NotNull String table, @NotNull String attribute) {
  74.  
  75. StringBuilder jpql = new StringBuilder("SELECT v FROM ")
  76. .append(table)
  77. .append(" v WHERE :entity MEMBER OF ")
  78. .append("v.").append(attribute);
  79.  
  80. return jpql.toString();
  81. }
  82.  
  83. @NotNull
  84. private String singleObjectQuery(@NotNull String table, @NotNull String attribute) {
  85.  
  86. StringBuilder jpql = new StringBuilder("SELECT v FROM ")
  87. .append(table)
  88. .append(" v WHERE v.")
  89. .append(attribute)
  90. .append(" = :entity");
  91.  
  92. return jpql.toString();
  93. }
  94. }
  95.  
  96. /**
  97. * A Spring service to verify an entity relationship.
  98. *
  99. * @author Rivaldo
  100. */
  101. @Service
  102. public class RelationshipService {
  103.  
  104. private EntityManager entityManager;
  105.  
  106. private Reflections reflections = new Reflections("com.project.model");
  107.  
  108. @Autowired
  109. public RelationshipService(EntityManager entityManager) {
  110. this.entityManager = entityManager;
  111. }
  112.  
  113. /**
  114. * Verify if the desired entity is being referenced in the database and
  115. * returns all classes in which the desired entity is being related.
  116. *
  117. * Checks if that specific entity object is being related by foreign key.
  118. *
  119. * @param entity the entity to verify the relationships
  120. * @return a list of classes where this specific entity is being related
  121. */
  122. public Set<Class> checkRelationship(MyEntity entity) {
  123.  
  124. Set<Class> relationshipList = Sets.newHashSet();
  125.  
  126. Set<Class<?>> entities = reflections.getTypesAnnotatedWith(Entity.class);
  127.  
  128. if (CollectionUtils.isNotEmpty(entities)) {
  129.  
  130. entities.forEach(clazz -> {
  131. Arrays.asList(Relationship.values()).forEach(relationship -> {
  132. if (hasRelationship(relationship, clazz, entity)) {
  133. relationshipList.add(clazz);
  134. }
  135. });
  136. });
  137. }
  138.  
  139. return relationshipList;
  140. }
  141.  
  142. private boolean hasRelationship(Relationship relationship, Class<?> clazz, MyEntity entity) {
  143.  
  144. String tableName = clazz.getAnnotation(Entity.class).name();
  145.  
  146. if (StringUtils.isNotBlank(tableName)) {
  147.  
  148. Set<Field> fields = getAllFields(clazz, withAnnotation(relationship.getAnnotation()),
  149. withTypeAssignableTo(entity.getClass()));
  150.  
  151. if (CollectionUtils.isNotEmpty(fields)) {
  152.  
  153. String jpql;
  154.  
  155. for (Field field : fields) {
  156.  
  157. jpql = relationship.getQuery(tableName, field.getName());
  158.  
  159. List result = entityManager.createQuery(jpql)
  160. .setParameter("entity", entity)
  161. .setMaxResults(1)
  162. .getResultList();
  163.  
  164. if (CollectionUtils.isNotEmpty(result)) {
  165. return true;
  166. }
  167. }
  168. }
  169. }
  170.  
  171. return false;
  172. }
  173. }
Add Comment
Please, Sign In to add comment