Search in sources :

Example 26 with Criterion

use of org.hibernate.criterion.Criterion in project ACS by ACS-Community.

the class HibernateWDALImpl method updateComponentsTableMap.

protected void updateComponentsTableMap(String curl) {
    m_logger.info("clear_cache(curl): ComponentsTable1");
    final String keyField = "Name";
    final Class type = alma.TMCDB.maci.Component.class;
    Map<String, Object> rootMap = (Map<String, Object>) rootNode;
    Map<String, Object> map = (Map<String, Object>) ((Map<String, Object>) rootMap.get("MACI")).get("Components");
    m_logger.info("clear_cache(curl): ComponentsTable2");
    try {
        Session session = hibernateUtil.getSession();
        Method accessor = DOMJavaClassIntrospector.getAccessorMethod(type, keyField);
        m_logger.info("clear_cache(curl): ComponentsTable3");
        Field field = null;
        if (accessor == null) {
            try {
                field = type.getField(keyField);
            } catch (Throwable th) {
                throw new IllegalStateException("failed to obtain key ");
            }
        }
        m_logger.info("clear_cache(curl): ComponentsTable4");
        String[] els = curl.split("/");
        String rpath = "^/*";
        String rsubpath = "^/*";
        String rcpath = "^/*";
        String rcname = els[els.length - 1];
        for (int i = 0; i < els.length; i++) {
            rpath += els[i];
            rsubpath += els[i];
            if (i < els.length - 1) {
                rpath += "/+";
                rsubpath += "/+";
                rcpath += els[i];
                if (i < els.length - 2)
                    rcpath += "/+";
            }
        }
        rpath += "/*$";
        rsubpath += "/+.*";
        rcpath += "/*$";
        System.out.println(rpath);
        System.out.println(rsubpath);
        System.out.println(rcpath + "|" + rcname);
        m_logger.info("clear_cache(curl): ComponentsTable5");
        //Consider the cases where the curl matches exactly the Path, where
        //it is part of the path and when it matches exactly the path and
        //the component name.
        Criterion cr = Restrictions.disjunction().add(getRegularExpressionRestriction("Path", rpath)).add(getRegularExpressionRestriction("Path", rsubpath)).add(Restrictions.and(getRegularExpressionRestriction("Path", rcpath), Restrictions.eq("Name", rcname)));
        m_logger.info("clear_cache(curl): ComponentsTable6");
        List list = getListForConfiguration(session, type, cr);
        m_logger.info("clear_cache(curl): ComponentsTable7");
        System.out.println("\nFound the following Components");
        for (Object data : list) {
            System.out.println(((alma.TMCDB.maci.Component) data).Path + "/" + ((alma.TMCDB.maci.Component) data).getName());
        }
        m_logger.info("clear_cache(curl): ComponentsTable8");
        //Remove the entries from existing maps.
        System.out.println("\nChecking maps to remove");
        Map rParentMap = map;
        for (int i = 0; i < els.length; i++) {
            System.out.println("Checking path " + els[i] + ".");
            System.out.println("Parent keys: " + rParentMap.keySet().toString());
            Object data = rParentMap.get(els[i]);
            if (data == null) {
                System.out.println("No element found with the given curl");
                break;
            } else {
                if (data instanceof alma.TMCDB.maci.Component) {
                    System.out.println("Instance of Component (Component!).");
                } else if (data instanceof alma.TMCDB.maci.ComponentNode) {
                    System.out.println("Instance of ComponentNode (Path!).");
                } else {
                    System.out.println("Unknown type! Details: " + data.toString());
                }
                if (i < els.length - 1) {
                    System.out.println("There are elements remaining, so we proceed to next element in path.");
                    rParentMap = ((alma.TMCDB.maci.ComponentNode) data)._;
                } else {
                    System.out.println("There are no elements remaining, we remove all entries from this element in path and on.");
                    rParentMap.remove(els[i]);
                }
            }
        }
        m_logger.info("clear_cache(curl): ComponentsTable9");
        // Sort the list by path + component to ensure that parent components are added before their children
        Comparator<alma.TMCDB.maci.Component> comparator = new Comparator<alma.TMCDB.maci.Component>() {

            public int compare(alma.TMCDB.maci.Component o1, alma.TMCDB.maci.Component o2) {
                String fullName1 = ((o1.Path == null ? "" : o1.Path) + "/") + o1.getName();
                String fullName2 = ((o2.Path == null ? "" : o2.Path) + "/") + o2.getName();
                return fullName1.compareTo(fullName2);
            }
        };
        Collections.sort(list, comparator);
        m_logger.info("clear_cache(curl): ComponentsTable10");
        for (Object data : list) {
            String baseKey;
            if (accessor != null)
                baseKey = accessor.invoke(data, (Object[]) null).toString();
            else
                //if (field != null)
                baseKey = field.get(data).toString();
            // baseKey should not be null
            Map parentMap = map;
            alma.TMCDB.maci.Component component = (alma.TMCDB.maci.Component) data;
            // some cleaning
            if (component.getComponentLogger().getMinLogLevel() == -1 && component.getComponentLogger().getMinLogLevelLocal() == -1)
                component.setComponentLogger(null);
            // now find its map
            String path = getNormalizedPath(component.Path);
            while (path != null && path.length() > 0) {
                // remove trailing slashes, to have unique curl (used for key)
                if (path.charAt(0) == '/') {
                    path = path.substring(1);
                    continue;
                }
                int pos = path.indexOf('/');
                String parentPath = (pos > 0) ? path.substring(0, pos) : path;
                String subpath = (pos > 0) ? path.substring(pos + 1, path.length()) : null;
                alma.TMCDB.maci.ComponentNode parentComponent = (alma.TMCDB.maci.ComponentNode) parentMap.get(parentPath);
                if (parentComponent == null) {
                    parentComponent = new alma.TMCDB.maci.ComponentNode();
                    parentMap.put(parentPath, parentComponent);
                }
                parentMap = parentComponent._;
                path = subpath;
            }
            // unique key generation
            int count = 0;
            String key = baseKey;
            while (parentMap.containsKey(key)) key = baseKey + String.valueOf(++count);
            parentMap.put(key, data);
            if (data instanceof alma.TMCDB.maci.Component) {
                alma.TMCDB.maci.Component comp = (alma.TMCDB.maci.Component) data;
                m_logger.finer("Loaded component name=" + comp.Path + comp.getName() + ", type=" + comp.getType() + ", container=" + comp.getContainer() + ", implLang=" + comp.getImplLang());
            } else {
                m_logger.warning("Bad component class '" + data.getClass().getName() + "' read from TMCDB.");
            }
            m_logger.info("clear_cache(curl): ComponentsTable11");
        }
        m_logger.info("clear_cache(curl): ComponentsTable12");
    } catch (Throwable th) {
        th.printStackTrace();
    }
    m_logger.info("clear_cache(curl): ComponentsTable13");
}
Also used : Method(java.lang.reflect.Method) Comparator(java.util.Comparator) Field(java.lang.reflect.Field) Criterion(org.hibernate.criterion.Criterion) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Component(alma.acs.tmcdb.Component) Map(java.util.Map) RootMap(com.cosylab.cdb.jdal.hibernate.RootMap) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Session(org.hibernate.Session)

