Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/vpro/jcr-criteria

JCR queries using Java code
https://github.com/vpro/jcr-criteria

jackrabbit jcr magnolia magnolia-cms magnolia-component

Last synced: about 1 month ago
JSON representation

JCR queries using Java code

Awesome Lists containing this project

README

        

= jcr-criteria

image:https://img.shields.io/maven-central/v/nl.vpro/jcr-criteria.svg?label=Maven%20Central[Maven Central,link=https://search.maven.org/search?q=g:%22nl.vpro%22%20AND%20a:%22jcr-criteria%22]
image:http://www.javadoc.io/badge/nl.vpro/jcr-criteria.svg?color=blue[javadoc,link=http://www.javadoc.io/doc/nl.vpro/jcr-criteria]
image:https://github.com/vpro/jcr-criteria/workflows/build/badge.svg?[Build Status,link=https://github.com/vpro/jcr-criteria/actions?query=workflow%3Abuild]
image:https://img.shields.io/nexus/s/https/oss.sonatype.org/nl.vpro/jcr-criteria.svg[snapshots,link=https://oss.sonatype.org/content/repositories/snapshots/nl/vpro/jcr-criteria/]
image:https://codecov.io/gh/vpro/jcr-criteria/branch/main/graph/badge.svg[codecov,link=https://codecov.io/gh/vpro/jcr-criteria]

This is a way to create and execute JCR queries using Java code, using an interface which was inspired by the Criteria API as used by Hibernate/JPA. This code is based on the http://www.openmindlab.com/lab/products/mgnlcriteria.html[Criteria API for Magnolia CMS] (openutils-mgnlcriteria) module which was developed by Openmind.

In contrast to `openutils-mgnlcriteria` there is no dependency on any Magnolia CMS code in `jcr-criteria`, so this is a generic JCR Criteria API implementation. It can still be used with Magnolia CMS, but should work with other JCR-based projects as well.

== Usage

You can download the most recent jar from https://oss.sonatype.org/content/repositories/snapshots/nl/vpro/jcr-criteria/ or you can add this to your `pom.xml`:

[source,xml]
----

nl.vpro
jcr-criteria
2.11

----

Example:

[source,java]
----
import nl.vpro.jcr.criteria.query.AdvancedResult;
import nl.vpro.jcr.criteria.query.AdvancedResultItem;
import nl.vpro.jcr.criteria.query.Criteria;
import nl.vpro.jcr.criteria.query.JCRCriteriaFactory;
import nl.vpro.jcr.criteria.query.criterion.Criterion;
import nl.vpro.jcr.criteria.query.criterion.Restrictions;

..

Criteria criteria = JCRCriteriaFactory.createCriteria()
.setBasePath(basePath)
.setPaging(1, 1)
.addOrder(Order.ascending(field))
.add(Restrictions.eq(Criterion.JCR_PRIMARYTYPE, NodeTypes.Page.NAME))
.add(Restrictions.in("@" + NodeTypes.Renderable.TEMPLATE, templates))
.add(Restrictions.gt(field, begin));

AdvancedResult ar = criteria.execute(MgnlContext.getJCRSession(RepositoryConstants.WEBSITE));
log.debug("JCR query : " + criteria.toXpathExpression());
AdvancedResultItem item = ar.getFirstResult();

----

It can also be done, if you prefer, using the builder pattern

[source,java]
----

static import nl.vpro.jcr.criteria.query.criterion.Restrictions.*;

ExecutableQuery criteria = JCRCriteriaFactory.builder()
.basePath(basePath)
.order(Order.ascending(field))
.add(eq(Criterion.JCR_PRIMARYTYPE, NodeTypes.Page.NAME))
.add(in(attr(NodeTypes.Renderable.TEMPLATE), templates))
.add(gt(field, begin))
.build()
;
AdvancedResult result = criteria.execute(session);
----

== Java 11 and higher

This project is shipped (since 2.2) with a `module-info.java` which is though the only file compiled with java 11. The rest is compiled with java 8, so it is java 8 compatible, but it does participate in jigsaw if you happen to use java 11 already.