Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // solution for this problem: http://stackoverflow.com/questions/2948578/jpa-entitylisteners-and-embeddable
- package com.mycompany.entities;
- import java.beans.PropertyDescriptor;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Map;
- import javax.persistence.Embeddable;
- import javax.persistence.PrePersist;
- import javax.persistence.PreUpdate;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.BeanWrapper;
- import org.springframework.beans.BeanWrapperImpl;
- import org.springframework.core.annotation.AnnotationUtils;
- import com.mycompany.entities.validation.Validateable.Persist;
- import com.mycompany.entities.validation.Validateable.PersistOrUpdate;
- import com.mycompany.entities.validation.Validateable.Update;
- public class ValidatorListener {
- private enum Type {
- PERSIST, UPDATE
- }
- private Map<Class<?>, Collection<String>> validatingPropertiesMap;
- /**
- * Validate all entities that implement {@link Persist} or
- * {@link PersistOrUpdate}.
- *
- * @param entity
- */
- @PrePersist
- public void checkPersist(final Object entity) {
- if (entity instanceof Validateable) {
- this.check((Validateable) entity, Type.PERSIST);
- }
- this.checkEmbeddables(entity, Type.PERSIST);
- }
- /**
- * Validate all entities that implement {@link Update} or
- * {@link PersistOrUpdate}.
- *
- * @param entity
- */
- @PreUpdate
- public void checkUpdate(final Object entity) {
- if (entity instanceof Validateable) {
- this.check((Validateable) entity, Type.UPDATE);
- }
- this.checkEmbeddables(entity, Type.UPDATE);
- }
- private void check(final Validateable entity, final Type persist) {
- switch (persist) {
- case PERSIST:
- if (entity instanceof Persist) {
- ((Persist) entity).persist();
- }
- if (entity instanceof PersistOrUpdate) {
- ((PersistOrUpdate) entity).persistOrUpdate();
- }
- break;
- case UPDATE:
- if (entity instanceof Update) {
- ((Update) entity).update();
- }
- if (entity instanceof PersistOrUpdate) {
- ((PersistOrUpdate) entity).persistOrUpdate();
- }
- break;
- default:
- break;
- }
- }
- private void checkEmbeddables(final Object entity, final Type type) {
- Collection<String> validatingEmbeddableProperties;
- final Class<? extends Object> entityClass = entity.getClass();
- synchronized (this.validatingPropertiesMap) {
- if (this.validatingPropertiesMap.containsKey(entityClass)) {
- validatingEmbeddableProperties = this.validatingPropertiesMap
- .get(entity.getClass());
- } else {
- validatingEmbeddableProperties = new ArrayList<String>();
- // spring beanutils: org.springframework.beans.BeanUtils
- for (final PropertyDescriptor desc : BeanUtils
- .getPropertyDescriptors(entity.getClass())) {
- final Class<?> propertyType = desc.getPropertyType();
- if (Validateable.class.isAssignableFrom(propertyType)) {
- // spring annotationutils: org.springframework.core.annotation.AnnotationUtils
- final Embeddable embeddableAnnotation = AnnotationUtils
- .findAnnotation(propertyType, Embeddable.class);
- if (embeddableAnnotation != null) {
- validatingEmbeddableProperties.add(desc.getName());
- }
- }
- }
- this.validatingPropertiesMap.put(entityClass,
- validatingEmbeddableProperties);
- }
- }
- if (!validatingEmbeddableProperties.isEmpty()) {
- // spring beanwrapper: org.springframework.beans.BeanWrapperImpl
- final BeanWrapper wrapper = new BeanWrapperImpl(entity);
- for (final String propertyName : validatingEmbeddableProperties) {
- final Object propertyValue = wrapper
- .getPropertyValue(propertyName);
- if (propertyValue instanceof Validateable) {
- this.check((Validateable) propertyValue, type);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement