Java Code Examples for org.apache.jackrabbit.commons.JcrUtils#getOrAddNode()

The following examples show how to use org.apache.jackrabbit.commons.JcrUtils#getOrAddNode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestAtomicCounter.java    From jackrabbit-filevault with Apache License 2.0 6 votes vote down vote up
/**
 * Tests if installing a package with a mix:atomicCounter works (update)
 */
@Test
public void updateAtomicCounter() throws RepositoryException, IOException, PackageException {
    Assume.assumeTrue(isOak());

    Node tmp = JcrUtils.getOrAddNode(admin.getRootNode(), "tmp", NodeType.NT_UNSTRUCTURED);
    Node testroot = JcrUtils.getOrAddNode(tmp, "testroot", NodeType.NT_UNSTRUCTURED);
    testroot.addMixin("mix:atomicCounter");
    testroot.setProperty("oak:increment", 5);
    admin.save();
    assertEquals(5L, testroot.getProperty("oak:counter").getLong());

    JcrPackage pack = packMgr.upload(getStream("/test-packages/atomic-counter-test.zip"), false);
    assertNotNull(pack);
    ImportOptions opts = getDefaultOptions();
    pack.install(opts);

    assertProperty("/tmp/testroot/oak:counter", "42");
}
 
Example 2
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 6 votes vote down vote up
private Node getOrAddNodeByPath(Node baseNode, String name, String nodeType, boolean primaryType)
        throws RepositoryException {
    log.debug("getOrAddNodeByPath " + baseNode + " " + name + " " + nodeType);
    Node node = baseNode;
    for (String n : name.split("/")) {
        if (nodeType != null && primaryType) {
            node = JcrUtils.getOrAddNode(node, n, nodeType);
        } else {
            node = JcrUtils.getOrAddNode(node, n);
            if (nodeType != null && !node.isNodeType(nodeType)) {
                node.addMixin(nodeType);
            }
        }
        if (!node.hasProperty("id")) {
            node.setProperty("id", n);
        }
    }
    return node;
}
 
Example 3
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 6 votes vote down vote up
private Node getOrAddNodeByPath(Node baseNode, String name, String primaryType, String... mixinTypes)
        throws RepositoryException {
    log.debug("getOrAddNodeByPath baseNode={}, name={}, primary={}, mixin={}", baseNode, name, primaryType, mixinTypes);
    Node node = baseNode;
    for (String n : name.split("/")) {
        node = JcrUtils.getOrAddNode(node, n, primaryType);
        for (String mixin : mixinTypes) {
            if (mixin != null && !node.isNodeType(mixin)) {
                node.addMixin(mixin);
            }

        }
        if (!node.hasProperty("id")) {
            node.setProperty("id", n);
        }
    }
    return node;
}
 
Example 4
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 6 votes vote down vote up
private Node findArtifactNode(Session jcrSession, String namespace, String projectId,
                              String projectVersion, String id) throws RepositoryException {

    if (namespace==null || projectId==null||projectVersion==null||id==null) {
        return null;
    }
    Node root = jcrSession.getRootNode();
    Node node = JcrUtils.getOrAddNode(root, "repositories");
    for (Node n : JcrUtils.getChildNodes(node)) {
        String repositoryId = n.getName();
        Node repo = getOrAddRepositoryContentNode(jcrSession, repositoryId);
        Node nsNode = JcrUtils.getNodeIfExists(repo, StringUtils.replaceChars(namespace, '.', '/'));
        if (nsNode!=null) {
            Node projNode = JcrUtils.getNodeIfExists(nsNode, projectId);
            if (projNode !=null ) {
                Node projVersionNode = JcrUtils.getNodeIfExists(projNode, projectVersion);
                if (projVersionNode != null) {
                    return JcrUtils.getNodeIfExists(projVersionNode, id);
                }
            }
        }
    }

    return null;
}
 
