ParameterMapper.java 8.06 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.foundation.forms.FormsHelper
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.request.RequestParameter
 *  org.apache.sling.api.request.RequestParameterMap
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.mcm.campaign.servlets.util;

import com.day.cq.mcm.campaign.CampaignException;
import com.day.cq.mcm.campaign.servlets.util.EncryptedPKParameter;
import com.day.cq.mcm.campaign.servlets.util.Mapping;
import com.day.cq.wcm.foundation.forms.FormsHelper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.request.RequestParameterMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ParameterMapper {
    private static final String PRM_RECON_KEY = "key";
    private static final String PRM_RECON_KEY_VALUE = "keyValue";
    private static final String PRM_DATA = "data";
    private final Logger log;
    private final HashMap<Mapping, String> values;

    public ParameterMapper() {
        this.log = LoggerFactory.getLogger(this.getClass());
        this.values = new HashMap();
    }

    public void parseParameters(SlingHttpServletRequest request, Resource form) {
        String value;
        Iterator formElements = FormsHelper.getFormElements((Resource)form);
        ArrayList<Mapping> mappings = new ArrayList<Mapping>();
        while (formElements.hasNext()) {
            String name;
            Resource fieldResource = (Resource)formElements.next();
            ValueMap props = ResourceUtil.getValueMap((Resource)fieldResource);
            boolean isEncryptedPK = ResourceUtil.isA((Resource)fieldResource, (String)"mcm/campaign/components/form/profile/encryptedPK");
            if (isEncryptedPK) {
                if (!props.containsKey((Object)"name")) continue;
                name = (String)props.get("name", String.class);
                mappings.add(new Mapping(name, (String)props.get("urlParameter", String.class)));
                continue;
            }
            if (!props.containsKey((Object)"name") || !props.containsKey((Object)"acMapping")) continue;
            name = (String)props.get("name", String.class);
            String mapping = (String)props.get("acMapping", String.class);
            boolean isReconciliationKey = "true".equals(props.get("acReconciliationKey", String.class));
            Object[] arrobject = new String[3];
            arrobject[0] = name;
            arrobject[1] = mapping;
            arrobject[2] = isReconciliationKey ? "reconcKey" : "";
            this.log.debug("Mapping: {} {} {}", arrobject);
            Mapping def = new Mapping(name, mapping, isReconciliationKey);
            if (isReconciliationKey) {
                def.setOriginalValue(request.getParameter(":original:" + name));
            }
            mappings.add(def);
        }
        RequestParameterMap prms = request.getRequestParameterMap();
        Set keys = prms.keySet();
        this.values.clear();
        block1 : for (String key : keys) {
            for (Mapping mapping : mappings) {
                if (!mapping.getName().equals(key)) continue;
                value = prms.getValue(key).getString();
                this.log.debug("Value for mapping '{}': {}", (Object[])new String[]{mapping.getMapping(), value});
                this.values.put(mapping, value);
                continue block1;
            }
        }
        for (Mapping mapping : mappings) {
            if (this.values.containsKey(mapping)) continue;
            String name = mapping.getName();
            RequestParameter falsePrm = request.getRequestParameter(":false:" + name);
            if (falsePrm == null) continue;
            value = falsePrm.getString();
            this.values.put(mapping, value);
            this.log.debug("Enhancing mapping '{}' with value '{}'", (Object[])new String[]{mapping.getMapping(), value});
        }
    }

    private String getRecipientData(Mapping mapping) {
        String mappingId = mapping.getMapping();
        int sepPos = mappingId.indexOf(46);
        if (sepPos >= 0) {
            mappingId = mappingId.substring(sepPos + 1);
        }
        return mappingId;
    }

    public EncryptedPKParameter getEncryptedPKParameter() {
        for (Map.Entry<Mapping, String> entry : this.values.entrySet()) {
            Mapping mapping = entry.getKey();
            if (!mapping.isEncryptedPK()) continue;
            return new EncryptedPKParameter(mapping.getMapping(), entry.getValue());
        }
        return null;
    }

    public void addEncryptedPK(Map<String, String> parameters) {
        for (Map.Entry<Mapping, String> toProcess : this.values.entrySet()) {
            Mapping mapping = toProcess.getKey();
            if (!mapping.isEncryptedPK()) continue;
            String value = toProcess.getValue();
            this.log.debug("Encrypted PK: {}", (Object)value);
            parameters.put("encryptedPK", value);
            return;
        }
    }

    public void addReconciliationKey(Map<String, String> parameters, boolean useOriginalValues) {
        StringBuilder reconKey = new StringBuilder();
        StringBuilder reconKeyValue = new StringBuilder();
        for (Map.Entry<Mapping, String> toProcess : this.values.entrySet()) {
            String keyValue;
            Mapping mapping = toProcess.getKey();
            if (!mapping.isReconciliationKey() || (keyValue = useOriginalValues ? mapping.getOriginalValue() : toProcess.getValue()) == null) continue;
            if (reconKey.length() > 0) {
                reconKey.append(",");
            }
            String dataId = this.getRecipientData(mapping);
            reconKey.append(dataId);
            if (reconKeyValue.length() > 0) {
                reconKeyValue.append(":");
            }
            reconKeyValue.append(keyValue);
        }
        String key = reconKey.toString();
        String keyValue = reconKeyValue.toString();
        this.log.debug("Reconciliation key: {} = {}", (Object[])new String[]{key, keyValue});
        parameters.put("key", key);
        parameters.put("keyValue", keyValue);
    }

    public void addData(Map<String, String> parameters) throws CampaignException {
        JSONObject json = new JSONObject();
        try {
            for (Map.Entry<Mapping, String> toProcess : this.values.entrySet()) {
                Mapping mapping = toProcess.getKey();
                if (mapping.isEncryptedPK()) continue;
                String dataId = mapping.getMapping();
                if (!dataId.startsWith("context.")) {
                    dataId = this.getRecipientData(mapping);
                }
                String[] hierarchy = dataId.split("\\.");
                int levels = hierarchy.length;
                JSONObject insertObj = json;
                for (int h = 0; h < levels - 1; ++h) {
                    String key = hierarchy[h];
                    if (!insertObj.has(key)) {
                        insertObj.put(key, (Object)new JSONObject());
                    }
                    insertObj = insertObj.getJSONObject(key);
                }
                insertObj.put(hierarchy[levels - 1], (Object)toProcess.getValue());
            }
        }
        catch (JSONException je) {
            throw new CampaignException("Could not create JSON representation", (Throwable)je);
        }
        String jsonStr = json.toString();
        this.log.debug("Data JSON: {}", (Object)jsonStr);
        parameters.put("data", jsonStr);
    }
}