JcrPromotionImpl.java
4.38 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* org.apache.sling.adapter.SlingAdaptable
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.osgi.OsgiUtil
*/
package com.adobe.cq.commerce.impl.promotion;
import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.promotion.Promotion;
import com.adobe.cq.commerce.api.promotion.PromotionHandler;
import com.adobe.cq.commerce.api.promotion.PromotionManager;
import com.day.cq.wcm.api.Page;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.apache.sling.adapter.SlingAdaptable;
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;
import org.apache.sling.commons.osgi.OsgiUtil;
public class JcrPromotionImpl
extends SlingAdaptable
implements Promotion {
public static final String PN_PROMOTION_TYPE = "promotionType";
public static final String PN_PRIORITY = "priority";
private static final Long DEFAULT_PRIORITY = 100;
private static final String CONFIG_RESOURCE_NAME = "config";
private Resource resource;
protected Page page;
public JcrPromotionImpl(Resource resource) throws CommerceException {
this.page = (Page)resource.adaptTo(Page.class);
if (this.page == null || !ResourceUtil.isA((Resource)resource.getChild("jcr:content"), (String)"commerce/components/promotion")) {
throw new CommerceException("Resource is not a Promotion.");
}
this.resource = this.page.getContentResource();
}
@Override
public String getPath() {
return this.page.getPath();
}
@Override
public String getTitle() {
return this.page.getTitle();
}
@Override
public String getDescription() {
return this.page.getDescription();
}
@Override
public String getType() {
return (String)((ValueMap)this.resource.adaptTo(ValueMap.class)).get("promotionType", String.class);
}
@Override
public long getPriority() {
return (Long)((ValueMap)this.resource.adaptTo(ValueMap.class)).get("priority", (Object)DEFAULT_PRIORITY);
}
@Override
public List<String> getSegments() {
ArrayList<String> segmentsList = new ArrayList<String>();
for (Page parent = this.page.getParent(); parent != null; parent = parent.getParent()) {
String[] segmentsArray;
Resource parentContent = parent.getContentResource();
if (!ResourceUtil.isA((Resource)parentContent, (String)"cq/personalization/components/experiencepage") || (segmentsArray = OsgiUtil.toStringArray((Object)ResourceUtil.getValueMap((Resource)parentContent).get((Object)"cq:segments"))) == null) continue;
segmentsList.addAll(Arrays.asList(segmentsArray));
}
return segmentsList;
}
@Override
public boolean isValid() {
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;
}
@Deprecated
@Override
public Resource getConfigResource() {
return this.resource.getChild("config");
}
@Override
public ValueMap getConfig() {
return ResourceUtil.getValueMap((Resource)this.resource.getChild("config"));
}
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
if (type == Resource.class) {
return (AdapterType)this.resource;
}
if (type == Page.class) {
return (AdapterType)this.page;
}
if (type == PromotionHandler.class) {
PromotionManager pm = (PromotionManager)this.resource.getResourceResolver().adaptTo(PromotionManager.class);
return (AdapterType)pm.getHandler(this.getType());
}
Object result = this.resource.adaptTo(type);
if (result != null) {
return (AdapterType)result;
}
return (AdapterType)super.adaptTo(type);
}
}