First version of a document doesn't have aspect values

cancel
Showing results for 
Search instead for 
Did you mean: 
Established Member

First version of a document doesn't have aspect values

Jump to solution

In code, I use CMIS to communicate with Alfresco 5.1.x community edition. I followed "CMIS and Apache Chemistry in Action" book. 

We have our own custom model defined, whose parent is cm:content. Our model has its own (custom meta-data) set of properties. This is defined as an aspect

Things are working good in general. I have one issue that I could not figure out. This is with respect to versions. When I view the version history, the first version (oldest version), does not have our custom meta-data (aspect) properties set. However, the later versions have values for these properties. 

It is the same code that creates versions whenever in needed.

Do you know how could this happen?

In the attachments, please see the version 1.0. It does not have values for the aspect properties. 

Whereas, the version 2.0 has the values populated.

It does not matter how many versions I end up creating, always the very first version that Alfresco creates does not have the aspect values populated. 

Code

uniqueProperties.add(categoryProperty);
    uniqueProperties.add(linkProperty);
    uniqueProperties.add(nameProperty);
    Document existingDocument = getAlfrescoCMISDocumentByProperties(uniqueProperties, user);
    
    if (existingDocument.isVersionSeriesCheckedOut()) {
     existingDocument.cancelCheckOut();
    }
    // Check out the document
    ObjectId documentOjectId = existingDocument.checkOut();
    insertDocument = (Document) cmisSession.getObject(documentOjectId);
    
    String mimeType = "application/octet-stream";
    if (mDocument.getMimeType() != null) {
     mimeType = mDocument.getMimeType();
    }
    
    InputStream stream = null;
    
    try {
     byte[] content = mDocument.getBData();
     stream = new ByteArrayInputStream(content);
     ContentStream contentStream = cmisSession.getObjectFactory().createContentStream(
       mDocument.getFileName(), Long.valueOf(content.length), mimeType, stream);
     
     cmisProperties = document2AlfrescoDocumentProperties(mDocument, true);
     cmisProperties.remove("cmis:name");
     insertDocument.getObjectOfLatestVersion(false);
     ObjectId insertedDocumentObjectId = insertDocument.checkIn(true, cmisProperties, contentStream, user);
     CmisObject insertedDocument = (Document) cmisSession.getObject(insertedDocumentObjectId);
     insertedDocument.refresh();
    }
    catch (Exception inExc) {
     message = "Error occurred while uploading a new version of the document [" + (namePropertyValue == null ? "" : namePropertyValue) + "] - " + inExc.getMessage();
     inExc.printStackTrace();
    }
    finally {
     
     if (stream != null) { try { stream.close(); } catch (Exception e) {} }
    }

Gnanasekaran Sakthivel
1 Solution

Accepted Solutions
Master

Re: First version of a document doesn't have aspect values

Jump to solution

Since I don't know and can't see how all your methods mentioned in the code create / process the document (nor do I know the exact definition of your model), it is hard to say what is the cause for this behaviour. Bear in mind though that with CMIS every call to create / modify the state of a document is an individual transaction, so if you create your document and then in a second operation add the aspect to it, this behaviour would be expected as the first version is already created when the document is being created, and so does not (yet) have the aspect.

View solution in original post

5 Replies
Master

Re: First version of a document doesn't have aspect values

Jump to solution

Since I don't know and can't see how all your methods mentioned in the code create / process the document (nor do I know the exact definition of your model), it is hard to say what is the cause for this behaviour. Bear in mind though that with CMIS every call to create / modify the state of a document is an individual transaction, so if you create your document and then in a second operation add the aspect to it, this behaviour would be expected as the first version is already created when the document is being created, and so does not (yet) have the aspect.

View solution in original post

Established Member

Re: First version of a document doesn't have aspect values

Jump to solution

Thank you Axel for your answers.

Following code is a snippet of what I am doing on the first insert/index/create...

As you hinted, I would try to add the aspect in the first createDocument call itself. I kind of think, there is a reason I did this way, I mean in two steps. 

I will update here my result.

Even in this case, that is to creating as the first action and then updating the secondary type properties as a second call, should keep these properties in the very first version right? Updating the (aspect) properties should not be considered as an outside-of-version action, right?

Please let me know. Appreciations.

cmisProperties = dmsDocument2AlfrescoDocumentProperties(mDocument, false); // false to including custom secondary type properties

InputStream stream = null;
    String objectId = null;
    Long contentSize = null;
    
    try {
     byte[] content = mDocument.getBData();
     stream = new ByteArrayInputStream(content);
     ContentStream contentStream = cmisSession.getObjectFactory().createContentStream(
       mDocument.getFileName(), Long.valueOf(content.length), mimeType, stream);
     insertedDocument = entityFolder.createDocument(cmisProperties, contentStream, VersioningState.MAJOR);
     objectId = insertedDocument.getId();
     contentSize = new Long(content.length);
    }
    catch(Exception inExc) {
     message = "Error occurred while uploading the document [" + (namePropertyValue == null ? "" : namePropertyValue) + "] - " + inExc.getMessage();
     inExc.printStackTrace();
    }

List<Object> aspects = insertedDocument.getProperty("cmis:secondaryObjectTypeIds").getValues();
    if (!aspects.contains("P:salo:documentProperties")) {
     aspects.add("P:salo:documentProperties");
     HashMap<String, Object> props = new HashMap<String, Object>();
     props.put("cmis:secondaryObjectTypeIds", aspects);
    }
    cmisProperties = dmsDocument2AlfrescoDocumentProperties(mDocument, true); // true to include secondary properties and values
    insertedDocument.updateProperties(cmisProperties);
    insertedDocument.refresh();
Gnanasekaran Sakthivel
Established Member

Re: First version of a document doesn't have aspect values

Jump to solution

Can I create a document (under the folder) without versioning, I mean by calling 

entityFolder.createDocument(cmisProperties, contentStream, VersioningState.NONE);

I wonder what this VersioningState.NONE mean. Does this mean, we can never have versioning on this document that we create.

Or does this mean that we are telling Alfresco to not version this document at this time?

I am assuming that I can create first without versioning, and then start/do versioning after/while updating the properties (aspect/secondary-type) for the first time.

As a result, once we create a document without version, then we can check it out, update the properties (aspect/secondary-type), and then version it.

Does this make sense or is this possible?

Gnanasekaran Sakthivel
Master

Re: First version of a document doesn't have aspect values

Jump to solution

As far as I know the Alfresco CMIS integration, you cannot create a document without a version, as versions are intrically a part of a CMIS document. The VersioningState.NONE is only used to specify in an update operation that you do not want a new version to be created for that update.

You need to provide all metadata that you want in the initial version with the initial "create document" call.

Established Member

Re: First version of a document doesn't have aspect values

Jump to solution

Well, it worked. 

All it took was the difference between the following two. Since the secondary object type is given as a list, it ignores the secondary type if we do not give it as a list. Then it says, a certain property is not valid.

Property 'salo:m_effective_date' is not valid for this type or one of the secondary types!.

Anyways....

Correct way:

List<String> secondaryTypes = new ArrayList<String>();
secondaryTypes.add("P:salo:documentProperties");
cmisDocumentProperties.put(PropertyIds.SECONDARY_OBJECT_TYPE_IDS, secondaryTypes);

Wrong way:

cmisDocumentProperties.put(PropertyIds.SECONDARY_OBJECT_TYPE_IDS, "P:salo:documentProperties");

Now, following Axel's hints, the first version in the version history also captured all the property values. With approach 1, after we make more versions, we notice the 1st version of the document does not have the custom aspect property values. With approach 2, it is good. So, approach 2 is correct.

Approach 1:

create the document without the custom model's custom aspect properties

then update the properties later.

Approach 2:

create the document in the first place with custom model's custom aspect properties

Gnanasekaran Sakthivel