DefaultJcrCartEntry.java
6.25 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.collections.CollectionUtils
* org.apache.commons.collections.Predicate
* org.apache.sling.api.resource.ValueMap
*/
package com.adobe.cq.commerce.common;
import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.CommerceSession;
import com.adobe.cq.commerce.api.PriceInfo;
import com.adobe.cq.commerce.api.Product;
import com.adobe.cq.commerce.common.PriceFilter;
import com.adobe.cq.commerce.common.ValueMapDecorator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Currency;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.sling.api.resource.ValueMap;
public class DefaultJcrCartEntry
implements CommerceSession.CartEntry {
private int index;
private Product product;
private int quantity;
private List<PriceInfo> prices;
private ValueMap properties;
public DefaultJcrCartEntry(int index, Product product, int quantity) {
this.index = index;
this.product = product;
this.quantity = quantity;
this.properties = new ValueMapDecorator(new HashMap<String, Object>());
}
@Override
public int getEntryIndex() {
return this.index;
}
public void setEntryIndex(int index) {
this.index = index;
}
@Override
public Product getProduct() throws CommerceException {
return this.product;
}
@Override
public int getQuantity() {
return this.quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
@Override
public List<PriceInfo> getPriceInfo(Predicate filter) throws CommerceException {
if (filter != null) {
ArrayList<PriceInfo> filtered = new ArrayList<PriceInfo>();
CollectionUtils.select(this.prices, (Predicate)filter, filtered);
return filtered;
}
return this.prices;
}
@Override
public String getPrice(Predicate filter) throws CommerceException {
List<PriceInfo> filtered = this.getPriceInfo(filter);
if (filtered != null && filtered.size() > 0) {
return filtered.get(0).getFormattedString();
}
return "";
}
public /* varargs */ void setPrice(PriceInfo priceInfo, String ... types) {
if (this.prices == null) {
this.prices = new ArrayList<PriceInfo>();
}
ArrayList<String> typeList = new ArrayList<String>(Arrays.asList(types));
typeList.add(priceInfo.getCurrency().getCurrencyCode());
int index = this.prices.size();
for (int i = 0; i < this.prices.size(); ++i) {
PriceInfo price = this.prices.get(i);
Set priceTypes = (Set)price.get((Object)"com.adobe.cq.commerce.common.PriceFilter.types");
if (!CollectionUtils.isEqualCollection((Collection)priceTypes, typeList)) continue;
index = i;
break;
}
priceInfo.put("com.adobe.cq.commerce.common.PriceFilter.types", new HashSet<String>(typeList));
if (index == this.prices.size()) {
this.prices.add(priceInfo);
} else {
this.prices.set(index, priceInfo);
}
}
@Override
public <T> T getProperty(String name, Class<T> type) {
return (T)(this.properties == null ? null : this.properties.get(name, type));
}
protected void updateProperties(Map<String, Object> propertyMap) {
String key;
Object value;
if (propertyMap == null) {
return;
}
HashMap<String, Object> internalMap = new HashMap<String, Object>();
for (Map.Entry<String, Object> entry2 : propertyMap.entrySet()) {
key = entry2.getKey();
value = entry2.getValue();
if ("quantity".equals(key)) {
if (value == null) {
this.setQuantity(0);
continue;
}
if (value instanceof Number) {
this.setQuantity(((Number)value).intValue());
continue;
}
try {
this.setQuantity(Double.valueOf(String.valueOf(value)).intValue());
}
catch (NumberFormatException x) {}
continue;
}
if (key == null) continue;
internalMap.put(key, value);
}
if (internalMap.isEmpty()) {
return;
}
if (this.properties.isEmpty()) {
this.properties = new ValueMapDecorator(internalMap);
} else {
for (Map.Entry entry : internalMap.entrySet()) {
key = (String)entry.getKey();
value = entry.getValue();
if (value == null) {
this.properties.remove((Object)key);
continue;
}
this.properties.put((Object)key, value);
}
}
}
public ValueMap getProperties() {
return this.properties;
}
@Deprecated
@Override
public String getUnitPrice() {
try {
return this.getPriceInfo(new PriceFilter("UNIT")).get(0).getFormattedString();
}
catch (CommerceException e) {
return "";
}
}
@Deprecated
@Override
public String getPreTaxPrice() {
try {
return this.getPriceInfo(new PriceFilter("LINE", "PRE_TAX")).get(0).getFormattedString();
}
catch (CommerceException e) {
return "";
}
}
@Deprecated
@Override
public String getTax() {
try {
return this.getPriceInfo(new PriceFilter("LINE", "TAX")).get(0).getFormattedString();
}
catch (CommerceException e) {
return "";
}
}
@Deprecated
@Override
public String getTotalPrice() {
try {
return this.getPriceInfo(new PriceFilter("LINE", "POST_TAX")).get(0).getFormattedString();
}
catch (CommerceException e) {
return "";
}
}
}