Example usage for org.hibernate.mapping Subclass isJoinedSubclass

List of usage examples for org.hibernate.mapping Subclass isJoinedSubclass

Introduction

In this page you can find the example usage for org.hibernate.mapping Subclass isJoinedSubclass.

Prototype

public boolean isJoinedSubclass() 

Source Link

Usage

From source file:gov.nih.nci.system.util.ClassCache.java

License:BSD License

/**
 * initialize with a list of the classes obtained from each DAO class within
 * the System// ww w  .  jav  a2 s.c  o m
 */
private void initialize() throws DAOException {

    String unqualifiedClassName = null;
    Class klass = null;

    HashSet<String> tmpPackageNames = new HashSet<String>();

    List<String> allClassNames;
    Set<String> implicitClassNames = new HashSet<String>();

    for (DAO dao : daoList) {

        allClassNames = dao.getAllClassNames();

        // Implicit superclasses have no hibernate mapping and so are not
        // part of the dao class names
        String implicitSuperclass = null;
        for (String klassName : allClassNames) {
            implicitSuperclass = klassName;
            do {
                try {
                    implicitSuperclass = Class.forName(implicitSuperclass).getSuperclass().getName();
                    log.debug("Checking if class " + implicitSuperclass + " is implicit");

                    if (!(implicitSuperclass.equalsIgnoreCase("java.lang.Object"))
                            && !(allClassNames.contains(implicitSuperclass))) {
                        log.debug("Adding " + implicitSuperclass + " as an implicit superclass");
                        implicitClassNames.add(implicitSuperclass);
                    }
                } catch (ClassNotFoundException e) {
                    log.error("Error:  Class not found: " + implicitSuperclass);
                    implicitSuperclass = null;
                }
            } while ((!implicitSuperclass.equalsIgnoreCase("java.lang.Object"))
                    && !(implicitSuperclass == null));
        }

        log.debug("Number of implicit superclasses found: " + implicitClassNames.size());
        allClassNames.addAll(implicitClassNames);

        // Certain metadata needs to be generated prior to caching the rest
        // of the info
        for (String klassName : allClassNames) {

            try {
                klass = Class.forName(klassName);
            } catch (ClassNotFoundException e) {
                log.error("ClassNotFoundException caught: ", e);
            }
            String packageName = klass.getPackage().getName();
            unqualifiedClassName = klassName.substring(klassName.lastIndexOf(".") + 1);
            log.debug("Unqualified class name: " + unqualifiedClassName);

            if ((pkgNameForClassCache.get(klassName.toLowerCase()) != null)
                    || (pkgNameForClassCache.get(unqualifiedClassName) != null)) {
                throw new DAOException(
                        "Duplicate Class name found while initializing ClassCache: " + klassName);
            }
            // Cache the package name for each klass
            pkgNameForClassCache.put(klassName.toLowerCase(), packageName);
            pkgNameForClassCache.put(unqualifiedClassName.toLowerCase(), packageName);
            nonPrimitiveFieldsCache.put(klassName, cacheNonPrimitiveFieldNames(klass));

            allFieldsCache.put(klassName, cacheAllFieldNames(klass));

            log.debug("Adding class " + klass.getName() + " to Class Cache.");
            classCache.put(klassName, klass);
            classCache.put(klassName.toLowerCase(), klass);
            classCache.put(unqualifiedClassName, klass);
            classCache.put(unqualifiedClassName.toLowerCase(), klass);

            log.debug("Adding class " + klass.getName() + " to DAO Cache for DAO: " + dao.getClass().getName());
            daoCache.put(klassName, dao);
            daoCache.put(klassName.toLowerCase(), dao);
            daoCache.put(unqualifiedClassName, dao);
            daoCache.put(unqualifiedClassName.toLowerCase(), dao);

            // Cache the identifier (id key) for each class
            if (dao instanceof ORMDAOImpl)
                classIdCache.put(klassName, ((ORMDAOImpl) dao).getClassIdentiferName(klassName));
        }

        // Certain metadata needs to be cached prior to caching the rest,
        // so here we loop through the second time now that we have the data
        // we need
        for (String klassName : allClassNames) {
            log.debug("Adding class " + klassName + " to allClassNames List");

            allQualClassNames.add(klassName);
            unqualifiedClassName = klassName.substring(klassName.lastIndexOf(".") + 1);
            log.debug("Unqualified class name: " + unqualifiedClassName);
            allUnqualClassNames.add(unqualifiedClassName);

            List<String> pkgClassNames = new ArrayList<String>();
            try {
                klass = Class.forName(klassName);

                String packageName = klass.getPackage().getName();

                // Cache all package names
                tmpPackageNames.add(packageName);

                // Cache associations for klass
                classAssociationsCache.put(klassName, cacheAssociations(klassName));

                // Collect all class names within a package
                if (!pkgClassNamesCache.containsKey(packageName)) {
                    pkgClassNames.add(klassName);
                    pkgClassNamesCache.put(packageName, pkgClassNames);
                } else {
                    List<String> existingCollection = pkgClassNamesCache.get(packageName);
                    existingCollection.add(klassName);
                }

            } catch (ClassNotFoundException cnfe) {
                log.error("Exception caught while initializing ClassCache for class: " + klassName, cnfe);
            }
        }

        for (String klassName : allClassNames) {
            log.debug("Adding class " + klassName + " to subClassCache List");
            try {
                klass = Class.forName(klassName);

                String currentKlassName = klassName;
                Class superKlass = klass.getSuperclass();
                String superKlassName = superKlass.getName();
                while (!"java.lang.Object".equals(superKlass.getName())) {
                    List<String> subKlassNames = subClassCache.get(superKlassName);
                    if (subKlassNames == null) {
                        subKlassNames = new ArrayList<String>();
                        subClassCache.put(superKlassName, subKlassNames);
                    }
                    if (!subKlassNames.contains(currentKlassName))
                        subKlassNames.add(currentKlassName);
                    currentKlassName = superKlass.getName();
                    superKlass = superKlass.getSuperclass();
                    superKlassName = superKlass.getName();
                }
            } catch (ClassNotFoundException e) {
                log.error("Exception caught while initializing ClassCache for class: " + klassName, e);
            }
        }

        if (dao instanceof ORMDAOImpl) {
            Configuration cfg = ((ORMDAOImpl) dao).getConfig();

            Iterator iter = cfg.getClassMappings();
            while (iter.hasNext()) {
                PersistentClass pklass = (PersistentClass) iter.next();
                Object identifier = null;
                log.debug("Getting discriminator details for : " + pklass.getClassName() + ":");
                if (pklass instanceof Subclass) {
                    Subclass subklass = (Subclass) pklass;
                    if (subklass.isJoinedSubclass())
                        identifier = subklass.getSubclassId();
                    else
                        identifier = getShortClassName(subklass.getClassName());
                } else if (pklass instanceof RootClass) {

                    RootClass rootklass = (RootClass) pklass;
                    if (rootklass.getDiscriminator() == null)
                        identifier = rootklass.getSubclassId();
                    else
                        identifier = getShortClassName(rootklass.getClassName());
                }
                log.debug(identifier);
                discriminatorMap.put(pklass.getClassName(), identifier);
            }
        }
        if (dao instanceof ORMDAOImpl) {
            Configuration cfg = ((ORMDAOImpl) dao).getConfig();
            for (String className : allClassNames) {
                Map<String, Map<String, List<Object>>> tempSearchFieldForObject = getMapOfSearchFields(cfg,
                        className);
                if (tempSearchFieldForObject != null)
                    searchableFieldsMap.putAll(tempSearchFieldForObject);
            }
        }
    }

    allPackageNamesCache = new ArrayList<String>(tmpPackageNames);

    Collections.sort(allPackageNamesCache);
    Collections.sort(allQualClassNames);
    Collections.sort(allUnqualClassNames);
}