Example 5
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
@Override
public void addMetadataFacet(RepositorySession session, String repositoryId, MetadataFacet metadataFacet)
        throws MetadataRepositoryException {
    final Session jcrSession = getSession(session);
    try {
        Node repo = getOrAddRepositoryNode(jcrSession, repositoryId);
        Node facets = JcrUtils.getOrAddNode(repo, "facets", FACETS_FOLDER_TYPE);

        String id = metadataFacet.getFacetId();
        Node facetNode = JcrUtils.getOrAddNode(facets, id, FACET_ID_CONTAINER_TYPE);
        if (!facetNode.hasProperty("id")) {
            facetNode.setProperty("id", id);
        }

        Node facetInstance = getOrAddNodeByPath(facetNode, metadataFacet.getName(), FACET_NODE_TYPE, true);
        if (!facetInstance.hasProperty("archiva:facetId")) {
            facetInstance.setProperty("archiva:facetId", id);
            facetInstance.setProperty("archiva:name", metadataFacet.getName());
        }

        for (Map.Entry<String, String> entry : metadataFacet.toProperties().entrySet()) {
            facetInstance.setProperty(entry.getKey(), entry.getValue());
        }
        session.save();
    } catch (RepositoryException | MetadataSessionException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
}
 
Example 6
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
private Node getOrAddRepositoryNode(Session jcrSession, String repositoryId)
        throws RepositoryException {
    log.debug("getOrAddRepositoryNode " + repositoryId);
    Node root = jcrSession.getRootNode();
    Node node = JcrUtils.getOrAddNode(root, "repositories");
    log.debug("Repositories " + node);
    node = JcrUtils.getOrAddNode(node, repositoryId, REPOSITORY_NODE_TYPE);
    if (!node.hasProperty("id")) {
        node.setProperty("id", repositoryId);
    }
    return node;
}
 
Example 7
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
private Node getOrAddProjectNode(Session jcrSession, String repositoryId, String namespace, String projectId)
        throws RepositoryException {
    Node namespaceNode = getOrAddNamespaceNode(jcrSession, repositoryId, namespace);
    Node node = JcrUtils.getOrAddNode(namespaceNode, projectId, FOLDER_TYPE);
    if (!node.isNodeType(PROJECT_MIXIN_TYPE)) {
        node.addMixin(PROJECT_MIXIN_TYPE);
    }
    if (!node.hasProperty("id")) {
        node.setProperty("id", projectId);
    }
    return node;
}
 
Example 8
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
private Node getOrAddProjectVersionNode(Session jcrSession, String repositoryId, String namespace, String projectId,
                                        String projectVersion)
        throws RepositoryException {
    Node projectNode = getOrAddProjectNode(jcrSession, repositoryId, namespace, projectId);
    log.debug("Project node {}", projectNode);
    Node projectVersionNode = JcrUtils.getOrAddNode(projectNode, projectVersion, PROJECT_VERSION_NODE_TYPE);
    if (!projectVersionNode.hasProperty("id")) {
        projectVersionNode.setProperty("id", projectVersion);
    }

    log.debug("Project version node {}", projectVersionNode);
    return projectVersionNode;
}
 
Example 9
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
private Node getOrAddArtifactNode(Session jcrSession, String repositoryId, String namespace, String projectId, String projectVersion,
                                  String id)
        throws RepositoryException {
    Node versionNode = getOrAddProjectVersionNode(jcrSession, repositoryId, namespace, projectId, projectVersion);
    Node node = JcrUtils.getOrAddNode(versionNode, id, ARTIFACT_NODE_TYPE);
    if (!node.hasProperty("id")) {
        node.setProperty("id", id);
    }
    return node;
}
 
Example 10
Source File: JcrRepositoryStatisticsGatheringTest.java    From archiva with Apache License 2.0 5 votes vote down vote up
private void loadContentIntoRepo( RepositorySession repoSession, String repoId )
    throws RepositoryException, IOException, MetadataRepositoryException
{
        jcrSession = ((JcrRepositorySession) repoSession).getJcrSession();
    Node n = JcrUtils.getOrAddNode( jcrSession.getRootNode( ), "repositories" );
    n = JcrUtils.getOrAddNode( n, repoId );
    n = JcrUtils.getOrAddNode( n, "content" );
    n = JcrUtils.getOrAddNode( n, "org" );
    n = JcrUtils.getOrAddNode( n, "apache" );

    InputStream inputStream = getClass( ).getResourceAsStream( "/artifacts.xml" );
    jcrSession.importXML( n.getPath( ), inputStream, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW );
    jcrSession.save( );
}
 
Example 11
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 4 votes vote down vote up
private Node getOrAddRepositoryContentNode(Session jcrSession, String repositoryId)
        throws RepositoryException {
    Node node = getOrAddRepositoryNode(jcrSession, repositoryId);
    return JcrUtils.getOrAddNode(node, "content", CONTENT_NODE_TYPE);
}