changeset 35:1cd9d9a67cad

added regular expression match option to AttributeFilter and searchEntityByAttribute. added regexp() function to mysql dialect to use mysql REGEXP.
author casties
date Mon, 29 Aug 2016 20:16:53 +0200
parents 5737ab564b94
children 63fe33172397
files src/main/java/org/mpi/openmind/cache/WrapperService.java src/main/java/org/mpi/openmind/repository/bo/Entity.java src/main/java/org/mpi/openmind/repository/services/PersistenceService.java src/main/java/org/mpi/openmind/repository/services/utils/AttributeFilter.java src/main/java/org/mpi/openmind/repository/services/utils/Filter.java src/main/java/org/mpi/openmind/repository/utils/CustomMysqlDialect.java
diffstat 6 files changed, 107 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- 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<Entity, Attribute> searchEntityByAttributeFilter(
+	/**
+	 * Search entities matching a list of attribute filters.
+	 * 
+	 * Returns a Map of Entities and Attributes.
+	 * 
+	 * @param filters
+	 * @param maxResult
+	 * @return
+	 */
+	public Map<Entity, Attribute> searchEntityByAttributeFilter (
 			List<AttributeFilter> filters, int maxResult) {
 		return this.getPS().searchEntityByAttributeFilter(filters, maxResult);
 	}
--- 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){
--- 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<Object> 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;
 	}
--- 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;
 	}
--- 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;
+    }
 }
--- 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";
     }