GQLConverter.java
8.57 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.jcr.JcrUtil
* com.day.cq.search.Predicate
* com.day.cq.search.PredicateConverter
* com.day.cq.search.PredicateGroup
* javax.jcr.RepositoryException
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.tenant.Tenant
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.commons.util;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.search.Predicate;
import com.day.cq.search.PredicateConverter;
import com.day.cq.search.PredicateGroup;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.tenant.Tenant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class GQLConverter {
private static final String OR_OP = "OR";
private static final String TAGS_PROP = "tags";
private static final String DATERANGE_PRED = "daterange";
private static final String RANGEPROP_PRED = "rangeproperty";
private static final String JCR_PROPERTIES_PATH = "/apps/dam/content/formitems";
private static final Logger log = LoggerFactory.getLogger(GQLConverter.class);
public static PredicateGroup buildQuery(Map conditions, ResourceResolver resolver) throws RepositoryException {
PredicateGroup rootGrp = PredicateConverter.createPredicates((Map)conditions);
List<String> gqlExpressions = GQLConverter.getValidGQLStatements(rootGrp);
if (gqlExpressions.size() > 0) {
for (String expression : gqlExpressions) {
PredicateGroup gqlPredicateGroup = PredicateConverter.createPredicatesFromGQL((String)expression.replace("*", "%"));
GQLConverter.transformPredicates(gqlPredicateGroup, GQLConverter.getJcrPropertyMapping(resolver));
rootGrp.addAll((Collection)gqlPredicateGroup);
}
}
return rootGrp;
}
public static void populateGqlfacets(Resource sourceNode, Resource destinationNode) {
if (sourceNode == null) {
log.error("source node null, skipping GQL facets extraction");
return;
}
try {
GQLConverter.findGqlFacets(sourceNode, destinationNode);
}
catch (PersistenceException e) {
log.error("Error while GQL facets extraction" + e.getMessage(), (Throwable)e);
}
}
private static List<String> getValidGQLStatements(PredicateGroup predicateGroup) {
ArrayList<String> gqlExpressions = new ArrayList<String>();
if (predicateGroup == null) {
return gqlExpressions;
}
for (int i = 0; i < predicateGroup.size(); ++i) {
String fulltext;
Predicate predicate;
if (predicateGroup.get(i) instanceof PredicateGroup) {
gqlExpressions.addAll(GQLConverter.getValidGQLStatements((PredicateGroup)predicateGroup.get(i)));
continue;
}
if (!(predicateGroup.get(i) instanceof Predicate) || !"fulltext".equals((predicate = predicateGroup.get(i)).getType()) || (fulltext = predicate.get("fulltext", "")) == null || !fulltext.contains(":") && !fulltext.contains("OR")) continue;
gqlExpressions.add(fulltext);
predicateGroup.remove(i);
}
return gqlExpressions;
}
private static Map<String, String> getJcrPropertyMapping(ResourceResolver resolver) throws RepositoryException {
HashMap<String, String> jcrPropertyMap = new HashMap<String, String>();
if (resolver == null) {
log.warn("Resource resolver is null, skipping property name conversion");
return jcrPropertyMap;
}
Resource formItemsNode = resolver.getResource(GQLConverter.getGqlFacetsDir(resolver));
Iterator itr = formItemsNode.listChildren();
while (itr.hasNext()) {
ValueMap prop = (ValueMap)((Resource)itr.next()).adaptTo(ValueMap.class);
if (prop == null || !prop.containsKey((Object)"fieldLabel") || !prop.containsKey((Object)"name")) continue;
jcrPropertyMap.put(prop.get((Object)"fieldLabel").toString().toLowerCase().trim().replace(" ", ""), prop.get((Object)"name").toString());
}
return jcrPropertyMap;
}
private static String getGqlFacetsDir(ResourceResolver resolver) {
String gqlFacetsDir = "/apps/dam/content/formitems";
Tenant tenant = (Tenant)resolver.adaptTo(Tenant.class);
if (tenant != null && tenant.getProperty("dam:gqlFacetsHome") != null) {
gqlFacetsDir = tenant.getProperty("dam:gqlFacetsHome").toString();
}
return gqlFacetsDir;
}
private static void transformPredicates(PredicateGroup predicateGroup, Map<String, String> jcrPropertyMap) {
if (predicateGroup == null) {
return;
}
for (int i = 0; i < predicateGroup.size(); ++i) {
if (predicateGroup.get(i) instanceof PredicateGroup) {
PredicateGroup optionalGrp = (PredicateGroup)predicateGroup.get(i);
GQLConverter.transformPredicates(optionalGrp, jcrPropertyMap);
predicateGroup.set(i, (Predicate)optionalGrp);
continue;
}
if (!(predicateGroup.get(i) instanceof Predicate)) continue;
Predicate predicate = predicateGroup.get(i);
GQLConverter.modifyPredicate(predicateGroup, predicate, i, jcrPropertyMap);
}
}
private static void modifyPredicate(PredicateGroup parentGroup, Predicate predicate, int index, Map<String, String> jcrPropertyMap) {
String property = predicate.get("property");
if ("property".equals(predicate.getType())) {
String value = predicate.get("value");
if (property.equals("tags")) {
property = jcrPropertyMap.containsKey(property.toLowerCase()) ? jcrPropertyMap.get(property.toLowerCase()) : property;
predicate = new Predicate("tagsearch");
predicate.set("property", property);
predicate.set("tagsearch", value);
} else {
property = jcrPropertyMap.containsKey(property.toLowerCase()) ? jcrPropertyMap.get(property.toLowerCase()) : property;
predicate.set("property", property);
predicate.set("value", value);
predicate.set("operation", "like");
}
} else if ("daterange".equals(predicate.getType()) || "rangeproperty".equals(predicate.getType())) {
property = jcrPropertyMap.containsKey(property) ? jcrPropertyMap.get(property) : property;
predicate.set("property", property);
predicate.set("lowerOperation", ">=");
predicate.set("upperOperation", "<=");
}
parentGroup.set(index, predicate);
}
private static void findGqlFacets(Resource sourceNode, Resource destinationNode) throws PersistenceException {
if (sourceNode == null) {
return;
}
Iterator itr = sourceNode.listChildren();
while (itr.hasNext()) {
ValueMap childProps;
Resource childNode = (Resource)itr.next();
ValueMap valueMap = childProps = childNode != null ? (ValueMap)childNode.adaptTo(ValueMap.class) : null;
if (childProps != null) {
String desNodeName;
String jcrProperty = (String)childProps.get("name", String.class);
String facetName = (String)childProps.get("fieldLabel", String.class);
if (jcrProperty != null && facetName != null && destinationNode.getChild(desNodeName = JcrUtil.createValidName((String)facetName)) == null) {
jcrProperty = jcrProperty.length() > 2 && jcrProperty.startsWith("./") ? jcrProperty.substring(2) : jcrProperty;
HashMap<String, String> props = new HashMap<String, String>();
props.put("fieldLabel", facetName);
props.put("name", jcrProperty);
destinationNode.getResourceResolver().create(destinationNode, desNodeName, props);
}
}
GQLConverter.findGqlFacets(childNode, destinationNode);
}
}
}