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/26 16:42:14 UTC
git commit: ISIS-592, ISIS-593, ISIS-640, ISIS-659,
ISIS-660: renaming...
Updated Branches:
refs/heads/master bd5357803 -> b1f1329ba
ISIS-592,ISIS-593,ISIS-640,ISIS-659,ISIS-660: renaming...
... ViewModelSupport to MementoService
Project: http://git-wip-us.apache.org/repos/asf/isis/repo
Commit: http://git-wip-us.apache.org/repos/asf/isis/commit/b1f1329b
Tree: http://git-wip-us.apache.org/repos/asf/isis/tree/b1f1329b
Diff: http://git-wip-us.apache.org/repos/asf/isis/diff/b1f1329b
Branch: refs/heads/master
Commit: b1f1329ba626fcc1c3ccd959c9f4971eaff1895f
Parents: bd53578
Author: Dan Haywood <da...@haywood-associates.co.uk>
Authored: Sun Jan 26 15:41:48 2014 +0000
Committer: Dan Haywood <da...@haywood-associates.co.uk>
Committed: Sun Jan 26 15:41:48 2014 +0000
----------------------------------------------------------------------
.../viewer/wicket/model/models/EntityModel.java | 9 +-
.../applib/services/memento/MementoService.java | 59 +++++
.../viewmodelsupport/ViewModelSupport.java | 59 -----
.../json/LayoutMetadataReaderFromJson.java | 2 +-
.../runtime/services/memento/Dom4jUtil.java | 232 +++++++++++++++++++
.../services/memento/MementoServiceDefault.java | 100 ++++++++
.../services/viewmodelsupport/Dom4jUtil.java | 232 -------------------
.../ViewModelSupportDefault.java | 99 --------
.../services/ServiceInstantiatorTest.java | 4 +-
.../memento/MementoServiceDefaultTest.java | 85 +++++++
.../ViewModelSupportDefaultTest.java | 84 -------
11 files changed, 484 insertions(+), 481 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/isis/blob/b1f1329b/component/viewer/wicket/model/src/main/java/org/apache/isis/viewer/wicket/model/models/EntityModel.java
----------------------------------------------------------------------
diff --git a/component/viewer/wicket/model/src/main/java/org/apache/isis/viewer/wicket/model/models/EntityModel.java b/component/viewer/wicket/model/src/main/java/org/apache/isis/viewer/wicket/model/models/EntityModel.java
index 5943709..aa5d859 100644
--- a/component/viewer/wicket/model/src/main/java/org/apache/isis/viewer/wicket/model/models/EntityModel.java
+++ b/component/viewer/wicket/model/src/main/java/org/apache/isis/viewer/wicket/model/models/EntityModel.java
@@ -32,8 +32,9 @@ import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.PrependingStringBuffer;
import org.apache.wicket.util.string.StringValue;
import org.apache.wicket.util.string.Strings;
+
import org.apache.isis.applib.annotation.BookmarkPolicy;
-import org.apache.isis.applib.services.viewmodelsupport.ViewModelSupport.Memento;
+import org.apache.isis.applib.services.memento.MementoService.Memento;
import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
import org.apache.isis.core.metamodel.adapter.mgr.AdapterManager.ConcurrencyChecking;
import org.apache.isis.core.metamodel.adapter.oid.OidMarshaller;
@@ -47,7 +48,7 @@ import org.apache.isis.core.metamodel.spec.ObjectSpecification;
import org.apache.isis.core.metamodel.spec.ObjectSpecifications.MemberGroupLayoutHint;
import org.apache.isis.core.metamodel.spec.SpecificationLoaderSpi;
import org.apache.isis.core.metamodel.spec.feature.OneToOneAssociation;
-import org.apache.isis.core.runtime.services.viewmodelsupport.ViewModelSupportDefault;
+import org.apache.isis.core.runtime.services.memento.MementoServiceDefault;
import org.apache.isis.core.runtime.system.context.IsisContext;
import org.apache.isis.viewer.wicket.model.hints.UiHintContainer;
import org.apache.isis.viewer.wicket.model.hints.UiHintPathSignificant;
@@ -234,7 +235,7 @@ public class EntityModel extends BookmarkableModel<ObjectAdapter> implements UiH
if(hints.isEmpty()) {
return;
}
- ViewModelSupportDefault vms = new ViewModelSupportDefault();
+ MementoServiceDefault vms = new MementoServiceDefault();
Memento memento = vms.create();
Set<String> hintKeys = hints.keySet();
for (String key : hintKeys) {
@@ -250,7 +251,7 @@ public class EntityModel extends BookmarkableModel<ObjectAdapter> implements UiH
String hintsStr = PageParameterNames.ANCHOR.getStringFrom(pageParameters);
if(hintsStr != null) {
try {
- Memento memento = new ViewModelSupportDefault().parse(hintsStr);
+ Memento memento = new MementoServiceDefault().parse(hintsStr);
Set<String> keys = memento.keySet();
for (String safeKey : keys) {
String value = memento.get(safeKey, String.class);
http://git-wip-us.apache.org/repos/asf/isis/blob/b1f1329b/core/applib/src/main/java/org/apache/isis/applib/services/memento/MementoService.java
----------------------------------------------------------------------
diff --git a/core/applib/src/main/java/org/apache/isis/applib/services/memento/MementoService.java b/core/applib/src/main/java/org/apache/isis/applib/services/memento/MementoService.java
new file mode 100644
index 0000000..1b42b35
--- /dev/null
+++ b/core/applib/src/main/java/org/apache/isis/applib/services/memento/MementoService.java
@@ -0,0 +1,59 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.isis.applib.services.memento;
+
+import java.util.Set;
+
+import org.apache.isis.applib.annotation.Programmatic;
+
+public interface MementoService {
+
+ public static interface Memento {
+
+ @Programmatic
+ public Memento set(String name, Object value);
+
+ @Programmatic
+ public <T> T get(String name, Class<T> cls);
+
+ @Programmatic
+ public String asString();
+
+ public Set<String> keySet();
+ }
+
+ /**
+ * Creates an empty {@link Memento}.
+ *
+ * <p>
+ * Typically followed by {@link Memento#set(String, Object)} for each of the data values to
+ * add to the {@link Memento}, then {@link Memento#asString()} to convert to a string format.
+ */
+ @Programmatic
+ public Memento create();
+
+ /**
+ * Parse string returned from {@link Memento#asString()}
+ *
+ * <p>
+ * Typically followed by {@link Memento#get(String, Class)} for each of the data values held
+ * in the {@link Memento}.
+ */
+ @Programmatic
+ public Memento parse(final String str);
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/b1f1329b/core/applib/src/main/java/org/apache/isis/applib/services/viewmodelsupport/ViewModelSupport.java
----------------------------------------------------------------------
diff --git a/core/applib/src/main/java/org/apache/isis/applib/services/viewmodelsupport/ViewModelSupport.java b/core/applib/src/main/java/org/apache/isis/applib/services/viewmodelsupport/ViewModelSupport.java
deleted file mode 100644
index 2c46e93..0000000
--- a/core/applib/src/main/java/org/apache/isis/applib/services/viewmodelsupport/ViewModelSupport.java
+++ /dev/null
@@ -1,59 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.isis.applib.services.viewmodelsupport;
-
-import java.util.Set;
-
-import org.apache.isis.applib.annotation.Programmatic;
-
-public interface ViewModelSupport {
-
- public static interface Memento {
-
- @Programmatic
- public Memento set(String name, Object value);
-
- @Programmatic
- public <T> T get(String name, Class<T> cls);
-
- @Programmatic
- public String asString();
-
- public Set<String> keySet();
- }
-
- /**
- * Creates an empty {@link Memento}.
- *
- * <p>
- * Typically followed by {@link Memento#set(String, Object)} for each of the data values to
- * add to the {@link Memento}, then {@link Memento#asString()} to convert to a string format.
- */
- @Programmatic
- public Memento create();
-
- /**
- * Parse string returned from {@link Memento#asString()}
- *
- * <p>
- * Typically followed by {@link Memento#get(String, Class)} for each of the data values held
- * in the {@link Memento}.
- */
- @Programmatic
- public Memento parse(final String str);
-
-}
http://git-wip-us.apache.org/repos/asf/isis/blob/b1f1329b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/layoutmetadata/json/LayoutMetadataReaderFromJson.java
----------------------------------------------------------------------
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/layoutmetadata/json/LayoutMetadataReaderFromJson.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/layoutmetadata/json/LayoutMetadataReaderFromJson.java
index 6b5b94f..05404c7 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/layoutmetadata/json/LayoutMetadataReaderFromJson.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/layoutmetadata/json/LayoutMetadataReaderFromJson.java
@@ -41,7 +41,7 @@ import org.apache.isis.applib.annotation.When;
import org.apache.isis.applib.annotation.Where;
import org.apache.isis.applib.filter.Filter;
import org.apache.isis.applib.filter.Filters;
-import org.apache.isis.applib.services.viewmodelsupport.ViewModelSupport.Memento;
+import org.apache.isis.applib.services.memento.MementoService.Memento;
import org.apache.isis.core.commons.lang.ClassExtensions;
import org.apache.isis.core.metamodel.facets.describedas.DescribedAsFacet;
import org.apache.isis.core.metamodel.facets.hide.HiddenFacet;
http://git-wip-us.apache.org/repos/asf/isis/blob/b1f1329b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/memento/Dom4jUtil.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/memento/Dom4jUtil.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/memento/Dom4jUtil.java
new file mode 100644
index 0000000..b7d1324
--- /dev/null
+++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/memento/Dom4jUtil.java
@@ -0,0 +1,232 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.isis.core.runtime.services.memento;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+import org.dom4j.Document;
+import org.dom4j.DocumentException;
+import org.dom4j.Element;
+import org.dom4j.io.OutputFormat;
+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) {
+ el.addElement(name).setText(encodeForNulls(value));
+ }
+
+ static boolean isSupportedClass(final Class<?> cls) {
+ return Parseable.isSupported(cls);
+ }
+
+ /**
+ * @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 = decodeForNulls(child.getText());
+ if(str == null) {
+ return null;
+ }
+ return Parseable.parse(str, cls);
+ }
+
+ static Document parse(final String xmlStr) {
+ try {
+ final SAXReader saxReader = new SAXReader();
+ Document doc = saxReader.read(new StringReader(xmlStr));
+ return doc;
+ } catch (DocumentException e) {
+ throw new IsisException(e);
+ }
+ }
+
+ static String asString(final Document doc) {
+ XMLWriter writer = null;
+ final StringWriter sw = new StringWriter();
+ try {
+ OutputFormat outputFormat = OutputFormat.createPrettyPrint();
+ writer = new XMLWriter(sw, outputFormat);
+ writer.write(doc);
+ return sw.toString();
+ } catch (IOException e) {
+ throw new IsisException(e);
+ } finally {
+ if(writer != null) {
+ try {
+ writer.close();
+ } catch (IOException e) {
+ // ignore
+ }
+ }
+ }
+ }
+
+ 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/b1f1329b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/memento/MementoServiceDefault.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/memento/MementoServiceDefault.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/memento/MementoServiceDefault.java
new file mode 100644
index 0000000..3b89699
--- /dev/null
+++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/memento/MementoServiceDefault.java
@@ -0,0 +1,100 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.isis.core.runtime.services.memento;
+
+import java.nio.charset.Charset;
+import java.util.List;
+import java.util.Set;
+
+import com.google.common.base.Function;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Sets;
+import com.google.common.io.BaseEncoding;
+
+import org.dom4j.Document;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Element;
+
+import org.apache.isis.applib.services.memento.MementoService;
+
+public class MementoServiceDefault implements MementoService {
+
+ static class MementoDefault implements Memento {
+
+ private Document doc;
+
+ MementoDefault(Document doc) {
+ this.doc = doc;
+ }
+
+ MementoDefault() {
+ this(DocumentHelper.createDocument());
+ doc.addElement("memento");
+ }
+
+ @Override
+ public Memento set(String name, Object value) {
+ final Element el = doc.getRootElement();
+ Dom4jUtil.addChild(el, name, value);
+ return this;
+ }
+
+ /**
+ *
+ */
+ @Override
+ public <T> T get(String name, Class<T> cls) {
+ final Element el = doc.getRootElement();
+ return Dom4jUtil.getChild(el, name, cls);
+ }
+
+ @Override
+ public String asString() {
+ final String xmlStr = Dom4jUtil.asString(doc);
+ return BaseEncoding.base64Url().encode(xmlStr.getBytes(Charset.forName("UTF-8")));
+ }
+
+ private static final Function<Element, String> ELEMENT_NAME = new Function<Element, String>(){
+ @Override
+ public String apply(final Element input) {
+ return input.getName();
+ }
+ };
+
+ @Override
+ public Set<String> keySet() {
+ Element element = doc.getRootElement();
+ @SuppressWarnings("unchecked")
+ List<Element> elements = element.elements();
+ return Sets.newLinkedHashSet(Iterables.transform(elements, ELEMENT_NAME));
+ }
+ }
+
+ @Override
+ public Memento create() {
+ return new MementoDefault();
+ }
+
+ @Override
+ public Memento parse(String str) {
+ final byte[] bytes = BaseEncoding.base64Url().decode(str);
+ String xmlStr = new String(bytes, Charset.forName("UTF-8"));
+ final Document doc = Dom4jUtil.parse(xmlStr);
+ return new MementoDefault(doc);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/b1f1329b/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
deleted file mode 100644
index 1537851..0000000
--- a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/viewmodelsupport/Dom4jUtil.java
+++ /dev/null
@@ -1,232 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.isis.core.runtime.services.viewmodelsupport;
-
-import java.io.IOException;
-import java.io.StringReader;
-import java.io.StringWriter;
-import java.math.BigDecimal;
-import java.math.BigInteger;
-
-import org.dom4j.Document;
-import org.dom4j.DocumentException;
-import org.dom4j.Element;
-import org.dom4j.io.OutputFormat;
-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) {
- el.addElement(name).setText(encodeForNulls(value));
- }
-
- static boolean isSupportedClass(final Class<?> cls) {
- return Parseable.isSupported(cls);
- }
-
- /**
- * @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 = decodeForNulls(child.getText());
- if(str == null) {
- return null;
- }
- return Parseable.parse(str, cls);
- }
-
- static Document parse(final String xmlStr) {
- try {
- final SAXReader saxReader = new SAXReader();
- Document doc = saxReader.read(new StringReader(xmlStr));
- return doc;
- } catch (DocumentException e) {
- throw new IsisException(e);
- }
- }
-
- static String asString(final Document doc) {
- XMLWriter writer = null;
- final StringWriter sw = new StringWriter();
- try {
- OutputFormat outputFormat = OutputFormat.createPrettyPrint();
- writer = new XMLWriter(sw, outputFormat);
- writer.write(doc);
- return sw.toString();
- } catch (IOException e) {
- throw new IsisException(e);
- } finally {
- if(writer != null) {
- try {
- writer.close();
- } catch (IOException e) {
- // ignore
- }
- }
- }
- }
-
- 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/b1f1329b/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
deleted file mode 100644
index 0b00e68..0000000
--- a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/viewmodelsupport/ViewModelSupportDefault.java
+++ /dev/null
@@ -1,99 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.isis.core.runtime.services.viewmodelsupport;
-
-import java.nio.charset.Charset;
-import java.util.List;
-import java.util.Set;
-
-import com.google.common.base.Function;
-import com.google.common.collect.Iterables;
-import com.google.common.collect.Sets;
-import com.google.common.io.BaseEncoding;
-
-import org.dom4j.Document;
-import org.dom4j.DocumentHelper;
-import org.dom4j.Element;
-
-import org.apache.isis.applib.services.viewmodelsupport.ViewModelSupport;
-
-public class ViewModelSupportDefault implements ViewModelSupport {
-
- static class MementoDefault implements Memento {
-
- private Document doc;
-
- MementoDefault(Document doc) {
- this.doc = doc;
- }
-
- MementoDefault() {
- this(DocumentHelper.createDocument());
- doc.addElement("memento");
- }
-
- @Override
- public Memento set(String name, Object value) {
- final Element el = doc.getRootElement();
- Dom4jUtil.addChild(el, name, value);
- return this;
- }
-
- /**
- *
- */
- @Override
- public <T> T get(String name, Class<T> cls) {
- final Element el = doc.getRootElement();
- return Dom4jUtil.getChild(el, name, cls);
- }
-
- @Override
- public String asString() {
- final String xmlStr = Dom4jUtil.asString(doc);
- return BaseEncoding.base64Url().encode(xmlStr.getBytes(Charset.forName("UTF-8")));
- }
-
- private static final Function<Element, String> ELEMENT_NAME = new Function<Element, String>(){
- @Override
- public String apply(final Element input) {
- return input.getName();
- }
- };
-
- @Override
- public Set<String> keySet() {
- Element element = doc.getRootElement();
- List<Element> elements = element.elements();
- return Sets.newLinkedHashSet(Iterables.transform(elements, ELEMENT_NAME));
- }
- }
-
- @Override
- public Memento create() {
- return new MementoDefault();
- }
-
- @Override
- public Memento parse(String str) {
- final byte[] bytes = BaseEncoding.base64Url().decode(str);
- String xmlStr = new String(bytes, Charset.forName("UTF-8"));
- final Document doc = Dom4jUtil.parse(xmlStr);
- return new MementoDefault(doc);
- }
-
-}
http://git-wip-us.apache.org/repos/asf/isis/blob/b1f1329b/core/runtime/src/test/java/org/apache/isis/core/runtime/services/ServiceInstantiatorTest.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/test/java/org/apache/isis/core/runtime/services/ServiceInstantiatorTest.java b/core/runtime/src/test/java/org/apache/isis/core/runtime/services/ServiceInstantiatorTest.java
index d5231db..8d0203d 100644
--- a/core/runtime/src/test/java/org/apache/isis/core/runtime/services/ServiceInstantiatorTest.java
+++ b/core/runtime/src/test/java/org/apache/isis/core/runtime/services/ServiceInstantiatorTest.java
@@ -30,9 +30,9 @@ import org.joda.time.LocalDate;
import org.junit.Before;
import org.junit.Test;
-import org.apache.isis.applib.services.viewmodelsupport.ViewModelSupport.Memento;
+import org.apache.isis.applib.services.memento.MementoService.Memento;
import org.apache.isis.core.commons.matchers.IsisMatchers;
-import org.apache.isis.core.runtime.services.viewmodelsupport.ViewModelSupportDefault;
+import org.apache.isis.core.runtime.services.memento.MementoServiceDefault;
public class ServiceInstantiatorTest {
http://git-wip-us.apache.org/repos/asf/isis/blob/b1f1329b/core/runtime/src/test/java/org/apache/isis/core/runtime/services/memento/MementoServiceDefaultTest.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/test/java/org/apache/isis/core/runtime/services/memento/MementoServiceDefaultTest.java b/core/runtime/src/test/java/org/apache/isis/core/runtime/services/memento/MementoServiceDefaultTest.java
new file mode 100644
index 0000000..028d548
--- /dev/null
+++ b/core/runtime/src/test/java/org/apache/isis/core/runtime/services/memento/MementoServiceDefaultTest.java
@@ -0,0 +1,85 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.isis.core.runtime.services.memento;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.assertThat;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+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.memento.MementoService.Memento;
+import org.apache.isis.core.runtime.services.memento.MementoServiceDefault;
+
+public class MementoServiceDefaultTest {
+
+ private MementoServiceDefault mementoService;
+
+ @Before
+ public void setUp() throws Exception {
+ mementoService = new MementoServiceDefault();
+ }
+
+ @Test
+ public void roundtrip() {
+ final Memento memento = mementoService.create();
+
+ memento.set("someString", "a string");
+ memento.set("someByte", (byte)123);
+ memento.set("someShort", (short)12345);
+ memento.set("someInt", 123456789);
+ memento.set("someLong", 1234567890123456789L);
+ memento.set("someFloat", 123.45F);
+ memento.set("someDouble", 1234567890.123456);
+ memento.set("someBooleanTrue", Boolean.TRUE);
+ memento.set("someBooleanFalse", Boolean.FALSE);
+ memento.set("someBigInteger", new BigInteger("123456789012345678901234567890"));
+ 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 = mementoService.parse(str);
+
+ assertThat(memento2.get("someString", String.class), is("a string"));
+ assertThat(memento2.get("someByte", Byte.class), is((byte)123));
+ assertThat(memento2.get("someShort", Short.class), is((short)12345));
+ assertThat(memento2.get("someInt", Integer.class), is(123456789));
+ assertThat(memento2.get("someLong", Long.class), is(1234567890123456789L));
+ assertThat(memento2.get("someFloat", Float.class), is(123.45F));
+ assertThat(memento2.get("someDouble", Double.class), is(1234567890.123456));
+ assertThat(memento2.get("someBooleanTrue", Boolean.class), is(Boolean.TRUE));
+ assertThat(memento2.get("someBooleanFalse", Boolean.class), is(Boolean.FALSE));
+ 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()));
+ }
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/b1f1329b/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
deleted file mode 100644
index 54ec3f0..0000000
--- a/core/runtime/src/test/java/org/apache/isis/core/runtime/services/viewmodelsupport/ViewModelSupportDefaultTest.java
+++ /dev/null
@@ -1,84 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.isis.core.runtime.services.viewmodelsupport;
-
-import static org.hamcrest.CoreMatchers.*;
-import static org.junit.Assert.assertThat;
-
-import java.math.BigDecimal;
-import java.math.BigInteger;
-
-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 {
-
- private ViewModelSupportDefault viewModelSupport;
-
- @Before
- public void setUp() throws Exception {
- viewModelSupport = new ViewModelSupportDefault();
- }
-
- @Test
- public void roundtrip() {
- final Memento memento = viewModelSupport.create();
-
- memento.set("someString", "a string");
- memento.set("someByte", (byte)123);
- memento.set("someShort", (short)12345);
- memento.set("someInt", 123456789);
- memento.set("someLong", 1234567890123456789L);
- memento.set("someFloat", 123.45F);
- memento.set("someDouble", 1234567890.123456);
- memento.set("someBooleanTrue", Boolean.TRUE);
- memento.set("someBooleanFalse", Boolean.FALSE);
- memento.set("someBigInteger", new BigInteger("123456789012345678901234567890"));
- 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);
-
- assertThat(memento2.get("someString", String.class), is("a string"));
- assertThat(memento2.get("someByte", Byte.class), is((byte)123));
- assertThat(memento2.get("someShort", Short.class), is((short)12345));
- assertThat(memento2.get("someInt", Integer.class), is(123456789));
- assertThat(memento2.get("someLong", Long.class), is(1234567890123456789L));
- assertThat(memento2.get("someFloat", Float.class), is(123.45F));
- assertThat(memento2.get("someDouble", Double.class), is(1234567890.123456));
- assertThat(memento2.get("someBooleanTrue", Boolean.class), is(Boolean.TRUE));
- assertThat(memento2.get("someBooleanFalse", Boolean.class), is(Boolean.FALSE));
- 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()));
- }
-}