Enhance checkin method to support setting properties simultaneously to close #CMIS-972

git-svn-id: https://svn.apache.org/repos/asf/chemistry/cmislib/trunk@1776466 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/cmislib/atompub/binding.py b/src/cmislib/atompub/binding.py
index 8da6145..4de8724 100644
--- a/src/cmislib/atompub/binding.py
+++ b/src/cmislib/atompub/binding.py
@@ -2363,7 +2363,8 @@
         self.reload()
         return self.getProperties()['cmis:versionSeriesCheckedOutBy']
 
-    def checkin(self, checkinComment=None, **kwargs):
+    def checkin(self, checkinComment=None, contentFile=None, contentType=None,
+                properties=None, **kwargs):
 
         """
         Checks in this :class:`Document` which must be a private
@@ -2379,10 +2380,7 @@
         >>> doc.isCheckedOut()
         False
 
-        The following optional arguments are supported:
-         - major
-         - properties
-         - contentStream
+        The following optional arguments are NOT supported:
          - policies
          - addACEs
          - removeACEs
@@ -2396,8 +2394,19 @@
         kwargs['checkin'] = 'true'
         kwargs['checkinComment'] = checkinComment
 
-        # Build an empty ATOM entry
-        entryXmlDoc = getEmptyXmlDoc()
+        if not properties and not contentFile:
+            # Build an empty ATOM entry
+            entryXmlDoc = getEmptyXmlDoc()
+        else:
+            # the getEntryXmlDoc function may need the object type
+            objectTypeId = None
+            if self.properties.has_key('cmis:objectTypeId') and not properties.has_key('cmis:objectTypeId'):
+                objectTypeId = self.properties['cmis:objectTypeId']
+                self.logger.debug('This object type is:%s', objectTypeId)
+
+            # build the entry based on the properties provided
+            entryXmlDoc = getEntryXmlDoc(
+                self._repository, objectTypeId, properties, contentFile, contentType)
 
         # Get the self link
         # Do a PUT of the empty ATOM to the self link
diff --git a/src/cmislib/browser/binding.py b/src/cmislib/browser/binding.py
index a9e010b..1c68e38 100644
--- a/src/cmislib/browser/binding.py
+++ b/src/cmislib/browser/binding.py
@@ -26,7 +26,8 @@
 from cmislib.exceptions import CmisException, InvalidArgumentException,\
                                NotSupportedException, ObjectNotFoundException
 from cmislib.net import RESTService as Rest
-from cmislib.util import parsePropValueByType, parseDateTimeValue, safe_quote
+from cmislib.util import parsePropValueByType, parseDateTimeValue, safe_quote,\
+                        safe_urlencode
 import json
 import logging
 import StringIO
@@ -1756,7 +1757,8 @@
         self.reload()
         return self.getProperties()['cmis:versionSeriesCheckedOutBy']
 
-    def checkin(self, checkinComment=None, **kwargs):
+    def checkin(self, checkinComment=None, contentFile=None, contentType=None,
+                properties=None, **kwargs):
 
         """
         Checks in this :class:`Document` which must be a private
@@ -1773,9 +1775,6 @@
         False
 
         The following optional arguments are NOT supported:
-         - major
-         - properties
-         - contentStream
          - policies
          - addACEs
          - removeACEs
@@ -1784,22 +1783,30 @@
         # major = true is supposed to be the default but inmemory 0.9 is throwing an error 500 without it
         if not kwargs.has_key('major'):
             kwargs['major'] = 'true'
+        else:
+            kwargs['major'] = 'false'
 
-        kwargs['checkinComment'] = checkinComment
+        props = {
+            'checkinComment': checkinComment or "",
+        }
+        props.update(kwargs)
+        propCount = 0
+        properties = properties or {}
+        for key, value in properties.iteritems():
+            props["propertyId[%s]" % propCount] = key
+            props["propertyValue[%s]" % propCount] = value
+            propCount += 1
 
-        ciUrl = self._repository.getRootFolderUrl()
+        ciUrl = self._repository.getRootFolderUrl() + "?objectId=" + self.id + "&cmisaction=checkin"
 
