TargetMediatorImpl.java
4.28 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.WCMMode
* com.day.cq.wcm.webservicesupport.Configuration
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Reference
* org.apache.sling.api.resource.LoginException
* org.apache.sling.api.resource.ModifiableValueMap
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.analytics.testandtarget.impl;
import com.day.cq.analytics.testandtarget.SaveOfferRequest;
import com.day.cq.analytics.testandtarget.TargetMediator;
import com.day.cq.analytics.testandtarget.TestandtargetException;
import com.day.cq.analytics.testandtarget.impl.TestandtargetCampaignMediatorImpl;
import com.day.cq.analytics.testandtarget.impl.TestandtargetPrivateService;
import com.day.cq.analytics.testandtarget.util.OfferHelper;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.WCMMode;
import com.day.cq.wcm.webservicesupport.Configuration;
import java.util.Collections;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TargetMediatorImpl
extends TestandtargetCampaignMediatorImpl
implements TargetMediator {
private static final Logger LOG = LoggerFactory.getLogger(TargetMediator.class);
@Reference
private TestandtargetPrivateService testandtargetService;
@Reference
private ResourceResolverFactory resolverFactory;
@Override
public long syncOffer(Configuration configuration, String offerPath) throws TestandtargetException {
ResourceResolver serviceResolver = null;
if (StringUtils.isEmpty((String)offerPath)) {
LOG.warn("Empty path provided, there's nothing to sync");
}
try {
serviceResolver = this.resolverFactory.getServiceResourceResolver(Collections.singletonMap("sling.service.subservice", "target"));
return this.syncOffer(offerPath, configuration, serviceResolver);
}
catch (LoginException e) {
LOG.error(e.getMessage(), (Throwable)e);
throw new TestandtargetException((Throwable)e);
}
catch (PersistenceException e) {
LOG.error(e.getMessage(), (Throwable)e);
throw new TestandtargetException((Throwable)e);
}
catch (TestandtargetException e) {
LOG.error(e.getMessage(), (Throwable)e);
throw e;
}
}
@Override
public long syncSegment(Configuration configuration, String segmentPath) {
return 0;
}
private long syncOffer(String offerPath, Configuration configuration, ResourceResolver resourceResolver) throws TestandtargetException, PersistenceException {
String offerName;
SaveOfferRequest saveOfferRequest;
long newOfferId;
Resource offerResource = resourceResolver.getResource(offerPath);
Page offerPage = (Page)offerResource.adaptTo(Page.class);
long offerId = OfferHelper.getOfferId(offerPage);
if (offerId != (newOfferId = this.testandtargetService.createHTMLOffer(configuration, saveOfferRequest = new SaveOfferRequest(offerId, offerName = OfferHelper.getOfferName(offerPage, WCMMode.PREVIEW), offerPage.getPath())))) {
ModifiableValueMap mvm = (ModifiableValueMap)offerPage.getContentResource().adaptTo(ModifiableValueMap.class);
mvm.put((Object)"cq:authorExternalId", (Object)newOfferId);
String legacyExternalId = "cq:authorExternalId".replace("cq:", "");
if (mvm.containsKey((Object)legacyExternalId)) {
mvm.remove((Object)legacyExternalId);
}
resourceResolver.commit();
}
return newOfferId;
}
}