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

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Fri Jul 19 18:11:30 CEST 2013


Author: mojays
Date: 2013-07-19 18:11:30 +0200 (Fri, 19 Jul 2013)
New Revision: 2367

Modified:
   trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/DBUtil.java
Log:
DBUtil: parameter "caseSensitive" for determineBasicType(.), getOrCreateBasicType(.), determineEntity(.), getOrCreateEntity(.)

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-07-19 13:23:06 UTC (rev 2366)
+++ trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/DBUtil.java	2013-07-19 16:11:30 UTC (rev 2367)
@@ -12,6 +12,7 @@
 import org.hibernate.Session;
 import org.hibernate.Transaction;
 import org.hibernate.criterion.Criterion;
+import org.hibernate.criterion.MatchMode;
 import org.hibernate.criterion.Order;
 import org.hibernate.criterion.Restrictions;
 
@@ -118,10 +119,12 @@
      * is used for possible database updates.
      * @param basicType basic type to determine the instance for
      * @param desc description for the {@link BasicTypeInterface} instance
+     * @param caseSentisive indicates whether for {@link String} attributes the search should
+     *                      be case-sensitive
      * @see #getGlobalSession()
      */
-    public static <E extends BasicTypeInterface> E getOrCreateBasicType(Class<E> basicType, String desc) {
-      return getOrCreateBasicType(basicType, desc, true);
+    public static <E extends BasicTypeInterface> E getOrCreateBasicType(Class<E> basicType, String desc, boolean caseSensitive) {
+      return getOrCreateBasicType(basicType, desc, caseSensitive, true);
     }
     
     /**
@@ -130,6 +133,8 @@
      * is used for possible database updates.
      * @param basicType basic type to determine the instance for
      * @param desc description for the {@link BasicTypeInterface} instance
+     * @param caseSentisive indicates whether for {@link String} attributes the search should
+     *                      be case-sensitive
      * @param saveOrUpdate
      *            if {@code false}, no {@code Session.saveOrUpdate(.)} is performed (on possible
      *            creation). This has to be done explicitly later, e.g. in case when
@@ -137,8 +142,8 @@
      *            filled before insert.
      * @see #getGlobalSession()
      */
-    public static <E extends BasicTypeInterface> E getOrCreateBasicType(Class<E> basicType, String desc, boolean saveOrUpdate) {
-        return getOrCreateBasicType(null, basicType, desc, saveOrUpdate);
+    public static <E extends BasicTypeInterface> E getOrCreateBasicType(Class<E> basicType, String desc, boolean caseSensitive, boolean saveOrUpdate) {
+        return getOrCreateBasicType(null, basicType, desc, caseSensitive, saveOrUpdate);
     }
 
     /**
@@ -155,12 +160,12 @@
      *            filled before insert.
      * @see #getGlobalSession()
 	 */
-	public static <E extends BasicTypeInterface> E getOrCreateBasicType(Session session, Class<E> basicType, String desc, boolean saveOrUpdate) {
-		if (org.apache.commons.lang.StringUtils.trimToNull(desc) == null)
+	public static <E extends BasicTypeInterface> E getOrCreateBasicType(Session session, Class<E> basicType, String desc, boolean caseSensitive, boolean saveOrUpdate) {
+		if (StringUtils.trimToNull(desc) == null)
 			return null;
 		if ( session == null )
 		  session = getGlobalSession();
-		E instance = determineBasicType(basicType, desc);
+		E instance = determineBasicType(basicType, desc, caseSensitive);
 		if (instance == null) {
 			instance = createBasicTypeInstance(basicType);
 			instance.setDescription(desc);
@@ -245,9 +250,11 @@
      * Retrieves an entity instances which satisfies multiple criteria.
      * The {@linkplain #getGlobalSession() global session} is used for database access.
      * @param entityType entity type
+     * @param caseSentisive indicates whether for {@link String} attributes the search should
+     *                      be case-sensitive
      * @param crit criteria to check as pair of property name (String) and value (Object)
      */
-    public static <E> E determineEntity(Class<E> entityType, Object... crit) {
+    public static <E> E determineEntity(Class<E> entityType, boolean caseSentisive, Object... crit) {
       if ( crit.length % 2 != 0 )
         throw new IllegalArgumentException("Number of criteria parameters must be even and pairs of property name (String) and value (Object).");
       
@@ -257,10 +264,16 @@
           throw new IllegalArgumentException("Criteria parameter "+i+" must be a property name (String): "+LangUtil.getSimpleClassName(crit[i]));
         String propName  = (String)crit[i];
         Object propValue = crit[i+1];
-        if ( propValue != null )
-          query.add(Restrictions.eq(propName, propValue));
-        else
-          query.add(Restrictions.isNull(propName));
+        Criterion restr;
+        if ( propValue == null )
+          restr = Restrictions.isNull(propName);
+        else {
+          if ( propValue instanceof String && !caseSentisive )
+            restr = Restrictions.ilike(propName, (String)propValue, MatchMode.EXACT);
+          else
+            restr = Restrictions.eq(propName, propValue);
+        }
+        query.add(restr);
       }
       List<E> resultList = (List<E>) query.list();
 
@@ -281,6 +294,8 @@
      *                    constructor!)
      * @param session session to use for update (if {@code null} the
      *                {@linkplain #getGlobalSession() global session} is used)
+     * @param caseSensitive indicates whether for {@link String} attributes the search should
+     *                      be case-sensitive
      * @param saveOrUpdate
      *            if {@code false}, no {@code Session.saveOrUpdate(.)} is performed (on possible
      *            creation). This has to be done explicitly later, e.g. in case when
@@ -289,10 +304,10 @@
      * @param crit criteria to check as pair of property name (String) and value (Object)
      * @see #getGlobalSession()
      */
-    public static <E extends BasicTypeInterface> E getOrCreateEntity(Session session, Class<E> entitiyType, boolean saveOrUpdate, Object... crit) {
+    public static <E extends BasicTypeInterface> E getOrCreateEntity(Session session, Class<E> entitiyType, boolean caseSensitive, boolean saveOrUpdate, Object... crit) {
       if ( session == null )
         session = getGlobalSession();
-      E instance = determineEntity(entitiyType, crit);
+      E instance = determineEntity(entitiyType, caseSensitive, crit);
       if (instance == null) {
         try {
           instance = entitiyType.newInstance();
@@ -340,10 +355,13 @@
 	/**
 	 * Retrieves an instances of a {@link BasicType} from database with the specified description.
 	 * The {@linkplain #getGlobalSession() global session} is used for database access.
+     * @param caseSentisive indicates whether for {@link String} attributes the search should
+     *                      be case-sensitive
 	 */
-	public static <E extends BasicTypeInterface> E determineBasicType(Class<E> basicType, String description) {
+	public static <E extends BasicTypeInterface> E determineBasicType(Class<E> basicType, String description, boolean caseSensitive) {
 		Criteria query = getGlobalSession().createCriteria(basicType);
-		query.add(Restrictions.eq(BasicType.DESCRIPTION_COL, description));
+		Criterion restr = caseSensitive ? Restrictions.eq(BasicType.DESCRIPTION_COL, description) : Restrictions.ilike(BasicType.DESCRIPTION_COL, description, MatchMode.EXACT);
+        query.add(restr);
 		List<E> resultList = (List<E>) query.list();
 
 		if (resultList.isEmpty())



More information about the Schmitzm-commits mailing list