0

我在 repo 层中有一个 Java 支持的 webscript,它在 Alfresco 的给定文件夹中创建文件(具有给定名称)。

为了处理文件名重复问题,我编写了以下代码:

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();

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();
    }

如果没有文件名重复(新名称),代码工作得很好。但是当有重复时它什么也不做,尽管它执行时没有任何错误!当我测试它时,它不会抛出任何异常,但也不会创建任何文件!

关于其原因的任何提示?

谢谢,

4

1 回答 1

0

我测试了这段代码,它工作正常

@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();
}
于 2016-03-16T21:29:11.680 回答