ProductFeedImpl.java
7.92 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
*/
package com.adobe.cq.targetrecommendations.impl.model;
import com.adobe.cq.targetrecommendations.api.TargetRecommendationsException;
import com.adobe.cq.targetrecommendations.api.model.ProductFeed;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
public class ProductFeedImpl
implements ProductFeed {
private static final String GOOGLE_FEED_TYPE_STR = "google-atom-1";
private static final String CSV_FEED_TYPE_STR = "csv";
private static final String SAINT_FEED_TYPE_STR = "saint";
private static final Map<String, ProductFeed.FeedType> FEED_MAPPING = new HashMap<String, ProductFeed.FeedType>();
private static final Map<ProductFeed.FeedSchedule, String> FEED_FREQUENCY_MAPPING;
private int id;
private String name;
private ProductFeed.FeedType type;
private ProductFeed.FeedSchedule updateSchedule;
private int environmentId;
private Map<String, String> attributeMapping;
private Map<String, String> connectionProperties;
public ProductFeedImpl(int id, String name, ProductFeed.FeedType type, ProductFeed.FeedSchedule updateSchedule, int environmentId, Map<String, String> attributeMapping, Map<String, String> connectionProperties) {
this.id = id;
this.name = name;
this.type = type;
this.updateSchedule = updateSchedule;
this.environmentId = environmentId;
this.attributeMapping = attributeMapping;
this.connectionProperties = connectionProperties;
}
@Override
public int getId() {
return this.id;
}
@Override
public String getName() {
return this.name;
}
@Override
public int getEnvironmentId() {
return this.environmentId;
}
@Override
public ProductFeed.FeedType getType() {
return this.type;
}
@Override
public ProductFeed.FeedSchedule getSchedule() {
return this.updateSchedule;
}
@Override
public Map<String, String> getAttributeMapping() {
return this.attributeMapping;
}
@Override
public Map<String, String> getConnectionProperties() {
return this.connectionProperties;
}
@Override
public String toJson() throws TargetRecommendationsException {
try {
JSONObject feedJsonRepresentation = new JSONObject();
if (this.getId() > 0) {
feedJsonRepresentation.put("id", this.getId());
}
feedJsonRepresentation.put("environmentId", this.getEnvironmentId());
feedJsonRepresentation.put("name", (Object)this.getName());
feedJsonRepresentation.put("scheduleType", (Object)FEED_FREQUENCY_MAPPING.get((Object)this.getSchedule()));
feedJsonRepresentation.put("feedType", (Object)ProductFeedImpl.getRecsFeedTypeString(this.getType()));
JSONObject attributeMappingsJson = new JSONObject();
for (Map.Entry<String, String> attributeEntry : this.getAttributeMapping().entrySet()) {
attributeMappingsJson.put(attributeEntry.getKey(), (Object)attributeEntry.getValue());
}
feedJsonRepresentation.put("attributeMapping", (Object)attributeMappingsJson);
JSONObject connectionPropertiesJson = new JSONObject();
String connectionType = this.getConnectionProperties().get("type");
for (Map.Entry<String, String> connectionPropEntry : this.getConnectionProperties().entrySet()) {
if ("type".equalsIgnoreCase(connectionPropEntry.getKey())) continue;
connectionPropertiesJson.put(connectionPropEntry.getKey(), (Object)connectionPropEntry.getValue());
}
JSONObject connectionJson = new JSONObject();
connectionJson.put("type", (Object)connectionType);
connectionJson.put("attributes", (Object)connectionPropertiesJson);
feedJsonRepresentation.put("connection", (Object)connectionJson);
return feedJsonRepresentation.toString();
}
catch (Exception e) {
throw new TargetRecommendationsException(e);
}
}
public static ProductFeed fromJson(JSONObject feedJson) throws JSONException {
int feedId = -1;
if (feedJson.has("id")) {
feedId = feedJson.getInt("id");
}
String feedName = feedJson.getString("name");
int feedEnvironmentId = -1;
if (feedJson.has("environmentId")) {
feedEnvironmentId = feedJson.getInt("environmentId");
}
String feedTypeStr = feedJson.getString("feedType");
ProductFeed.FeedType feedType = FEED_MAPPING.get(feedTypeStr);
String feedUpdateFrequencyStr = feedJson.getString("scheduleType");
ProductFeed.FeedSchedule feedUpdateSchedule = ProductFeedImpl.getFeedScheduleFromString(feedUpdateFrequencyStr);
HashMap<String, String> attributeMappings = new HashMap<String, String>();
HashMap<String, String> connectionConfig = new HashMap<String, String>();
if (feedJson.has("attributeMapping")) {
JSONObject attributeMapJson = feedJson.getJSONObject("attributeMapping");
Iterator attributeIterator = attributeMapJson.keys();
while (attributeIterator.hasNext()) {
String attributeName = (String)attributeIterator.next();
String attributeMapValue = attributeMapJson.getString(attributeName);
attributeMappings.put(attributeName, attributeMapValue);
}
}
if (feedJson.has("connection")) {
JSONObject connectionJson = feedJson.getJSONObject("connection");
connectionConfig.put("type", connectionJson.getString("type"));
JSONObject connectionConfigJson = connectionJson.getJSONObject("attributes");
Iterator connectionConfigIterator = connectionConfigJson.keys();
while (connectionConfigIterator.hasNext()) {
String attributeName = (String)connectionConfigIterator.next();
String attributeMapValue = connectionConfigJson.getString(attributeName);
connectionConfig.put(attributeName, attributeMapValue);
}
}
return new ProductFeedImpl(feedId, feedName, feedType, feedUpdateSchedule, feedEnvironmentId, attributeMappings, connectionConfig);
}
private static String getRecsFeedTypeString(ProductFeed.FeedType feedType) {
switch (feedType) {
case GOOGLE_PRODUCT: {
return "google-atom-1";
}
case CSV: {
return "csv";
}
case SITECATALYST_SAINT: {
return "saint";
}
}
return "";
}
private static ProductFeed.FeedSchedule getFeedScheduleFromString(String feedScheduleStr) {
for (Map.Entry<ProductFeed.FeedSchedule, String> feedFrequencyEntry : FEED_FREQUENCY_MAPPING.entrySet()) {
if (!feedScheduleStr.equalsIgnoreCase(feedFrequencyEntry.getValue())) continue;
return feedFrequencyEntry.getKey();
}
return ProductFeed.FeedSchedule.NEVER;
}
static {
FEED_MAPPING.put("google-atom-1", ProductFeed.FeedType.GOOGLE_PRODUCT);
FEED_MAPPING.put("csv", ProductFeed.FeedType.CSV);
FEED_MAPPING.put("saint", ProductFeed.FeedType.SITECATALYST_SAINT);
FEED_FREQUENCY_MAPPING = new HashMap<ProductFeed.FeedSchedule, String>();
FEED_FREQUENCY_MAPPING.put(ProductFeed.FeedSchedule.WEEKLY, "weekly");
FEED_FREQUENCY_MAPPING.put(ProductFeed.FeedSchedule.BI_WEEKLY, "biweekly");
FEED_FREQUENCY_MAPPING.put(ProductFeed.FeedSchedule.DAILY, "daily");
FEED_FREQUENCY_MAPPING.put(ProductFeed.FeedSchedule.NEVER, "never");
}
}