Search code examples
alfrescocmisalfresco-webscriptsopencmis

Is it possible to create a folder on the Alfresco site by using OpenCMIS API?


I have the Presentation Web Script (script A) and the Data Web Script (script B).

In the script A I build the dialog that interacts with the script B.

enter image description here

Here I am forming some path where the some file will be uploaded (group, year and number parameters define this path):

...
var submitHandler = function() {
    var dataWebScriptUrl = window.location.protocol + '//' + 
        window.location.host + "/alfresco/s/ms-ws/script-b?guest=true";

    var yearCombo = document.getElementById("year");
    var year = yearCombo.options[yearCombo.selectedIndex].value;

    var groupCombo = document.getElementById("group");
    var group = groupCombo.options[groupCombo.selectedIndex].value;

    var numberCombo = document.getElementById("number");
    var number = numberCombo.value;

    var uploadedFile = document.getElementById("uploadedFile");
    var file = uploadedFile.files[0];

    var formData = new FormData();
    formData.append("year", year);
    formData.append("group", group);
    formData.append("number", number);
    formData.append("uploadedFile", file);      

    var xhr = new XMLHttpRequest();
    xhr.open("POST", dataWebScriptUrl);
    xhr.send(formData);
};
...

In script B, I'm using the Apache Chemistry OpenCMIS API to create a path in the CMIS-compatible Alfresco repository:

public class CustomFileUploader extends DeclarativeWebScript implements OpenCmisConfig {
    ...
    private void retrievePostRequestParams(WebScriptRequest req) {
        String groupName = null, year = null, number = null;

        FormData formData = (FormData) req.parseContent();
        FormData.FormField[] fields = formData.getFields();
        for(FormData.FormField field : fields) {
            String fieldName = field.getName();
            String fieldValue = field.getValue();

            if(fieldName.equalsIgnoreCase("group")) {
                if(fieldValue.equalsIgnoreCase("services")) {
                    groupName = "Услуги";
                ...                         
        }

        firstLevelFolderName = "/" + groupName;
        secondLevelFolderName = groupName + " " + year; 
        thirdLevelFolderName = number;
    }

    ...

    Folder firstLevelFolder = 
                createFolderIfNotExists(cmisSession, docLibFolder, firstLevelFolderName);

    ...

    private Folder createFolderIfNotExists(Session cmisSession, 
            Folder parentFolder, String folderName) {
        Folder subFolder = null;
        for(CmisObject child : parentFolder.getChildren()) {
            if(folderName.equalsIgnoreCase(child.getName())) {
                subFolder = (Folder) child;
            }
        }

        if(subFolder == null) {
            Map<String, Object> props = new HashMap<>();
            props.put("cmis:objectTypeId", "cmis:folder");
            props.put("cmis:name", folderName);

            subFolder = parentFolder.createFolder(props);
        }
        return subFolder; 
    }

    private Folder getDocLibFolder(Session cmisSession, String siteName) {
        String path = "/Sites/" + siteName + "/documentLibrary";
        return (Folder) cmisSession.getObjectByPath(path);
    }

    private Session getCmisSession() {
        SessionFactory factory = SessionFactoryImpl.newInstance();
        Map<String, String> conf = new HashMap<>();

        // http://localhost:8080/alfresco/api/-default-/public/cmis/versions/1.1/atom
        conf.put(SessionParameter.ATOMPUB_URL, ATOMPUB_URL);
        conf.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
        conf.put(SessionParameter.USER, USER_NAME);
        conf.put(SessionParameter.PASSWORD, PASSWORD);

        // "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl"
        conf.put(SessionParameter.OBJECT_FACTORY_CLASS, OBJECT_FACTORY_CLASS);
        conf.put(SessionParameter.REPOSITORY_ID, "-default-");

        Session session = factory.createSession(conf);

        return session;
    }

    ...

It's all works well... But I need to create the directory structure on a specific site, e.g. "contracts-site", here:

/site/contracts-site/documentlibrary

When I specifying the following:

/Sites/contracts-site/documentLibrary/Услуги
/Sites/contracts-site/Услуги
/site/contracts-site/documentlibrary/Услуги

I get the following exception (depending on the path):

org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException: Object not found: /Sites/contracts-site/Услуги

When I specifying the following:

"/Услуги"

Everything works, but the directory structure is created outside the site...

How to create a folder on the Alfresco site by using OpenCMIS API?


Solution

  • Just accidentally found the solution. Works perfectly if specify the following path:

    // locate the document library
    String path = "/Сайты/contracts-site/documentLibrary";
    

    Ie, "Сайты" instead of "Sites"... (Cyrillic alphabet)

    I'm using ru_RU locale and UTF-8 encoding. Then this example also works.