You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2014/01/25 15:33:49 UTC

[3/5] git commit: ISIS-659: improved ViewModelSupport, handle bookmarkable) entities

ISIS-659: improved ViewModelSupport, handle bookmarkable) entities

In addition, provide the ability to store nulls as part of the
memento also.


Project: http://git-wip-us.apache.org/repos/asf/isis/repo
Commit: http://git-wip-us.apache.org/repos/asf/isis/commit/be1eb238
Tree: http://git-wip-us.apache.org/repos/asf/isis/tree/be1eb238
Diff: http://git-wip-us.apache.org/repos/asf/isis/diff/be1eb238

Branch: refs/heads/master
Commit: be1eb2382c977626a26bbffd51b9454c00c47053
Parents: 0ebc55a
Author: Dan Haywood <da...@haywood-associates.co.uk>
Authored: Sat Jan 25 14:22:12 2014 +0000
Committer: Dan Haywood <da...@haywood-associates.co.uk>
Committed: Sat Jan 25 14:22:12 2014 +0000

----------------------------------------------------------------------
 .../services/viewmodelsupport/Dom4jUtil.java    | 180 ++++++++++++++++---
 .../ViewModelSupportDefault.java                |   4 +-
 .../ViewModelSupportDefaultTest.java            |  12 +-
 3 files changed, 165 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/isis/blob/be1eb238/core/runtime/src/main/java/org/apache/isis/core/runtime/services/viewmodelsupport/Dom4jUtil.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/viewmodelsupport/Dom4jUtil.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/viewmodelsupport/Dom4jUtil.java
index 363b6ce..1537851 100644
--- a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/viewmodelsupport/Dom4jUtil.java
+++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/viewmodelsupport/Dom4jUtil.java
@@ -30,50 +30,41 @@ import org.dom4j.io.SAXReader;
 import org.dom4j.io.XMLWriter;
 import org.joda.time.LocalDate;
 
+import org.apache.isis.applib.services.bookmark.Bookmark;
 import org.apache.isis.core.commons.exceptions.IsisException;
 
 class Dom4jUtil {
     
+    
     private Dom4jUtil(){}
+    
+    private final static String NULL_MARKER = "$$_isis_null_value_$$";
 
     static void addChild(final Element el, final String name, final Object value) {
-        if(value != null) {
-            el.addElement(name).setText(value.toString());
-        }
+        el.addElement(name).setText(encodeForNulls(value));
+    }
+
+    static boolean isSupportedClass(final Class<?> cls) {
+        return Parseable.isSupported(cls);
     }
 
-    @SuppressWarnings("unchecked")
+    /**
+     * @param el
+     * @param name
+     * @param cls - see {@link Parseable}
+     * @return
+     */
     static <T> T getChild(final Element el, final String name, final Class<T> cls) {
+        Parseable.assertSupported(cls);
         final Element child = el.element(name);
         if(child == null) { 
             return null;
         }
-        final String str = child.getText();
-        if (cls == String.class) {
-            return (T) str;
-        } else if (cls == Boolean.class) {
-            return (T) new Boolean(str);
-        } else if(cls == Byte.class) {
-            return (T) new Byte(str);
-        }else if(cls == Short.class) {
-            return (T) new Short(str);
-        }else if(cls == Integer.class) {
-            return (T) new Integer(str);
-        }else  if(cls == Long.class) {
-            return (T) new Long(str);
-        }else if(cls == Float.class) {
-            return (T) new Float(str);
-        }else if(cls == Double.class) {
-            return (T) new Double(str);
-        }else if(cls == BigDecimal.class) {
-            return (T) new BigDecimal(str);
-        }else if(cls == BigInteger.class) {
-            return (T) new BigInteger(str);
-        }else if(cls == LocalDate.class) { 
-            return (T) new LocalDate(str);
-        }else {
-            throw new IllegalArgumentException("unsupported class '" + cls + "'");
+        final String str = decodeForNulls(child.getText());
+        if(str == null) {
+            return null;
         }
+        return Parseable.parse(str, cls);
     }
 
     static Document parse(final String xmlStr) {
@@ -107,4 +98,135 @@ class Dom4jUtil {
         }
     }
 
+    private static String encodeForNulls(final Object value) {
+        return value != null ? value.toString() : NULL_MARKER;
+    }
+    private static String decodeForNulls(final String valueStr) {
+        return NULL_MARKER.equals(valueStr)? null: valueStr;
+    }
+
+    // //////////////////////////////////////
+
+    static enum Parseable {
+        STRING(String.class) {
+            @SuppressWarnings("unchecked")
+            public <T> T parseStr(String str) {
+                return (T) str;
+            }
+        },
+        BOOLEAN(Boolean.class) {
+            @SuppressWarnings("unchecked")
+            @Override
+            public <T> T parseStr(String str) {
+                return (T) new Boolean(str);
+            }
+        },
+        BYTE(Byte.class) {
+            @SuppressWarnings("unchecked")
+            @Override
+            public <T> T parseStr(String str) {
+                return (T) new Byte(str);
+            }
+        },
+        SHORT(Short.class) {
+
+            @SuppressWarnings("unchecked")
+            @Override
+            <T> T parseStr(String str) {
+                return (T) new Short(str);
+            }
+        },
+        INTEGER(Integer.class) {
+            @SuppressWarnings("unchecked")
+            @Override
+            <T> T parseStr(String str) {
+                return (T) new Integer(str);
+            }
+        },
+        LONG(Long.class) {
+            @SuppressWarnings("unchecked")
+            @Override
+            <T> T parseStr(String str) {
+                return (T) new Long(str);
+            }
+        },
+        FLOAT(Float.class) {
+            @SuppressWarnings("unchecked")
+            @Override
+            <T> T parseStr(String str) {
+                return (T) new Float(str);
+            }
+        },
+        DOUBLE(Double.class) {
+            @SuppressWarnings("unchecked")
+            @Override
+            <T> T parseStr(String str) {
+                return (T) new Double(str);
+            }
+        },
+        BIG_DECIMAL(BigDecimal.class) {
+            @SuppressWarnings("unchecked")
+            @Override
+            <T> T parseStr(String str) {
+                return (T) new BigDecimal(str);
+            }
+        },
+        BIG_INTEGER(BigInteger.class) {
+            @SuppressWarnings("unchecked")
+            @Override
+            <T> T parseStr(String str) {
+                return (T) new BigInteger(str);
+            }
+        },
+        LOCAL_DATE(LocalDate.class) {
+            @SuppressWarnings("unchecked")
+            @Override
+            <T> T parseStr(String str) {
+                return (T) new LocalDate(str);
+            } 
+        },
+        BOOKMARK(Bookmark.class) {
+            @SuppressWarnings("unchecked")
+            @Override
+            <T> T parseStr(String str) {
+                return (T) new Bookmark(str);
+            } 
+        };
+        private final Class<?> cls;
+        private Parseable(Class<?> cls) {
+            this.cls = cls;
+        }
+        public Class<?> getCls() {
+            return cls;
+        }
+        abstract <T> T parseStr(String str);
+        
+        // //////////////////////////////////////
+
+        static <T> T parse(final String str, final Class<?> cls) {
+            assertSupported(cls);
+            for (Parseable sc : values()) {
+                if(sc.getCls().isAssignableFrom(cls)) {
+                    return sc.parseStr(str);
+                }
+            }
+            return null;
+        }
+
+        static boolean isSupported(final Class<?> cls) {
+            for (Parseable sc : values()) {
+                if(sc.getCls().isAssignableFrom(cls)) {
+                    return true;
+                }
+            }
+            return false;
+        }
+
+        static void assertSupported(final Class<?> cls) {
+            if(!isSupported(cls)) {
+                throw new IllegalArgumentException("Parsing string to type " + cls.getName() + " is not supported");
+            }
+        }
+        
+    }
 }

http://git-wip-us.apache.org/repos/asf/isis/blob/be1eb238/core/runtime/src/main/java/org/apache/isis/core/runtime/services/viewmodelsupport/ViewModelSupportDefault.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/viewmodelsupport/ViewModelSupportDefault.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/viewmodelsupport/ViewModelSupportDefault.java
index 646cccd..0b00e68 100644
--- a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/viewmodelsupport/ViewModelSupportDefault.java
+++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/viewmodelsupport/ViewModelSupportDefault.java
@@ -22,7 +22,6 @@ import java.util.Set;
 
 import com.google.common.base.Function;
 import com.google.common.collect.Iterables;
-import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
 import com.google.common.io.BaseEncoding;
 
@@ -54,6 +53,9 @@ public class ViewModelSupportDefault implements ViewModelSupport {
             return this;
         }
 
+        /**
+         * 
+         */
         @Override
         public <T> T get(String name, Class<T> cls) {
             final Element el = doc.getRootElement();

http://git-wip-us.apache.org/repos/asf/isis/blob/be1eb238/core/runtime/src/test/java/org/apache/isis/core/runtime/services/viewmodelsupport/ViewModelSupportDefaultTest.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/test/java/org/apache/isis/core/runtime/services/viewmodelsupport/ViewModelSupportDefaultTest.java b/core/runtime/src/test/java/org/apache/isis/core/runtime/services/viewmodelsupport/ViewModelSupportDefaultTest.java
index 05ac451..54ec3f0 100644
--- a/core/runtime/src/test/java/org/apache/isis/core/runtime/services/viewmodelsupport/ViewModelSupportDefaultTest.java
+++ b/core/runtime/src/test/java/org/apache/isis/core/runtime/services/viewmodelsupport/ViewModelSupportDefaultTest.java
@@ -16,7 +16,7 @@
  */
 package org.apache.isis.core.runtime.services.viewmodelsupport;
 
-import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.*;
 import static org.junit.Assert.assertThat;
 
 import java.math.BigDecimal;
@@ -26,6 +26,7 @@ import org.joda.time.LocalDate;
 import org.junit.Before;
 import org.junit.Test;
 
+import org.apache.isis.applib.services.bookmark.Bookmark;
 import org.apache.isis.applib.services.viewmodelsupport.ViewModelSupport.Memento;
 
 public class ViewModelSupportDefaultTest {
@@ -54,6 +55,9 @@ public class ViewModelSupportDefaultTest {
         memento.set("someBigDecimal", new BigDecimal("123456789012345678901234567890.123456789"));
         memento.set("someLocalDate", new LocalDate(2013,9,3));
         
+        memento.set("someBookmark", new Bookmark("CUS", "12345"));
+        memento.set("someNullValue", null);
+        
         final String str = memento.asString();
         
         final Memento memento2 = viewModelSupport.parse(str);
@@ -70,5 +74,11 @@ public class ViewModelSupportDefaultTest {
         assertThat(memento2.get("someBigInteger", BigInteger.class), is(new BigInteger("123456789012345678901234567890")));
         assertThat(memento2.get("someBigDecimal", BigDecimal.class), is(new BigDecimal("123456789012345678901234567890.123456789")));
         assertThat(memento2.get("someLocalDate", LocalDate.class), is(new LocalDate(2013,9,3)));
+        assertThat(memento2.get("someBookmark", Bookmark.class), is(new Bookmark("CUS", "12345")));
+        
+        // a nullValue can be grabbed as any type, will always succeed
+        assertThat(memento2.get("someNullValue", Integer.class), is(nullValue()));
+        assertThat(memento2.get("someNullValue", Bookmark.class), is(nullValue()));
+        assertThat(memento2.get("someNullValue", LocalDate.class), is(nullValue()));
     }
 }