CampaignStateCommand.java
9.75 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
209
210
211
212
213
214
215
216
217
218
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.replication.ReplicationActionType
* com.day.cq.replication.ReplicationException
* com.day.cq.replication.Replicator
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.webservicesupport.Configuration
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* javax.jcr.Session
* javax.servlet.ServletException
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* 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.ValueMap
* org.apache.sling.api.servlets.HtmlResponse
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.analytics.testandtarget.impl.servlets.commands;
import com.day.cq.analytics.testandtarget.TestandtargetCommand;
import com.day.cq.analytics.testandtarget.TestandtargetException;
import com.day.cq.analytics.testandtarget.impl.TestandtargetPrivateService;
import com.day.cq.analytics.testandtarget.impl.TntCampaignState;
import com.day.cq.analytics.testandtarget.util.CampaignType;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.Replicator;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import javax.jcr.Session;
import javax.servlet.ServletException;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
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.SlingHttpServletResponse;
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.ValueMap;
import org.apache.sling.api.servlets.HtmlResponse;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
public class CampaignStateCommand
implements TestandtargetCommand {
private static final Logger LOG = LoggerFactory.getLogger(CampaignStateCommand.class);
private static final String PARAM_CAMPAIGN_STATE = "campaignState";
private static final String PARAM_CAMPAIGN_PATH = "campaignPath";
private List<String> acceptedStates = Arrays.asList("approved", "deactivated");
@Reference
protected Replicator replicator;
@Reference
private TestandtargetPrivateService testandtargetService;
@Override
public String getName() {
return "campaignState";
}
@Override
public HtmlResponse performCommand(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
String campaignPath = request.getParameter("campaignPath");
if (StringUtils.isEmpty((String)campaignPath)) {
this.sendError(response, 400, "The parameter campaignPath is missing.");
return null;
}
String campaignState = request.getParameter("campaignState");
if (StringUtils.isEmpty((String)campaignPath)) {
this.sendError(response, 400, "The parameter campaignState is missing.");
return null;
}
if (!this.acceptedStates.contains(campaignState)) {
this.sendError(response, 400, "The state can only be \"approved\" and \"deactivated\".");
return null;
}
LOG.debug("Changing state for campaign at {}", (Object)campaignPath);
PageManager pMgr = (PageManager)request.getResourceResolver().adaptTo(PageManager.class);
Page campaignPage = pMgr.getPage(campaignPath);
if (campaignPage == null) {
this.sendError(response, 404, "The path " + campaignPath + " doesn't point to a campaign page.");
return null;
}
ValueMap properties = campaignPage.getProperties();
String tntConfig = (String)properties.get("cq:cloudserviceconfigs", (Object)"");
if (StringUtils.isEmpty((String)tntConfig)) {
this.sendError(response, 500, "The campaign doesn't have a Target cloud service configuration attached.");
return null;
}
ConfigurationManager configMgr = (ConfigurationManager)request.getResourceResolver().adaptTo(ConfigurationManager.class);
Configuration configuration = configMgr.getConfiguration(tntConfig);
if (configuration == null) {
this.sendError(response, 500, "The configuration at " + tntConfig + " cannot be found.");
return null;
}
String campaignType = (String)properties.get("campaignType", (Object)"");
LOG.debug("Campaign type is {}", (Object)campaignType);
String authorCampaignId = (String)properties.get("cq:authorExternalId", (Object)"");
if (StringUtils.isEmpty((String)authorCampaignId)) {
this.sendError(response, 500, "The campaign at doesn't have a Target id. Maybe it's not synchronized yet.");
}
LOG.debug("Campaign is synchronized, id is {}", (Object)authorCampaignId);
try {
LOG.debug("Deactivating the author campaign");
this.testandtargetService.setCampaignState(configuration, campaignState, authorCampaignId, null, CampaignType.fromString(campaignType));
String publishCampaignId = (String)properties.get("publishCampaignId", (Object)"");
if (StringUtils.isNotEmpty((String)publishCampaignId)) {
if (TntCampaignState.APPROVED.name().equals(campaignState)) {
this.syncPage(campaignPage, ReplicationActionType.ACTIVATE);
} else if (TntCampaignState.DEACTIVATED.name().equals(campaignState)) {
this.syncPage(campaignPage, ReplicationActionType.DEACTIVATE);
}
this.testandtargetService.setCampaignState(configuration, campaignState, publishCampaignId, null, CampaignType.fromString(campaignType));
}
this.updateCampaignState(campaignPage, campaignState);
this.sendOk(response, "The state of the activity has been changed on Target.");
}
catch (TestandtargetException e) {
this.sendError(response, 500, e.getMessage());
}
catch (ReplicationException e) {
this.sendError(response, 500, e.getMessage());
}
return null;
}
private void updateCampaignState(Page campaignPage, String state) throws PersistenceException {
Resource res = campaignPage.getContentResource();
ModifiableValueMap mvm = (ModifiableValueMap)res.adaptTo(ModifiableValueMap.class);
boolean campaignActive = TntCampaignState.APPROVED.name().equalsIgnoreCase(state);
mvm.put((Object)"campaignActive", (Object)campaignActive);
res.getResourceResolver().commit();
}
private void syncPage(Page page, ReplicationActionType actionType) throws ReplicationException {
LOG.debug("Unpublishing page at {}", (Object)page.getPath());
if (this.isPublished(page)) {
Session session = (Session)page.getContentResource().getResourceResolver().adaptTo(Session.class);
this.replicator.replicate(session, actionType, page.getPath());
}
}
private boolean isPublished(Page page) {
ValueMap properties = page.getProperties();
return properties.containsKey((Object)"cq:lastReplicationAction") && "Activate".equals(properties.get("cq:lastReplicationAction", (Object)""));
}
private void sendOk(SlingHttpServletResponse response, String msg) throws IOException {
response.setContentType("application/json");
response.setStatus(200);
PrintWriter out = response.getWriter();
out.print(this.buildMessageObject(msg));
}
private void sendError(SlingHttpServletResponse response, int status, String msg) throws IOException {
response.setContentType("application/json");
response.setStatus(status);
PrintWriter out = response.getWriter();
out.print(this.buildMessageObject(msg));
}
private String buildMessageObject(String msg) {
JSONObject messageObject = new JSONObject();
try {
messageObject.put("message", (Object)msg);
return messageObject.toString();
}
catch (JSONException e) {
return msg;
}
}
protected void bindReplicator(Replicator replicator) {
this.replicator = replicator;
}
protected void unbindReplicator(Replicator replicator) {
if (this.replicator == replicator) {
this.replicator = null;
}
}
protected void bindTestandtargetService(TestandtargetPrivateService testandtargetPrivateService) {
this.testandtargetService = testandtargetPrivateService;
}
protected void unbindTestandtargetService(TestandtargetPrivateService testandtargetPrivateService) {
if (this.testandtargetService == testandtargetPrivateService) {
this.testandtargetService = null;
}
}
}