You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@chemistry.apache.org by je...@apache.org on 2010/06/18 17:43:24 UTC

svn commit: r956026 - in /incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src: main/java/org/apache/chemistry/opencmis/inmemory/server/ test/java/org/apache/chemistry/opencmis/inmemory/

Author: jens
Date: Fri Jun 18 15:43:24 2010
New Revision: 956026

URL: http://svn.apache.org/viewvc?rev=956026&view=rev
Log:
Add missing support for default values in properties for InMemory Server

Modified:
    incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/server/InMemoryObjectServiceImpl.java
    incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/test/java/org/apache/chemistry/opencmis/inmemory/ObjectServiceTest.java

Modified: incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/server/InMemoryObjectServiceImpl.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/server/InMemoryObjectServiceImpl.java?rev=956026&r1=956025&r2=956026&view=diff
==============================================================================
--- incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/server/InMemoryObjectServiceImpl.java (original)
+++ incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/main/java/org/apache/chemistry/opencmis/inmemory/server/InMemoryObjectServiceImpl.java Fri Jun 18 15:43:24 2010
@@ -18,8 +18,11 @@
  */
 package org.apache.chemistry.opencmis.inmemory.server;
 
+import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.util.ArrayList;
+import java.util.GregorianCalendar;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -31,6 +34,7 @@ import org.apache.chemistry.opencmis.com
 import org.apache.chemistry.opencmis.commons.data.FailedToDeleteData;
 import org.apache.chemistry.opencmis.commons.data.ObjectData;
 import org.apache.chemistry.opencmis.commons.data.Properties;
+import org.apache.chemistry.opencmis.commons.data.PropertyBoolean;
 import org.apache.chemistry.opencmis.commons.data.PropertyData;
 import org.apache.chemistry.opencmis.commons.data.RenditionData;
 import org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition;
@@ -39,6 +43,7 @@ import org.apache.chemistry.opencmis.com
 import org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionContainer;
 import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
 import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
+import org.apache.chemistry.opencmis.commons.enums.PropertyType;
 import org.apache.chemistry.opencmis.commons.enums.UnfileObject;
 import org.apache.chemistry.opencmis.commons.enums.Updatability;
 import org.apache.chemistry.opencmis.commons.enums.VersioningState;
@@ -659,9 +664,9 @@ public class InMemoryObjectServiceImpl e
         checkRepositoryId(repositoryId);
 
         ObjectStore objectStore = fStoreManager.getObjectStore(repositoryId);
-
+        Map<String, PropertyData<?>> propMap = properties.getProperties();
         // get name from properties
-        PropertyData<?> pd = properties.getProperties().get(PropertyIds.NAME);
+        PropertyData<?> pd = propMap.get(PropertyIds.NAME);
         String name = (String) pd.getFirstValue();
 
         // Validation stuff
@@ -694,6 +699,9 @@ public class InMemoryObjectServiceImpl e
 
         TypeValidator.validateVersionStateForCreate((DocumentTypeDefinition) typeDef, versioningState);
         TypeValidator.validateProperties(typeDef, properties, true);
+        
+        // set properties that are not set but have a default:
+        propMap = setDefaultProperties(typeDef, propMap);
 
         // set user, creation date, etc.
         if (user == null)
@@ -712,8 +720,8 @@ public class InMemoryObjectServiceImpl e
             // set parent in doc
             else
                 verDoc.persist();
-            version.createSystemBasePropertiesWhenCreated(properties.getProperties(), user);
-            version.setCustomProperties(properties.getProperties());
+            version.createSystemBasePropertiesWhenCreated(propMap, user);
+            version.setCustomProperties(propMap);
             version.persist();
             so = version; // return the version and not the version series to
             // caller
@@ -721,8 +729,8 @@ public class InMemoryObjectServiceImpl e
             Document doc = fStoreManager.getObjectStore(repositoryId).createDocument(name);
             doc.setContent(contentStream, false);
             // add document to folder
-            doc.createSystemBasePropertiesWhenCreated(properties.getProperties(), user);
-            doc.setCustomProperties(properties.getProperties());
+            doc.createSystemBasePropertiesWhenCreated(propMap, user);
+            doc.setCustomProperties(propMap);
             if (null != folder)
                 folder.addChildDocument(doc); // add document to folder and set
             // parent in doc
