import java.util.Map;
import java.util.Set;
-import javax.naming.NamingException;
import javax.naming.Reference;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
+import org.hibernate.collection.internal.AbstractPersistentCollection;
+import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
-import org.hibernate.impl.SessionFactoryImpl;
-import org.hibernate.impl.SessionImpl;
+import org.hibernate.engine.spi.CollectionEntry;
+import org.hibernate.engine.spi.SessionFactoryImplementor;
+import org.hibernate.engine.spi.SessionImplementor;
+import org.hibernate.internal.SessionFactoryImpl;
+import org.hibernate.internal.SessionImpl;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.metadata.CollectionMetadata;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.type.FloatType;
import org.hibernate.type.IntegerType;
import org.hibernate.type.LongType;
+import org.hibernate.type.MaterializedClobType;
import org.hibernate.type.SerializableType;
import org.hibernate.type.SetType;
-import org.hibernate.type.StringClobType;
import org.hibernate.type.StringType;
import org.hibernate.type.Type;
-import org.joda.time.contrib.hibernate.PersistentDateTime;
+import org.jadira.usertype.dateandtime.joda.PersistentDateTime;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import eu.etaxonomy.cdm.common.CdmUtils;
import eu.etaxonomy.cdm.common.DoubleResult;
+import eu.etaxonomy.cdm.datagenerator.FullCoverageDataGenerator;
+import eu.etaxonomy.cdm.hibernate.DOIUserType;
+import eu.etaxonomy.cdm.hibernate.EnumUserType;
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
import eu.etaxonomy.cdm.hibernate.PartialUserType;
import eu.etaxonomy.cdm.hibernate.URIUserType;
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
import eu.etaxonomy.cdm.model.common.Annotation;
import eu.etaxonomy.cdm.model.common.CdmBase;
-import eu.etaxonomy.cdm.model.common.CdmMetaData;
import eu.etaxonomy.cdm.model.common.Extension;
import eu.etaxonomy.cdm.model.common.ICdmBase;
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
import eu.etaxonomy.cdm.model.common.Marker;
+import eu.etaxonomy.cdm.model.metadata.CdmMetaData;
import eu.etaxonomy.cdm.model.name.BotanicalName;
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
import eu.etaxonomy.cdm.model.taxon.Taxon;
super(CdmBase.class);
}
- /* (non-Javadoc)
- * @see eu.etaxonomy.cdm.persistence.dao.common.ICdmGenericDao#getCdmBasesByFieldAndClass(java.lang.Class, java.lang.String, eu.etaxonomy.cdm.model.common.CdmBase)
- */
-
+ @Override
public List<CdmBase> getCdmBasesByFieldAndClass(Class clazz, String propertyName, CdmBase referencedCdmBase){
Session session = super.getSession();
+
Criteria criteria = session.createCriteria(clazz);
criteria.add(Restrictions.eq(propertyName, referencedCdmBase));
return criteria.list();
}
- /* (non-Javadoc)
- * @see eu.etaxonomy.cdm.persistence.dao.common.ICdmGenericDao#getCdmBasesByFieldAndClass(java.lang.Class, java.lang.String, eu.etaxonomy.cdm.model.common.CdmBase)
- */
+ @Override
public List<CdmBase> getCdmBasesWithItemInCollection(Class itemClass, Class clazz, String propertyName, CdmBase item){
Session session = super.getSession();
String thisClassStr = itemClass.getSimpleName();
return result;
}
- /* (non-Javadoc)
- * @see eu.etaxonomy.cdm.persistence.dao.common.ICdmGenericDao#getAllCdmClasses(boolean)
- */
+ @Override
public Set<Class<? extends CdmBase>> getAllCdmClasses(boolean includeAbstractClasses){
Set<Class<? extends CdmBase>> result = new HashSet<Class<? extends CdmBase>>();
return result;
}
+ @Override
public Set<CdmBase> getReferencingObjects(CdmBase referencedCdmBase){
Set<CdmBase> result = new HashSet<CdmBase>();
try {
throw new RuntimeException(e);
}
+ }
+ @Override
+ public Set<CdmBase> getReferencingObjectsForDeletion(CdmBase referencedCdmBase){
+ Set<CdmBase> result = getReferencingObjects(referencedCdmBase);
+ Set<ReferenceHolder> holderSet = referenceMap.get(IdentifiableEntity.class);
+ try {
+ if (holderSet == null){
+ holderSet = makeHolderSet(IdentifiableEntity.class);
+ referenceMap.put(IdentifiableEntity.class, holderSet);
+ }
+ Set<CdmBase> resultIdentifiableEntity = new HashSet<CdmBase>();
+ for (ReferenceHolder refHolder: holderSet){
+ handleReferenceHolder(referencedCdmBase, resultIdentifiableEntity, refHolder);
+ }
+ result.removeAll(resultIdentifiableEntity);
+
+ return result;
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new RuntimeException(e);
+ }
+
}
/**
* @throws NoSuchFieldException
* @throws ClassNotFoundException
*/
- private Set<ReferenceHolder> makeHolderSet(Class referencedClass) throws ClassNotFoundException, NoSuchFieldException {
+ private Set<ReferenceHolder> makeHolderSet(Class<?> referencedClass) throws ClassNotFoundException, NoSuchFieldException {
Set<ReferenceHolder> result = new HashSet<ReferenceHolder>();
//init
private void makePropertyType(
// CdmBase referencedCdmBase,
Set<ReferenceHolder> result,
- Class referencedClass,
+ Class<?> referencedClass,
SessionFactory sessionFactory, Class<? extends CdmBase> cdmClass,
Type propertyType, String propertyName, boolean isCollection)
throws ClassNotFoundException, NoSuchFieldException {
if (propertyType.isEntityType()){
EntityType entityType = (EntityType)propertyType;
String associatedEntityName = entityType.getAssociatedEntityName();
- Class entityClass = Class.forName(associatedEntityName);
+ Class<?> entityClass = Class.forName(associatedEntityName);
if (entityClass.isInterface()){
logger.debug("There is an interface");
}
}else if (propertyType.isCollectionType()){
CollectionType collectionType = (CollectionType)propertyType;
//String role = collectionType.getRole();
- Type elType = collectionType.getElementType((SessionFactoryImpl)sessionFactory);
+ Type elType = collectionType.getElementType((SessionFactoryImplementor)sessionFactory);
makePropertyType(result, referencedClass, sessionFactory, cdmClass, elType, propertyName, true);
}else if (propertyType.isAnyType()){
- AnyType anyType = (AnyType)propertyType;
+// AnyType anyType = (AnyType)propertyType;
Field field = cdmClass.getDeclaredField(propertyName);
- Class returnType = field.getType();
+ Class<?> returnType = field.getType();
if (returnType.isInterface()){
logger.debug("There is an interface");
}
}
- private boolean makeSingleProperty(Class itemClass, Class type, String propertyName, Class cdmClass, Set<ReferenceHolder> result,/*CdmBase item,*/ boolean isCollection){
+ private boolean makeSingleProperty(Class itemClass, Class<?> type, String propertyName, Class cdmClass, Set<ReferenceHolder> result,/*CdmBase item,*/ boolean isCollection){
String fieldName = StringUtils.rightPad(propertyName, 30);
String className = StringUtils.rightPad(cdmClass.getSimpleName(), 30);
String returnTypeName = StringUtils.rightPad(type.getSimpleName(), 30);
*/
private boolean isNoDoType(Type propertyType) {
boolean result = false;
- Class[] classes = new Class[]{
+ Class<?>[] classes = new Class[]{
PersistentDateTime.class,
WSDLDefinitionUserType.class,
UUIDUserType.class,
StringType.class,
BooleanType.class,
IntegerType.class,
- StringClobType.class,
+ MaterializedClobType.class,
LongType.class,
FloatType.class,
SerializableType.class,
DoubleType.class,
URIUserType.class,
- EnumType.class
+ EnumType.class,
+ EnumUserType.class,
+ DOIUserType.class
};
Set<String> classNames = new HashSet<String>();
- for (Class clazz: classes){
+ for (Class<?> clazz: classes){
classNames.add(clazz.getCanonicalName());
if (clazz == propertyType.getClass()){
return true;
return result;
}
- /* (non-Javadoc)
- * @see eu.etaxonomy.cdm.persistence.dao.common.ICdmGenericDao#getHqlResult(java.lang.String)
- */
+ @Override
public List<CdmBase> getHqlResult(String hqlQuery){
Query query = getSession().createQuery(hqlQuery);
List<CdmBase> result = query.list();
return result;
}
+ @Override
+ public Query getHqlQuery(String hqlQuery){
+ Query query = getSession().createQuery(hqlQuery);
+ return query;
+ }
+
+ @Override
public <T extends CdmBase> void merge(T cdmBase1, T cdmBase2, IMergeStrategy mergeStrategy) throws MergeException {
Class<T> clazz = (Class<T>)cdmBase1.getClass();
SessionImpl session = (SessionImpl) getSession();
SessionFactoryImpl sessionFactory = (SessionFactoryImpl) session.getSessionFactory();
- Map allClassMetadata = sessionFactory.getAllClassMetadata();
+ Map<String, ClassMetadata> allClassMetadata = sessionFactory.getAllClassMetadata();
//TODO cast
getCollectionRoles(clazz, sessionFactory);
// TODO Auto-generated catch block
e.printStackTrace();
}
- Statistics statistics = sessionFactory.getStatistics();
+// Statistics statistics = sessionFactory.getStatistics();
logger.debug("");
ClassMetadata taxonMetaData = sessionFactory.getClassMetadata(Taxon.class);
String ename = taxonMetaData.getEntityName();
try {
Reference ref = sessionFactory.getReference();
logger.debug("");
- } catch (NamingException e) {
+ } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
+
//sessionFactory.get
ClassMetadata classMetadata = getSession().getSessionFactory().getClassMetadata(clazz);
Type[] propertyTypes = classMetadata.getPropertyTypes();
}else if (propertyType.isCollectionType()){
CollectionType collectionType = (CollectionType)propertyType;
String role = collectionType.getRole();
- Type elType = collectionType.getElementType((SessionFactoryImpl)sessionFactory);
+ Type elType = collectionType.getElementType((SessionFactoryImplementor)sessionFactory);
String n = collectionType.getAssociatedEntityName(sessionFactory);
CollectionMetadata collMetadata = sessionFactory.getCollectionMetadata(role);
if (collMetadata instanceof OneToManyPersister){
}
}
- /* (non-Javadoc)
- * @see eu.etaxonomy.cdm.persistence.dao.common.ICdmGenericDao#test()
- */
- public void test() {
+ //TODO Move to test classes if still needed
+ private void test() {
SessionFactoryImpl factory = (SessionFactoryImpl)getSession().getSessionFactory();
Type propType = factory.getReferencedPropertyType(BotanicalName.class.getCanonicalName(), "titleCache");
Map collMetadata = factory.getAllCollectionMetadata();
}
+ @Override
public <T extends CdmBase> T find(Class<T> clazz, int id){
Session session;
session = getSession();
//session = getSession().getSessionFactory().getCurrentSession();
- return (T)session.get(clazz, id);
+ Object o = session.get(clazz, id);
+ return (T)o;
}
- /* (non-Javadoc)
- * @see eu.etaxonomy.cdm.persistence.dao.common.ICdmGenericDao#findMatching(eu.etaxonomy.cdm.strategy.match.IMatchable, eu.etaxonomy.cdm.strategy.match.IMatchStrategy)
- */
+ @Override
public <T extends IMatchable> List<T> findMatching(T objectToMatch,
IMatchStrategy matchStrategy) throws MatchException {
try {
}
}
- public <T extends IMatchable> List<T> findMatchingNullSafe(T objectToMatch, IMatchStrategy matchStrategy) throws IllegalArgumentException, IllegalAccessException, MatchException {
+ private <T extends IMatchable> List<T> findMatchingNullSafe(T objectToMatch, IMatchStrategy matchStrategy) throws IllegalArgumentException, IllegalAccessException, MatchException {
List<T> result = new ArrayList<T>();
Session session = getSession();
- Class matchClass = objectToMatch.getClass();
+ Class<?> matchClass = objectToMatch.getClass();
ClassMetadata classMetaData = session.getSessionFactory().getClassMetadata(matchClass.getCanonicalName());
Criteria criteria = session.createCriteria(matchClass);
boolean noMatch = makeCriteria(objectToMatch, matchStrategy, classMetaData, criteria);
boolean cacheProtected = (Boolean)cacheMatcher.getProtectedField(matching).get(objectToMatch);
if (cacheProtected == true){
String cacheValue = (String)cacheMatcher.getField().get(objectToMatch);
- if (CdmUtils.isEmpty(cacheValue)){
+ if (StringUtils.isBlank(cacheValue)){
return true; //no match
}else{
criteria.add(Restrictions.eq(cacheMatcher.getPropertyName(), cacheValue));
List<CdmMetaData> results = crit.list();
return results;
}
+
+ @Override
+ public PersistentCollection initializeCollection(PersistentCollection col) {
+ Session session = getSession();
+ col.setCurrentSession((SessionImplementor) session);
+
+ if(!((SessionImplementor)session).getPersistenceContext().getCollectionEntries().containsKey(col)) {
+ ((SessionImplementor)session).getPersistenceContext().addUninitializedDetachedCollection(
+ ((SessionImplementor)session).getFactory().getCollectionPersister( col.getRole() ),col);
+ }
+ col.forceInitialization();
+ logger.debug("initialising persistent collection with with role : " + col.getRole() + " and key : " + col.getKey());
+ return col;
+ }
+
+ @Override
+ public boolean isEmpty(PersistentCollection col) {
+ return initializeCollection(col).empty();
+ }
+
+ @Override
+ public int size(PersistentCollection col) {
+ logger.debug("remote size() - for role : " + col.getRole() + " and key : " + col.getKey());
+ initializeCollection(col);
+ SessionImplementor session = (SessionImplementor)getSession();
+ CollectionEntry entry = session.getPersistenceContext().getCollectionEntry(col);
+
+ if ( entry != null ) {
+
+ CollectionPersister persister = entry.getLoadedPersister();
+ return persister.getSize( entry.getLoadedKey(), session );
+ }
+ return -1;
+ }
+
+ @Override
+ public Object get(PersistentCollection col, int index) {
+ logger.debug("remote get() - for role : " + col.getRole() + " and key : " + col.getKey());
+ initializeCollection(col);
+ SessionImplementor session = (SessionImplementor)getSession();
+ CollectionEntry entry = session.getPersistenceContext().getCollectionEntry(col);
+
+ if ( entry != null ) {
+
+ CollectionPersister persister = entry.getLoadedPersister();
+ return persister.getElementByIndex(entry.getLoadedKey(), index, session, col);
+
+ }
+ //FIXME:Remoting Should we not be throwing an exception here ?
+ return null;
+ }
+
+ @Override
+ public boolean contains(PersistentCollection col, Object element) {
+ logger.debug("remote contains() - for role : " + col.getRole() + " and key : " + col.getKey());
+ initializeCollection(col);
+ SessionImplementor session = (SessionImplementor)getSession();
+ CollectionEntry entry = session.getPersistenceContext().getCollectionEntry(col);
+
+ if ( entry != null ) {
+
+ CollectionPersister persister = entry.getLoadedPersister();
+ return persister.elementExists(entry.getLoadedKey(), element, session);
+ }
+ //FIXME:Remoting Should we not be throwing an exception here ?
+ return false;
+ }
+
+ @Override
+ public boolean containsKey(PersistentCollection col, Object key) {
+ logger.debug("remote containsKey() - for role : " + col.getRole() + " and key : " + col.getKey());
+ initializeCollection(col);
+ SessionImplementor session = (SessionImplementor)getSession();
+ CollectionEntry entry = session.getPersistenceContext().getCollectionEntry(col);
+
+ if ( entry != null ) {
+
+ CollectionPersister persister = entry.getLoadedPersister();
+ return persister.indexExists(entry.getLoadedKey(), key, session);
+ }
+ //FIXME:Remoting Should we not be throwing an exception here ?
+ return false;
+
+ }
+
+ @Override
+ public boolean containsValue(PersistentCollection col, Object element) {
+ return contains(col, element);
+ }
+
+ @Override
+ public void createFullSampleData() {
+ FullCoverageDataGenerator dataGenerator = new FullCoverageDataGenerator();
+ dataGenerator.fillWithData(getSession());
+ }
+
}