CMAssetViewHandler.java
10.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.icc.dbforms.obj.Letter
* com.adobe.icc.helper.util.CMAssetsQuery
* com.adobe.icc.services.api.LetterService
* com.day.cq.search.QueryBuilder
* com.day.cq.wcm.core.contentfinder.ViewHandler
* com.day.cq.wcm.core.contentfinder.ViewQuery
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang3.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.ReferenceCardinality
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.Service
* org.apache.jackrabbit.commons.query.GQL
* org.apache.jackrabbit.commons.query.GQL$ParserCallback
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.fd.adaddon.internal.servlet;
import com.adobe.fd.adaddon.internal.servlet.DocumentFragmentCFViewQuery;
import com.adobe.icc.dbforms.obj.Letter;
import com.adobe.icc.helper.util.CMAssetsQuery;
import com.adobe.icc.services.api.LetterService;
import com.day.cq.search.QueryBuilder;
import com.day.cq.wcm.core.contentfinder.ViewHandler;
import com.day.cq.wcm.core.contentfinder.ViewQuery;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang3.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.ReferenceCardinality;
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.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(label="CMAssetViewHandler", description="CMAssetViewHandler for listing CM Asset on Content finder search")
@Service
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"fd/ad/contentfinder/cmassets/view"})})
public class CMAssetViewHandler
extends ViewHandler {
private static Logger logger = LoggerFactory.getLogger(CMAssetViewHandler.class);
@Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.OPTIONAL_UNARY)
private LetterService letterService;
@Reference
private QueryBuilder queryBuilder;
private static final int DEFAULT_LIMIT = 50;
private static final String ASSET_TYPE = "assetType";
private static final String ASSET_TYPE_ALL = "ALL";
private static final String ASSET_TYPE_TEXT = "TEXT_MODULE";
private static final String ASSET_TYPE_LIST = "LIST_MODULE";
private static final String ASSET_TYPE_CONDITION = "CONDITIONAL_MODULE";
private static final String ASSET_TYPE_IMAGE = "DAM_IMAGE";
private static final Map<String, String[]> assetTypeMap = new HashMap<String, String[]>();
private static final String GUIDE_PATH_PARAM = "guidePath";
private static final String GUIDE_CONTAINER_PATH = "/jcr:content/guideContainer";
private static final String DD_REF = "ddRef";
private static final String START = "start";
private String[] getAssetTypes(SlingHttpServletRequest request) {
String[] assetTypes;
String assetType = request.getParameter("assetType");
if (StringUtils.isBlank((CharSequence)assetType)) {
assetType = "ALL";
}
if (assetType.indexOf(",") > -1) {
String[] parts = assetType.split(",");
ArrayList<Object> assetTypeList = new ArrayList<Object>();
for (String part : parts) {
if (assetTypeMap.get(part) == null) continue;
assetTypeList.addAll(Arrays.asList((Object[])assetTypeMap.get(part)));
}
assetTypes = assetTypeList.toArray(new String[assetTypeList.size()]);
} else {
assetTypes = assetTypeMap.get(assetType);
}
if (assetTypes == null) {
logger.error("Unknown asset type: " + assetType);
}
return assetTypes;
}
private Map<String, Object> getCMAssetsQueryParams(SlingHttpServletRequest request, QueryParser parsedQuery) {
String ddRef = this.getDDRef(request);
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("PARAM_ASSET_TYPES", this.getAssetTypes(request));
if (StringUtils.isNotBlank((CharSequence)parsedQuery.getName())) {
params.put("PARAM_NAME", parsedQuery.getName());
}
if (StringUtils.isNotBlank((CharSequence)parsedQuery.getFulltext())) {
params.put("PARAM_FULLTEXT", parsedQuery.getFulltext());
}
if (StringUtils.isNotBlank((CharSequence)ddRef)) {
params.put("PARAM_DD_NAME", ddRef);
}
return params;
}
protected ViewQuery createQuery(SlingHttpServletRequest request, Session session, String queryString) throws Exception {
QueryParser queryParser = new QueryParser(session);
queryParser.parse(queryString);
Map<String, Object> params = this.getCMAssetsQueryParams(request, queryParser);
CMAssetsQuery queryObj = new CMAssetsQuery(this.queryBuilder, session, params);
queryObj.setOffset(this.getStart(request));
queryObj.setHitsPerPage(this.getLimit(request));
return new DocumentFragmentCFViewQuery(request, session, queryObj);
}
private int getLimit(SlingHttpServletRequest request) {
String limitParamValue = request.getParameter("limit");
int limit = 50;
try {
if (StringUtils.isNotBlank((CharSequence)limitParamValue)) {
limit = Integer.parseInt(limitParamValue);
}
}
catch (NumberFormatException e) {
logger.warn("Exception while parsing limit param", (Throwable)e);
}
return limit;
}
private int getStart(SlingHttpServletRequest request) {
String startParamValue = request.getParameter("start");
int start = 0;
try {
if (StringUtils.isNotBlank((CharSequence)startParamValue)) {
start = Integer.parseInt(startParamValue);
}
}
catch (NumberFormatException e) {
logger.warn("Exception while parsing start param", (Throwable)e);
}
return start;
}
private String getDDRef(SlingHttpServletRequest request) {
Resource guideContainer;
ResourceResolver rr;
String guidePath = request.getParameter("guidePath");
String ddRef = null;
if (StringUtils.isNotBlank((CharSequence)guidePath) && (rr = request.getResourceResolver()) != null && (guideContainer = rr.getResource(guidePath + "/jcr:content/guideContainer")) != null) {
ValueMap map = (ValueMap)guideContainer.adaptTo(ValueMap.class);
ddRef = map.get((Object)"letterRef") != null ? this.getDDRefFromLetter((String)map.get("letterRef", (Object)"")) : (String)map.get("ddRef", (Object)null);
}
return ddRef;
}
private String getDDRefFromLetter(String letterRef) {
String ddRef = null;
if (StringUtils.isNotBlank((CharSequence)letterRef)) {
if (this.letterService != null) {
Letter letter = this.letterService.getLetter(letterRef, false);
if (letter != null) {
ddRef = letter.getDataDictionaryRef();
} else {
logger.error("getDDRefFromLetter: Could not read letter: " + letterRef);
}
} else {
logger.error("getDDRefFromLetter: Letter service is null ");
}
} else {
logger.error("getDDRefFromLetter: LetterRef is blank " + letterRef);
}
return ddRef;
}
static {
assetTypeMap.put("ALL", new String[]{"com.adobe.icc.dbforms.obj.TextModule", "com.adobe.icc.dbforms.obj.ListDataModule", "com.adobe.icc.dbforms.obj.ConditionalDataModule", "IMAGE"});
assetTypeMap.put("TEXT_MODULE", new String[]{"com.adobe.icc.dbforms.obj.TextModule"});
assetTypeMap.put("LIST_MODULE", new String[]{"com.adobe.icc.dbforms.obj.ListDataModule"});
assetTypeMap.put("CONDITIONAL_MODULE", new String[]{"com.adobe.icc.dbforms.obj.ConditionalDataModule"});
assetTypeMap.put("DAM_IMAGE", new String[]{"IMAGE"});
}
protected void bindLetterService(LetterService letterService) {
this.letterService = letterService;
}
protected void unbindLetterService(LetterService letterService) {
if (this.letterService == letterService) {
this.letterService = null;
}
}
protected void bindQueryBuilder(QueryBuilder queryBuilder) {
this.queryBuilder = queryBuilder;
}
protected void unbindQueryBuilder(QueryBuilder queryBuilder) {
if (this.queryBuilder == queryBuilder) {
this.queryBuilder = null;
}
}
static class QueryParser
implements GQL.ParserCallback {
Session session;
String fulltext;
String name;
public QueryParser(Session session) {
this.session = session;
}
public void parse(String query) throws RepositoryException {
GQL.parse((String)query, (Session)this.session, (GQL.ParserCallback)this);
}
public void term(String property, String value, boolean b) throws RepositoryException {
if ("name".equals(property)) {
this.name = value;
} else if (StringUtils.isEmpty((CharSequence)property)) {
this.fulltext = value;
}
}
public String getFulltext() {
return this.fulltext;
}
public String getName() {
return this.name;
}
}
}