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");
}
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;
}
Aggregations