MobileAppsPropertiesOperation.java
7.42 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.ui.components.HtmlResponse
* com.day.cq.i18n.I18n
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* 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.jackrabbit.commons.JcrUtils
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.resource.ModifiableValueMap
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.servlets.post.Modification
*/
package com.adobe.cq.mobile.platform.impl.operations;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceLocator;
import com.adobe.cq.mobile.platform.MobileResourceType;
import com.adobe.cq.mobile.platform.impl.operations.MobileAbstractOperation;
import com.adobe.granite.ui.components.HtmlResponse;
import com.day.cq.i18n.I18n;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
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.jackrabbit.commons.JcrUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.servlets.post.Modification;
@Component(metatype=0, label="AEM Mobile Metric Updater")
@Service
@Properties(value={@Property(name="sling.post.operation", value={"mobileapps:updateProperties"})})
public class MobileAppsPropertiesOperation
extends MobileAbstractOperation {
public static final String OPERATION_NAME = "updateProperties";
@Override
protected void perform(SlingHttpServletRequest request, HtmlResponse response, List<Modification> changes) {
I18n i18n = new I18n((HttpServletRequest)request);
Resource resource = request.getResource();
MobileResource mobileResource = (MobileResource)resource.adaptTo(MobileResource.class);
if (!mobileResource.isA(MobileResourceType.INSTANCE.getType())) {
String message = i18n.get("Unable to resolve mobile application {0}", null, new Object[]{resource.getPath()});
String title = i18n.get("Error");
this.generateError(response, message, title);
return;
}
HashMap<String, String[]> props = new HashMap<String, String[]>();
HashMap<String, String> typeHints = new HashMap<String, String>();
Map params = request.getParameterMap();
Set paramNames = params.keySet();
for (String paramName : paramNames) {
if (paramName.startsWith(":") || paramName.startsWith("_")) continue;
if (paramName.contains("@TypeHint")) {
String key = paramName.replace("@TypeHint", "");
typeHints.put(key, request.getParameter(paramName));
continue;
}
props.put(paramName, request.getParameterValues(paramName));
}
try {
ResourceResolver resolver = request.getResourceResolver();
this.updateFields(mobileResource, resolver, props, typeHints);
resolver.refresh();
Resource resourceInstance = resolver.getResource(mobileResource.getPath());
String widgetTitle = (String)((MobileResource)resourceInstance.adaptTo(MobileResource.class)).getProperties().get("widget/name/text", String.class);
this.setTitle(widgetTitle, mobileResource, request.getResourceResolver());
}
catch (Exception e) {
String message = i18n.get("Unable to update properties");
this.generateError(response, message, "Update Properties Error", e);
}
}
private void updateFields(MobileResource mobileRes, ResourceResolver resolver, Map<String, String[]> props, Map<String, String> typeHints) throws PersistenceException, RepositoryException {
ValueMap mobileProps = mobileRes.getProperties();
Set<String> paramNames = props.keySet();
for (String paramName : paramNames) {
String[] propValue = props.get(paramName);
if (propValue == null || propValue.length <= 0 || !typeHints.containsKey(paramName) && propValue.length == 1 && ((String)mobileProps.get(paramName, (Object)"")).equals(propValue[0])) continue;
String trimmedParamName = null;
int pos = paramName.lastIndexOf(47);
String childPath = "";
if (pos > 0) {
childPath = paramName.substring(0, pos);
trimmedParamName = paramName.substring(pos + 1);
} else if (pos == -1) {
childPath = ".";
trimmedParamName = paramName;
}
Node parentNode = JcrUtils.getOrCreateByPath((String)(mobileRes.getPath() + "/" + childPath), (String)"nt:unstructured", (Session)((Session)resolver.adaptTo(Session.class)));
boolean hasProperty = parentNode.hasProperty(trimmedParamName);
if (propValue == null || propValue.length == 1 && propValue[0].length() == 0) {
if (!hasProperty) continue;
javax.jcr.Property property = parentNode.getProperty(trimmedParamName);
property.setValue((String)null);
continue;
}
String typeHint = null;
if (typeHints.containsKey(paramName)) {
typeHint = typeHints.get(paramName);
}
if (hasProperty) {
javax.jcr.Property property = parentNode.getProperty(trimmedParamName);
property.setValue((String)null);
}
if (propValue.length > 1 || typeHint != null && typeHint.contains("[]")) {
parentNode.setProperty(trimmedParamName, propValue);
continue;
}
parentNode.setProperty(trimmedParamName, propValue[0]);
}
resolver.commit();
}
private void setTitle(String title, MobileResource instance, ResourceResolver resolver) throws RepositoryException, PersistenceException {
MobileResourceLocator locator = (MobileResourceLocator)resolver.adaptTo(MobileResourceLocator.class);
MobileResource group = locator.findAncestorResourceByType((Resource)instance.adaptTo(Resource.class), MobileResourceType.GROUP.getType(), new String[0]);
Resource groupResource = (Resource)group.adaptTo(Resource.class);
Resource content = groupResource.getChild("jcr:content");
ModifiableValueMap groupProps = (ModifiableValueMap)content.adaptTo(ModifiableValueMap.class);
if (StringUtils.isNotBlank((CharSequence)title)) {
groupProps.put((Object)"jcr:title", (Object)title);
}
}
}