Oscar Scholten pushed to branch release/3.2 at cms-community / hippo-cms

Commits:
5ec53fc9 by Oscar Scholten at 2016-09-22T14:52:37+02:00
CMS-10451 [backport 10.2] mapping NULL_DATE to null in all paths of 
get/setObject and get/setValue
- this ensures regular and optional values are set to NULL_DATE and are not 
removed when emptied in the CMS UI

(cherry picked from commit 6e0772866aa1721d4ed46eb85e490956b70b7ac3)

- - - - -
c954476f by Oscar Scholten at 2016-09-22T14:53:16+02:00
CMS-10451 [backport 10.2] adding javadoc explaining the NULL_DATE to null 
mapping

(cherry picked from commit cd5b4eba495773a929c1b7a3f656881baa0f00b2)

- - - - -


1 changed file:

- 
api/src/main/java/org/hippoecm/frontend/model/properties/JcrPropertyValueModel.java


Changes:

=====================================
api/src/main/java/org/hippoecm/frontend/model/properties/JcrPropertyValueModel.java
=====================================
--- 
a/api/src/main/java/org/hippoecm/frontend/model/properties/JcrPropertyValueModel.java
+++ 
b/api/src/main/java/org/hippoecm/frontend/model/properties/JcrPropertyValueModel.java
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2008-2013 Hippo B.V. (http://www.onehippo.com)
+ *  Copyright 2008-2016 Hippo B.V. (http://www.onehippo.com)
  * 
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -47,6 +47,12 @@ import org.slf4j.LoggerFactory;
  * of the property.  I.e. JCR type string maps to {@link String}, date maps to 
{@link Date}.
  * <p>
  * One can also set and retrieve the underlying {@link Value}.
+ * </p>
+ * <p>
+ * Note that Date property values equaling {@link 
PropertyValueProvider#NULL_DATE} are converted to null in
+ * {@link #getValue()} and {@link #getObject()}, and vice versa from null to 
{@link PropertyValueProvider#NULL_DATE} in
+ * {@link #setValue(Value)} and {@link #setObject(Serializable)}.
+ * </p>
  */
 public class JcrPropertyValueModel<T extends Serializable> implements 
IModel<T>, IObjectClassAwareModel<T> {
 
@@ -104,8 +110,11 @@ public class JcrPropertyValueModel<T extends Serializable> 
implements IModel<T>,
                 Property property = propertyModel.getProperty();
                 try {
                     this.type = property.getType();
+                    if (this.type == PropertyType.DATE) {
+                        this.value = createNullDateValue();
+                    }
                 } catch (RepositoryException e) {
-                    throw new RuntimeException("Could not determine type of 
property", e);
+                    throw new RuntimeException("Error initializing 
JcrPropertyValueModel", e);
                 }
             }
         }
@@ -176,15 +185,29 @@ public class JcrPropertyValueModel<T extends 
Serializable> implements IModel<T>,
         return propertyModel;
     }
 
+    /**
+     * Gets the model's {#link Value}.
+     * Note that Date property values equaling {@link 
PropertyValueProvider#NULL_DATE} are converted to null.
+     */
     public Value getValue() {
         load();
+        if (value != null && value.equals(PropertyValueProvider.NULL_DATE)) {
+            return null;
+        }
         return value;
     }
 
+    /**
+     * Sets the model's {#link Value}.
+     * Note that Date property values equaling null are converted to {@link 
PropertyValueProvider#NULL_DATE}.
+     */
     public void setValue(Value value) {
         load();
 
         try {
+            if (value == null && getType() == PropertyType.DATE) {
+                value = createNullDateValue();
+            }
             if (this.value != null && value != null) {
                 String oldValue = this.value.getString();
                 String newValue = value.getString();
@@ -253,6 +276,17 @@ public class JcrPropertyValueModel<T extends Serializable> 
implements IModel<T>,
         }
     }
 
+    private Value createNullDateValue() throws RepositoryException {
+        final Calendar calendar = Calendar.getInstance();
+        calendar.setTime(PropertyValueProvider.NULL_DATE);
+        final ValueFactory factory = 
UserSession.get().getJcrSession().getValueFactory();
+        return factory.createValue(calendar);
+    }
+
+    /**
+     * Gets the model's object.
+     * Note that Date values equaling {@link PropertyValueProvider#NULL_DATE} 
are converted to null.
+     */
     @SuppressWarnings("unchecked")
     public T getObject() {
         try {
@@ -279,8 +313,15 @@ public class JcrPropertyValueModel<T extends Serializable> 
implements IModel<T>,
         return null;
     }
 
-    public void setObject(final Serializable object) {
+    /**
+     * Sets the model's object.
+     * Note that Date values equaling null are converted to {@link 
PropertyValueProvider#NULL_DATE}.
+     */
+    public void setObject(Serializable object) {
         load();
+        if (object == null && getType() == PropertyType.DATE) {
+            object = PropertyValueProvider.NULL_DATE;
+        }
         if (object == null) {
             if (value != null) {
                 setValue(null);



View it on GitLab: 
https://code.onehippo.org/cms-community/hippo-cms/compare/089e97c209c78ba26f7109d4304b2522648b0e6b...c954476f5f46e19d0ab723937afa74e0ee94c1ce
_______________________________________________
Hippocms-svn mailing list
Hippocms-svn@lists.onehippo.org
https://lists.onehippo.org/mailman/listinfo/hippocms-svn

Reply via email to