Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/vpro/jcr-criteria
- Owner: vpro
- License: apache-2.0
- Created: 2015-02-09T16:07:46.000Z (almost 10 years ago)
- Default Branch: main
- Last Pushed: 2024-08-14T09:03:41.000Z (6 months ago)
- Last Synced: 2024-08-14T10:26:35.769Z (6 months ago)
- Topics: jackrabbit, jcr, magnolia, magnolia-cms, magnolia-component
- Language: Java
- Homepage:
- Size: 733 KB
- Stars: 15
- Watchers: 15
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.adoc
- License: LICENSE
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.