Skip to content

Instantly share code, notes, and snippets.

@jpotts
Created April 22, 2014 22:44
Show Gist options
  • Save jpotts/11196922 to your computer and use it in GitHub Desktop.
Save jpotts/11196922 to your computer and use it in GitHub Desktop.
Add a category to a document in Alfresco using CMIS 1.1 and cmis:item support
def addCategory(documentPath, categoryName) {
println("Adding '" + categoryName + "' to the document at " + documentPath)
rs = session.query("select alfcmis:nodeRef from cm:category where cmis:name = '" + categoryName + "'", false)
if (rs.getTotalNumItems() == 0) {
println("Couldn't find category with name: " + categoryName)
return
}
catId = rs.getAt(0).getPropertyValueById("alfcmis:nodeRef")
testDoc = session.getObjectByPath(documentPath)
testDoc.refresh() //make sure we didn't get the object from the cache
aspects = testDoc.getPropertyValue("cmis:secondaryObjectTypeIds")
if (!aspects.contains("P:cm:generalclassifiable")) {
println("Adding aspect")
aspects.add("P:cm:generalclassifiable")
props = new HashMap<String, Object>()
props["cmis:secondaryObjectTypeIds"] = aspects
testDoc.updateProperties(props, true)
println("Aspects: " + testDoc.getPropertyValue("cmis:secondaryObjectTypeIds"))
}
// always want the latest version for this
if (!testDoc.isLatestVersion()) {
testDoc = testDoc.getObjectOfLatestVersion(false)
}
catIds = testDoc.getPropertyValue("cm:categories")
if (catIds == null) {
catIds = new ArrayList<Object>()
}
if (catIds.isEmpty()) {
catIds = new ArrayList<Object>()
}
if (!catIds.contains(catId)) {
println("Adding category")
catIds.add(catId)
println(catIds)
props = new HashMap<String, Object>()
props["cm:categories"] = catIds
testDoc.updateProperties(props, false)
}
println("Done")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment