Advertisement
Guest User

JPA entitylisteners and @embeddable

a guest
Jun 1st, 2010
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.50 KB | None | 0 0
  1. // solution for this problem: http://stackoverflow.com/questions/2948578/jpa-entitylisteners-and-embeddable
  2.  
  3. package com.mycompany.entities;
  4.  
  5. import java.beans.PropertyDescriptor;
  6. import java.util.ArrayList;
  7. import java.util.Collection;
  8. import java.util.Map;
  9.  
  10. import javax.persistence.Embeddable;
  11. import javax.persistence.PrePersist;
  12. import javax.persistence.PreUpdate;
  13.  
  14. import org.springframework.beans.BeanUtils;
  15. import org.springframework.beans.BeanWrapper;
  16. import org.springframework.beans.BeanWrapperImpl;
  17. import org.springframework.core.annotation.AnnotationUtils;
  18.  
  19. import com.mycompany.entities.validation.Validateable.Persist;
  20. import com.mycompany.entities.validation.Validateable.PersistOrUpdate;
  21. import com.mycompany.entities.validation.Validateable.Update;
  22.  
  23. public class ValidatorListener {
  24.  
  25.     private enum Type {
  26.         PERSIST, UPDATE
  27.     }
  28.  
  29.     private Map<Class<?>, Collection<String>> validatingPropertiesMap;
  30.  
  31.     /**
  32.      * Validate all entities that implement {@link Persist} or
  33.      * {@link PersistOrUpdate}.
  34.      *
  35.      * @param entity
  36.      */
  37.     @PrePersist
  38.     public void checkPersist(final Object entity) {
  39.         if (entity instanceof Validateable) {
  40.             this.check((Validateable) entity, Type.PERSIST);
  41.         }
  42.         this.checkEmbeddables(entity, Type.PERSIST);
  43.     }
  44.  
  45.     /**
  46.      * Validate all entities that implement {@link Update} or
  47.      * {@link PersistOrUpdate}.
  48.      *
  49.      * @param entity
  50.      */
  51.     @PreUpdate
  52.     public void checkUpdate(final Object entity) {
  53.         if (entity instanceof Validateable) {
  54.             this.check((Validateable) entity, Type.UPDATE);
  55.         }
  56.         this.checkEmbeddables(entity, Type.UPDATE);
  57.     }
  58.  
  59.     private void check(final Validateable entity, final Type persist) {
  60.         switch (persist) {
  61.         case PERSIST:
  62.             if (entity instanceof Persist) {
  63.                 ((Persist) entity).persist();
  64.             }
  65.             if (entity instanceof PersistOrUpdate) {
  66.                 ((PersistOrUpdate) entity).persistOrUpdate();
  67.             }
  68.             break;
  69.         case UPDATE:
  70.             if (entity instanceof Update) {
  71.                 ((Update) entity).update();
  72.             }
  73.             if (entity instanceof PersistOrUpdate) {
  74.                 ((PersistOrUpdate) entity).persistOrUpdate();
  75.             }
  76.             break;
  77.  
  78.         default:
  79.             break;
  80.         }
  81.     }
  82.  
  83.     private void checkEmbeddables(final Object entity, final Type type) {
  84.         Collection<String> validatingEmbeddableProperties;
  85.         final Class<? extends Object> entityClass = entity.getClass();
  86.         synchronized (this.validatingPropertiesMap) {
  87.             if (this.validatingPropertiesMap.containsKey(entityClass)) {
  88.                 validatingEmbeddableProperties = this.validatingPropertiesMap
  89.                         .get(entity.getClass());
  90.             } else {
  91.                 validatingEmbeddableProperties = new ArrayList<String>();
  92.                 // spring beanutils: org.springframework.beans.BeanUtils
  93.                 for (final PropertyDescriptor desc : BeanUtils
  94.                         .getPropertyDescriptors(entity.getClass())) {
  95.                     final Class<?> propertyType = desc.getPropertyType();
  96.                     if (Validateable.class.isAssignableFrom(propertyType)) {
  97.                         // spring annotationutils: org.springframework.core.annotation.AnnotationUtils
  98.                         final Embeddable embeddableAnnotation = AnnotationUtils
  99.                                 .findAnnotation(propertyType, Embeddable.class);
  100.                         if (embeddableAnnotation != null) {
  101.                             validatingEmbeddableProperties.add(desc.getName());
  102.                         }
  103.                     }
  104.                 }
  105.                 this.validatingPropertiesMap.put(entityClass,
  106.                         validatingEmbeddableProperties);
  107.             }
  108.         }
  109.         if (!validatingEmbeddableProperties.isEmpty()) {
  110.             // spring beanwrapper: org.springframework.beans.BeanWrapperImpl
  111.             final BeanWrapper wrapper = new BeanWrapperImpl(entity);
  112.             for (final String propertyName : validatingEmbeddableProperties) {
  113.                 final Object propertyValue = wrapper
  114.                         .getPropertyValue(propertyName);
  115.                 if (propertyValue instanceof Validateable) {
  116.                     this.check((Validateable) propertyValue, type);
  117.                 }
  118.             }
  119.         }
  120.     }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement