1 package org
.argeo
.cms
.jcr
.acr
.search
;
3 import java
.util
.ArrayList
;
6 import javax
.jcr
.RepositoryException
;
7 import javax
.jcr
.Session
;
8 import javax
.jcr
.query
.Query
;
9 import javax
.jcr
.query
.QueryManager
;
10 import javax
.jcr
.query
.qom
.DynamicOperand
;
11 import javax
.jcr
.query
.qom
.QueryObjectModelConstants
;
12 import javax
.jcr
.query
.qom
.QueryObjectModelFactory
;
13 import javax
.jcr
.query
.qom
.Selector
;
14 import javax
.jcr
.query
.qom
.StaticOperand
;
15 import javax
.xml
.namespace
.QName
;
17 import org
.argeo
.api
.acr
.NamespaceUtils
;
18 import org
.argeo
.api
.acr
.search
.BasicSearch
;
19 import org
.argeo
.api
.acr
.search
.Constraint
;
20 import org
.argeo
.api
.acr
.search
.ContentFilter
;
21 import org
.argeo
.api
.acr
.search
.ContentFilter
.Eq
;
22 import org
.argeo
.api
.acr
.search
.ContentFilter
.IsContentClass
;
23 import org
.argeo
.api
.acr
.search
.ContentFilter
.Not
;
25 public class JcrBasicSearch
{
26 private Session session
;
27 private QueryManager queryManager
;
28 private BasicSearch basicSearch
;
29 QueryObjectModelFactory factory
;
31 QName contentClass
= null;
33 String selectorName
= "content";
35 public JcrBasicSearch(Session session
, BasicSearch basicSearch
, String relPath
) throws RepositoryException
{
36 this.queryManager
= session
.getWorkspace().getQueryManager();
37 this.basicSearch
= basicSearch
;
38 factory
= queryManager
.getQOMFactory();
41 public Query
createQuery() throws RepositoryException
{
42 ContentFilter
<?
> where
= basicSearch
.getWhere();
43 // scan for content classes
44 // TODO deal with complex cases of multiple types
46 javax
.jcr
.query
.qom
.Constraint qomConstraint
= toQomConstraint(where
);
47 if (contentClass
== null)
48 throw new IllegalArgumentException("No content class specified");
50 Selector source
= factory
.selector(NamespaceUtils
.toPrefixedName(contentClass
), selectorName
);
52 return factory
.createQuery(source
, qomConstraint
, null, null);
55 private javax
.jcr
.query
.qom
.Constraint
toQomConstraint(Constraint constraint
) throws RepositoryException
{
56 javax
.jcr
.query
.qom
.Constraint qomConstraint
;
57 if (constraint
instanceof ContentFilter
<?
> where
) {
58 List
<Constraint
> constraints
= new ArrayList
<>();
59 for (Constraint c
: where
.getConstraints()) {
60 if (c
instanceof IsContentClass icc
) {
61 if (icc
.getContentClasses().length
> 1 || contentClass
!= null)
62 throw new IllegalArgumentException("Multiple content class is not supported");
63 contentClass
= icc
.getContentClasses()[0];
69 if (constraints
.isEmpty()) {
71 } else if (constraints
.size() == 1) {
72 qomConstraint
= toQomConstraint(constraints
.get(0));
74 javax
.jcr
.query
.qom
.Constraint currQomConstraint
= toQomConstraint(constraints
.get(0));
75 for (int i
= 1; i
< constraints
.size(); i
++) {
76 Constraint c
= constraints
.get(i
);
77 if (where
.isUnion()) {
78 currQomConstraint
= factory
.or(currQomConstraint
, toQomConstraint(c
));
80 currQomConstraint
= factory
.and(currQomConstraint
, toQomConstraint(c
));
83 qomConstraint
= currQomConstraint
;
86 } else if (constraint
instanceof Eq comp
) {
87 DynamicOperand dynamicOperand
= factory
.propertyValue(selectorName
,
88 NamespaceUtils
.toPrefixedName(comp
.getProp()));
89 // TODO better convert attribute value
90 StaticOperand staticOperand
= factory
91 .literal(session
.getValueFactory().createValue(comp
.getValue().toString()));
92 qomConstraint
= factory
.comparison(dynamicOperand
, QueryObjectModelConstants
.JCR_OPERATOR_EQUAL_TO
,
94 } else if (constraint
instanceof Not not
) {
95 qomConstraint
= factory
.not(toQomConstraint(not
.getNegated()));
97 throw new IllegalArgumentException("Constraint " + constraint
.getClass() + " is not supported");