AmbitChangePostProcessor.java
8.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.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.resource.LoginException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.event.jobs.Job
* org.apache.sling.event.jobs.JobManager
* org.apache.sling.event.jobs.consumer.JobConsumer
* org.apache.sling.event.jobs.consumer.JobConsumer$JobResult
* org.apache.sling.servlets.post.Modification
* org.apache.sling.servlets.post.ModificationType
* org.apache.sling.servlets.post.SlingPostProcessor
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.analytics.testandtarget.impl;
import com.day.cq.analytics.testandtarget.TargetMediator;
import com.day.cq.analytics.testandtarget.TestandtargetException;
import com.day.cq.analytics.testandtarget.impl.TargetHelperService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
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.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.JobManager;
import org.apache.sling.event.jobs.consumer.JobConsumer;
import org.apache.sling.servlets.post.Modification;
import org.apache.sling.servlets.post.ModificationType;
import org.apache.sling.servlets.post.SlingPostProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
@Properties(value={@Property(name="job.topics", value={"com/adobe/integrations/target/ambitchange"})})
public class AmbitChangePostProcessor
implements SlingPostProcessor,
JobConsumer {
public static final String JOB_TOPIC = "com/adobe/integrations/target/ambitchange";
private static final String JOB_PROPERTY_CAMPAIGNPATHS = "campaignpaths";
private static final String PN_TARGETAMBITS = "cq:target-ambits";
private final Logger LOG;
@Reference
private ResourceResolverFactory resolverFactory;
@Reference
private TargetHelperService targetHelperService;
@Reference
private JobManager jobMgr;
@Reference
private TargetMediator targetMediator;
public AmbitChangePostProcessor() {
this.LOG = LoggerFactory.getLogger(this.getClass());
}
public void process(SlingHttpServletRequest request, List<Modification> changes) throws Exception {
String[] affectedCampaigns;
if (this.handle(changes) && (affectedCampaigns = this.getAffectedCampaigns(request)).length > 0) {
HashMap<String, String[]> payload = new HashMap<String, String[]>();
payload.put("campaignpaths", affectedCampaigns);
this.jobMgr.addJob("com/adobe/integrations/target/ambitchange", payload);
}
}
public JobConsumer.JobResult process(Job job) {
String[] affectedCampaigns;
for (String campaignPath : affectedCampaigns = (String[])job.getProperty("campaignpaths", String[].class)) {
try {
this.targetMediator.syncAuthorCampaign(campaignPath);
continue;
}
catch (TestandtargetException e) {
this.LOG.error(String.format("Unable to sync %s to Target", campaignPath), (Throwable)e);
}
}
return JobConsumer.JobResult.OK;
}
private boolean handle(List<Modification> changes) {
for (Modification modification : changes) {
if (!ModificationType.MODIFY.equals((Object)modification.getType()) || !StringUtils.endsWith((String)modification.getSource(), (String)"cq:target-ambits")) continue;
return true;
}
return false;
}
private String[] getAffectedCampaigns(SlingHttpServletRequest request) {
String[] persisted = this.getPersistedAmbits(request.getResource().getPath());
String[] submitted = request.getParameterValues("./cq:target-ambits");
HashSet<String> campaigns = new HashSet<String>();
try {
ArrayList<String> oldambits = new ArrayList<String>(Arrays.asList(persisted));
ArrayList<String> newambits = new ArrayList<String>(Arrays.asList(submitted));
Collection intersection = CollectionUtils.intersection(oldambits, newambits);
newambits.removeAll(intersection);
if (!newambits.isEmpty()) {
ResourceResolver resolver = request.getResourceResolver();
for (String ambitPath : newambits) {
Resource ambitResource = resolver.getResource(ambitPath);
Set<Resource> targetActivities = this.targetHelperService.getTargetCampaigns(ambitResource);
for (Resource r : targetActivities) {
campaigns.add(r.getPath());
}
}
}
}
catch (UnsupportedOperationException e) {
this.LOG.error("Unable to determine affected campaigns due to unexpected error.", (Throwable)e);
}
return campaigns.toArray(new String[0]);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private String[] getPersistedAmbits(String path) {
ResourceResolver serviceResolver = null;
try {
serviceResolver = this.resolverFactory.getServiceResourceResolver(Collections.singletonMap("sling.service.subservice", "target"));
Resource persistedResource = serviceResolver.getResource(path);
if (persistedResource != null) {
String[] arrstring = (String[])persistedResource.getValueMap().get("cq:target-ambits", (Object)new String[0]);
return arrstring;
}
}
catch (LoginException e) {
this.LOG.error("Unable to get persisted ambit values.", (Throwable)e);
}
finally {
if (serviceResolver != null) {
serviceResolver.close();
}
}
return new String[0];
}
protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resolverFactory = resourceResolverFactory;
}
protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resolverFactory == resourceResolverFactory) {
this.resolverFactory = null;
}
}
protected void bindTargetHelperService(TargetHelperService targetHelperService) {
this.targetHelperService = targetHelperService;
}
protected void unbindTargetHelperService(TargetHelperService targetHelperService) {
if (this.targetHelperService == targetHelperService) {
this.targetHelperService = null;
}
}
protected void bindJobMgr(JobManager jobManager) {
this.jobMgr = jobManager;
}
protected void unbindJobMgr(JobManager jobManager) {
if (this.jobMgr == jobManager) {
this.jobMgr = null;
}
}
protected void bindTargetMediator(TargetMediator targetMediator) {
this.targetMediator = targetMediator;
}
protected void unbindTargetMediator(TargetMediator targetMediator) {
if (this.targetMediator == targetMediator) {
this.targetMediator = null;
}
}
}