Example 27 with Criterion

use of org.hibernate.criterion.Criterion in project dhis2-core by dhis2.

the class HibernateGenericStore method getSharingDetachedCriteria.

/**
     * Creates a sharing Criteria for the implementation Class type restricted by the
     * given Criterions.
     *
     * @param expressions the Criterions for the Criteria.
     * @return a Criteria instance.
     */
protected final Criteria getSharingDetachedCriteria(Criterion... expressions) {
    Criteria criteria = getSharingCriteria();
    for (Criterion expression : expressions) {
        criteria.add(expression);
    }
    criteria.setCacheable(cacheable);
    return criteria;
}
Also used : Criterion(org.hibernate.criterion.Criterion) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria)

Aggregations

Criterion (org.hibernate.criterion.Criterion)27 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)7 List (java.util.List)6 RootMap (com.cosylab.cdb.jdal.hibernate.RootMap)5 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)5 LinkedList (java.util.LinkedList)5 Map (java.util.Map)5 Session (org.hibernate.Session)5 Criteria (org.hibernate.Criteria)4 Field (java.lang.reflect.Field)3 Method (java.lang.reflect.Method)3 Component (alma.acs.tmcdb.Component)2 Container (alma.acs.tmcdb.Container)2 Comparator (java.util.Comparator)2 IrrelevantEntity (org.hibernate.IrrelevantEntity)2 SessionFactory (org.hibernate.SessionFactory)2 Configuration (org.hibernate.cfg.Configuration)2 DetachedCriteria (org.hibernate.criterion.DetachedCriteria)2