@@ -869,4 +877,40 @@ public class InMemoryObjectServiceImpl e
         return csd;
     }
 
+    @SuppressWarnings("unchecked")
+    private Map<String, PropertyData<?>> setDefaultProperties(TypeDefinition typeDef, Map<String, PropertyData<?>> properties) {
+        Map<String, PropertyDefinition<?>> propDefs = typeDef.getPropertyDefinitions();
+        boolean hasCopied = false;
+        
+        for ( PropertyDefinition<?> propDef : propDefs.values()) {
+            String propId = propDef.getId();
+            List<?> defaultVal = propDef.getDefaultValue();
+            PropertyData<?> pd = null;
+            if (defaultVal != null && null == properties.get(propId)) {
+                if (!hasCopied) {
+                    properties = new HashMap<String, PropertyData<?>>(properties); // copy because it is an unmodified collection
+                    hasCopied = true;
+                }
+                if (propDef.getPropertyType() == PropertyType.BOOLEAN) 
+                    pd = fStoreManager.getObjectFactory().createPropertyBooleanData(propId, (List<Boolean>)defaultVal);
+                else if (propDef.getPropertyType() == PropertyType.DATETIME)
+                    pd = fStoreManager.getObjectFactory().createPropertyDateTimeData(propId, (List<GregorianCalendar>)defaultVal);
+                else if (propDef.getPropertyType() == PropertyType.DECIMAL)
+                    pd = fStoreManager.getObjectFactory().createPropertyDecimalData(propId, (List<BigDecimal>)defaultVal);
+                else if (propDef.getPropertyType() == PropertyType.HTML)
+                    pd = fStoreManager.getObjectFactory().createPropertyHtmlData(propId, (List<String>)defaultVal);
+                else if (propDef.getPropertyType() == PropertyType.ID)
+                    pd = fStoreManager.getObjectFactory().createPropertyIdData(propId, (List<String>)defaultVal);
+                else if (propDef.getPropertyType() == PropertyType.INTEGER)
+                    pd = fStoreManager.getObjectFactory().createPropertyIntegerData(propId, (List<BigInteger>)defaultVal);
+                else if (propDef.getPropertyType() == PropertyType.STRING)
+                    pd = fStoreManager.getObjectFactory().createPropertyStringData(propId, (List<String>)defaultVal);
+                else if (propDef.getPropertyType() == PropertyType.URI)
+                    pd = fStoreManager.getObjectFactory().createPropertyUriData(propId, (List<String>)defaultVal);
+                // set property:
+                properties.put(propId, pd);
+            }
+        }
+        return properties;
+    }
 }

Modified: incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/test/java/org/apache/chemistry/opencmis/inmemory/ObjectServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/test/java/org/apache/chemistry/opencmis/inmemory/ObjectServiceTest.java?rev=956026&r1=956025&r2=956026&view=diff
==============================================================================
--- incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/test/java/org/apache/chemistry/opencmis/inmemory/ObjectServiceTest.java (original)
+++ incubator/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-inmemory/src/test/java/org/apache/chemistry/opencmis/inmemory/ObjectServiceTest.java Fri Jun 18 15:43:24 2010
@@ -79,11 +79,13 @@ public class ObjectServiceTest extends A
     private static Log log = LogFactory.getLog(ObjectServiceTest.class);
     public static final String TEST_FOLDER_TYPE_ID = "MyFolderType";
     public static final String TEST_DOCUMENT_TYPE_ID = "MyDocumentType";
+    public static final String TEST_DEFAULT_TYPE_ID = "DocumentTypeWithDefault";
     public static final String TEST_FOLDER_STRING_PROP_ID = "MyFolderStringProp";
     public static final String TEST_DOCUMENT_STRING_PROP_ID = "MyDocumentStringProp";
     private static final String TEST_CUSTOM_DOCUMENT_TYPE_ID = "MyCustomDocumentType";
     private static final String TEST_INHERITED_CUSTOM_DOCUMENT_TYPE_ID = "MyCustomInheritedDocType";
     private static final String TEST_DOCUMENT_MY_STRING_PROP_ID = "MyCustomDocumentStringProp";
