ClassificationServlet.java
19.8 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.ui.components.HtmlResponse
* com.adobe.granite.xss.XSSAPI
* com.day.cq.commons.jcr.JcrUtil
* com.day.cq.i18n.I18n
* javax.servlet.ServletException
* javax.servlet.http.HttpServletRequest
* javax.servlet.http.HttpServletResponse
* 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.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.commerce.impl.classification;
import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.classification.Classification;
import com.adobe.cq.commerce.api.classification.ClassificationAttribute;
import com.adobe.cq.commerce.api.classification.ClassificationCategory;
import com.adobe.cq.commerce.api.classification.ClassificationManager;
import com.adobe.granite.ui.components.HtmlResponse;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.i18n.I18n;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, label="Adobe CQ Commerce Product Classification Servlet", description="Manages Product Classifications")
@Service
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"sling/servlet/default"}), @Property(name="sling.servlet.methods", value={"GET", "POST"}), @Property(name="sling.servlet.selectors", value={"productclassification"})})
public class ClassificationServlet
extends SlingAllMethodsServlet {
private final Logger log = LoggerFactory.getLogger(ClassificationServlet.class);
private static final String REQUEST_PARAM_OPERATION = ":operation";
private static final String REQUEST_PARAM_PARENT_PATH = "parentPath";
private static final String REQUEST_PARAM_PATH = "path";
private static final String REQUEST_PARAM_TITLE = "title";
private static final String REQUEST_PARAM_DESC = "description";
private static final String REQUEST_PARAM_ATTRIBUTE_NAME = "attributeName";
private static final String REQUEST_PARAM_ATTRIBUTE_UNIT = "attributeUnit";
private static final String REQUEST_PARAM_BASE_PATH = "basePath";
private static final String REQUEST_PARAM_MATCH_TAGS = "matchTags";
private static final String REQUEST_PARAM_QUERY_TEXT = "search";
private static final String REQUEST_PARAM_QUERY_TYPE = "searchType";
private static final String CREATE_CLASSIFICATION_OPERATION = "createClassification";
private static final String UPDATE_CLASSIFICATION_OPERATION = "updateClassification";
private static final String REMOVE_CLASSIFICATION_OPERATION = "removeClassification";
private static final String CREATE_ROOT_CATEGORY_OPERATION = "createRootCategory";
private static final String CREATE_CATEGORY_OPERATION = "createCategory";
private static final String UPDATE_CATEGORY_OPERATION = "updateCategory";
private static final String REMOVE_CATEGORY_OPERATION = "removeCategory";
private static final String ADD_ATTRIBUTE_OPERATION = "addAttribute";
private static final String UPDATE_ATTRIBUTE_OPERATION = "updateAttribute";
private static final String REMOVE_ATTRIBUTE_OPERATION = "removeAttribute";
private static final String ASSIGN_PRODUCTS_OPERATION = "assignProducts";
@Reference
private XSSAPI xssApi;
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
XSSAPI xssAPI = this.xssApi.getRequestSpecificAPI(request);
I18n i18n = new I18n((HttpServletRequest)request);
Resource res = request.getResource();
String redirectUrl = request.getContextPath() + res.getPath();
String parentLocation = request.getContextPath() + res.getParent().getPath();
HtmlResponse htmlRes = new HtmlResponse(xssAPI, i18n, request.getLocale());
ResourceResolver resolver = request.getResourceResolver();
ClassificationManager classificationManager = (ClassificationManager)resolver.adaptTo(ClassificationManager.class);
String operation = request.getParameter(":operation");
String parentPath = request.getParameter("parentPath");
String path = request.getParameter("path");
String title = request.getParameter("title");
String description = request.getParameter("description");
String attributeName = request.getParameter("attributeName");
String attributeUnit = request.getParameter("attributeUnit");
String basePath = request.getParameter("basePath");
String[] matchTags = request.getParameterValues("matchTags");
String search = request.getParameter("search");
String searchType = request.getParameter("searchType");
HashMap<String, Object> properties = new HashMap<String, Object>();
if (StringUtils.isNotEmpty((String)title)) {
properties.put("jcr:title", title);
}
if (StringUtils.isNotEmpty((String)description)) {
properties.put("jcr:description", description);
}
if (StringUtils.isNotEmpty((String)attributeName)) {
properties.put("name", attributeName);
}
if (StringUtils.isNotEmpty((String)attributeUnit)) {
properties.put("unit", attributeUnit);
}
if (StringUtils.isNotEmpty((String)basePath)) {
properties.put("basePath", basePath);
}
if (matchTags != null && matchTags.length > 0) {
properties.put("matchTags", matchTags);
}
if (StringUtils.isNotEmpty((String)search)) {
properties.put("search", search);
}
if (StringUtils.isNotEmpty((String)searchType)) {
properties.put("searchType", searchType);
}
if (StringUtils.isEmpty((String)operation)) {
return;
}
if ("createClassification".equals(operation)) {
try {
String status;
String name = JcrUtil.createValidName((String)title);
Classification newClassification = classificationManager.createClassification(parentPath, name, properties);
String statusTitle = status = i18n.get("Created");
String statusDesc = i18n.get("<div id=\"Message\">Created {0}</div>", "response description", new Object[]{newClassification.getPath()});
this.setResponseObject(htmlRes, 201, status, statusTitle, statusDesc, parentPath, newClassification.getPath(), request.getContextPath() + newClassification.getPath());
htmlRes.onCreated(newClassification.getPath());
htmlRes.setCreateRequest(true);
}
catch (CommerceException e) {
String errorMsg = "Error while creating the new classification.";
this.log.error(errorMsg);
String status = errorMsg;
String statusTitle = errorMsg;
String statusDesc = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
}
} else if ("updateClassification".equals(operation)) {
try {
String status;
classificationManager.updateClassification(path, properties);
String statusTitle = status = i18n.get("Updated classification");
String statusDesc = i18n.get("<div id=\"Message\">Updated {0}</div>", "response description", new Object[]{path});
this.setResponseObject(htmlRes, 200, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
}
catch (Exception e) {
String errorMsg;
this.log.error("Could not update the classification {}.", (Object)path);
String status = errorMsg = "Error while updating the classification.";
String statusTitle = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
String statusDesc = errorMsg;
this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
}
} else if ("removeClassification".equals(operation)) {
try {
String status;
classificationManager.removeClassification(path);
String statusTitle = status = i18n.get("Removed classification");
String statusDesc = i18n.get("<div id=\"Message\">Removed {0}</div>", "response description", new Object[]{path});
this.setResponseObject(htmlRes, 200, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
}
catch (Exception e) {
String errorMsg;
this.log.error("Could not remove the classification {}", (Object)path);
String status = errorMsg = "Could not remove the classification.";
String statusTitle = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
String statusDesc = errorMsg;
this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
}
} else if ("createRootCategory".equals(operation)) {
try {
String status;
ClassificationCategory rootCategory = classificationManager.addRootCategory(path, properties);
String statusTitle = status = i18n.get("Created");
String statusDesc = i18n.get("<div id=\"Message\">Created {0}</div>", "response description", new Object[]{rootCategory.getPath()});
this.setResponseObject(htmlRes, 201, status, statusTitle, statusDesc, parentPath, rootCategory.getPath(), request.getContextPath() + rootCategory.getPath());
htmlRes.onCreated(rootCategory.getPath());
htmlRes.setCreateRequest(true);
}
catch (CommerceException e) {
String errorMsg = "Error while creating the root category.";
this.log.error(errorMsg);
String status = errorMsg;
String statusTitle = errorMsg;
String statusDesc = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
}
} else if ("createCategory".equals(operation)) {
try {
String status;
String name = JcrUtil.createValidName((String)title);
ClassificationCategory category = classificationManager.addCategory(parentPath, name, properties);
String statusTitle = status = i18n.get("Created");
String statusDesc = i18n.get("<div id=\"Message\">Created {0}</div>", "response description", new Object[]{category.getPath()});
this.setResponseObject(htmlRes, 201, status, statusTitle, statusDesc, parentPath, category.getPath(), request.getContextPath() + category.getPath());
htmlRes.onCreated(category.getPath());
htmlRes.setCreateRequest(true);
}
catch (CommerceException e) {
String errorMsg = "Error while creating the sub category.";
this.log.error(errorMsg);
String status = errorMsg;
String statusTitle = errorMsg;
String statusDesc = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
}
} else if ("updateCategory".equals(operation)) {
try {
String status;
classificationManager.updateCategory(path, properties);
String statusTitle = status = i18n.get("Updated category");
String statusDesc = i18n.get("<div id=\"Message\">Updated {0}</div>", "response description", new Object[]{path});
this.setResponseObject(htmlRes, 200, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
}
catch (Exception e) {
String errorMsg;
this.log.error("Could not update the category of the classification {}.", (Object)path);
String status = errorMsg = "Error while updating the category.";
String statusTitle = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
String statusDesc = errorMsg;
this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
}
} else if ("removeCategory".equals(operation)) {
try {
String status;
classificationManager.removeCategory(path);
String statusTitle = status = i18n.get("Removed category");
String statusDesc = i18n.get("<div id=\"Message\">Removed {0}</div>", "response description", new Object[]{path});
this.setResponseObject(htmlRes, 200, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
}
catch (Exception e) {
String errorMsg;
this.log.error("Could not remove the category {}", (Object)path);
String status = errorMsg = "Could not remove the category.";
String statusTitle = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
String statusDesc = errorMsg;
this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
}
} else if ("addAttribute".equals(operation)) {
try {
String status;
String name = JcrUtil.createValidName((String)title);
ClassificationAttribute attribute = classificationManager.addAttribute(parentPath, name, properties);
String statusTitle = status = i18n.get("Created");
String statusDesc = i18n.get("<div id=\"Message\">Created {0}</div>", "response description", new Object[]{attribute.getPath()});
this.setResponseObject(htmlRes, 201, status, statusTitle, statusDesc, parentPath, attribute.getPath(), request.getContextPath() + attribute.getPath());
htmlRes.onCreated(attribute.getPath());
htmlRes.setCreateRequest(true);
}
catch (CommerceException e) {
String errorMsg = "Error while creating the attribute.";
this.log.error(errorMsg);
String status = errorMsg;
String statusTitle = errorMsg;
String statusDesc = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
}
} else if ("updateAttribute".equals(operation)) {
try {
String status;
classificationManager.updateAttribute(path, properties);
String statusTitle = status = i18n.get("Updated attribute");
String statusDesc = i18n.get("<div id=\"Message\">Updated {0}</div>", "response description", new Object[]{path});
this.setResponseObject(htmlRes, 200, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
}
catch (Exception e) {
String errorMsg;
this.log.error("Could not update the attribute {} of the classification.", (Object)path);
String status = errorMsg = "Error while updating the category.";
String statusTitle = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
String statusDesc = errorMsg;
this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
}
} else if ("removeAttribute".equals(operation)) {
try {
String status;
classificationManager.removeAttribute(path);
String statusTitle = status = i18n.get("Removed attribute");
String statusDesc = i18n.get("<div id=\"Message\">Removed {0}</div>", "response description", new Object[]{path});
this.setResponseObject(htmlRes, 200, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
}
catch (Exception e) {
String errorMsg;
this.log.error("Could not remove the attribute {}", (Object)path);
String status = errorMsg = "Could not remove the attribute.";
String statusTitle = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
String statusDesc = errorMsg;
this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
}
} else if ("assignProducts".equals(operation)) {
try {
String status;
classificationManager.assignProducts(path, properties);
String statusTitle = status = i18n.get("Assigned products");
String statusDesc = i18n.get("<div id=\"Message\">Removed {0}</div>", "response description", new Object[]{path});
this.setResponseObject(htmlRes, 200, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
}
catch (Exception e) {
String errorMsg;
this.log.error("Could not assign products to the category at {}", (Object)path);
String status = errorMsg = "Could not assign products.";
String statusTitle = i18n.get("<div id=\"Message\">" + errorMsg + "</div>");
String statusDesc = errorMsg;
this.setResponseObject(htmlRes, 500, status, statusTitle, statusDesc, parentLocation, res.getPath(), redirectUrl);
}
}
htmlRes.send((HttpServletResponse)response, true);
}
private void setResponseObject(HtmlResponse response, int statusCode, String status, String title, String desc, String parentLocation, String path, String location) {
response.setStatus(statusCode, status);
response.setTitle(title);
response.setDescription(desc);
response.setParentLocation(parentLocation);
response.setLocation(location);
response.setPath(path);
}
protected void bindXssApi(XSSAPI xSSAPI) {
this.xssApi = xSSAPI;
}
protected void unbindXssApi(XSSAPI xSSAPI) {
if (this.xssApi == xSSAPI) {
this.xssApi = null;
}
}
}