Java Code Examples for javax.jcr.Session#getNodeByIdentifier()

The following examples show how to use javax.jcr.Session#getNodeByIdentifier() . 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: CommentServiceImpl.java    From publick-sling-blog with Apache License 2.0 6 votes vote down vote up
/**
 * Delete comment by setting it's display property to false.
 *
 * @param request The current request to get session and Resource Resolver
 * @param id The comment UUID
 * @return true if the operation was successful
 */
public boolean deleteComment(final SlingHttpServletRequest request, final String id) {
    boolean result = false;

    try {
        Session session = request.getResourceResolver().adaptTo(Session.class); 
        Node node = session.getNodeByIdentifier(id);

        if (node != null) {
            JcrResourceUtil.setProperty(node, PublickConstants.COMMENT_PROPERTY_DISPLAY, false);
            session.save();
            result = true;
        }
    } catch (RepositoryException e) {
        LOGGER.error("Could not delete comment from JCR", e);
    }

    return result;
}
 
Example 2
Source File: CommentServiceImpl.java    From publick-sling-blog with Apache License 2.0 6 votes vote down vote up
/**
 * Edit comment and mark it as author edited.
 *
 * @param request The current request to get session and Resource Resolver
 * @param id The comment UUID
 * @param text The comment text
 * @return true if the operation was successful
 */
public boolean editComment(final SlingHttpServletRequest request, final String id, String text) {
    boolean result = false;

    try {
        Session session = request.getResourceResolver().adaptTo(Session.class);
        Node node = session.getNodeByIdentifier(id);

        if (node != null) {
            JcrResourceUtil.setProperty(node, PublickConstants.COMMENT_PROPERTY_COMMENT, text);
            JcrResourceUtil.setProperty(node, PublickConstants.COMMENT_PROPERTY_EDITED, true);
            session.save();
            result = true;
        }
    } catch (RepositoryException e) {
        LOGGER.error("Could not update comment from JCR", e);
    }

    return result;
}
 
Example 3
Source File: CommentServiceImpl.java    From publick-sling-blog with Apache License 2.0 6 votes vote down vote up
/**
 * Mark comment as spam, submit it to Akismet and delete it by setting
 * it's display property to false.
 *
 * @param request The current request to get session and Resource Resolver
 * @param id The comment UUID
 * @return true if the operation was successful
 */
public boolean markAsSpam(final SlingHttpServletRequest request, final String id) {
    boolean result = false;

    try {
        final ResourceResolver resolver = request.getResourceResolver();
        final Session session = resolver.adaptTo(Session.class);
        final Node node = session.getNodeByIdentifier(id);

        if (node != null) {
            final Resource resource = resolver.getResource(node.getPath());
            result = akismetService.submitSpam(resource);
        }
    } catch (RepositoryException e) {
        LOGGER.error("Could not submit spam.", e);
    }

    return result;
}
 
Example 4
Source File: CommentServiceImpl.java    From publick-sling-blog with Apache License 2.0 6 votes vote down vote up
/**
 * Mark comment as ham, submit it to Akismet and mark it valid it by setting
 * it's display property to true.
 *
 * @param request The current request to get session and Resource Resolver
 * @param id The comment UUID
 * @return true if the operation was successful
 */
public boolean markAsHam(final SlingHttpServletRequest request, final String id) {
    boolean result = false;

    try {
        final ResourceResolver resolver = request.getResourceResolver();
        final Session session = resolver.adaptTo(Session.class);
        final Node node = session.getNodeByIdentifier(id);

        if (node != null) {
            final Resource resource = resolver.getResource(node.getPath());
            result = akismetService.submitHam(resource);
        }
    } catch (RepositoryException e) {
        LOGGER.error("Could not submit ham.", e);
    }

    return result;
}
 
Example 5
Source File: JcrIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testJcrProducer() throws Exception {

    WildFlyCamelContext camelctx = new WildFlyCamelContext();
    camelctx.getNamingContext().bind("repository", repository);

    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            getContext().setUseBreadcrumb(false);
            from("direct:a").to("jcr://user:pass@repository/home/test");
        }
    });

    camelctx.start();
    try {
        String content = "<hello>world!</hello>";
        HashMap<String, Object> headers = new HashMap<>();
        headers.put(JcrConstants.JCR_NODE_NAME, "node");
        headers.put("my.contents.property", content);
        ProducerTemplate template = camelctx.createProducerTemplate();
        String result = template.requestBodyAndHeaders("direct:a", null, headers, String.class);
        Assert.assertNotNull(result);
        Session session = openSession();
        try {
            Node node = session.getNodeByIdentifier(result);
            Assert.assertEquals("/home/test/node", node.getPath());
            Assert.assertEquals(content, node.getProperty("my.contents.property").getString());
        } finally {
            session.logout();
        }
    } finally {
        camelctx.close();
    }
}