AbstractJcrVoucher.java
4.97 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.i18n.I18n
* com.day.cq.wcm.api.Page
* javax.servlet.http.HttpServletRequest
* org.apache.sling.adapter.SlingAdaptable
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
*/
package com.adobe.cq.commerce.common.promotion;
import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.promotion.Promotion;
import com.adobe.cq.commerce.api.promotion.Voucher;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.api.Page;
import javax.servlet.http.HttpServletRequest;
import org.apache.sling.adapter.SlingAdaptable;
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.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
@Deprecated
public class AbstractJcrVoucher
extends SlingAdaptable
implements Voucher {
public static final String VOUCHER_RESOURCE_TYPE = "commerce/components/voucher";
public static final long VOUCHER_DEFAULT_PRIORITY = 100;
protected Resource resource;
protected Page page;
public AbstractJcrVoucher(Resource resource) throws CommerceException {
this.resource = resource;
this.page = (Page)resource.adaptTo(Page.class);
if (this.page == null || !ResourceUtil.isA((Resource)resource.getChild("jcr:content"), (String)"commerce/components/voucher")) {
throw new CommerceException("Resource is not a JcrVoucher.");
}
}
@Override
public String getCode() {
ValueMap properties = (ValueMap)this.resource.adaptTo(ValueMap.class);
return (String)properties.get("jcr:content/code", String.class);
}
@Override
public String getPath() {
return this.resource.getPath();
}
@Override
public String getTitle() {
return this.page.getTitle();
}
@Override
public String getDescription() {
return this.page.getDescription();
}
public String getMessage() {
ValueMap properties = (ValueMap)this.resource.adaptTo(ValueMap.class);
return (String)properties.get("jcr:content/message", (Object)"");
}
@Override
public long getPriority() {
for (Page parent = this.page.getParent(); parent != null; parent = parent.getParent()) {
if (!ResourceUtil.isA((Resource)parent.getContentResource(), (String)"cq/personalization/components/campaignpage")) continue;
return ((Integer)ResourceUtil.getValueMap((Resource)parent.getContentResource()).get("priority", (Object)100)).intValue();
}
return 100;
}
@Override
public boolean isValid(SlingHttpServletRequest request) {
for (Page parent = this.page.getParent(); parent != null; parent = parent.getParent()) {
if (!ResourceUtil.isA((Resource)parent.getContentResource(), (String)"cq/personalization/components/campaignpage")) continue;
return parent.isValid();
}
return false;
}
@Override
public String getInvalidMessage(SlingHttpServletRequest request) {
I18n i18n = new I18n((HttpServletRequest)request);
for (Page parent = this.page.getParent(); parent != null; parent = parent.getParent()) {
if (!ResourceUtil.isA((Resource)parent.getContentResource(), (String)"cq/personalization/components/campaignpage")) continue;
long time = parent.timeUntilValid();
if (time > 0) {
return i18n.get("Invalid coupon code.");
}
if (time >= 0) continue;
return i18n.get("JcrVoucher expired.");
}
return "";
}
public Promotion getPromotion() {
ValueMap properties = (ValueMap)this.resource.adaptTo(ValueMap.class);
String promoPath = (String)properties.get("jcr:content/promotion", String.class);
if (promoPath != null && promoPath.length() > 0) {
Resource promoResource = this.resource.getResourceResolver().getResource(promoPath);
return promoResource == null ? null : (Promotion)promoResource.adaptTo(Promotion.class);
}
return null;
}
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
if (type == Resource.class) {
return (AdapterType)this.resource;
}
if (type == Page.class) {
return (AdapterType)this.page;
}
Object ret = super.adaptTo(type);
if (ret == null) {
ret = this.resource.adaptTo(type);
}
return (AdapterType)ret;
}
@Override
public String getMessage(SlingHttpServletRequest request) {
throw new UnsupportedOperationException();
}
@Override
public ValueMap getConfig() {
throw new UnsupportedOperationException();
}
}