0

I have a Java-backed webscript in repo tier that creates files (with given name) in a given folder in Alfresco.

To handle the file name duplication issue I wrote this code:

NodeRef node = null;
        try {
            node = createNode(fullName, folderNodeRefId);
        } catch (DuplicateChildNodeNameException e) {
            System.out.println("Catched");
            boolean done = false;
            for (int i = 1; !done; i++) {
                String newName = filename + "_" + i + "." + fileFormat;
                System.out.println("Duplicate Name. Trying: " + newName);
                try {
                    node = createNode(newName, folderNodeRefId);
                    done = true;
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
        System.out.println("Done");
        ContentWriter writer = serviceRegistry.getContentService().getWriter(node, ContentModel.PROP_CONTENT, true);
        writer.setMimetype(getFileFormatMimetype(fileFormat));
        writer.putContent(inputStream);
        writer.guessEncoding();

and

private NodeRef createNode(String filename, String folderNodeRefId)
            throws InvalidNodeRefException, InvalidTypeException, InvalidQNameException {
        System.out.println("In " + filename);
        NodeRef folderNodeRef = new NodeRef(folderNodeRefId);
        Map<QName, Serializable> props = new HashMap<QName, Serializable>(1);
        props.put(ContentModel.PROP_NAME, filename);
        return serviceRegistry.getNodeService()
                .createNode(folderNodeRef, ContentModel.ASSOC_CONTAINS,
                        QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, filename), ContentModel.TYPE_CONTENT,
                        props)
                .getChildRef();
    }

The codes work very fine if there is no file name duplication (a new name). But it does nothing when there is a duplication, although it executes without any errors! When I test it it doesn't throw any exceptions but no file is created either!

Any hints about the cause of that?

Thanks,

skywards
  • 79
  • 1
  • 8
  • 1
    If it were me, I would try catching any exceptions from the nodeService call to see what I get. Work from there. Another option may be to check to see if the node already exists before creation (I see why this may not be ideal, but it is an option). – DocWatson Mar 16 '16 at 15:08
  • I tried that, it doesn't throw any other exception. – skywards Mar 17 '16 at 12:34
  • Are you injecting the public NodeService bean via Spring config or the private nodeService bean (capitalization difference)? – Jeff Potts Mar 21 '16 at 19:54
  • I am injecting the "ServiceRegistry" bean via Spring config and getting the NodeService from it. (serviceRegistry.getNodeService() ) – skywards Mar 22 '16 at 13:37

1 Answers1

0

I tested this code , It's working fine

@Test
public void createNode() {
    AuthenticationUtil.setFullyAuthenticatedUser(ADMIN_USER_NAME);
    NodeRef node = null;
    String fileFormat = "txt";
    String filename = "test";
    NodeRef folderNodeRef = getCompanyHome();
    //Create first node
    node = createNode(filename, folderNodeRef);
    try {
        node = createNode(filename, folderNodeRef);
    } catch (DuplicateChildNodeNameException e) {
        System.out.println("Catched");
        boolean done = false;
        for (int i = 1; !done; i++) {
            String newName = filename + "_" + i + "." + fileFormat;
            System.out.println("Duplicate Name. Trying: " + newName);
            try {
                node = createNode(newName, folderNodeRef);
                done = true;
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
    System.out.println("Done");
}

private NodeRef getCompanyHome()    {
    return nodeLocatorService.getNode("companyhome", null, null);
}

private NodeRef createNode(String filename, NodeRef folderNodeRef) throws InvalidNodeRefException, InvalidTypeException, InvalidQNameException {
    System.out.println("In " + filename);
    Map<QName, Serializable> props = new HashMap<QName, Serializable>(1);
    props.put(ContentModel.PROP_NAME, filename);
    return serviceRegistry.getNodeService().createNode(folderNodeRef, ContentModel.ASSOC_CONTAINS,
            QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, filename), ContentModel.TYPE_CONTENT,props).getChildRef();
}
  • The code doesn't cause any errors or exception. I tried your test, it passes but it doesn't create a file. – skywards Mar 17 '16 at 12:35