# HG changeset patch # User casties # Date 1472494613 -7200 # Node ID 1cd9d9a67cad90f46bce3d07a127226464a67ac2 # Parent 5737ab564b947dab8b90efbf7a5a969a694ba14e added regular expression match option to AttributeFilter and searchEntityByAttribute. added regexp() function to mysql dialect to use mysql REGEXP. diff -r 5737ab564b94 -r 1cd9d9a67cad src/main/java/org/mpi/openmind/cache/WrapperService.java --- a/src/main/java/org/mpi/openmind/cache/WrapperService.java Fri Aug 26 19:09:57 2016 +0200 +++ b/src/main/java/org/mpi/openmind/cache/WrapperService.java Mon Aug 29 20:16:53 2016 +0200 @@ -49,7 +49,16 @@ srcObjClass); } - public Map searchEntityByAttributeFilter( + /** + * Search entities matching a list of attribute filters. + * + * Returns a Map of Entities and Attributes. + * + * @param filters + * @param maxResult + * @return + */ + public Map searchEntityByAttributeFilter ( List filters, int maxResult) { return this.getPS().searchEntityByAttributeFilter(filters, maxResult); } diff -r 5737ab564b94 -r 1cd9d9a67cad src/main/java/org/mpi/openmind/repository/bo/Entity.java --- a/src/main/java/org/mpi/openmind/repository/bo/Entity.java Fri Aug 26 19:09:57 2016 +0200 +++ b/src/main/java/org/mpi/openmind/repository/bo/Entity.java Mon Aug 29 20:16:53 2016 +0200 @@ -1018,13 +1018,29 @@ return "[" + this.getId() + "] " + this.getOwnValue(); } + /* (non-Javadoc) + * @see java.lang.Comparable#compareTo(java.lang.Object) + */ @Override public int compareTo(Entity e) { + // compares by own value if(e == null) return 1; - if(StringUtils.isNotEmpty(this.getOwnValue())) - return this.getOwnValue().compareTo(e.getOwnValue()); - else return 0; + String ov = this.getOwnValue(); + String eov = e.getOwnValue(); + if (ov != null && eov != null) { + // String <> String + return ov.compareTo(eov); + } else if (ov == null && eov == null){ + // null == null + return 0; + } else if (ov == null) { + // null <> String + return -1; + } else { + // String <> null + return 1; + } } public Relation containsSrcRel(Relation other){ diff -r 5737ab564b94 -r 1cd9d9a67cad src/main/java/org/mpi/openmind/repository/services/PersistenceService.java --- a/src/main/java/org/mpi/openmind/repository/services/PersistenceService.java Fri Aug 26 19:09:57 2016 +0200 +++ b/src/main/java/org/mpi/openmind/repository/services/PersistenceService.java Mon Aug 29 20:16:53 2016 +0200 @@ -1375,12 +1375,13 @@ hql += " AND "; } if (StringUtils.isNotEmpty(filter.getOwnValue())) { - if (filter.isNormalize()) { - hql += "att.normalizedOwnValue like :ownValue" + count - + " "; - } else { - hql += "att.ownValue like :ownValue" + count + " "; - } + if (filter.isNormalize()) { + hql += "att.normalizedOwnValue LIKE :ownValue" + count + " "; + } else if (filter.isRegex()) { + hql += "REGEXP(att.ownValue, :ownValue" + count + ") = 1 "; + } else { + hql += "att.ownValue LIKE :ownValue" + count + " "; + } } hql += " ) "; @@ -1389,29 +1390,49 @@ hql += " OR "; } - /* ROC: led to hibernate errors */ - hql += " ) group by att.sourceId order by ent.ownValue"; - // hql += " ) order by ent.ownValue"; + /* ROC: led to hibernate errors when using search by attribute + hql += " ) group by att.sourceId order by ent.ownValue"; + */ + hql += " ) order by ent.ownValue"; + // create hibernate Query from hql Query query = session.createQuery(hql); - if (maxResult > 0) + + // set maxResults + if (maxResult > 0) { query.setMaxResults(maxResult); + } + + // only query CURRENT_VERSIONs query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); count = 0; - for (AttributeFilter filter : filters) { - if (StringUtils.isNotEmpty(filter.getName())) - query.setString("name" + count, filter.getName()); - if (StringUtils.isNotEmpty(filter.getOwnValue())) - query.setString("ownValue" + count, - "%" + filter.getOwnValue() + "%"); - if (StringUtils.isNotEmpty(filter.getEntObjectClass())) - query.setString("sourceObjectClass" + count, - filter.getEntObjectClass()); - count++; - } + // map AttributeFilters to query parameters + for (AttributeFilter filter : filters) { + if (StringUtils.isNotEmpty(filter.getName())) { + // objectClass = :name + query.setString("name" + count, filter.getName()); + } + if (StringUtils.isNotEmpty(filter.getOwnValue())) { + if (filter.isRegex()) { + // ownValue rlike :ownValue + query.setString("ownValue" + count, filter.getOwnValue()); + } else { + // ownValue like %:ownValue% + query.setString("ownValue" + count, "%" + filter.getOwnValue() + "%"); + } + } + if (StringUtils.isNotEmpty(filter.getEntObjectClass())) { + // sourceObjectClass = :sourceObjectClass + query.setString("sourceObjectClass" + count, filter.getEntObjectClass()); + } + count++; + } + // execute query List listO = query.list(); + + // pack query result in Map for (Object o : listO) { Object[] array = (Object[]) o; if (array.length > 1) { @@ -1423,9 +1444,9 @@ } session.getTransaction().commit(); + } catch (Exception e) { logger.error(e.getMessage()); - e.printStackTrace(); } return result; } diff -r 5737ab564b94 -r 1cd9d9a67cad src/main/java/org/mpi/openmind/repository/services/utils/AttributeFilter.java --- a/src/main/java/org/mpi/openmind/repository/services/utils/AttributeFilter.java Fri Aug 26 19:09:57 2016 +0200 +++ b/src/main/java/org/mpi/openmind/repository/services/utils/AttributeFilter.java Mon Aug 29 20:16:53 2016 +0200 @@ -5,10 +5,9 @@ * * @author jurzua */ -public class AttributeFilter extends Filter{ +public class AttributeFilter extends Filter { private String name; private String entObjectClass; - public AttributeFilter(){} public AttributeFilter(String name, String value, String entObjectClass){ @@ -17,6 +16,13 @@ this.setName(name); } + public AttributeFilter(String name, String value, String entObjectClass, boolean isRegex){ + this.setEntObjectClass(entObjectClass); + this.setOwnValue(value); + this.setName(name); + this.setRegex(isRegex); + } + public String getEntObjectClass() { return entObjectClass; } diff -r 5737ab564b94 -r 1cd9d9a67cad src/main/java/org/mpi/openmind/repository/services/utils/Filter.java --- a/src/main/java/org/mpi/openmind/repository/services/utils/Filter.java Fri Aug 26 19:09:57 2016 +0200 +++ b/src/main/java/org/mpi/openmind/repository/services/utils/Filter.java Mon Aug 29 20:16:53 2016 +0200 @@ -10,6 +10,7 @@ private String ownValue; private boolean normalize = false; + protected boolean isRegex = false; public boolean isNormalize() { return normalize; @@ -26,4 +27,18 @@ public void setOwnValue(String value) { this.ownValue = value; } + + /** + * @return the isRegex + */ + public boolean isRegex() { + return isRegex; + } + + /** + * @param isRegex the isRegex to set + */ + public void setRegex(boolean isRegex) { + this.isRegex = isRegex; + } } diff -r 5737ab564b94 -r 1cd9d9a67cad src/main/java/org/mpi/openmind/repository/utils/CustomMysqlDialect.java --- a/src/main/java/org/mpi/openmind/repository/utils/CustomMysqlDialect.java Fri Aug 26 19:09:57 2016 +0200 +++ b/src/main/java/org/mpi/openmind/repository/utils/CustomMysqlDialect.java Mon Aug 29 20:16:53 2016 +0200 @@ -1,7 +1,19 @@ package org.mpi.openmind.repository.utils; +import org.hibernate.Hibernate; +import org.hibernate.dialect.function.SQLFunctionTemplate; + public class CustomMysqlDialect extends org.hibernate.dialect.MySQLDialect { + + public CustomMysqlDialect() { + super(); + + /** + * Function to evaluate regexp in MySQL + */ + registerFunction("regexp", new SQLFunctionTemplate(Hibernate.INTEGER, "?1 REGEXP ?2")); } + public String getTableTypeString() { return " ENGINE=InnoDB DEFAULT CHARSET=utf8"; }