MobileAppsCollectionCreationOperation.java
6.82 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.mobile.platform.MobileResource
* com.adobe.cq.mobile.platform.MobileResourceLocator
* com.adobe.cq.mobile.platform.MobileResourceType
* com.adobe.granite.ui.components.HtmlResponse
* com.adobe.granite.xss.XSSAPI
* com.day.cq.i18n.I18n
* com.day.cq.wcm.api.Page
* com.day.text.Text
* javax.servlet.http.HttpServletRequest
* org.apache.commons.lang3.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.request.RequestParameter
* org.apache.sling.api.request.RequestPathInfo
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.servlets.post.Modification
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.dps.impl.operations;
import com.adobe.cq.mobile.dps.impl.DPSClient;
import com.adobe.cq.mobile.dps.impl.DPSEntityImporter;
import com.adobe.cq.mobile.dps.impl.operations.MobilePublishAbstractOperation;
import com.adobe.cq.mobile.dps.impl.utils.EntityCreationUtil;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceLocator;
import com.adobe.cq.mobile.platform.MobileResourceType;
import com.adobe.granite.ui.components.HtmlResponse;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.api.Page;
import com.day.text.Text;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.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.request.RequestParameter;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.servlets.post.Modification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0, label="Experience Manager Mobile Collection Creator")
@Service
@Properties(value={@Property(name="sling.post.operation", value={"dpsapps:createCollection"}), @Property(name="sling.servlet.methods", value={"POST"})})
public class MobileAppsCollectionCreationOperation
extends MobilePublishAbstractOperation {
private static final String PARAM_TEMPLATE = "template";
private static final String PARAM_COLLECTION_NAME = "collectionName";
private static final String PARAM_COLLECTION_IMAGE = "dps-collectionImage";
private static final String PARAM_COLLECTION_BACKGROUND_IMAGE = "dps-collectionBackgroundImage";
private static final Logger LOGGER = LoggerFactory.getLogger(MobileAppsCollectionCreationOperation.class);
private static final String CQ_APPS_ADMIN_CREATECOLLECTION_EDIT = "cq-apps-admin-createcollection-edit";
private final String[] allowedProperties = new String[]{"dps-title", "dps-shortTitle", "dps-abstract", "dps-shortAbstract", "dps-productId", "dps-readingPosition", "dps-horizontalSwipe", "dps-allowDownload", "dps-openDefault", "dps-layout", "dps-layoutTitle", "dps-department", "dps-category", "dps-keywords", "dps-internalKeywords", "dps-importance", "pge-type"};
private final String[] mandatoryParameters = new String[]{"template", "dps-title", "collectionName", "dps-layout"};
@Override
protected void perform(SlingHttpServletRequest request, HtmlResponse response, List<Modification> modifications) {
MobileResource group;
I18n i18n = new I18n((HttpServletRequest)request);
String errorTitle = i18n.get("Error");
Page newCollection = null;
String appInstanceSuffix = request.getRequestPathInfo().getSuffix();
ResourceResolver resolver = request.getResourceResolver();
MobileResourceLocator locator = (MobileResourceLocator)resolver.adaptTo(MobileResourceLocator.class);
Resource appPublishInstance = resolver.getResource(appInstanceSuffix);
ArrayList<String> warnings = new ArrayList<String>();
try {
group = locator.findClosestResourceByType(appPublishInstance, MobileResourceType.GROUP.getType(), new String[0]);
String title = request.getRequestParameter("dps-title").getString("utf-8");
newCollection = EntityCreationUtil.createEntity(request, this.allowedProperties, this.mandatoryParameters, this.getDPSClient(request), false, "dps:Collection".substring(4), "collections", "collectionName", group, title, warnings);
}
catch (Exception dpsEx) {
this.generateError(response, dpsEx.getMessage(), errorTitle);
return;
}
for (int i = 0; i < warnings.size(); ++i) {
this.addLink(response, "cq-apps-dps-create-entity-warning-" + i, "", warnings.get(i));
}
try {
EntityCreationUtil.saveProperties(request, newCollection, this.allowedProperties, "dps:Collection", this.getDPSClient(request), this.getDPSEntityImporter(request), group);
}
catch (Exception dpsEx) {
this.generateError(response, dpsEx.getMessage(), errorTitle);
return;
}
try {
EntityCreationUtil.setImage(request, newCollection, "dps:Collection".substring(4), "dps-collectionImage", "image");
}
catch (Exception dpsEx) {
this.generateError(response, dpsEx.getMessage(), errorTitle);
return;
}
try {
EntityCreationUtil.setImage(request, newCollection, "dps:Collection".substring(4), "dps-collectionBackgroundImage", "background-image");
}
catch (Exception dpsEx) {
this.generateError(response, dpsEx.getMessage(), errorTitle);
return;
}
String redirectTo = this.getRedirect(request);
if (StringUtils.isBlank((CharSequence)redirectTo)) {
redirectTo = "/libs/mobileapps/admin/content/dashboard.html" + Text.escapePath((String)group.getPath());
}
response.setPath(newCollection.getPath());
response.setLocation(appInstanceSuffix);
response.addLink("cq-apps-admin-createcollection-edit", this.xssAPI.getValidHref("/libs/mobileapps/dps/admin/content/dashboard/collections.html" + newCollection.getPath()), i18n.get("View Collection"));
String title = i18n.get("Collection created");
String message = i18n.get("Your Collection, {0}, has been created.", "Collection Title", new Object[]{newCollection.getTitle()});
this.generateResponse(response, 201, message, title, redirectTo, i18n.get("Done"));
}
}