ProductCollectionViewHandler.java
13.3 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.commerce.api.collection.ProductCollection
* com.adobe.granite.xss.XSSAPI
* com.day.cq.search.Predicate
* com.day.cq.search.PredicateConverter
* com.day.cq.search.PredicateGroup
* com.day.cq.search.Query
* com.day.cq.search.QueryBuilder
* com.day.cq.search.result.Hit
* com.day.cq.search.result.SearchResult
* com.day.cq.tagging.Tag
* com.day.cq.tagging.TagManager
* com.day.cq.wcm.core.contentfinder.Hit
* com.day.cq.wcm.core.contentfinder.ViewHandler
* com.day.cq.wcm.core.contentfinder.ViewQuery
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.query.Row
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.Service
* org.apache.jackrabbit.commons.query.GQL
* org.apache.jackrabbit.commons.query.GQL$Filter
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.request.RequestPathInfo
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.commerce.pim.impl.contentfinder;
import com.adobe.cq.commerce.api.collection.ProductCollection;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.search.Predicate;
import com.day.cq.search.PredicateConverter;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;
import com.day.cq.search.result.SearchResult;
import com.day.cq.tagging.Tag;
import com.day.cq.tagging.TagManager;
import com.day.cq.wcm.core.contentfinder.Hit;
import com.day.cq.wcm.core.contentfinder.ViewHandler;
import com.day.cq.wcm.core.contentfinder.ViewQuery;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.query.Row;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.commons.query.GQL;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
@Properties(value={@Property(name="service.description", value={"Handles the display of the product collections tab in the content finder"}), @Property(name="sling.servlet.paths", value={"/bin/wcm/contentfinder/productcollection/view"})})
public class ProductCollectionViewHandler
extends ViewHandler {
private static Logger log = LoggerFactory.getLogger(ProductCollectionViewHandler.class);
private TagManager tagManager;
@Reference
private XSSAPI xssAPI;
@Reference(policy=ReferencePolicy.STATIC)
private QueryBuilder queryBuilder;
private static final String DEFAULT_START_PATH = "/etc/commerce/collections";
public static final String PROPPERTY_STEP = "*/";
private static final String RESOURCE_TYPE = "sling:resourceType";
private static final String PRODUCT_COLLECTION_TYPE = "commerce/collection";
private static final String TAGS_PROP = "tags";
private static final String CQ_TAGS_PROP = "cq:tags";
private Set<String> predicateSet;
protected ViewQuery createQuery(SlingHttpServletRequest request, Session session, String queryString) throws RepositoryException {
String startPath;
if (queryString != null) {
queryString = ProductCollectionViewHandler.preserveWildcards(queryString);
}
PredicateGroup gqlPredicateGroup = PredicateConverter.createPredicatesFromGQL((String)queryString);
this.tagManager = (TagManager)request.getResourceResolver().adaptTo(TagManager.class);
this.predicateSet = this.customizePredicateGroup(gqlPredicateGroup);
RequestPathInfo pathInfo = request.getRequestPathInfo();
String string = startPath = pathInfo.getSuffix() != null && pathInfo.getSuffix().startsWith("/etc/commerce/collections") ? pathInfo.getSuffix() : "/etc/commerce/collections";
if (!this.predicateSet.contains("path")) {
gqlPredicateGroup.add(new Predicate("path").set("path", startPath));
}
if (gqlPredicateGroup.get("limit") == null) {
String limit = request.getParameter("limit");
if (limit != null && !limit.equals("")) {
int offset = Integer.parseInt(StringUtils.substringBefore((String)limit, (String)".."));
int total = Integer.parseInt(StringUtils.substringAfter((String)limit, (String)".."));
gqlPredicateGroup.set("limit", Long.toString(total - offset));
gqlPredicateGroup.set("offset", Long.toString(offset));
} else {
gqlPredicateGroup.set("limit", Long.toString(20));
}
}
ProductCollectionViewHandler.addProductCollectionConstraint(request, gqlPredicateGroup);
if (!this.predicateSet.contains("orderby")) {
gqlPredicateGroup.add(new Predicate("orderby").set("orderby", "@jcr:lastModified").set("sort", "desc"));
}
return new GQLViewQuery(request.getResourceResolver(), this.xssAPI, this.queryBuilder, gqlPredicateGroup, this.predicateSet);
}
private static Hit createHit(ProductCollection productCollection, String excerpt, XSSAPI xssAPI) throws RepositoryException {
Hit hit = new Hit();
String path = productCollection.getPath();
String name = path.substring(path.lastIndexOf("/") + 1);
String title = productCollection.getTitle();
if (title == null || title.length() == 0) {
title = name;
}
hit.set("name", (Object)xssAPI.encodeForHTML(name));
hit.set("path", (Object)path);
hit.set("thumbnail", (Object)xssAPI.getValidHref(path + ".folderthumbnail.jpg?width=240&height=240"));
hit.set("title", (Object)xssAPI.encodeForHTML(title));
hit.set("excerpt", (Object)xssAPI.filterHTML(excerpt));
return hit;
}
private Set<String> customizePredicateGroup(PredicateGroup predicateGroup) {
if (predicateGroup == null) {
return new HashSet<String>();
}
HashSet<String> predicateSet = new HashSet<String>();
for (int i = 0; i < predicateGroup.size(); ++i) {
if (predicateGroup.get(i) instanceof PredicateGroup) {
PredicateGroup optionalGrp = (PredicateGroup)predicateGroup.get(i);
predicateSet.addAll(this.customizePredicateGroup(optionalGrp));
predicateGroup.set(i, (Predicate)optionalGrp);
continue;
}
if (!(predicateGroup.get(i) instanceof Predicate)) continue;
Predicate predicate = predicateGroup.get(i);
predicateSet.add(predicate.getType());
this.modifyPredicate(predicateGroup, predicate, i);
}
return predicateSet;
}
private void modifyPredicate(PredicateGroup parentGroup, Predicate predicate, int index) {
String fulltext;
String property = predicate.get("property");
if ("property".equals(predicate.getType())) {
String value = predicate.get("value");
if (property.equals("tags") || property.equals("tag")) {
Tag tag;
predicate.set("property", "cq:tags");
parentGroup.set(index, predicate);
if (this.tagManager != null && (tag = this.tagManager.resolve(value)) == null) {
Predicate tagSearchPredicate = new Predicate("tagsearch");
tagSearchPredicate.set("property", "cq:tags");
tagSearchPredicate.set("tagsearch", value);
parentGroup.set(index, tagSearchPredicate);
}
} else {
int depth = StringUtils.countMatches((String)(property = ProductCollectionViewHandler.revertWildcards(property)), (String)"*/");
if (depth > 0) {
predicate.set("property", property.replace("*/", ""));
predicate.set("depth", String.valueOf(depth));
parentGroup.set(index, predicate);
}
}
} else if ("fulltext".equals(predicate.getType()) && (fulltext = predicate.get("fulltext", "")) != null) {
if (fulltext != null) {
fulltext = ProductCollectionViewHandler.revertWildcards(fulltext);
}
predicate.set("fulltext", fulltext);
Predicate tagSearchPredicate = new Predicate("tagsearch");
tagSearchPredicate.set("property", "cq:tags");
tagSearchPredicate.set("tagsearch", fulltext);
PredicateGroup optionalGrp = new PredicateGroup();
optionalGrp.setAllRequired(false);
optionalGrp.add(predicate);
optionalGrp.add(tagSearchPredicate);
parentGroup.set(index, (Predicate)optionalGrp);
}
}
private static String preserveWildcards(String queryString) {
if (queryString != null) {
queryString = queryString.replace("*", "%");
}
return queryString;
}
private static String revertWildcards(String queryString) {
if (queryString != null) {
queryString = queryString.replace("%", "*");
}
return queryString;
}
private static void addProductCollectionConstraint(SlingHttpServletRequest request, PredicateGroup gqlPredicateGroup) {
Predicate predicate = new Predicate("property");
predicate.set("property", "sling:resourceType");
predicate.set("value", "commerce/collection");
predicate.set("operation", "equals");
gqlPredicateGroup.add(predicate);
}
protected void bindXssAPI(XSSAPI xSSAPI) {
this.xssAPI = xSSAPI;
}
protected void unbindXssAPI(XSSAPI xSSAPI) {
if (this.xssAPI == xSSAPI) {
this.xssAPI = null;
}
}
protected void bindQueryBuilder(QueryBuilder queryBuilder) {
this.queryBuilder = queryBuilder;
}
protected void unbindQueryBuilder(QueryBuilder queryBuilder) {
if (this.queryBuilder == queryBuilder) {
this.queryBuilder = null;
}
}
private static class GQLViewQuery
implements ViewQuery {
Session session;
ResourceResolver resolver;
private final XSSAPI xssAPI;
private final QueryBuilder qb;
private final PredicateGroup predicateGroup;
private final Set<String> predicateSet;
public GQLViewQuery(ResourceResolver resolver, XSSAPI xssAPI, QueryBuilder qb, PredicateGroup predicateGroup, Set<String> predicateSet) {
this.session = (Session)resolver.adaptTo(Session.class);
this.resolver = resolver;
this.xssAPI = xssAPI;
this.predicateGroup = predicateGroup;
this.qb = qb;
this.predicateSet = predicateSet;
}
public Collection<Hit> execute() {
ArrayList<Hit> hits = new ArrayList<Hit>();
Query query = this.qb.createQuery(this.predicateGroup, this.session);
Iterator searchHits = query.getResult().getHits().iterator();
try {
while (searchHits.hasNext()) {
String excerpt;
com.day.cq.search.result.Hit searchHit = (com.day.cq.search.result.Hit)searchHits.next();
String path = searchHit.getPath();
ProductCollection productCollection = (ProductCollection)this.resolver.getResource(path).adaptTo(ProductCollection.class);
if (productCollection == null) continue;
try {
excerpt = searchHit.getExcerpt();
}
catch (Exception e) {
excerpt = "";
}
hits.add(ProductCollectionViewHandler.createHit(productCollection, excerpt, this.xssAPI));
}
}
catch (RepositoryException re) {
log.error("A repository error occurred", (Throwable)re);
}
return hits;
}
}
private static class ProductCollectionFilter
implements GQL.Filter {
ResourceResolver resolver;
ProductCollectionFilter(ResourceResolver resolver) {
this.resolver = resolver;
}
public boolean include(Row row) throws RepositoryException {
String path = row.getPath();
Resource resource = this.resolver.getResource(path);
if (resource == null) {
return false;
}
ProductCollection collection = (ProductCollection)resource.adaptTo(ProductCollection.class);
return collection != null;
}
}
}