-        # TODO don't hardcode major flag
-        props = {"objectId": self.id,
-                 "cmisaction": "checkIn"}
+        contentType, body = encode_multipart_formdata(props, contentFile, contentType)
 
         # invoke the URL
         result = self._cmisClient.binding.post(ciUrl.encode('utf-8'),
-                                               safe_urlencode(props),
-                                               'application/x-www-form-urlencoded',
+                                               body,
+                                               contentType,
                                                self._cmisClient.username,
-                                               self._cmisClient.password,
-                                               **kwargs)
+                                               self._cmisClient.password)
 
         return getSpecializedObject(BrowserCmisObject(self._cmisClient, self._repository, data=result))
 
diff --git a/src/cmislib/domain.py b/src/cmislib/domain.py
index a362339..bef7f30 100644
--- a/src/cmislib/domain.py
+++ b/src/cmislib/domain.py
@@ -1241,7 +1241,8 @@
 
         pass
 
-    def checkin(self, checkinComment=None, **kwargs):
+    def checkin(self, checkinComment=None, contentFile=None, contentType=None,
+                properties=None, **kwargs):
 
         """
         Checks in this :class:`Document` which must be a private
@@ -1257,10 +1258,7 @@
         >>> doc.isCheckedOut()
         False
 
-        The following optional arguments are supported:
-         - major
-         - properties
-         - contentStream
+        The following optional arguments are NOT supported:
          - policies
          - addACEs
          - removeACEs
diff --git a/src/cmislib/util.py b/src/cmislib/util.py
index 4455271..00b9c70 100644
--- a/src/cmislib/util.py
+++ b/src/cmislib/util.py
@@ -25,7 +25,7 @@
 import logging
 import datetime
 from cmislib.domain import CmisId
-from urllib import quote
+from urllib import urlencode, quote
 
 moduleLogger = logging.getLogger('cmislib.util')
 
diff --git a/src/tests/cmislibtest.py b/src/tests/cmislibtest.py
index 70f9415..3c2be3e 100644
--- a/src/tests/cmislibtest.py
+++ b/src/tests/cmislibtest.py
@@ -899,6 +899,48 @@
             if testDoc.isCheckedOut():
                 pwcDoc.delete()
 
+    def testCheckinContentAndProperties(self):
+        """Checkin a document with a new content a modifed properties"""
+        testFilename = settings.TEST_BINARY_1.split('/')[-1]
+        contentFile = open(testFilename, 'rb')
+        props = {'cmis:objectTypeId': settings.VERSIONABLE_TYPE_ID}
+        testDoc = self._testFolder.createDocument(testFilename, contentFile=contentFile, properties=props)
+        contentFile.close()
+        self.assertEquals(testFilename, testDoc.getName())
+        if not 'canCheckOut' in testDoc.allowableActions.keys():
+            print 'The test doc cannot be checked out...skipping'
+            return
+        pwcDoc = testDoc.checkout()
+
+        try:
+            self.assertTrue(testDoc.isCheckedOut())
+            testFile2 = settings.TEST_BINARY_2
+            testFile2Size = os.path.getsize(testFile2)
+            exportFile2 = testFile2.replace('.', 'export.')
+            contentFile2 = open(testFile2, 'rb')
+            props = {'cmis:name': 'testDocument2'}
+            testDoc = pwcDoc.checkin(
+                contentFile=contentFile2,
+                properties=props)
+            contentFile2.close()
+            self.assertFalse(testDoc.isCheckedOut())
+            self.assertEqual('testDocument2', testDoc.getName())
+
+            # expport the result
+            result = testDoc.getContentStream()
+            outfile = open(exportFile2, 'wb')
+            outfile.write(result.read())
+            result.close()
+            outfile.close()
+
+            # the file we exported should be the same size as the file we
+            # originally created
+            self.assertEquals(testFile2Size, os.path.getsize(exportFile2))
+
+        finally:
+            if testDoc.isCheckedOut():
+                pwcDoc.delete()
+
     def testCheckinAfterGetPWC(self):
         """Create a document in a test folder, check it out, call getPWC, then checkin"""
         if not self._repo.getCapabilities()['PWCUpdatable'] == True: