You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@chemistry.apache.org by fg...@apache.org on 2009/07/16 18:31:40 UTC
svn commit: r794739 - in /incubator/chemistry/trunk/chemistry:
chemistry-api/src/main/java/org/apache/chemistry/
chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/
chemistry-atompub-server/src/main/java/org/apache/chemistry/ato...
Author: fguillaume
Date: Thu Jul 16 16:31:39 2009
New Revision: 794739
URL: http://svn.apache.org/viewvc?rev=794739&view=rev
Log:
CMIS-33: Implement defaultValue handling for PropertyDescription
Modified:
incubator/chemistry/trunk/chemistry/chemistry-api/src/main/java/org/apache/chemistry/Updatability.java
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPObject.java
incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPPropertyDefinition.java
incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISTypesCollection.java
incubator/chemistry/trunk/chemistry/chemistry-atompub/src/main/java/org/apache/chemistry/atompub/abdera/PropertiesElement.java
incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/impl/simple/SimpleObject.java
incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicHelper.java
incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java
Modified: incubator/chemistry/trunk/chemistry/chemistry-api/src/main/java/org/apache/chemistry/Updatability.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-api/src/main/java/org/apache/chemistry/Updatability.java?rev=794739&r1=794738&r2=794739&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-api/src/main/java/org/apache/chemistry/Updatability.java (original)
+++ incubator/chemistry/trunk/chemistry/chemistry-api/src/main/java/org/apache/chemistry/Updatability.java Thu Jul 16 16:31:39 2009
@@ -79,6 +79,14 @@
return o;
}
+ public static Updatability get(String value, Updatability def) {
+ Updatability o = all.get(value);
+ if (o == null) {
+ o = def;
+ }
+ return o;
+ }
+
@Override
public String toString() {
return value;
Modified: incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPObject.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPObject.java?rev=794739&r1=794738&r2=794739&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPObject.java (original)
+++ incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPObject.java Thu Jul 16 16:31:39 2009
@@ -164,13 +164,16 @@
}
public Serializable getValue(String name) {
- PropertyDefinition propertyDefinition = getType().getPropertyDefinition(
- name);
- if (propertyDefinition == null) {
+ PropertyDefinition pd = getType().getPropertyDefinition(name);
+ if (pd == null) {
throw new IllegalArgumentException(name);
}
// TODO deal with unfetched properties
- return entry.getValue(name);
+ Serializable value = entry.getValue(name);
+ if (value == null) {
+ value = pd.getDefaultValue();
+ }
+ return value;
}
public void save() {
Modified: incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPPropertyDefinition.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPPropertyDefinition.java?rev=794739&r1=794738&r2=794739&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPPropertyDefinition.java (original)
+++ incubator/chemistry/trunk/chemistry/chemistry-atompub-client/src/main/java/org/apache/chemistry/atompub/client/APPPropertyDefinition.java Thu Jul 16 16:31:39 2009
@@ -14,6 +14,7 @@
* Authors:
* Florent Guillaume, Nuxeo
* Bogdan Stefanescu, Nuxeo
+ * Ugo Cei, Sourcesense
*/
package org.apache.chemistry.atompub.client;
@@ -21,6 +22,7 @@
import java.lang.reflect.Array;
import java.net.URI;
import java.util.HashMap;
+import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@@ -54,6 +56,8 @@
protected Updatability updatability;
+ protected Serializable defaultValue;
+
protected APPPropertyDefinition(Map<String, Object> map) {
this.map = map;
}
@@ -114,29 +118,23 @@
}
public Serializable getDefaultValue() {
- throw new UnsupportedOperationException("Not yet implemented");
+ return defaultValue;
}
public Updatability getUpdatability() {
if (updatability == null) {
- String text = (String) map.get("updateability");
- if ("readonly".equals(text)) {
- updatability = Updatability.READ_ONLY;
- } else if ("whencheckedout".equals(text)) {
- updatability = Updatability.WHEN_CHECKED_OUT;
- } else {
- updatability = Updatability.READ_WRITE;
- }
+ String text = (String) map.get(CMIS.UPDATABILITY.getLocalPart());
+ updatability = Updatability.get(text, Updatability.READ_WRITE);
}
return updatability;
}
public boolean isQueryable() {
- return getBooleanValue("queryable");
+ return getBooleanValue(CMIS.QUERYABLE.getLocalPart());
}
public boolean isOrderable() {
- return getBooleanValue("orderable");
+ return getBooleanValue(CMIS.ORDERABLE.getLocalPart());
}
public int getPrecision() {
@@ -212,17 +210,25 @@
public static APPPropertyDefinition fromXml(StaxReader reader)
throws XMLStreamException {
HashMap<String, Object> map = new HashMap<String, Object>();
+ List<String> defaultValues = null;
APPPropertyDefinition pd = new APPPropertyDefinition(map);
ChildrenNavigator nav = reader.getChildren();
while (nav.next()) {
String tag = reader.getLocalName();
- if ("name".equals(tag)) {
+ if (tag.equals(CMIS.NAME.getLocalPart())) {
pd.name = reader.getElementText();
- } else if ("cardinality".equals(tag)) {
+ } else if (tag.equals(CMIS.CARDINALITY.getLocalPart())) {
String text = reader.getElementText();
pd.multiValued = isMultiValued(text);
- } else if ("defaultValue".equals(tag)) {
- // TODO not yet implemented
+ } else if (tag.equals(CMIS.DEFAULT_VALUE.getLocalPart())) {
+ defaultValues = new LinkedList<String>();
+ ChildrenNavigator nav2 = reader.getChildren();
+ while (nav2.next()) {
+ String tag2 = reader.getLocalName();
+ if (tag2.equals(CMIS.VALUE.getLocalPart())) {
+ defaultValues.add(reader.getElementText());
+ }
+ }
} else if (tag.startsWith("choice")) {
// TODO not yet implemented
} else {
@@ -236,6 +242,32 @@
map.put(tag, val);
}
}
+ // set default value now that we know if we're multi-valued
+ if (defaultValues != null) {
+ ValueAdapter va = ValueAdapter.getAdapter(pd.getType());
+ if (pd.isMultiValued()) {
+ Serializable[] ar = va.createArray(defaultValues.size());
+ if (pd.getType() == PropertyType.STRING) {
+ // optimization for string lists
+ pd.defaultValue = defaultValues.toArray(ar);
+ } else {
+ int i = 0;
+ for (String s : defaultValues) {
+ ar[i++] = va.readValue(s);
+ }
+ pd.defaultValue = ar;
+ }
+ } else {
+ if (defaultValues.size() != 1) {
+ log.error("Single-valued property " + pd.getName()
+ + " got a defaultValue of size: "
+ + defaultValues.size());
+ }
+ if (!defaultValues.isEmpty()) {
+ pd.defaultValue = va.readValue(defaultValues.get(0));
+ }
+ }
+ }
return pd;
}
Modified: incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISTypesCollection.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISTypesCollection.java?rev=794739&r1=794738&r2=794739&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISTypesCollection.java (original)
+++ incubator/chemistry/trunk/chemistry/chemistry-atompub-server/src/main/java/org/apache/chemistry/atompub/server/CMISTypesCollection.java Thu Jul 16 16:31:39 2009
@@ -16,6 +16,7 @@
*/
package org.apache.chemistry.atompub.server;
+import java.io.Serializable;
import java.util.Date;
import java.util.List;
@@ -35,6 +36,7 @@
import org.apache.chemistry.Repository;
import org.apache.chemistry.Type;
import org.apache.chemistry.atompub.CMIS;
+import org.apache.chemistry.atompub.abdera.PropertiesElement;
/**
* CMIS Collection for the Types.
@@ -211,8 +213,16 @@
el.setText(pd.isQueryable() ? "true" : "false");
el = factory.newElement(CMIS.ORDERABLE, def);
el.setText(pd.isOrderable() ? "true" : "false");
+ Serializable defaultValue = pd.getDefaultValue();
+ if (defaultValue != null) {
+ Element dv = factory.newElement(CMIS.DEFAULT_VALUE, def);
+ for (String s : PropertiesElement.getStringsForValue(
+ defaultValue, pd)) {
+ el = factory.newElement(CMIS.VALUE, dv);
+ el.setText(s);
+ }
+ }
// TODO choices
- // TODO defaultValue
switch (pd.getType().ordinal()) {
case PropertyType.STRING_ORD:
// TODO maxLength
Modified: incubator/chemistry/trunk/chemistry/chemistry-atompub/src/main/java/org/apache/chemistry/atompub/abdera/PropertiesElement.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-atompub/src/main/java/org/apache/chemistry/atompub/abdera/PropertiesElement.java?rev=794739&r1=794738&r2=794739&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-atompub/src/main/java/org/apache/chemistry/atompub/abdera/PropertiesElement.java (original)
+++ incubator/chemistry/trunk/chemistry/chemistry-atompub/src/main/java/org/apache/chemistry/atompub/abdera/PropertiesElement.java Thu Jul 16 16:31:39 2009
@@ -86,7 +86,6 @@
}
}
- @SuppressWarnings("null")
public void setProperty(Serializable value,
PropertyDefinition propertyDefinition) {
if (value == null) {
@@ -94,13 +93,35 @@
return;
}
QName qname = propertyQName(propertyDefinition);
+ List<String> values = getStringsForValue(value, propertyDefinition);
+ ExtensibleElement el = addExtension(qname);
+ el.setAttributeValue(CMIS.NAME, propertyDefinition.getName());
+ for (String s : values) {
+ Element val = el.addExtension(CMIS.VALUE);
+ // don't merge these two lines as JDK 5 has problems compiling it
+ val.setText(s);
+ }
+ }
+
+ /**
+ * Finds the list of Strings that are the XML form for the value.
+ *
+ * @param value the native value
+ * @param propertyDefinition the property definition
+ * @return the list of serialized strings
+ */
+ // TODO move this to a helper somewhere else
+ @SuppressWarnings("null")
+ public static List<String> getStringsForValue(Serializable value,
+ PropertyDefinition propertyDefinition) {
boolean multi = propertyDefinition.isMultiValued();
List<String> values = null;
if (multi) {
if (value.getClass().isArray()) {
values = new ArrayList<String>(Array.getLength(value));
- } else { // TODO: complex property don't know how to handle skip it
- return;
+ } else {
+ // TODO: complex property don't know how to handle skip it
+ return null;
}
}
PropertyType type = propertyDefinition.getType();
@@ -158,13 +179,7 @@
default:
throw new UnsupportedOperationException(type.toString());
}
- ExtensibleElement el = addExtension(qname);
- el.setAttributeValue(CMIS.NAME, propertyDefinition.getName());
- for (String s : values) {
- Element val = el.addExtension(CMIS.VALUE);
- // don't merge these two lines as JDK 5 has problems compiling it
- val.setText(s);
- }
+ return values;
}
protected static QName propertyQName(PropertyDefinition def) {
Modified: incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/impl/simple/SimpleObject.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/impl/simple/SimpleObject.java?rev=794739&r1=794738&r2=794739&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/impl/simple/SimpleObject.java (original)
+++ incubator/chemistry/trunk/chemistry/chemistry-commons/src/main/java/org/apache/chemistry/impl/simple/SimpleObject.java Thu Jul 16 16:31:39 2009
@@ -121,12 +121,15 @@
}
public Serializable getValue(String name) {
- PropertyDefinition propertyDefinition = getType().getPropertyDefinition(
- name);
- if (propertyDefinition == null) {
+ PropertyDefinition pd = getType().getPropertyDefinition(name);
+ if (pd == null) {
throw new IllegalArgumentException(name);
}
- return entry.data.get(name);
+ Serializable value = entry.data.get(name);
+ if (value == null) {
+ value = pd.getDefaultValue();
+ }
+ return value;
}
public Property getProperty(String name) {
Modified: incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicHelper.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicHelper.java?rev=794739&r1=794738&r2=794739&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicHelper.java (original)
+++ incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicHelper.java Thu Jul 16 16:31:39 2009
@@ -49,8 +49,8 @@
public static Repository makeRepository(String rootId) throws IOException {
PropertyDefinition p1 = new SimplePropertyDefinition("title",
"def:title", "Title", "", false, PropertyType.STRING, false,
- null, false, false, "", Updatability.READ_WRITE, true, true, 0,
- null, null, -1, null, null);
+ null, false, false, "(no title)", Updatability.READ_WRITE,
+ true, true, 0, null, null, -1, null, null);
PropertyDefinition p2 = new SimplePropertyDefinition("description",
"def:description", "Description", "", false,
PropertyType.STRING, false, null, false, false, "",
@@ -99,8 +99,7 @@
Document doc3 = folder2.newDocument("doc");
doc3.setName("doc 3");
- doc3.setValue("title", "doc 3 title");
- doc3.setValue("description", "The doc 3 descr");
+ // no title, description or date
ContentStream cs = new SimpleContentStream(
TEST_FILE_CONTENT.getBytes("UTF-8"), "text/plain", "doc3.txt",
null);
Modified: incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java?rev=794739&r1=794738&r2=794739&view=diff
==============================================================================
--- incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java (original)
+++ incubator/chemistry/trunk/chemistry/chemistry-tests/src/main/java/org/apache/chemistry/test/BasicTestCase.java Thu Jul 16 16:31:39 2009
@@ -18,6 +18,7 @@
package org.apache.chemistry.test;
import java.io.InputStream;
+import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@@ -78,6 +79,29 @@
assertEquals(1, entries.size());
}
+ public void testDefaultValues() {
+ Folder root = conn.getRootFolder();
+ Folder f1 = (Folder) root.getChildren(BaseType.FOLDER).get(0);
+ Folder f2 = (Folder) f1.getChildren(BaseType.FOLDER).get(0);
+ List<CMISObject> children = f2.getChildren(null);
+ assertEquals(3, children.size());
+ // check default values
+ for (CMISObject child : children) {
+ String name = child.getName();
+ String title = (String) child.getValue("title");
+ String descr = (String) child.getValue("description");
+ Calendar date = (Calendar) child.getValue("date");
+ if (name.equals("doc 3")) {
+ assertEquals("(no title)", title); // uses defaultValue
+ assertEquals("", descr); // emptry string defaultValue
+ } else {
+ assertNotSame("", title);
+ assertNotNull(descr);
+ }
+ assertNull(date);
+ }
+ }
+
public void testQuery() {
Connection conn = repository.getConnection(null);
Collection<CMISObject> res = conn.query("SELECT * FROM doc", false);