Example usage for org.hibernate.type CollectionType isCollectionType

List of usage examples for org.hibernate.type CollectionType isCollectionType

Introduction

In this page you can find the example usage for org.hibernate.type CollectionType isCollectionType.

Prototype

@Override
    public boolean isCollectionType() 

Source Link

Usage

From source file:org.openmrs.module.auditlog.api.db.DAOUtils.java

License:Open Source License

/**
 * Finds all the types for associations to audit in as recursive way i.e if a Persistent type is
 * found, then we also find its collection element types and types for fields mapped as one to
 * one.//from w ww.j a v a 2 s  .  c  o  m
 * 
 * @param clazz the Class to match against
 * @param foundAssocTypes the found association types
 * @return a set of found class names
 */
private static Set<Class<?>> getAssociationTypesToAuditInternal(Class<?> clazz, Set<Class<?>> foundAssocTypes) {
    if (foundAssocTypes == null) {
        foundAssocTypes = new HashSet<Class<?>>();
    }

    ClassMetadata cmd = getSessionFactory().getClassMetadata(clazz);
    if (cmd != null) {
        for (Type type : cmd.getPropertyTypes()) {
            //If this is a OneToOne or a collection type
            if (type.isCollectionType() || OneToOneType.class.isAssignableFrom(type.getClass())) {
                CollectionType collType = (CollectionType) type;
                boolean isManyToManyColl = false;
                if (collType.isCollectionType()) {
                    collType = (CollectionType) type;
                    isManyToManyColl = ((SessionFactoryImplementor) getSessionFactory())
                            .getCollectionPersister(collType.getRole()).isManyToMany();
                }
                Class<?> assocType = type.getReturnedClass();
                if (type.isCollectionType()) {
                    assocType = collType.getElementType((SessionFactoryImplementor) getSessionFactory())
                            .getReturnedClass();
                }

                //Ignore non persistent types
                if (getSessionFactory().getClassMetadata(assocType) == null) {
                    continue;
                }

                if (!foundAssocTypes.contains(assocType)) {
                    //Don't implicitly audit types for many to many collections items
                    if (!type.isCollectionType() || (type.isCollectionType() && !isManyToManyColl)) {
                        foundAssocTypes.add(assocType);
                        //Recursively inspect each association type
                        foundAssocTypes.addAll(getAssociationTypesToAuditInternal(assocType, foundAssocTypes));
                    }
                }
            }
        }
    }
    return foundAssocTypes;
}