001 package org.trails.seeddata;
002
003
004 import ognl.Ognl;
005 import ognl.OgnlException;
006 import org.apache.commons.logging.Log;
007 import org.apache.commons.logging.LogFactory;
008 import org.hibernate.criterion.DetachedCriteria;
009 import org.hibernate.criterion.Restrictions;
010 import org.hibernate.validator.InvalidStateException;
011 import org.hibernate.validator.InvalidValue;
012 import org.springframework.beans.BeansException;
013 import org.springframework.beans.factory.BeanIsAbstractException;
014 import org.springframework.context.ApplicationContext;
015 import org.springframework.context.ApplicationContextAware;
016 import org.springframework.transaction.annotation.Transactional;
017 import org.trails.descriptor.DescriptorService;
018 import org.trails.descriptor.IClassDescriptor;
019 import org.trails.descriptor.IPropertyDescriptor;
020 import org.trails.persistence.HibernatePersistenceService;
021 import org.trails.persistence.PersistenceException;
022 import org.trails.validation.ValidateUniqueness;
023
024 import javax.persistence.Entity;
025 import java.util.List;
026
027 public class SpringSeedEntityInitializer implements ApplicationContextAware, SeedDataInitializer
028 {
029 private static final Log log = LogFactory.getLog(SpringSeedEntityInitializer.class);
030
031 private HibernatePersistenceService persistenceService;
032
033 private ApplicationContext applicationContext;
034
035 private DescriptorService descriptorService;
036
037 public void setPersistenceService(HibernatePersistenceService persistenceService)
038 {
039 this.persistenceService = persistenceService;
040 }
041
042 public void setDescriptorService(DescriptorService descriptorService)
043 {
044 this.descriptorService = descriptorService;
045 }
046
047 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
048 {
049 this.applicationContext = applicationContext;
050 }
051
052 /* (non-Javadoc)
053 * @see org.trails.seeddata.SeedDataInitializer#init()
054 */
055 @Transactional
056 public void init()
057 {
058 String[] beanNames = applicationContext.getBeanDefinitionNames();
059 descriptorService.getAllDescriptors();
060
061 for (String beanName : beanNames) {
062 try {
063 Object object = applicationContext.getBean(beanName);
064
065 if (object.getClass().getAnnotation(Entity.class) != null && object != this)
066 {
067 IClassDescriptor classDescriptor = descriptorService.getClassDescriptor(object.getClass());
068 if (classDescriptor == null)
069 {
070 log.error("Cannot handle entity of type " + object.getClass() + " because of non-existent class descriptor");
071 log.warn("Skipped seeding the entity bean " + beanName + ", check that hibernate configuration exists in the correct location and/or is generated correctly");
072 continue;
073 }
074 IPropertyDescriptor identifierDescriptor = classDescriptor.getIdentifierDescriptor();
075 Object id = null, savedObject = null;
076 String propertyName = identifierDescriptor.getName();
077 try
078 {
079 id = Ognl.getValue(propertyName, object);
080 } catch (OgnlException e)
081 {
082 log.warn("Couldn't get the id of a seed bean " + object + " because of: ", e);
083 }
084
085 // Try to find if a persistent entity already exists based on unique property or manually set id
086 ValidateUniqueness validateUniqueness = object.getClass().getAnnotation(ValidateUniqueness.class);
087 if (validateUniqueness == null && id == null)
088 {
089 log.info("Entity of type " + object.getClass() + " doesn't have uniquely identifying property. Searching using the whole entity as an example " + object);
090 List objects = persistenceService.getInstances(object, classDescriptor);
091 if (objects.size() == 0) log.info("Couldn't find an existing seed entity");
092 else if (objects.size() == 1)
093 {
094 log.info("Found exactly one existing matching entity, assuming it is an earlier seeded entity");
095 savedObject = objects.get(0);
096 } else
097 {
098 log.warn("Found more than one existing entity based on the seed entity example, won't add a new one. You should make sure seed entities can be uniquely identified.");
099 continue;
100 }
101 } else
102 {
103 DetachedCriteria criteria = DetachedCriteria.forClass(object.getClass());
104 if (validateUniqueness != null)
105 {
106 propertyName = validateUniqueness.property();
107
108 try
109 {
110 Object value = Ognl.getValue(propertyName, object);
111 if (value == null) criteria.add(Restrictions.isNull(propertyName));
112 else criteria.add(Restrictions.eq(propertyName, value));
113 } catch (OgnlException e)
114 {
115 log.error("Couldn't find if an entity already exists because of: ", e);
116 }
117 } else criteria.add(Restrictions.eq(propertyName, id));
118
119 savedObject = persistenceService.getInstance(object.getClass(), criteria);
120 }
121
122 if (savedObject != null)
123 {
124 try
125 {
126 log.info("Entity of type " + object.getClass() + " identified by unique property " + propertyName + " " + Ognl.getValue(propertyName, savedObject) + " already exists");
127 } catch (OgnlException e)
128 {
129 log.warn("Entity of type " + object.getClass() + " identified by unique property " + propertyName + " exists, but couldn't display value of identifying property because of: ", e);
130 }
131
132 // Need to set the ids to seed beans so a new seed entity with a relationship to existing seed entities can be saved
133 try
134 {
135 id = Ognl.getValue(identifierDescriptor.getName(), savedObject);
136 Ognl.setValue(identifierDescriptor.getName(), object, id);
137 } catch (OgnlException e)
138 {
139 log.warn("Couldn't set the id of an already existing entity because of: ", e);
140 }
141 continue;
142 }
143 try
144 {
145 persistenceService.saveOrUpdate(object);
146 } catch (InvalidStateException ivex)
147 {
148 StringBuilder erroMessageBuilder = new StringBuilder();
149 for (InvalidValue invalidValue : ivex.getInvalidValues())
150 {
151 String message = invalidValue.getPropertyName() + ": " + invalidValue.getMessage();
152 log.fatal(message);
153 erroMessageBuilder.append(message).append("\n");
154 }
155 throw new PersistenceException(erroMessageBuilder.toString(), ivex);
156 }
157 }
158 } catch (BeanIsAbstractException e) {
159 log.debug("Bean named " + beanName + " is abstract, ignore from entity bean discovery");
160 }
161 }
162 }
163 }