MobileAppsPropertiesOperation.java 7.42 KB
/*
 * 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);
        }
    }
}