SmartListImpl.java
7.41 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.lang.StringUtils
* 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.api.resource.ValueMap
* org.apache.sling.resource.collection.ResourceCollection
* org.osgi.service.event.Event
* org.osgi.service.event.EventAdmin
*/
package com.adobe.cq.commerce.impl.smartlist;
import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.Product;
import com.adobe.cq.commerce.api.smartlist.SmartList;
import com.adobe.cq.commerce.api.smartlist.SmartListEntry;
import com.adobe.cq.commerce.impl.collection.AbstractProductCollection;
import com.adobe.cq.commerce.impl.smartlist.SmartListEntryImpl;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
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.api.resource.ValueMap;
import org.apache.sling.resource.collection.ResourceCollection;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
public class SmartListImpl
extends AbstractProductCollection
implements SmartList {
protected static final String TYPE_VALUE = "product-based";
protected static final String PN_SMART_LIST_OWNER = "userIdentifier";
protected static final String PN_SMART_LIST_DEFAULT = "smartlist_default_b";
protected static final String PN_SMART_LIST_PRIVACY = "smartlist_privacy_s";
protected static final String SMART_LIST_RESOURCE_TYPE_VALUE = "commerce/smartlist";
protected static final String SMART_LIST_EVENT_BASE = "com/adobe/cq/commerce/smartlist/";
private EventAdmin eventAdmin;
public SmartListImpl(Resource resource, EventAdmin eventAdmin) throws CommerceException {
super(resource);
if (!resource.isResourceType("commerce/smartlist")) {
throw new CommerceException("Resource is not a SmartListImpl.");
}
this.eventAdmin = eventAdmin;
}
@Override
public void add(String path) throws CommerceException {
this.checkItemType(path);
Product product = (Product)this.resolver.getResource(path).adaptTo(Product.class);
this.add(new SmartListEntryImpl(product));
}
@Override
public void add(SmartListEntry smartListEntry) throws CommerceException {
if (smartListEntry == null) {
throw new IllegalArgumentException("Cannot add undefined item");
}
Product product = smartListEntry.getProduct();
if (product == null) {
throw new IllegalArgumentException("Cannot add smart list entry without product");
}
Resource productRes = this.resolver.getResource(product.getPath());
boolean added = false;
try {
if (!this.resourceCollection.contains(productRes)) {
added = this.resourceCollection.add(productRes, smartListEntry.getProperties());
} else {
ModifiableValueMap productResProperties = this.resourceCollection.getProperties(productRes);
int newQuantity = smartListEntry.getQuantity();
if (productResProperties.containsKey((Object)"quantity")) {
newQuantity += ((Integer)productResProperties.get("quantity", Integer.class)).intValue();
}
productResProperties.putAll(smartListEntry.getProperties());
productResProperties.put((Object)"quantity", (Object)newQuantity);
added = true;
}
this.resolver.commit();
this.logEvent("com/adobe/cq/commerce/smartlist/PRODUCT_ADDED", this.resourceCollection.getPath(), smartListEntry.getProduct().getPath());
}
catch (PersistenceException e) {
throw new CommerceException("Cannot add item " + product.getPath() + " to smart list " + this.getPath());
}
if (!added) {
throw new CommerceException("Cannot add item " + product.getPath() + " to smart list " + this.getPath());
}
}
@Override
public void update(String path, Map<String, Object> properties) throws CommerceException {
Resource productRes = this.resolver.getResource(path);
try {
ModifiableValueMap productResProperties = this.resourceCollection.getProperties(productRes);
productResProperties.putAll(properties);
this.resolver.commit();
this.logEvent("com/adobe/cq/commerce/smartlist/PRODUCT_MODIFIED", this.resourceCollection.getPath(), path);
}
catch (PersistenceException e) {
throw new CommerceException("Cannot update item " + path + " on collection " + this.getPath());
}
}
@Override
public void remove(String path) throws CommerceException {
super.remove(path);
this.logEvent("com/adobe/cq/commerce/smartlist/PRODUCT_DELETED", this.resourceCollection.getPath(), path);
}
@Override
public String getPath() {
return StringUtils.substringAfterLast((String)super.getPath(), (String)"/");
}
@Override
public Iterator<SmartListEntry> getSmartListEntries() {
ArrayList<SmartListEntryImpl> smartListEntries = new ArrayList<SmartListEntryImpl>();
Iterator resources = this.resourceCollection.getResources();
while (resources.hasNext()) {
Resource r = (Resource)resources.next();
Product p = (Product)r.adaptTo(Product.class);
if (p == null) continue;
smartListEntries.add(new SmartListEntryImpl(p, (ValueMap)this.resourceCollection.getProperties(r)));
}
return smartListEntries.iterator();
}
@Override
public Iterator<Product> getProducts() {
ArrayList<Product> products = new ArrayList<Product>();
Iterator resources = this.resourceCollection.getResources();
while (resources.hasNext()) {
Resource r = (Resource)resources.next();
Product p = (Product)r.adaptTo(Product.class);
if (p == null) continue;
products.add(p);
}
return products.iterator();
}
@Override
public String getOwner() {
return (String)((Object)this.getProperty("userIdentifier", String.class));
}
@Override
public boolean isDefault() {
return this.getProperty("smartlist_default_b", Boolean.FALSE);
}
@Override
public SmartList.Privacy getPrivacy() {
return SmartList.Privacy.valueOf(this.getProperty("smartlist_privacy_s", SmartList.Privacy.PERSONAL.name()));
}
protected void logEvent(String eventName, String smartListPath, String productPath) {
if (this.eventAdmin != null) {
Hashtable<String, String> eventProperties = new Hashtable<String, String>();
eventProperties.put("smartlist", smartListPath);
eventProperties.put("userid", this.resolver.getUserID());
if (StringUtils.isNotEmpty((String)productPath)) {
eventProperties.put("product", productPath);
}
this.eventAdmin.postEvent(new Event(eventName, eventProperties));
}
}
}