DPSPageManagerImpl.java
10.5 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.adapter.AdapterManager
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.osgi.service.event.EventAdmin
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.dps.impl.service;
import com.adobe.cq.mobile.dps.DPSArticle;
import com.adobe.cq.mobile.dps.DPSBanner;
import com.adobe.cq.mobile.dps.DPSCollection;
import com.adobe.cq.mobile.dps.DPSEntity;
import com.adobe.cq.mobile.dps.DPSException;
import com.adobe.cq.mobile.dps.DPSObject;
import com.adobe.cq.mobile.dps.DPSProject;
import com.adobe.cq.mobile.dps.impl.DPSPageManager;
import com.adobe.cq.mobile.dps.impl.utils.MetadataJSONUtil;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import java.util.HashSet;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.adapter.AdapterManager;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.osgi.service.event.EventAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DPSPageManagerImpl
implements DPSPageManager {
private static final Logger LOGGER = LoggerFactory.getLogger(DPSPageManagerImpl.class);
private static String[] UNLINK_PROPERTY_NAMES = new String[]{"dps-id", "dps-lastUploaded", "dps-lastUploadedBy", "dps-lastSynced"};
private Session userSession = null;
private ConfigurationManager configurationManagerService = null;
private AdapterManager adapterManager = null;
protected EventAdmin eventAdminService = null;
public DPSPageManagerImpl(Session userSession, ConfigurationManager configurationManagerService, EventAdmin eventAdminService, AdapterManager adapterManager) {
this.userSession = userSession;
this.configurationManagerService = configurationManagerService;
this.eventAdminService = eventAdminService;
this.adapterManager = adapterManager;
}
@Override
public JSONArray getAEMArticles(DPSProject dpsProject) throws DPSException {
JSONArray articles = new JSONArray();
try {
List<DPSArticle> aemArticles = dpsProject.getArticles();
for (DPSArticle dpsArticle : aemArticles) {
JSONObject articleJSON = MetadataJSONUtil.getArticleJSON(dpsArticle);
this.validateJSONDataForPublishing(articleJSON);
articles.put((Object)articleJSON);
}
}
catch (Exception ex) {
if (dpsProject != null && StringUtils.isNotEmpty((String)dpsProject.getName())) {
throw new DPSException("Failed to get AEM articles for project: " + dpsProject.getName(), ex);
}
throw new DPSException("Failed to get AEM articles.", ex);
}
return articles;
}
@Override
public JSONArray getAEMBanners(DPSProject dpsProject) throws DPSException {
JSONArray entities = new JSONArray();
try {
List<DPSBanner> aemBanners = dpsProject.getBanners();
for (DPSBanner dpsBanner : aemBanners) {
JSONObject bannerJSON = MetadataJSONUtil.getBannerJSON(dpsBanner);
this.validateJSONDataForPublishing(bannerJSON);
entities.put((Object)bannerJSON);
}
}
catch (Exception ex) {
if (dpsProject != null && StringUtils.isNotEmpty((String)dpsProject.getName())) {
throw new DPSException("Failed to get AEM banners for project: " + dpsProject.getName(), ex);
}
throw new DPSException("Failed to get AEM banner.", ex);
}
return entities;
}
@Override
public JSONObject getAEMCollection(DPSCollection dpsCollection, boolean includeReferences, boolean incStatus, boolean incContentDetails) throws DPSException {
JSONObject jsonObject = null;
try {
jsonObject = MetadataJSONUtil.getCollectionJSON(dpsCollection);
}
catch (Exception ex) {
throw new DPSException("Failed to get AEM collection " + dpsCollection.getPath(), ex);
}
return jsonObject;
}
@Override
public JSONArray getAEMCollections(DPSProject dpsProject, boolean flat) throws DPSException {
JSONArray collections = new JSONArray();
try {
JSONObject collectionJSON;
List<DPSCollection> aemCollections = dpsProject.getCollections();
HashSet<String> collectionThatAreNested = new HashSet<String>();
for (DPSCollection dpsCollection : aemCollections) {
JSONArray dpsContentLinks;
collectionJSON = MetadataJSONUtil.getCollectionJSON(dpsCollection);
this.validateJSONDataForPublishing(collectionJSON);
collections.put((Object)collectionJSON);
if (flat || (dpsContentLinks = dpsCollection.getContentLinks()).length() <= 0) continue;
for (int i = 0; i < dpsContentLinks.length(); ++i) {
Object next = dpsContentLinks.get(i);
if (next instanceof JSONObject) {
JSONObject linkJSON = (JSONObject)next;
String name = linkJSON.getString("name");
String type = linkJSON.getString("type");
if (type.equals("collection")) {
collectionThatAreNested.add(name);
continue;
}
if (type.equals("article") || type.equals("banner")) continue;
LOGGER.warn("Invalid link type " + type);
continue;
}
LOGGER.warn("Invalid link entry " + next);
}
}
if (!flat) {
JSONArray rootCollections = new JSONArray();
for (int i = 0; i < collections.length(); ++i) {
collectionJSON = collections.getJSONObject(i);
if (collectionThatAreNested.contains(collectionJSON.getString("name"))) continue;
rootCollections.put((Object)collectionJSON);
}
collections = rootCollections;
}
}
catch (Exception ex) {
if (dpsProject != null && StringUtils.isNotEmpty((String)dpsProject.getName())) {
throw new DPSException("Failed to get AEM collections for project: " + dpsProject.getName(), ex);
}
throw new DPSException("Failed to get AEM collections.", ex);
}
return collections;
}
@Override
public void unlink(DPSObject dpsObject) throws DPSException {
try {
if (dpsObject instanceof DPSProject) {
LOGGER.info("Unlinking content from project " + dpsObject.getPath());
DPSProject dpsProject = (DPSProject)dpsObject;
int unlinked = 0;
unlinked += this.unlinkContent(dpsProject.getCollections());
unlinked += this.unlinkContent(dpsProject.getArticles());
LOGGER.info("Unlinked {} entities from {}", (Object)(unlinked += this.unlinkContent(dpsProject.getBanners())), (Object)dpsObject.getPath());
} else if (dpsObject instanceof DPSEntity) {
this.unlinkImpl((DPSEntity)dpsObject);
}
}
catch (Exception dpsEx) {
throw new DPSException("Failed to unlink " + dpsObject.getPath());
}
}
private int unlinkContent(List<? extends DPSEntity> entities) throws RepositoryException {
int unlinked = 0;
for (DPSEntity entity : entities) {
boolean cleaned;
if (entity.getId() == null || !(cleaned = this.unlinkImpl(entity))) continue;
++unlinked;
}
return unlinked;
}
private boolean unlinkImpl(DPSEntity entity) throws RepositoryException {
boolean cleaned = false;
if (!entity.isImported()) {
LOGGER.info("Unlinking " + entity.getPath());
Node node = (Node)entity.adaptTo(Node.class);
if (node.hasNode("jcr:content")) {
node = node.getNode("jcr:content");
for (String key : UNLINK_PROPERTY_NAMES) {
if (!node.hasProperty(key)) continue;
node.getProperty(key).setValue((String)null);
cleaned = true;
LOGGER.debug("Cleaned up {} for {}", (Object)key, (Object)node.getPath());
}
}
} else {
LOGGER.debug("Skipping unlinking of imported entity {} ", (Object)entity.getPath());
}
return cleaned;
}
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
if (Session.class == type) {
return (AdapterType)this.userSession;
}
return null;
}
private void validateJSONDataForPublishing(JSONObject json) throws JSONException {
String type = json.getString("entityType");
JSONObject validationError = new JSONObject();
JSONArray missingProperties = new JSONArray();
if (!type.isEmpty()) {
if ("collection".equals(type)) {
if (!json.has("productId")) {
missingProperties.put((Object)"productId");
}
if (!json.has("image") && "AEM".equals(json.getString("source"))) {
missingProperties.put((Object)"image");
}
if (!json.has("layout")) {
missingProperties.put((Object)"layout");
}
} else if ("banner".equals(type)) {
if (!json.has("image")) {
missingProperties.put((Object)"image");
}
} else if ("article".equals(type) && !json.has("image")) {
missingProperties.put((Object)"image");
}
if (missingProperties.length() > 0) {
validationError.put("missingProperties", (Object)missingProperties);
json.put("validations", (Object)validationError);
}
}
}
}