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

Commits:
6e077286 by Oscar Scholten at 2016-09-20T10:59:19+02:00
CMS-10227 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

- - - - -
cd5b4eba by Oscar Scholten at 2016-09-21T09:43:35+02:00
CMS-10227 adding javadoc explaining the NULL_DATE to null mapping

- - - - -
f8ca719f by Oscar Scholten at 2016-09-22T14:35:12+02:00
CMS-10227 reintegrate branch 'bugfix/CMS-10227' into release/4.0

- - - - -


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/2d01ca56888eb47ed8697e3d6a89546e787f89f9...f8ca719f51c6f2b624f0369f8cd79c0713e567be
_______________________________________________
Hippocms-svn mailing list
Hippocms-svn@lists.onehippo.org
https://lists.onehippo.org/mailman/listinfo/hippocms-svn

Reply via email to