Skip to content

Instantly share code, notes, and snippets.

@emrul
Created September 27, 2016 19:23
Show Gist options
  • Save emrul/e572c68801c1a9f4025a8e4bcc9f2da6 to your computer and use it in GitHub Desktop.
Save emrul/e572c68801c1a9f4025a8e4bcc9f2da6 to your computer and use it in GitHub Desktop.
Create document types in CMIS from definitions stored in files in local filesystem using OpenCMIS Workbench scripting
import org.apache.chemistry.opencmis.commons.*
import org.apache.chemistry.opencmis.commons.data.*
import org.apache.chemistry.opencmis.commons.enums.*
import org.apache.chemistry.opencmis.commons.impl.*
import org.apache.chemistry.opencmis.client.api.*
import org.apache.chemistry.opencmis.commons.definitions.*
import org.apache.chemistry.opencmis.client.util.*
import static groovy.io.FileType.*
import static groovy.io.FileVisitResult.*
cmis = new scripts.CMIS(session)
def dirPath = "/Path/To/Some/Dir"
// upload folder tree
createTypes(new File(dirPath))
//--------------------------------------------------
def createTypes(File dir) {
println "Checking files...\n"
def typesList = []
def hasError = false;
dir.traverse(type: FILES, preDir: {println "- Entering directory ${it.name}" }, nameFilter: ~/.*\.json$/) {
println "Creating type in file '${it.name}'"
InputStream is
try {
is = new BufferedInputStream(new FileInputStream(it), 64 * 1024);
TypeDefinition type = TypeUtils.readFromJSON(is);
if ( !checkTypeDefinition(type) ) {
hasError = true
}
else {
typesList += type
}
}
finally {
is.close();
}
}
if ( hasError ) {
println "\n...errors occured, not continuing."
}
else {
println "\nCreating types...\n"
typesList.each {
session.createType(it)
}
}
return
}
def checkTypeDefinition(TypeDefinition type) {
StringBuilder sb = new StringBuilder(128);
def typeResult = TypeUtils.validateTypeDefinition(type);
if (CollectionsHelper.isNotEmpty(typeResult)) {
sb.append("\nType Definition:\n");
for (def error : typeResult) {
sb.append("- ");
sb.append(error.toString());
sb.append('\n');
}
}
if (type.getPropertyDefinitions() != null) {
for (PropertyDefinition<?> propDef : type.getPropertyDefinitions().values()) {
def propResult = TypeUtils.validatePropertyDefinition(propDef);
if (CollectionsHelper.isNotEmpty(propResult)) {
sb.append("\nProperty Definition '" + propDef.getId() + "':\n");
for (def error : propResult) {
sb.append("- ");
sb.append(error.toString());
sb.append('\n');
}
}
}
}
if (sb.length() == 0) {
return true;
}
else {
println(sb.toString())
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment