Fixes issue 40905: Values of Date properties are automatically truncated
authorAugusto Mauch <augusto.mauch@openbravo.com>
Mon, 29 Jul 2019 16:40:42 +0200
changeset 36385 6457d6616229
parent 36384 b7c07ac600a4
child 36386 635b9156c59d
Fixes issue 40905: Values of Date properties are automatically truncated

Now before storing a date property in the database, the data is automatically truncated, its time
part is stripped.

This can be done because if a column uses the Date reference, its time part is not taken into account.
Stripping the time part allows to filter the date columns properly, otherwise the time part was
wrongfully taken into account.

There was not mechanism to transform a value at reference level, so a new hook has been added to
the DomainType class, with an empty default behaviour.
src/org/openbravo/base/model/Property.java
src/org/openbravo/base/model/domaintype/DateDomainType.java
src/org/openbravo/base/model/domaintype/DomainType.java
src/org/openbravo/base/structure/BaseOBObject.java
--- a/src/org/openbravo/base/model/Property.java	Mon Jul 29 14:50:32 2019 +0200
+++ b/src/org/openbravo/base/model/Property.java	Mon Jul 29 16:40:42 2019 +0200
@@ -1377,4 +1377,15 @@
   public void setChildPropertyInParent(boolean isChildPropertyInParent) {
     this.isChildPropertyInParent = isChildPropertyInParent;
   }
+
+  /**
+   * Given an object, applies the transformation defined in its domain type
+   * 
+   * @param value
+   *          the value to be transformed
+   * @return the transformed value
+   */
+  public Object transformValue(Object value) {
+    return getDomainType().transformValue(this, value);
+  }
 }
--- a/src/org/openbravo/base/model/domaintype/DateDomainType.java	Mon Jul 29 14:50:32 2019 +0200
+++ b/src/org/openbravo/base/model/domaintype/DateDomainType.java	Mon Jul 29 16:40:42 2019 +0200
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2009-2011 Openbravo SLU 
+ * All portions are Copyright (C) 2009-2019 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -21,8 +21,12 @@
 
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.Calendar;
 import java.util.Date;
 
+import org.apache.commons.lang.time.DateUtils;
+import org.openbravo.base.model.Property;
+
 /**
  * The type for a date column.
  * 
@@ -77,4 +81,14 @@
     return "ob:dateTime";
   }
 
+  @Override
+  public Object transformValue(Property property, Object value) {
+    if (!(value instanceof Date)) {
+      return value;
+    }
+    // Date properties should not include information about the time, truncate the rest
+    Date dateValue = (Date) value;
+    return DateUtils.truncate(dateValue, Calendar.DATE);
+  }
+
 }
--- a/src/org/openbravo/base/model/domaintype/DomainType.java	Mon Jul 29 14:50:32 2019 +0200
+++ b/src/org/openbravo/base/model/domaintype/DomainType.java	Mon Jul 29 16:40:42 2019 +0200
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2009-2010 Openbravo SLU 
+ * All portions are Copyright (C) 2009-2019 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -93,4 +93,18 @@
    *          the property to check
    */
   void checkObjectIsValid(BaseOBObjectDef obObject, Property property) throws ValidationException;
+
+  /**
+   * Transforms the value of an object. The default implementation is to return the original value
+   * as it is
+   * 
+   * @param property
+   *          the property which has this value
+   * @param value
+   *          the value to be transformed
+   * @return the transformed value
+   */
+  default Object transformValue(Property property, Object value) {
+    return value;
+  }
 }
--- a/src/org/openbravo/base/structure/BaseOBObject.java	Mon Jul 29 14:50:32 2019 +0200
+++ b/src/org/openbravo/base/structure/BaseOBObject.java	Mon Jul 29 16:40:42 2019 +0200
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2008-2018 Openbravo SLU 
+ * All portions are Copyright (C) 2008-2019 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -171,7 +171,8 @@
           + "This happens when setting property " + propName + " " + p + " with value " + value
           + " in entity " + getEntity());
     }
-    data[p.getIndexInEntity()] = value;
+    Object transformedValue = p.transformValue(value);
+    data[p.getIndexInEntity()] = transformedValue;
   }
 
   @Override