CampaignReferenceProvider.java
5.47 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.Filter
* com.day.cq.personalization.TargetedContentManager
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.reference.Reference
* com.day.cq.wcm.api.reference.ReferenceProvider
* javax.jcr.RepositoryException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.personalization.impl;
import com.day.cq.commons.Filter;
import com.day.cq.personalization.TargetedContentManager;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.reference.Reference;
import com.day.cq.wcm.api.reference.ReferenceProvider;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class CampaignReferenceProvider
implements ReferenceProvider {
private static final String RT_ANYMBOX = "cq/personalization/components/anymbox";
private static final String TYPE_CAMPAIGN = "campaign";
private final Logger log = LoggerFactory.getLogger(CampaignReferenceProvider.class);
private TargetedContentManager targetedContentManager;
public List<Reference> findReferences(Resource resource) {
final ArrayList<String> anyMboxLocations = new ArrayList<String>();
this.findAnyMboxLocations(resource, anyMboxLocations);
this.log.debug("For page at {} found anyMbox locations {}", (Object)resource.getPath(), anyMboxLocations);
try {
List campaigns = this.targetedContentManager.getCampaigns(resource.getResourceResolver());
final HashMap<String, Reference> references = new HashMap<String, Reference>();
for (Page campaign : campaigns) {
Iterator matchingTeaserPages = campaign.listChildren((Filter)new Filter<Page>(){
public boolean includes(Page page) {
return "cq/personalization/components/teaserpage".equals(page.getContentResource().getResourceType()) && anyMboxLocations.contains(((ValueMap)page.getContentResource().adaptTo(ValueMap.class)).get("location", null)) && !references.containsKey(page.getParent(2).getPath());
}
}, true);
if (!matchingTeaserPages.hasNext()) continue;
long lastModified = this.computeLastModified(campaign);
while (matchingTeaserPages.hasNext()) {
Page teaserPage = (Page)matchingTeaserPages.next();
Page campaignPage = teaserPage.getParent(2);
references.put(campaignPage.getPath(), new Reference("campaign", campaignPage.getName(), (Resource)campaignPage.adaptTo(Resource.class), lastModified));
this.log.debug("For page at {} adding reference to campaign at {}", (Object)resource.getPath(), (Object)campaignPage.getPath());
}
}
return new ArrayList<Reference>(references.values());
}
catch (RepositoryException e) {
this.log.warn("Unable to find campaign references for resource at " + resource.getPath(), (Throwable)e);
return Collections.emptyList();
}
}
private long computeLastModified(Page campaign) {
long lastModified = this.lastModifiedInMillis(campaign);
Iterator children = campaign.listChildren((Filter)new Filter<Page>(){
public boolean includes(Page page) {
String resourceType = page.getContentResource().getResourceType();
return "cq/personalization/components/experiencepage".equals(resourceType) || "cq/personalization/components/teaserpage".equals(resourceType);
}
}, true);
while (children.hasNext()) {
lastModified = Math.max(lastModified, this.lastModifiedInMillis((Page)children.next()));
}
return lastModified;
}
private long lastModifiedInMillis(Page matchingTeaserPage) {
return matchingTeaserPage.getLastModified() != null ? matchingTeaserPage.getLastModified().getTimeInMillis() : -1;
}
private void findAnyMboxLocations(Resource resource, List<String> locations) {
Iterator children = resource.listChildren();
while (children.hasNext()) {
Resource child = (Resource)children.next();
if (child.getResourceType().equals("cq/personalization/components/anymbox")) {
this.log.debug("Found anymbox at {}", (Object)child.getPath());
locations.add(child.getPath());
continue;
}
this.findAnyMboxLocations(child, locations);
}
}
protected void bindTargetedContentManager(TargetedContentManager targetedContentManager) {
this.targetedContentManager = targetedContentManager;
}
protected void unbindTargetedContentManager(TargetedContentManager targetedContentManager) {
if (this.targetedContentManager == targetedContentManager) {
this.targetedContentManager = null;
}
}
}