EntityImportOperation.java
7.48 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.ui.components.HtmlResponse
* com.day.cq.i18n.I18n
* javax.servlet.http.HttpServletRequest
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.resource.Resource
* org.apache.sling.servlets.post.Modification
* org.slf4j.Logger
*/
package com.adobe.cq.mobile.dps.impl.operations;
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.DPSEntityImporter;
import com.adobe.cq.mobile.dps.impl.ImportStatus;
import com.adobe.cq.mobile.dps.impl.operations.MobilePublishAbstractOperation;
import com.adobe.granite.ui.components.HtmlResponse;
import com.day.cq.i18n.I18n;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.servlets.post.Modification;
import org.slf4j.Logger;
@Component(metatype=0, label="Experience Manager Mobile Import")
@Service
@Properties(value={@Property(name="sling.post.operation", value={"dpsapps:dpsImport"}), @Property(name="sling.servlet.methods", value={"POST"})})
public class EntityImportOperation
extends MobilePublishAbstractOperation {
private static final String ENTITY_TYPE_ALL = "ALL";
private static final String ENTITY_TYPE_ARTICLES = "articles";
private static final String ENTITY_TYPE_BANNERS = "banners";
private static final String ENTITY_TYPE_COLLECTIONS = "collections";
private static final String[] validTypes = new String[]{"ALL", "articles", "banners", "collections"};
private static final String PARAM_ENTITY_TYPE = "entityType";
@Override
protected void perform(SlingHttpServletRequest request, HtmlResponse response, List<Modification> modifications) {
I18n i18n = new I18n((HttpServletRequest)request);
Resource resource = request.getResource();
String errorTitle = i18n.get("Error");
String responseTitle = i18n.get("Experience Manager Mobile Items imported");
String responseMessage = "";
try {
DPSObject dpsObject = this.getDPSObject(request);
if (dpsObject == null) {
String message = i18n.get("Invalid dps resource {0}", "resource path", new Object[]{resource.getPath()});
throw new DPSException(message);
}
if (dpsObject instanceof DPSProject) {
ImportStatus importStatus;
DPSProject dpsProject = (DPSProject)dpsObject;
String importType = request.getParameter("entityType");
if (StringUtils.isEmpty((String)importType)) {
String message = i18n.get("Missing mandatory parameter: {0}", "parameter name", new Object[]{"entityType"});
this.generateError(response, message, errorTitle);
return;
}
if (!Arrays.asList(validTypes).contains(importType)) {
String validList = StringUtils.join((Object[])validTypes, (String)", ");
String message = i18n.get("Invalid entityType parameter: {0}. Should be one of {1}", "parameter name", new Object[]{importType, validList});
this.generateError(response, message, errorTitle);
return;
}
String entitiesName = this.getItemsString(i18n, importType);
boolean all = importType.equals("ALL");
DPSEntityImporter dpsEntityImporter = this.getDPSEntityImporter(request);
ImportStatus cummulativeImportStatus = new ImportStatus();
if (all || importType.equals("articles")) {
importStatus = dpsEntityImporter.importDPSArticles(dpsProject);
cummulativeImportStatus.merge(importStatus);
}
if (all || importType.equals("banners")) {
importStatus = dpsEntityImporter.importDPSBanners(dpsProject);
cummulativeImportStatus.merge(importStatus);
}
if (all || importType.equals("collections")) {
importStatus = dpsEntityImporter.importDPSCollections(dpsProject);
cummulativeImportStatus.merge(importStatus);
}
if (!cummulativeImportStatus.getSkipped().isEmpty()) {
List badNames = cummulativeImportStatus.getSkipped();
String status = "" + badNames.size() + "/" + cummulativeImportStatus.getTotalProcessed();
throw new DPSException("Failed to import " + status + ". The following have invalid names and could not be imported; " + StringUtils.join((Object[])badNames.toArray()));
}
responseTitle = i18n.get("Experience Manager Mobile Items imported");
responseMessage = i18n.get("{0} for {1} have been imported.", "plural, localized entity name (i.e. Articles) and project path", new Object[]{entitiesName, dpsProject.getPath()});
} else if (dpsObject instanceof DPSEntity) {
DPSEntityImporter dpsEntityImporter = this.getDPSEntityImporter(request);
ImportStatus importStatus = dpsEntityImporter.importDPSEntity((DPSEntity)dpsObject);
responseTitle = i18n.get("Experience Manager Mobile Item imported");
responseMessage = i18n.get("{0} has been imported.", "entity title", new Object[]{dpsObject.getTitle()});
}
response.setPath(resource.getPath());
this.generateResponse(response, 200, responseMessage, responseTitle, request.getPathInfo(), "");
}
catch (Exception ex) {
String cause = this.getCauseMessage(ex);
if (StringUtils.isEmpty((String)cause)) {
cause = ex.getLocalizedMessage();
}
if (StringUtils.isEmpty((String)cause)) {
cause = ex.getClass().getName();
}
String resourceName = resource == null ? "" : resource.getName();
String message = i18n.get("Error importing {0}: {1}.", "plural, localized entity name (i.e. Articles), project name and root cause", new Object[]{resourceName, cause});
this.getLogger().error(message, (Throwable)ex);
this.generateError(response, message, errorTitle, ex);
}
}
private String getItemsString(I18n i18n, String importType) {
String entitiesName = i18n.get("Items");
if (importType.equals("ALL")) {
entitiesName = i18n.get("All entities");
} else if (importType.equals("articles")) {
entitiesName = i18n.get("Articles");
} else if (importType.equals("banners")) {
entitiesName = i18n.get("Banners");
} else if (importType.equals("collections")) {
entitiesName = i18n.get("Collections");
}
return entitiesName;
}
}