TargetRecommendationImpl.java
6.69 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* 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.targetrecommendations.impl.model;
import com.adobe.cq.targetrecommendations.api.TargetRecommendationsException;
import com.adobe.cq.targetrecommendations.api.model.TargetRecommendation;
import com.adobe.cq.targetrecommendations.impl.util.TargetRecommendationsJSONUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
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 TargetRecommendationImpl
implements TargetRecommendation {
private static final Logger LOG = LoggerFactory.getLogger(TargetRecommendationImpl.class);
private int id;
private String name;
private int defaultContent;
private int catalogId;
private TargetRecommendation.RecommendationState state;
private String locationMBox;
private List<String> templates;
private List<Integer> algorithms;
private String startDate;
private String endDate;
public TargetRecommendationImpl(int id, String name, String stateString, int catalogId, String locationMBox, List<String> templates, List<Integer> algorithms, String startDate, String endDate, int defaultContent) {
this.id = id;
this.name = name;
this.catalogId = catalogId;
this.locationMBox = locationMBox;
this.templates = templates;
this.algorithms = algorithms;
this.startDate = startDate;
this.endDate = endDate;
this.defaultContent = defaultContent;
try {
this.state = TargetRecommendation.RecommendationState.valueOf(stateString.toUpperCase());
}
catch (Exception e) {
LOG.warn("Invalid Target recommendation state string, defaulting to 'inactive'!");
this.state = TargetRecommendation.RecommendationState.INACTIVE;
}
}
@Override
public int getId() {
return this.id;
}
@Override
public String getName() {
return this.name;
}
@Override
public int getCatalogId() {
return this.catalogId;
}
@Override
public List<String> getTemplateNames() {
return this.templates;
}
@Override
public TargetRecommendation.RecommendationState getState() {
return this.state;
}
@Override
public List<Integer> getAlgorithmIds() {
return this.algorithms;
}
@Override
public String getLocationDisplayMbox() {
return this.locationMBox;
}
@Override
public int getDefaultContentId() {
return this.defaultContent;
}
@Override
public String getStartDate() {
return this.startDate;
}
@Override
public String getEndDate() {
return this.endDate;
}
@Override
public String toJson() throws TargetRecommendationsException {
try {
JSONObject recJsonRepresentation = new JSONObject();
if (this.getId() > 0) {
recJsonRepresentation.put("id", this.getId());
}
recJsonRepresentation.put("name", (Object)this.getName());
recJsonRepresentation.put("state", (Object)this.getState().toString().toLowerCase());
recJsonRepresentation.put("catalogId", (Object)(this.getCatalogId() > 0 ? Integer.valueOf(this.getCatalogId()) : null));
recJsonRepresentation.put("algorithmIds", this.getAlgorithmIds());
recJsonRepresentation.put("templates", this.getTemplateNames());
recJsonRepresentation.put("location", (Object)new JSONObject().put("displayMbox", (Object)this.getLocationDisplayMbox()));
JSONObject configJson = new JSONObject();
if (this.getStartDate() != null) {
configJson.put("start", (Object)this.getStartDate());
}
if (this.getEndDate() != null) {
configJson.put("end", (Object)this.getEndDate());
}
if (this.getDefaultContentId() >= 0) {
configJson.put("defaultContent", (Object)new JSONObject().put("percentage", this.getDefaultContentId()));
}
if (configJson.length() > 0) {
recJsonRepresentation.put("configuration", (Object)configJson);
}
return recJsonRepresentation.toString();
}
catch (Exception e) {
throw new TargetRecommendationsException(e);
}
}
public static TargetRecommendation fromJson(JSONObject recommendationJson) throws JSONException {
int recId = -1;
if (recommendationJson.has("id")) {
recId = recommendationJson.getInt("id");
}
String recName = recommendationJson.getString("name");
int recCatalogId = recommendationJson.has("catalogId") ? recommendationJson.getInt("catalogId") : -1;
String recState = recommendationJson.has("state") ? recommendationJson.getString("state") : null;
String recLocationMBox = recommendationJson.getJSONObject("location").getString("displayMbox");
JSONObject configJson = recommendationJson.has("configuration") ? recommendationJson.getJSONObject("configuration") : new JSONObject();
int recDefaultContent = 0;
if (configJson.has("defaultContent")) {
try {
JSONObject defaultContentJson = configJson.getJSONObject("defaultContent");
if (defaultContentJson.has("percentage")) {
recDefaultContent = defaultContentJson.getInt("percentage");
}
}
catch (JSONException jse) {
recDefaultContent = configJson.getInt("defaultContent");
}
}
String recStartDate = configJson.has("start") ? configJson.getString("start") : null;
String recEndDate = configJson.has("end") ? configJson.getString("end") : null;
ArrayList<Integer> recAlgorithms = recommendationJson.has("algorithmIds") ? TargetRecommendationsJSONUtil.unmarshall(recommendationJson.getJSONArray("algorithmIds"), Integer.class) : new ArrayList<Integer>();
ArrayList<String> recTemplates = recommendationJson.has("templates") ? TargetRecommendationsJSONUtil.unmarshall(recommendationJson.getJSONArray("templates"), String.class) : new ArrayList<String>();
return new TargetRecommendationImpl(recId, recName, recState, recCatalogId, recLocationMBox, recTemplates, recAlgorithms, recStartDate, recEndDate, recDefaultContent);
}
}