[Schmitzm-commits] r2287 - trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Tue Mar 26 00:15:36 CET 2013


Author: mojays
Date: 2013-03-26 00:15:36 +0100 (Tue, 26 Mar 2013)
New Revision: 2287

Modified:
   trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/DBUtil.java
Log:


Modified: trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/DBUtil.java
===================================================================
--- trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/DBUtil.java	2013-03-25 23:05:45 UTC (rev 2286)
+++ trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/DBUtil.java	2013-03-25 23:15:36 UTC (rev 2287)
@@ -3,11 +3,12 @@
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Locale;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
 import org.hibernate.Criteria;
 import org.hibernate.JDBCException;
+import org.hibernate.Query;
 import org.hibernate.Session;
 import org.hibernate.Transaction;
 import org.hibernate.criterion.Criterion;
@@ -18,7 +19,6 @@
 import de.schmitzm.db.hibernate.types.BasicTypeInterface;
 import de.schmitzm.db.hibernate.types.UniqueIDType;
 import de.schmitzm.lang.LangUtil;
-import de.schmitzm.lang.ResourceProvider;
 import de.schmitzm.swing.ExceptionDialog;
 
 /**
@@ -415,4 +415,54 @@
 
     }
 
+    /**
+     * Prepares an OR-connected WHERE-Condition for the given attributes. The LIKE-condition
+     * is automatically converted to lower case. The pattern is not included directly in the
+     * WHERE condition, but only as ?-wildcards  
+     * @param pattern pattern to check; only to check whether there is a condition to check
+     * @param condAttr attributes
+     * @return "TRUE" is pattern is blank
+     */
+    public static String prepareSQLKontaktFilterClause(String pattern, String... condAttr) {
+      String[] queryAttr = condAttr;
+      // Wenn keine Attribute angegeben, dann "TRUE" als Bedingung
+      // zurueck geben
+      if ( queryAttr.length == 0 || StringUtils.isBlank(pattern) )
+        return "TRUE";
+      
+      // Query zusammen basteln
+      StringBuffer queryString = new StringBuffer("(");
+      for (int i = 0; i < queryAttr.length; i++) {
+        if (i > 0)
+          queryString.append(" OR ");
+        queryString.append("LOWER(").append(queryAttr[i]).append(")")
+                   .append(" LIKE ?");
+      }
+      queryString.append(")");
+      return queryString.toString();
+    }
+
+    /**
+     * Fills the ?-wildcards in a {@link Query} with the given pattern.
+     * is automatically converted to lower case. The pattern is not included directly in the
+     * WHERE condition, but only as ?-wildcards
+     * @param query query
+     * @param idx first wildcard to replace  (zero based)
+     * @param pattern pattern to replace the wildcards with
+     * @param condAttr attributes to replace the wildcards for
+     * @return the next wuildcard index to replace in query
+     */
+    public static int fillQueryParameters(Query query, int idx, /*ExtendedSQLFilter[] extFilters,*/ String pattern, String... condAttr ) {
+      boolean patternEmpty = StringUtils.isBlank(pattern);
+      // Pattern-Attribute nur vorhanden, wenn Pattern nicht leer
+      if ( !patternEmpty )
+        for (int i=0; i<condAttr.length; i++)
+          query.setString(idx++, "%"+pattern.toLowerCase()+"%");
+//      // Zusaetzliche Filter fuellen
+//      for (ExtendedSQLFilter filter : extFilters)
+//        if ( filter.isApplyableTo(kontaktType) )
+//          idx = filter.fillQueryParameters(query, idx);
+      
+      return idx;
+    }
 }



More information about the Schmitzm-commits mailing list