SmartListManagerImpl.java
18.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.social.srp.SocialResourceProvider
* com.adobe.cq.social.srp.config.SocialResourceConfiguration
* com.adobe.cq.social.ugc.api.Constraint
* com.adobe.cq.social.ugc.api.ConstraintGroup
* com.adobe.cq.social.ugc.api.PathConstraint
* com.adobe.cq.social.ugc.api.PathConstraintType
* com.adobe.cq.social.ugc.api.SearchResults
* com.adobe.cq.social.ugc.api.UgcFilter
* com.adobe.cq.social.ugc.api.UgcSearch
* com.adobe.cq.social.ugc.api.UgcSort
* com.adobe.cq.social.ugc.api.UgcSort$Direction
* com.adobe.cq.social.ugc.api.ValueConstraint
* com.adobe.cq.social.ugcbase.SocialUtils
* com.adobe.granite.security.user.UserProperties
* com.day.cq.commons.jcr.JcrUtil
* javax.jcr.RepositoryException
* org.apache.commons.lang.RandomStringUtils
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.resource.ModifiableValueMap
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.resource.collection.ResourceCollection
* org.apache.sling.resource.collection.ResourceCollectionManager
* org.osgi.service.event.Event
* org.osgi.service.event.EventAdmin
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.commerce.impl.smartlist;
import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.smartlist.SmartList;
import com.adobe.cq.commerce.api.smartlist.SmartListManager;
import com.adobe.cq.commerce.impl.smartlist.SmartListImpl;
import com.adobe.cq.social.srp.SocialResourceProvider;
import com.adobe.cq.social.srp.config.SocialResourceConfiguration;
import com.adobe.cq.social.ugc.api.Constraint;
import com.adobe.cq.social.ugc.api.ConstraintGroup;
import com.adobe.cq.social.ugc.api.PathConstraint;
import com.adobe.cq.social.ugc.api.PathConstraintType;
import com.adobe.cq.social.ugc.api.SearchResults;
import com.adobe.cq.social.ugc.api.UgcFilter;
import com.adobe.cq.social.ugc.api.UgcSearch;
import com.adobe.cq.social.ugc.api.UgcSort;
import com.adobe.cq.social.ugc.api.ValueConstraint;
import com.adobe.cq.social.ugcbase.SocialUtils;
import com.adobe.granite.security.user.UserProperties;
import com.day.cq.commons.jcr.JcrUtil;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import javax.jcr.RepositoryException;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ModifiableValueMap;
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.resource.collection.ResourceCollection;
import org.apache.sling.resource.collection.ResourceCollectionManager;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SmartListManagerImpl
implements SmartListManager {
protected static final String SMART_LIST_RESOURCE_TYPE_VALUE = "commerce/smartlist";
protected static final String PN_PRODUCT_COLLECTION_TYPE = "commerce.collection.type";
protected static final String SMART_LIST_BASE_PATH = "/etc/commerce/smartlists";
private static final Logger LOGGER = LoggerFactory.getLogger(SmartListManagerImpl.class);
private static final String FILTER_PERSONAL = "personal";
private static final String FILTER_FRIENDS = "friends";
private final ResourceResolver resourceResolver;
private final SlingHttpServletRequest request;
private final ResourceCollectionManager resourceCollectionManager;
private final SocialUtils socialUtils;
private final UgcSearch ugcSearch;
private final EventAdmin eventAdmin;
public SmartListManagerImpl(SlingHttpServletRequest request, SocialUtils socialUtils, UgcSearch ugcSearch, EventAdmin eventAdmin) throws CommerceException {
this.request = request;
this.resourceResolver = request.getResourceResolver();
this.resourceCollectionManager = (ResourceCollectionManager)this.resourceResolver.adaptTo(ResourceCollectionManager.class);
this.socialUtils = socialUtils;
this.ugcSearch = ugcSearch;
this.eventAdmin = eventAdmin;
Resource smartListBaseResource = this.getRootResource(this.resourceResolver);
SocialResourceConfiguration storageConfig = socialUtils.getStorageConfig(smartListBaseResource);
SocialResourceProvider srp = socialUtils.getConfiguredProvider(smartListBaseResource);
srp.setConfig(storageConfig);
}
@Override
public SmartList getSmartList(String path) {
try {
Resource rcResource = this.resourceResolver.getResource(path);
if (rcResource == null) {
rcResource = this.resourceResolver.getResource(this.getRootResource(this.resourceResolver), path);
}
if (rcResource == null) {
return null;
}
if (!rcResource.isResourceType("commerce/smartlist")) {
return null;
}
SmartListImpl smartList = new SmartListImpl(rcResource, this.eventAdmin);
if (this.canRead(smartList)) {
return smartList;
}
}
catch (CommerceException e) {
LOGGER.error("Cannot get smart list collection " + path, (Throwable)e);
}
return null;
}
@Override
public List<SmartList> getSmartLists(String filter) {
ArrayList<SmartList> smartLists = new ArrayList<SmartList>();
try {
UgcFilter ugcFilter = null;
if (filter == null || StringUtils.equals((String)filter, (String)"personal")) {
ugcFilter = this.getPersonalListsFilter();
} else if (StringUtils.equals((String)filter, (String)"friends")) {
ugcFilter = this.getFriendsListsFilter();
}
if (ugcFilter != null) {
SearchResults results = this.ugcSearch.find(null, this.resourceResolver, ugcFilter, 0, 100, false);
for (Resource resultRes : results.getResults()) {
if (resultRes.isResourceType("commerce/smartlist")) {
LOGGER.debug("found smart list collection {}", (Object)resultRes.getPath());
smartLists.add(new SmartListImpl(resultRes, this.eventAdmin));
continue;
}
LOGGER.debug("Search returned {} but resource is not {}", (Object)resultRes.getPath(), (Object)"commerce/smartlist");
}
}
}
catch (RepositoryException e) {
LOGGER.error("Cannot get smart lists, search failed", (Throwable)e);
}
catch (CommerceException e) {
LOGGER.error("Cannot get smart lists", (Throwable)e);
}
return smartLists;
}
@Override
public SmartList createSmartList(String name) throws CommerceException {
return this.createSmartList(name, new HashMap<String, Object>());
}
@Override
public SmartList createSmartList(String name, Map<String, Object> properties) throws CommerceException {
try {
if (properties == null) {
properties = new HashMap<String, Object>();
}
properties.put("sling:resourceType", "commerce/smartlist");
if (properties.get("commerce.collection.type") == null) {
properties.put("commerce.collection.type", "product-based");
}
if (properties.get("jcr:primaryType") == null) {
properties.put("jcr:primaryType", "nt:unstructured");
}
if (properties.get("userIdentifier") == null) {
String userIdentifier = this.getUserIdentifier();
properties.put("userIdentifier", userIdentifier);
}
if (properties.get("smartlist_privacy_s") == null) {
properties.put("smartlist_privacy_s", SmartList.Privacy.PERSONAL.name());
}
ResourceCollection resourceCollection = this.resourceCollectionManager.createCollection(this.getRootResource(this.resourceResolver), this.getUniqueSmartListName(name), properties);
this.resourceResolver.commit();
LOGGER.debug("Smart list resource collection created {}", (Object)resourceCollection.getName());
this.logEvent("com/adobe/cq/commerce/smartlist/ADDED", resourceCollection.getPath());
return new SmartListImpl(this.resourceResolver.getResource(resourceCollection.getPath()), this.eventAdmin);
}
catch (PersistenceException e) {
throw new CommerceException("Cannot create a smart list collection " + name, (Throwable)e);
}
}
@Override
public void updateSmartList(String path, Map<String, Object> properties) throws CommerceException {
if (path == null) {
throw new CommerceException("Cannot update the smart list as it is undefined.");
}
if (!this.isOwner(path)) {
throw new CommerceException("Cannot update the smart list " + path);
}
try {
Resource slResource = this.getRootResource(this.resourceResolver).getChild(path);
String slResPath = slResource.getPath();
ModifiableValueMap currentProps = (ModifiableValueMap)slResource.adaptTo(ModifiableValueMap.class);
currentProps.putAll(properties);
this.resourceResolver.commit();
this.logEvent("com/adobe/cq/commerce/smartlist/MODIFIED", slResPath);
}
catch (PersistenceException e) {
throw new CommerceException("Cannot update the collection at " + path, (Throwable)e);
}
}
@Override
public void deleteSmartList(String path) throws CommerceException {
boolean deleted;
if (path == null) {
throw new CommerceException("Cannot delete the smart list as it is undefined.");
}
if (!this.isOwner(path)) {
throw new CommerceException("Cannot delete the smart list " + path);
}
try {
Resource slResource = this.getRootResource(this.resourceResolver).getChild(path);
String slResPath = slResource.getPath();
deleted = this.resourceCollectionManager.deleteCollection(slResource);
this.resourceResolver.commit();
this.logEvent("com/adobe/cq/commerce/smartlist/DELETED", slResPath);
}
catch (PersistenceException e) {
throw new CommerceException("Cannot delete the collection at " + path);
}
if (!deleted) {
throw new CommerceException("Cannot delete the collection at " + path);
}
}
@Override
public void makeSmartListDefault(String path) throws CommerceException {
if (path == null) {
throw new CommerceException("Cannot mark the smart list as it is undefined.");
}
if (!this.isOwner(path)) {
throw new CommerceException("Cannot mark the smart list " + path + " as default");
}
try {
List<SmartList> smartLists = this.getSmartLists("personal");
Resource rootResource = this.getRootResource(this.resourceResolver);
for (SmartList list : smartLists) {
Resource listResource = rootResource.getChild(list.getPath());
ModifiableValueMap listResourceModifiableValueMap = (ModifiableValueMap)listResource.adaptTo(ModifiableValueMap.class);
listResourceModifiableValueMap.remove((Object)"smartlist_default_b");
}
HashMap<String, Boolean> properties = new HashMap<String, Boolean>();
properties.put("smartlist_default_b", Boolean.TRUE);
Resource smartListResource = this.getRootResource(this.resourceResolver).getChild(path);
ModifiableValueMap currentProps = (ModifiableValueMap)smartListResource.adaptTo(ModifiableValueMap.class);
currentProps.putAll(properties);
this.resourceResolver.commit();
LOGGER.debug("{} marked as default", (Object)path);
}
catch (PersistenceException e) {
throw new CommerceException("Cannot update the collection at " + path);
}
}
/*
* Enabled force condition propagation
* Lifted jumps to return sites
*/
@Override
public void shareSmartList(String path, Map<String, Object> properties) throws CommerceException {
if (path == null) {
throw new CommerceException("Cannot change sharing status of smart list as it is undefined.");
}
if (!this.isOwner(path)) {
throw new CommerceException("Cannot change sharing status of smart list " + path);
}
try {
Resource slResource = this.getRootResource(this.resourceResolver).getChild(path);
ModifiableValueMap currentProps = (ModifiableValueMap)slResource.adaptTo(ModifiableValueMap.class);
if (properties != null) {
if (!properties.containsKey("smartlist_privacy_s")) throw new CommerceException("Cannot update the sharing status for " + path + " sharing privacy status is missing");
currentProps.putAll(properties);
} else {
currentProps.put((Object)"smartlist_privacy_s", (Object)SmartList.Privacy.PERSONAL.name());
}
this.resourceResolver.commit();
return;
}
catch (PersistenceException e) {
throw new CommerceException("Cannot update the sharing status for " + path, (Throwable)e);
}
}
private Resource getRootResource(ResourceResolver resourceResolver) throws CommerceException {
Resource baseResource = resourceResolver.getResource("/etc/commerce/smartlists");
if (baseResource == null) {
throw new CommerceException("Cannot read root resource /etc/commerce/smartlists");
}
String rootResourcePath = this.socialUtils.resourceToUGCStoragePath(baseResource);
Resource rootResource = resourceResolver.getResource(rootResourcePath);
if (rootResource == null) {
HashMap<String, String> properties = new HashMap<String, String>();
properties.put("jcr:primaryType", "sling:Folder");
SocialResourceProvider srp = this.socialUtils.getConfiguredProvider(baseResource);
try {
rootResource = srp.create(resourceResolver, rootResourcePath, properties);
resourceResolver.commit();
}
catch (PersistenceException e) {
throw new CommerceException("Cannot create root resource for " + baseResource.getPath(), (Throwable)e);
}
}
return rootResource;
}
private UgcFilter getPersonalListsFilter() throws CommerceException {
UgcFilter filter = new UgcFilter();
ConstraintGroup cg = new ConstraintGroup();
cg.addConstraint((Constraint)new ValueConstraint("sling:resourceType", (Object)"commerce/smartlist"));
cg.addConstraint((Constraint)new PathConstraint("/etc/commerce/smartlists", PathConstraintType.IsDescendantNode));
String userIdentifier = this.getUserIdentifier();
if (StringUtils.isNotEmpty((String)userIdentifier)) {
cg.and((Constraint)new ValueConstraint("userIdentifier", (Object)userIdentifier));
}
filter.and((Constraint)cg);
filter.addSort(new UgcSort("smartlist_default_b", UgcSort.Direction.Desc));
filter.addSort(new UgcSort("timestamp", UgcSort.Direction.Desc));
return filter;
}
private UgcFilter getFriendsListsFilter() throws CommerceException {
UgcFilter filter = new UgcFilter();
ConstraintGroup cg = new ConstraintGroup();
cg.addConstraint((Constraint)new ValueConstraint("sling:resourceType", (Object)"commerce/smartlist"));
cg.addConstraint((Constraint)new PathConstraint("/etc/commerce/smartlists", PathConstraintType.IsDescendantNode));
String userIdentifier = "todo";
if (StringUtils.isNotEmpty((String)"todo")) {
cg.and((Constraint)new ValueConstraint("userIdentifier", (Object)"todo"));
}
filter.and((Constraint)cg);
filter.addSort(new UgcSort("smartlist_default_b", UgcSort.Direction.Desc));
filter.addSort(new UgcSort("timestamp", UgcSort.Direction.Desc));
return filter;
}
private String getUserIdentifier() throws CommerceException {
String userIdentifier;
UserProperties up = (UserProperties)this.request.adaptTo(UserProperties.class);
String string = userIdentifier = up == null ? null : up.getAuthorizableID();
if (userIdentifier == null) {
throw new CommerceException("No user identifier available");
}
return userIdentifier;
}
private boolean isOwner(String smartListPath) {
return this.isOwner(this.getSmartList(smartListPath));
}
private boolean isOwner(SmartList smartList) {
try {
if (smartList != null && StringUtils.equals((String)this.getUserIdentifier(), (String)smartList.getOwner())) {
return true;
}
}
catch (CommerceException e) {
LOGGER.error("Owner check failed, only the smart list owner is allowed to execute this operation", (Throwable)e);
}
return false;
}
private boolean canRead(SmartList smartList) {
return smartList != null && (this.isOwner(smartList) || !smartList.getPrivacy().equals((Object)SmartList.Privacy.PERSONAL));
}
private String getUniqueSmartListName(String title) {
return JcrUtil.createValidName((String)title, (String[])JcrUtil.HYPHEN_LABEL_CHAR_MAPPING) + "-" + RandomStringUtils.randomAlphanumeric((int)6);
}
protected void logEvent(String eventName, String smartListPath) {
if (this.eventAdmin != null) {
Hashtable<String, String> eventProperties = new Hashtable<String, String>();
eventProperties.put("smartlist", smartListPath);
eventProperties.put("userid", this.resourceResolver.getUserID());
this.eventAdmin.postEvent(new Event(eventName, eventProperties));
}
}
}