+    private static final String TEST_DOCUMENT_MY_MULTI_STRING_PROP_ID = "MyCustomDocumentMultiStringProp";
     private static final String TEST_DOCUMENT_MY_INT_PROP_ID = "MyCustomDocumentIntProp";
     private static final String TEST_DOCUMENT_MY_SUB_STRING_PROP_ID = "MyInheritedStringProp";
     private static final String TEST_DOCUMENT_MY_SUB_INT_PROP_ID = "MyInheritedIntProp";
@@ -708,6 +710,29 @@ public class ObjectServiceTest extends A
         log.info("... testAllowableActions() finished.");
     }
 
+    @Test
+    public void testDefaultProperties() {
+        log.info("starting testDefaultProperties() ...");
+        String id = createDocument("DefPropDoc", fRootFolderId, TEST_DEFAULT_TYPE_ID, false);
+        if (id != null)
+            log.info("createDocument succeeded with created id: " + id);
+        ObjectData res = getDocumentObjectData(id);
+        Map<String, PropertyData<?>> props = res.getProperties().getProperties();
+        PropertyData<?> pd =  props.get(TEST_DOCUMENT_MY_INT_PROP_ID);
+        assertNotNull(pd);
+        Object bi = pd.getFirstValue();
+        assertNotNull(bi);
+        assertEquals(BigInteger.valueOf(100), bi);
+        
+        pd =  props.get(TEST_DOCUMENT_MY_MULTI_STRING_PROP_ID);
+        assertNotNull(pd);
+        List<String> valueList = (List<String>) pd.getValues();
+        assertNotNull(valueList);
+        assertTrue(valueList.contains("Apache"));
+        assertTrue(valueList.contains("CMIS"));
+        log.info("... testDefaultProperties() finished.");
+    }
+    
     private void verifyAllowableActionsDocument(Set<Action> actions, boolean isVersioned, boolean hasContent) {
         assertTrue(actions.contains(Action.CAN_DELETE_OBJECT));
         assertTrue(actions.contains(Action.CAN_UPDATE_PROPERTIES));
@@ -959,6 +984,7 @@ public class ObjectServiceTest extends A
             typesList.add(cmisFolderType);
             typesList.add(customDocType);
             typesList.add(createCustomInheritedType(customDocType));
+            typesList.add(createTypeWithDefault());
             return typesList;
         }
 
@@ -999,6 +1025,29 @@ public class ObjectServiceTest extends A
             cmisDocumentType.addCustomPropertyDefinitions(propertyDefinitions);
             return cmisDocumentType;
         }
+        
+        private static InMemoryDocumentTypeDefinition createTypeWithDefault() {
+            InMemoryDocumentTypeDefinition cmisDocumentType = new InMemoryDocumentTypeDefinition(
+                    TEST_DEFAULT_TYPE_ID, "Document Type With default values", InMemoryDocumentTypeDefinition
+                    .getRootDocumentType());
+            Map<String, PropertyDefinition<?>> propertyDefinitions = new HashMap<String, PropertyDefinition<?>>();
+            PropertyStringDefinitionImpl prop = PropertyCreationHelper.createStringMultiDefinition(
+                    TEST_DOCUMENT_MY_MULTI_STRING_PROP_ID, "Test Multi String Property");
+            prop.setIsRequired(false);
+            List<String> defValS = new ArrayList<String>() {{ add("Apache"); add("CMIS"); }};
+            prop.setDefaultValue(defValS);
+            propertyDefinitions.put(prop.getId(), prop);
+
+            PropertyIntegerDefinitionImpl prop2 = PropertyCreationHelper.createIntegerDefinition(
+                    TEST_DOCUMENT_MY_INT_PROP_ID, "Test Integer Property");
+            prop2.setIsRequired(false);
+            List<BigInteger> defVal = new ArrayList<BigInteger>() {{ add(BigInteger.valueOf(100)); }};
+            prop2.setDefaultValue(defVal);
+            propertyDefinitions.put(prop2.getId(), prop2);
+            
+            cmisDocumentType.addCustomPropertyDefinitions(propertyDefinitions);
+            return cmisDocumentType;
+        }
 
     }