ActivityResourceVisitor.java
4.6 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.personalization.TargetedContentManager
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.resource.AbstractResourceVisitor
* 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.json.JSONArray
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.contentinsight;
import com.day.cq.personalization.TargetedContentManager;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.*;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
public class ActivityResourceVisitor
extends AbstractResourceVisitor {
private static final Logger log = LoggerFactory.getLogger(ActivityResourceVisitor.class);
private static final String PN_PUBLISH_CAMPAIGN_ID = "publishCampaignId";
private static final String TARGET_COMPONENT = "cq/personalization/components/target";
private List<Resource> activities = new ArrayList<Resource>();
private String pagePath = "";
private TargetedContentManager targetedContentManager;
public ActivityResourceVisitor(String pagePath, TargetedContentManager targetedContentManager) {
if (!pagePath.endsWith("jcr:content")) {
pagePath = pagePath + "/jcr:content";
}
this.pagePath = pagePath;
this.targetedContentManager = targetedContentManager;
if (targetedContentManager == null) {
log.warn("No TargetedContentManager available, will not be able to determine campaigns used on the current page!");
}
}
protected void visit(Resource res) {
ValueMap properties = ResourceUtil.getValueMap((Resource)res);
if (ResourceUtil.isA((Resource)res, (String)"cq/personalization/components/campaignpage") && properties.get("publishCampaignId", String.class) != null && this.matchesPage(res)) {
this.activities.add(res);
}
}
public List<Resource> getActivityResources() {
return this.activities;
}
private boolean matchesPage(Resource campaignResource) {
boolean matchesPage = false;
ResourceResolver resolver = campaignResource.getResourceResolver();
Resource pageRes = resolver.getResource(this.pagePath);
if (pageRes != null && campaignResource != null) {
String campaignPathPrefix = campaignResource.getParent().getPath() + "/";
List<Resource> targetResources = this.getTargetComponents(pageRes, null);
try {
block2 : for (Resource targetResource : targetResources) {
ValueMap targetResProps = (ValueMap)targetResource.adaptTo(ValueMap.class);
String locationProp = (String)targetResProps.get("location", null);
String locationID = StringUtils.isNotBlank((String)locationProp) ? locationProp : targetResource.getPath();
JSONObject teaserInfo = this.targetedContentManager.getTeaserInfo(resolver, null, locationID);
JSONArray allTeasers = teaserInfo.getJSONArray("allTeasers");
for (int i = 0; i < allTeasers.length(); ++i) {
JSONObject teaser = allTeasers.getJSONObject(i);
if (!teaser.getString("path").startsWith(campaignPathPrefix)) continue;
matchesPage = true;
continue block2;
}
}
}
catch (JSONException e) {
log.error("Could not determine if page {} uses campaign {}!", new Object[]{pageRes.getPath(), campaignResource.getPath()});
log.error("", (Throwable)e);
}
}
return matchesPage;
}
private List<Resource> getTargetComponents(Resource rootResource, List<Resource> resList) {
if (resList == null) {
resList = new ArrayList<Resource>();
}
for (Resource child : rootResource.getChildren()) {
if (child.isResourceType("cq/personalization/components/target")) {
resList.add(child);
continue;
}
this.getTargetComponents(child, resList);
}
return resList;
}
}