在alfresco

时间:2016-03-16 14:31:39

标签: alfresco alfresco-webscripts

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

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

有关原因的任何提示?

谢谢,

1 个答案:

答案 0 :(得分: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();
}