You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ofbiz.apache.org by Jacopo Cappellato <ja...@hotwaxmedia.com> on 2010/11/26 12:09:08 UTC

Entity function for removing spaces

Goal: provide the ability to create an entity function to suppress all the spaces from a db field; the entity function will create an SQL function like: REPLACE(string, ' ', '')

I have implemented this feature as in the attached patch, please have a look at it.

The new function can be used in the following way:

exprs.add(EntityCondition.makeCondition(EntityFunction.REMOVESPACES("geoName"), EntityOperator.EQUALS, someString);

The code is working as expected but what I was hoping to implement was a more generic REPLACE function that could be used also to suppress the spaces:

exprs.add(EntityCondition.makeCondition(EntityFunction. REPLACE("geoName", " ", ""), EntityOperator.EQUALS, someString);

Unfortunately, because of the way the Fetcher and SQLFunctionFactory are defined, I couldn't find a clean way of implementing this.

Any suggestions?

Kind regards,

Jacopo

Index: framework/entity/src/org/ofbiz/entity/condition/EntityFunction.java
===================================================================
--- framework/entity/src/org/ofbiz/entity/condition/EntityFunction.java	(revision 1026942)
+++ framework/entity/src/org/ofbiz/entity/condition/EntityFunction.java	(working copy)
@@ -59,11 +59,12 @@
     }
 
     public static enum SQLFunction {
-        LENGTH, TRIM, UPPER, LOWER;
+        LENGTH, TRIM, REMOVESPACES, UPPER, LOWER;
     }
 
     public static final int ID_LENGTH = SQLFunction.LENGTH.ordinal();
     public static final int ID_TRIM = SQLFunction.TRIM.ordinal();
+    public static final int ID_REMOVESPACES = SQLFunction.REMOVESPACES.ordinal();
     public static final int ID_UPPER = SQLFunction.UPPER.ordinal();
     public static final int ID_LOWER = SQLFunction.LOWER.ordinal();
 
@@ -71,6 +72,8 @@
     public static EntityFunction<Integer> LENGTH(Object value) { return LENGTH.lengthFactory.createFunction(value); }
     public static EntityFunction<String> TRIM(EntityConditionValue nested) { return TRIM.trimFactory.createFunction(nested); }
     public static EntityFunction<String> TRIM(Object value) { return TRIM.trimFactory.createFunction(value); }
+    public static EntityFunction<String> REMOVESPACES(EntityConditionValue nested) { return REMOVESPACES.trimFactory.createFunction(nested); }
+    public static EntityFunction<String> REMOVESPACES(Object value) { return REMOVESPACES.trimFactory.createFunction(value); }
     public static EntityFunction<String> UPPER(EntityConditionValue nested) { return UPPER.upperFactory.createFunction(nested); }
     public static EntityFunction<String> UPPER(Object value) { return UPPER.upperFactory.createFunction(value); }
     public static EntityFunction<String> UPPER_FIELD(String fieldName) { return UPPER.upperFactory.createFunction(EntityFieldValue.makeFieldValue(fieldName)); }
@@ -119,6 +122,38 @@
         }
     }
 
+    public static class REMOVESPACES extends EntityFunction<String> {
+        public static Fetcher<String> FETCHER = new Fetcher<String>() {
+            public String getValue(Object value) { return value.toString().replaceAll(" ", ""); }
+        };
+        protected static final SQLFunctionFactory<String, REMOVESPACES> trimFactory = new SQLFunctionFactory<String, REMOVESPACES>() {
+            @Override
+            protected REMOVESPACES create() {
+                return new REMOVESPACES();
+            }
+
+            @Override
+            protected void init(REMOVESPACES function, Object value) {
+                function.init(value);
+            }
+        };
+        protected REMOVESPACES() {}
+        public void init(Object value) {
+            super.init(FETCHER, SQLFunction.REMOVESPACES, value);
+        }
+
+        @Override
+        public void addSqlValue(StringBuilder sql, Map<String, String> tableAliases, ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams, boolean includeTableNamePrefix, DatasourceInfo datasourceinfo) {
+            sql.append("REPLACE(");
+            if (nested != null) {
+                nested.addSqlValue(sql, tableAliases, modelEntity, entityConditionParams, includeTableNamePrefix, datasourceinfo);
+            } else {
+                addValue(sql, null, value, entityConditionParams);
+            }
+            sql.append(", ' ', '')");
+        }
+    }
+
     public static class UPPER extends EntityFunction<String> {
         public static Fetcher<String> FETCHER = new Fetcher<String>() {
             public String getValue(Object value) { return value.toString().toUpperCase(); }