PushNotificationImpl.java
11.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.lang3.StringUtils
* org.apache.sling.api.resource.Resource
* 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.mobile.notifications.impl.push;
import com.adobe.cq.mobile.notifications.impl.LocalizedNotification;
import com.adobe.cq.mobile.notifications.impl.Notification;
import com.adobe.cq.mobile.notifications.impl.NotificationState;
import com.adobe.cq.mobile.notifications.impl.exceptions.NotificationException;
import com.adobe.cq.mobile.notifications.impl.push.LocalizedNotificationImpl;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceType;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
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;
public class PushNotificationImpl
implements Notification {
private String id;
private String title;
private String description;
private Map<String, LocalizedNotification> localizedNotifications;
private NotificationState state;
private Calendar lastSentDate = null;
private String[] platforms;
private String filterParameters;
private String path;
private final Logger logger = LoggerFactory.getLogger(PushNotificationImpl.class);
private PushNotificationImpl() {
}
public PushNotificationImpl(String[] platforms, String appId, LocalizedNotification localizedNotification) throws NotificationException {
if (platforms == null || platforms.length == 0) {
throw new NotificationException("No platforms were specified for this notification.");
}
if (appId == null) {
throw new NotificationException("No application ID was provided for this notification.");
}
if (localizedNotification == null) {
throw new NotificationException("No message was specified for this notification.");
}
this.addLocalizedNotification(localizedNotification);
this.setPlatforms(platforms);
this.setState(NotificationState.READY);
this.setId(appId + "-" + System.currentTimeMillis());
}
public PushNotificationImpl(Resource resource) throws NotificationException {
MobileResource notification = (MobileResource)resource.adaptTo(MobileResource.class);
if (notification == null || !notification.isA(MobileResourceType.NOTIFICATION.getType())) {
throw new NotificationException("The resource is not a notification resource: " + resource.getPath());
}
this.setPath(resource.getPath());
Resource resourceContent = resource.getChild("jcr:content");
ValueMap vm = resourceContent.getValueMap();
this.setId((String)vm.get("id", String.class));
this.setTitle((String)vm.get("jcr:title", String.class));
this.setDescription((String)vm.get("jcr:description", String.class));
this.setFilterParameters((String)vm.get("targetFilter", String.class));
this.setPlatforms((String[])vm.get("platforms", String[].class));
String stateStr = "";
try {
stateStr = (String)vm.get("state", (Object)NotificationState.READY.asString());
this.setState(NotificationState.getFromString(stateStr.trim()));
}
catch (IllegalArgumentException iaEx) {
throw new NotificationException("The notification does not have a valid state: " + stateStr + " - " + resource.getPath());
}
this.lastSentDate = (Calendar)vm.get("lastSent", Calendar.class);
Iterator messages = resourceContent.getChildren().iterator();
while (messages.hasNext()) {
LocalizedNotificationImpl nextNotification = new LocalizedNotificationImpl((Resource)messages.next());
this.addLocalizedNotification(nextNotification);
}
}
private void setId(String id) {
this.id = id;
}
@Override
public String getId() {
return this.id;
}
@Override
public void setTitle(String title) {
this.title = title;
}
@Override
public String getTitle() {
return this.title;
}
@Override
public void setDescription(String description) {
this.description = description;
}
@Override
public String getDescription() {
return this.description;
}
@Override
public void setPath(String notificationPath) {
this.path = notificationPath;
}
@Override
public String getPath() {
return this.path;
}
@Override
public void addLocalizedNotification(LocalizedNotification localizedNotification) throws NotificationException {
if (this.localizedNotifications != null) {
throw new NotificationException("Currently only one message per notification is supported.");
}
this.localizedNotifications = new HashMap<String, LocalizedNotification>();
String locale = localizedNotification.getLocaleAsString();
if (!locale.equals("en_US")) {
throw new NotificationException("Currently only the 'en_US' locale is supported for localizedNotifications.");
}
this.localizedNotifications.put(localizedNotification.getLocaleAsString(), localizedNotification);
}
@Override
public Map<String, LocalizedNotification> getLocalizedNotifications() {
return this.localizedNotifications;
}
@Override
public LocalizedNotification getLocalizedNotification(String locale) {
return this.localizedNotifications.get(locale);
}
@Override
public void setState(NotificationState state) {
this.state = state;
if (NotificationState.SENT.equals((Object)state)) {
this.lastSentDate = Calendar.getInstance();
}
}
@Override
public NotificationState getState() {
return this.state;
}
@Override
public Calendar getLastSent() {
return this.lastSentDate;
}
@Override
public void setFilterParameters(String filterParameters) {
this.filterParameters = filterParameters;
}
@Override
public String getFilterParameters() {
return this.filterParameters;
}
@Override
public void setPlatforms(String[] platforms) {
this.platforms = platforms;
}
@Override
public String[] getPlatforms() {
return this.platforms;
}
@Override
public String getPlatformsString() {
if (this.platforms == null || this.platforms.length == 0) {
return "[]";
}
return StringUtils.join((Object[])this.platforms, (String)",");
}
@Override
public JSONObject toJSONObject() {
JSONObject json = new JSONObject();
this.addJsonStringProperty(json, "path", this.getPath());
this.addJsonStringProperty(json, "jcr:title", this.getTitle());
this.addJsonStringProperty(json, "jcr:description", this.getDescription());
this.addJsonStringProperty(json, "state", this.getState().toString());
this.addJsonStringProperty(json, "id", this.getId());
this.addJsonStringProperty(json, "targetFilter", this.getFilterParameters());
JSONArray platforms = new JSONArray();
if (this.getPlatforms() == null || this.getPlatforms().length == 0) {
platforms.put((Object)"all");
} else {
for (int i = 0; i < this.getPlatforms().length; ++i) {
platforms.put((Object)this.getPlatforms()[i]);
}
}
this.addJsonArrayProperty(json, "platforms", platforms);
JSONArray messageArray = new JSONArray();
if (this.getLocalizedNotifications() != null && !this.getLocalizedNotifications().isEmpty()) {
Iterator<Map.Entry<String, LocalizedNotification>> messagesIt = this.getLocalizedNotifications().entrySet().iterator();
while (messagesIt.hasNext()) {
LocalizedNotification notMess = messagesIt.next().getValue();
messageArray.put((Object)notMess.toJSONObject());
}
}
this.addJsonArrayProperty(json, "message", messageArray);
return json;
}
@Override
public Map<String, Object> getProperties() {
HashMap<String, Object> props = new HashMap<String, Object>();
this.addPropertyToMap(props, "id", this.getId());
this.addPropertyToMap(props, "platforms", this.getPlatforms());
this.addPropertyToMap(props, "jcr:title", this.getTitle());
this.addPropertyToMap(props, "jcr:description", this.getDescription());
this.addPropertyToMap(props, "state", this.getState().toString());
this.addPropertyToMap(props, "lastSent", this.getLastSent());
this.addPropertyToMap(props, "targetFilter", this.getFilterParameters());
return Collections.unmodifiableMap(props);
}
public String toString() {
String platformsStr = this.getPlatforms() == null ? "[]" : Arrays.toString(this.getPlatforms());
String message = "";
String notificationTitle = "";
String linkText = "";
String link = "";
if (this.localizedNotifications != null) {
message = Integer.toString(this.localizedNotifications.size());
if (this.localizedNotifications.size() == 1) {
LocalizedNotification localizedNotification = this.localizedNotifications.entrySet().iterator().next().getValue();
message = localizedNotification.getMessage();
notificationTitle = localizedNotification.getTitle();
linkText = localizedNotification.getLinkText();
link = localizedNotification.getLink();
}
}
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String lastSentString = this.lastSentDate == null ? "" : format1.format(this.lastSentDate.getTime());
return "Platforms: " + platformsStr + ", Notification ID: " + this.id + ", Title: " + notificationTitle + ", Message: " + message + ", Link Text: " + linkText + ", Link: " + link + ", State: " + (this.state == null ? "null" : this.state.toString()) + (this.lastSentDate == null ? "" : new StringBuilder().append(", Last Sent: ").append(lastSentString).toString()) + ", Target Filter: " + this.filterParameters + ", App Path: " + this.path;
}
private void addPropertyToMap(Map<String, Object> map, String key, Object value) {
if (value != null) {
map.put(key, value);
}
}
private void addJsonStringProperty(JSONObject json, String key, String value) {
try {
json.put(key, (Object)value);
}
catch (JSONException jsonEx) {
this.logger.warn("Error converting Notification to JSON with value '" + key + "'", (Throwable)jsonEx);
}
}
private void addJsonArrayProperty(JSONObject json, String key, JSONArray value) {
try {
json.put(key, (Object)value);
}
catch (JSONException jsonEx) {
this.logger.warn("Error converting Notification to JSON with value '" + key + "'", (Throwable)jsonEx);
}
}
}