| 1 |
|
package org.trails.seeddata; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
import java.util.List; |
| 5 |
|
import javax.persistence.Entity; |
| 6 |
|
|
| 7 |
|
import ognl.Ognl; |
| 8 |
|
import ognl.OgnlException; |
| 9 |
|
import org.apache.commons.logging.Log; |
| 10 |
|
import org.apache.commons.logging.LogFactory; |
| 11 |
|
import org.hibernate.criterion.DetachedCriteria; |
| 12 |
|
import org.hibernate.criterion.Restrictions; |
| 13 |
|
import org.springframework.beans.BeansException; |
| 14 |
|
import org.springframework.context.ApplicationContext; |
| 15 |
|
import org.springframework.context.ApplicationContextAware; |
| 16 |
|
import org.springframework.transaction.annotation.Transactional; |
| 17 |
|
import org.trails.descriptor.DescriptorService; |
| 18 |
|
import org.trails.descriptor.IClassDescriptor; |
| 19 |
|
import org.trails.descriptor.IPropertyDescriptor; |
| 20 |
|
import org.trails.persistence.HibernatePersistenceService; |
| 21 |
|
import org.trails.validation.ValidateUniqueness; |
| 22 |
|
|
| 23 |
2 |
public class SpringSeedEntityInitializer implements ApplicationContextAware, SeedDataInitializer |
| 24 |
|
{ |
| 25 |
1 |
private static final Log log = LogFactory.getLog(SpringSeedEntityInitializer.class); |
| 26 |
|
|
| 27 |
|
private HibernatePersistenceService persistenceService; |
| 28 |
|
|
| 29 |
|
private ApplicationContext applicationContext; |
| 30 |
|
|
| 31 |
|
private DescriptorService descriptorService; |
| 32 |
|
|
| 33 |
|
public void setPersistenceService(HibernatePersistenceService persistenceService) |
| 34 |
|
{ |
| 35 |
1 |
this.persistenceService = persistenceService; |
| 36 |
1 |
} |
| 37 |
|
|
| 38 |
|
public void setDescriptorService(DescriptorService descriptorService) |
| 39 |
|
{ |
| 40 |
1 |
this.descriptorService = descriptorService; |
| 41 |
1 |
} |
| 42 |
|
|
| 43 |
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException |
| 44 |
|
{ |
| 45 |
1 |
this.applicationContext = applicationContext; |
| 46 |
1 |
} |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
@Transactional |
| 52 |
|
public void init() |
| 53 |
|
{ |
| 54 |
1 |
String[] beanNames = applicationContext.getBeanDefinitionNames(); |
| 55 |
1 |
descriptorService.getAllDescriptors(); |
| 56 |
|
|
| 57 |
28 |
for (String beanName : beanNames) |
| 58 |
|
{ |
| 59 |
27 |
Object object = applicationContext.getBean(beanName); |
| 60 |
27 |
if (object.getClass().getAnnotation(Entity.class) != null && object != this) |
| 61 |
|
{ |
| 62 |
6 |
IClassDescriptor classDescriptor = descriptorService.getClassDescriptor(object.getClass()); |
| 63 |
6 |
if (classDescriptor == null) |
| 64 |
|
{ |
| 65 |
0 |
log.error("Cannot handle entity of type " + object.getClass() + " because of non-existent class descriptor"); |
| 66 |
0 |
log.warn("Skipped seeding the entity bean " + beanName + ", check that hibernate configuration exists in the correct location and/or is generated correctly"); |
| 67 |
0 |
continue; |
| 68 |
|
} |
| 69 |
6 |
IPropertyDescriptor identifierDescriptor = classDescriptor.getIdentifierDescriptor(); |
| 70 |
6 |
Object id = null, savedObject = null; |
| 71 |
6 |
String propertyName = identifierDescriptor.getName(); |
| 72 |
|
try |
| 73 |
|
{ |
| 74 |
6 |
id = Ognl.getValue(propertyName, object); |
| 75 |
0 |
} catch (OgnlException e) |
| 76 |
|
{ |
| 77 |
0 |
log.warn("Couldn't get the id of a seed bean " + object + " because of: ", e); |
| 78 |
|
} |
| 79 |
|
|
| 80 |
|
|
| 81 |
6 |
ValidateUniqueness validateUniqueness = object.getClass().getAnnotation(ValidateUniqueness.class); |
| 82 |
6 |
if (validateUniqueness == null && id == null) |
| 83 |
|
{ |
| 84 |
4 |
log.info("Entity of type " + object.getClass() + " doesn't have uniquely identifying property. Searching using the whole entity as an example " + object); |
| 85 |
4 |
List objects = persistenceService.getInstances(object, classDescriptor); |
| 86 |
4 |
if (objects.size() == 0) log.info("Couldn't find an existing seed entity"); |
| 87 |
0 |
else if (objects.size() == 1) |
| 88 |
|
{ |
| 89 |
0 |
log.info("Found exactly one existing matching entity, assuming it is an earlier seeded entity"); |
| 90 |
0 |
savedObject = objects.get(0); |
| 91 |
|
} else |
| 92 |
|
{ |
| 93 |
0 |
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."); |
| 94 |
0 |
continue; |
| 95 |
|
} |
| 96 |
|
} else |
| 97 |
|
{ |
| 98 |
2 |
DetachedCriteria criteria = DetachedCriteria.forClass(object.getClass()); |
| 99 |
2 |
if (validateUniqueness != null) |
| 100 |
|
{ |
| 101 |
1 |
propertyName = validateUniqueness.property(); |
| 102 |
|
|
| 103 |
|
try |
| 104 |
|
{ |
| 105 |
1 |
Object value = Ognl.getValue(propertyName, object); |
| 106 |
1 |
if (value == null) criteria.add(Restrictions.isNull(propertyName)); |
| 107 |
1 |
else criteria.add(Restrictions.eq(propertyName, value)); |
| 108 |
0 |
} catch (OgnlException e) |
| 109 |
|
{ |
| 110 |
0 |
log.error("Couldn't find if an entity already exists because of: ", e); |
| 111 |
|
} |
| 112 |
1 |
} else criteria.add(Restrictions.eq(propertyName, id)); |
| 113 |
|
|
| 114 |
2 |
savedObject = persistenceService.getInstance(object.getClass(), criteria); |
| 115 |
|
} |
| 116 |
|
|
| 117 |
6 |
if (savedObject != null) |
| 118 |
|
{ |
| 119 |
|
try |
| 120 |
|
{ |
| 121 |
0 |
log.info("Entity of type " + object.getClass() + " identified by unique property " + propertyName + " " + Ognl.getValue(propertyName, savedObject) + " already exists"); |
| 122 |
0 |
} catch (OgnlException e) |
| 123 |
|
{ |
| 124 |
0 |
log.warn("Entity of type " + object.getClass() + " identified by unique property " + propertyName + " exists, but couldn't display value of identifying property because of: ", e); |
| 125 |
|
} |
| 126 |
|
|
| 127 |
|
|
| 128 |
|
try |
| 129 |
|
{ |
| 130 |
0 |
id = Ognl.getValue(identifierDescriptor.getName(), savedObject); |
| 131 |
0 |
Ognl.setValue(identifierDescriptor.getName(), object, id); |
| 132 |
0 |
} catch (OgnlException e) |
| 133 |
|
{ |
| 134 |
0 |
log.warn("Couldn't set the id of an already existing entity because of: ", e); |
| 135 |
|
} |
| 136 |
0 |
continue; |
| 137 |
|
} |
| 138 |
6 |
persistenceService.saveOrUpdate(object); |
| 139 |
|
} |
| 140 |
|
} |
| 141 |
1 |
} |
| 142 |
|
} |