AssetQueryTransformer.java
10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.rest.RestException
* com.adobe.granite.rest.query.QueryTransformer
* com.adobe.granite.rest.utils.ModifiableMappedValueMapDecorator
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.ValueFactory
* javax.jcr.Workspace
* javax.jcr.query.InvalidQueryException
* javax.jcr.query.Query
* javax.jcr.query.QueryManager
* javax.jcr.query.qom.And
* javax.jcr.query.qom.Column
* javax.jcr.query.qom.Comparison
* javax.jcr.query.qom.Constraint
* javax.jcr.query.qom.DescendantNode
* javax.jcr.query.qom.DynamicOperand
* javax.jcr.query.qom.FullTextSearch
* javax.jcr.query.qom.FullTextSearchScore
* javax.jcr.query.qom.Length
* javax.jcr.query.qom.LowerCase
* javax.jcr.query.qom.Not
* javax.jcr.query.qom.Or
* javax.jcr.query.qom.Ordering
* javax.jcr.query.qom.PropertyExistence
* javax.jcr.query.qom.PropertyValue
* javax.jcr.query.qom.QueryObjectModel
* javax.jcr.query.qom.QueryObjectModelFactory
* javax.jcr.query.qom.Selector
* javax.jcr.query.qom.Source
* javax.jcr.query.qom.StaticOperand
* javax.jcr.query.qom.UpperCase
* org.apache.jackrabbit.commons.query.sql2.Parser
* org.apache.jackrabbit.value.ValueFactoryImpl
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
*/
package com.adobe.granite.rest.assets.impl;
import com.adobe.granite.rest.RestException;
import com.adobe.granite.rest.assets.impl.AbstractAssetResource;
import com.adobe.granite.rest.assets.impl.AssetValueMapDecorator;
import com.adobe.granite.rest.query.QueryTransformer;
import com.adobe.granite.rest.utils.ModifiableMappedValueMapDecorator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFactory;
import javax.jcr.Workspace;
import javax.jcr.query.InvalidQueryException;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.qom.And;
import javax.jcr.query.qom.Column;
import javax.jcr.query.qom.Comparison;
import javax.jcr.query.qom.Constraint;
import javax.jcr.query.qom.DescendantNode;
import javax.jcr.query.qom.DynamicOperand;
import javax.jcr.query.qom.FullTextSearch;
import javax.jcr.query.qom.FullTextSearchScore;
import javax.jcr.query.qom.Length;
import javax.jcr.query.qom.LowerCase;
import javax.jcr.query.qom.Not;
import javax.jcr.query.qom.Or;
import javax.jcr.query.qom.Ordering;
import javax.jcr.query.qom.PropertyExistence;
import javax.jcr.query.qom.PropertyValue;
import javax.jcr.query.qom.QueryObjectModel;
import javax.jcr.query.qom.QueryObjectModelFactory;
import javax.jcr.query.qom.Selector;
import javax.jcr.query.qom.Source;
import javax.jcr.query.qom.StaticOperand;
import javax.jcr.query.qom.UpperCase;
import org.apache.jackrabbit.commons.query.sql2.Parser;
import org.apache.jackrabbit.value.ValueFactoryImpl;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
public class AssetQueryTransformer
implements QueryTransformer {
private static final String NN_METADATA = "jcr:content/metadata";
private Ordering[] rewriteOrderings(Ordering[] origOrderings, String selectorName, QueryObjectModelFactory qomFactory, Resource resource) throws InvalidQueryException, RepositoryException {
Ordering[] orderings = new Ordering[origOrderings.length];
for (int i = 0; i < origOrderings.length; ++i) {
orderings[i] = "jcr.order.ascending".equals(origOrderings[i].getOrder()) ? qomFactory.ascending(this.rewriteOperand(origOrderings[i].getOperand(), selectorName, qomFactory, resource)) : qomFactory.descending(this.rewriteOperand(origOrderings[i].getOperand(), selectorName, qomFactory, resource));
}
return orderings;
}
private Constraint rewriteConstraint(Constraint origConstraint, String selectorName, QueryObjectModelFactory qomFactory, Resource resource) throws InvalidQueryException, RepositoryException {
Constraint constraint;
if (origConstraint instanceof Comparison) {
Comparison c = (Comparison)origConstraint;
DynamicOperand op1 = this.rewriteOperand(c.getOperand1(), selectorName, qomFactory, resource);
constraint = qomFactory.comparison(op1, c.getOperator(), c.getOperand2());
} else if (origConstraint instanceof PropertyExistence) {
PropertyExistence exists = (PropertyExistence)origConstraint;
String pn = exists.getPropertyName();
ValueMap vm = (ValueMap)resource.adaptTo(ValueMap.class);
if (vm != null && vm instanceof AssetValueMapDecorator) {
pn = ((AssetValueMapDecorator)vm).unmapProperty(pn);
}
constraint = qomFactory.propertyExistence(selectorName, this.getPropertyName(pn));
} else if (origConstraint instanceof And) {
And and = (And)origConstraint;
constraint = qomFactory.and(this.rewriteConstraint(and.getConstraint1(), selectorName, qomFactory, resource), this.rewriteConstraint(and.getConstraint2(), selectorName, qomFactory, resource));
} else if (origConstraint instanceof Or) {
Or or = (Or)origConstraint;
constraint = qomFactory.or(this.rewriteConstraint(or.getConstraint1(), selectorName, qomFactory, resource), this.rewriteConstraint(or.getConstraint2(), selectorName, qomFactory, resource));
} else if (origConstraint instanceof Not) {
Not not = (Not)origConstraint;
constraint = qomFactory.not(this.rewriteConstraint(not.getConstraint(), selectorName, qomFactory, resource));
} else if (origConstraint instanceof FullTextSearch) {
FullTextSearch fts = (FullTextSearch)origConstraint;
String pn = fts.getPropertyName();
ValueMap vm = (ValueMap)resource.adaptTo(ValueMap.class);
if (vm != null && vm instanceof AssetValueMapDecorator) {
pn = ((AssetValueMapDecorator)vm).unmapProperty(pn);
}
constraint = qomFactory.fullTextSearch(selectorName, this.getPropertyName(pn), fts.getFullTextSearchExpression());
} else {
constraint = origConstraint;
}
return constraint;
}
private DynamicOperand rewriteOperand(DynamicOperand op1, String selectorName, QueryObjectModelFactory qomFactory, Resource resource) throws InvalidQueryException, RepositoryException {
if (op1 instanceof PropertyValue) {
PropertyValue pv = (PropertyValue)op1;
String pn = pv.getPropertyName();
ValueMap vm = (ValueMap)resource.adaptTo(ValueMap.class);
if (vm != null && vm instanceof ModifiableMappedValueMapDecorator) {
pn = ((AssetValueMapDecorator)vm).unmapProperty(pn);
}
op1 = qomFactory.propertyValue(selectorName, this.getPropertyName(pn));
} else if (op1 instanceof LowerCase) {
op1 = qomFactory.lowerCase(this.rewriteOperand(((LowerCase)op1).getOperand(), selectorName, qomFactory, resource));
} else if (op1 instanceof UpperCase) {
op1 = qomFactory.upperCase(this.rewriteOperand(((UpperCase)op1).getOperand(), selectorName, qomFactory, resource));
} else if (op1 instanceof Length) {
PropertyValue pv = ((Length)op1).getPropertyValue();
op1 = qomFactory.length((PropertyValue)this.rewriteOperand((DynamicOperand)pv, selectorName, qomFactory, resource));
} else if (op1 instanceof FullTextSearchScore) {
op1 = qomFactory.fullTextSearchScore(selectorName);
}
return op1;
}
private String getSelectorName(QueryObjectModel qom) {
return ((Selector)qom.getSource()).getSelectorName();
}
private QueryManager getQueryManager(Resource resource) throws RepositoryException {
ResourceResolver resolver = resource.getResourceResolver();
Session session = (Session)resolver.adaptTo(Session.class);
QueryManager queryManager = session.getWorkspace().getQueryManager();
return queryManager;
}
private QueryObjectModel getQueryObjectModel(QueryManager queryManager, String stmt) throws InvalidQueryException, RepositoryException {
Query query = queryManager.createQuery(stmt, "JCR-SQL2");
QueryObjectModelFactory qomFactory = queryManager.getQOMFactory();
if (query instanceof QueryObjectModel) {
return (QueryObjectModel)query;
}
Parser parser = new Parser(qomFactory, ValueFactoryImpl.getInstance());
return parser.createQueryObjectModel(stmt);
}
public String getSelectorName(Resource resource, String query) throws RestException {
try {
QueryManager queryMgr = this.getQueryManager(resource);
QueryObjectModel qom = this.getQueryObjectModel(queryMgr, query);
return this.getSelectorName(qom);
}
catch (RepositoryException e) {
throw new RestException(e.getMessage(), (Throwable)e);
}
}
private String getPropertyName(String propertyName) {
return "jcr:content/metadata/" + propertyName;
}
public String transform(Resource resource, String stmt) throws RestException {
try {
QueryManager queryManager = this.getQueryManager(resource);
QueryObjectModelFactory qomFactory = queryManager.getQOMFactory();
QueryObjectModel qom = this.getQueryObjectModel(queryManager, stmt);
String selectorName = this.getSelectorName(qom);
Selector asset = qomFactory.selector("dam:Asset", selectorName);
Constraint constraint = this.rewriteConstraint(qom.getConstraint(), selectorName, qomFactory, resource);
String path = resource instanceof AbstractAssetResource ? ((AbstractAssetResource)resource).getWrappedResourcePath() : resource.getPath();
DescendantNode descendant = qomFactory.descendantNode(selectorName, path);
constraint = constraint != null ? qomFactory.and(constraint, (Constraint)descendant) : descendant;
Ordering[] orderings = qom.getOrderings();
if (orderings != null) {
orderings = this.rewriteOrderings(orderings, selectorName, qomFactory, resource);
}
QueryObjectModel query = qomFactory.createQuery((Source)asset, constraint, orderings, qom.getColumns());
return query.getStatement();
}
catch (RepositoryException e) {
throw new RestException(e.getMessage(), (Throwable)e);
}
}
}