IBM SBT: Create a folder in a community

493 views Asked by At

I'm getting my hands on the SBT Toolkit with Java. Works great but having some difficulties with folders:

I need to create a folder in a community and put some files into it. Unfortunately, the class CommunityService has no such method.

I could use the FileService.createFolder(name, description, shareWith) method and share it with the community, but actually i only want the files in the community, because otherwise they're visible in the files application (public, warning message: "Sharing with the public community 'COMMUNITY NAME' will make this folder public.")

How can I achieve this?

I checked out the button in the community / files widget and found out that it is doing a POST to the communities feed:

Target URL: https://connections.host.ch/files/form/api/communitycollection/{community-uuid}/feed

POST content:

<entry xmlns="http://www.w3.org/2005/Atom">
  <category term="collection" label="collection" scheme="tag:ibm.com,2006:td/type"></category>
  <label xmlns="urn:ibm.com/td" makeUnique="true">TEST Folder</label>
  <title>TEST Folder</title>
  <summary type="text">teset set e</summary>
</entry>

So, could I use the communityService.createData method to invoke this REST service? If yes, whats the syntax? I haven't found any documentation or examples for it.

Also, I need to get the ID of the folder after it is created, but I this i can parse from the response..

Adding files to the newly created folder should be easy (SBT provides corresponding classes and methods), but i didn't work with this yet :-)

2

There are 2 answers

1
juiceterry007 On

I've found a solution .. altough i would rather use JSON, and maybe the handle of the Xerces XML Document is not really elegant since i have not experience with it.. but it works..

    public String createFolderInCommunity() {

    String folderId = "";

    try {

        // this is the atom entry.. would be nices if it was JSON..
        String entry = "<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:app=\"http://www.w3.org/2007/app\" xmlns:snx=\"http://www.ibm.com/xmlns/prod/sn\">"
                + "<category term=\"collection\" label=\"collection\" scheme=\"tag:ibm.com,2006:td/type\"></category>"
                + "<label xmlns=\"urn:ibm.com/td\" makeUnique=\"true\">TESTssss4444</label>"
                + "<title>Test: "
                + (new Date()).toString()
                + "</title>"
                + "<summary type=\"text\">teset set e</summary>"
                + "</entry>";

        // Request URI with the Community ID.. of course the community id
        // will be given as a parameter
        String requestUri = "/files/form/api/communitycollection/1802d0e8-f6b8-4d51-8db0-75997ed83489/feed";
        String payload = entry;

        // here would be the point of using APPLICATION_JSON, but did not
        // find any documentation about the JSON Object format :-(
        ClientService.ContentString cc = new ClientService.ContentString(
                payload, CommonConstants.APPLICATION_ATOM_XML);

        // create the service uppon the IBM Connections endpoint (in this
        // case SSO)
        Response response = getEndPoint().getClientService().post(
                requestUri, cc);

        // Getting the object as a apache xerces DeferredDocumentImpl, with
        // which i have absolutely no experience..
        DeferredDocumentImpl obj = (DeferredDocumentImpl) response
                .getData();

        NodeList lstNodes = obj.getFirstChild().getChildNodes();

        // so getting the value this way might be clumsy, but it works...
        for (int x = 0; x < lstNodes.getLength(); x++) {
            String name = lstNodes.item(x).getNodeName();
            if (name.equals("td:uuid")) {
                folderId = lstNodes.item(x).getFirstChild()
                        .getTextContent();
                break;
            }
        }
    } catch (Exception e) {
        Util.logError(e);
    }

    return folderId;
}
0
Nick On

Adding files to a community folder is quite simple, I was just working on this and I thought I'd share my solution with you.

I'm using the latest version of the core file: com.ibm.sbt.core-1.1.0.20140717-1200.jar

The class com.ibm.sbt.services.client.connections.files.FileService already has a method to add a file to either MYUSERLIBRARY or USERLIBRARY. The only thing you need to modify is the requestUri.

public void addFileToCommunityFolder(String communityId, String fileId, List<String> folderIds, Map<String, String> parameters) throws ClientServicesException {
    String accessType = AccessType.AUTHENTICATED.getText();
    // COMMUNITY_FILE_FEED : {files}/{authType}/{accessType}/communitylibrary/{communityId}/document/{fileId}/feed
    String requestUri = FileUrls.COMMUNITY_FILE_FEED.format(this, FileUrlParts.accessType.get(accessType), FileUrlParts.communityId.get(communityId), FileUrlParts.fileId.get(fileId));

    Map<String, String> headers = new HashMap<String, String>();
    headers.put(Headers.ContentType, Headers.ATOM);
    headers.put(Headers.ContentLanguage, Headers.UTF);

    parameters = (null == parameters) ? new HashMap<String, String>() : parameters;
    String payload = new EntityIdSerializer(folderIds,FileConstants.CATEGORY_COLLECTION).fileIdListPayload();
    Response response = createData(requestUri, parameters, headers, payload);
    checkResponseCode(response, HTTPCode.NO_CONTENT);
}

Please note that it's not possible to add a file to multiple community folders. This might be supported in the future

Edit:

Had a look at creating a folder in a community, you can easily create a folder in a community by modifying the public File createFolder(File folder) throws ClientServicesException method in com.ibm.sbt.services.client.connections.files.FileService

public File createCommunityFolder(String communityId, File folder) throws ClientServicesException {
    String accessType = AccessType.AUTHENTICATED.getText();

    // COMMUNITY_COLLECTIONS_FEED : {files}/{authType}/{accessType}/communitycollection/{communityId}/feed
    String requestUri = FileUrls.COMMUNITY_COLLECTIONS_FEED.format(this, FileUrlParts.accessType.get(accessType), FileUrlParts.communityId.get(communityId));
    String payload = new FileSerializer(folder).generateFileUpdatePayload();

    Response response = createData(requestUri, null, new ClientService.ContentString(payload, CommonConstants.APPLICATION_ATOM_XML));
    checkResponseCode(response, HTTPCode.CREATED);
    File r = getFileFeedHandler().createEntity(response);
    folder.clearFieldsMap();
    folder.setDataHandler(r.getDataHandler());
    return folder;
}

Make sure to set the category (=collection) in your File object, or it will return a One or more of the following required paramters are missing: itemId. error.