LetterDataMerger.java 6.61 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemds.guide.utils.DocumentDataMerger
 *  com.adobe.icc.ddg.api.HTML5ModuleResolverService
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 */
package com.adobe.fd.adaddon.internal.utils;

import com.adobe.aemds.guide.utils.DocumentDataMerger;
import com.adobe.icc.ddg.api.HTML5ModuleResolverService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.w3c.dom.Document;

public class LetterDataMerger
extends DocumentDataMerger {
    private static String ROW_ID_PREFIX = "ROW_ID:";
    private final HTML5ModuleResolverService html5ModuleResolverService;
    private Map<String, Object> fieldsValueMap;
    private Map<String, Object> currentContext;

    public LetterDataMerger(JSONObject guideJson, Document mergeReferenceDocument, Map<String, Object> params, HTML5ModuleResolverService html5ModuleResolverService) {
        super(guideJson, mergeReferenceDocument, params);
        if (guideJson.has("letterRef")) {
            try {
                params.put("letterRef", guideJson.getString("letterRef"));
            }
            catch (Exception e) {
                this.logger.error("Failed to retrieve letter ref from guide json: " + (Object)guideJson, (Throwable)e);
            }
        }
        this.fieldsValueMap = (Map)params.get("FIELD_VALUE_MAP");
        this.currentContext = this.fieldsValueMap;
        this.html5ModuleResolverService = html5ModuleResolverService;
    }

    protected String getComputedBindProperty(JSONObject jsonObject) throws JSONException {
        String bindProp;
        if (jsonObject.has("bindRef") && StringUtils.isNotBlank((CharSequence)(bindProp = jsonObject.getString("bindRef"))) && bindProp.startsWith("FA_ID:")) {
            return bindProp;
        }
        if (jsonObject.has("sling:resourceType") && "fd/af/components/tableRow".equals(jsonObject.getString("sling:resourceType")) && StringUtils.isNotBlank((CharSequence)(bindProp = this.handleDynamicTableCase(jsonObject)))) {
            return bindProp;
        }
        return super.getComputedBindProperty(jsonObject);
    }

    private String handleDynamicTableCase(JSONObject jsonObject) throws JSONException {
        ArrayList<String> faIds = new ArrayList<String>();
        ArrayList<Object> fieldValues = new ArrayList<Object>();
        if (jsonObject.has("maxOccur") && "-1".equals(jsonObject.getString("maxOccur")) && jsonObject.has("items")) {
            JSONObject itemObject = jsonObject.getJSONObject("items");
            Iterator keys = itemObject.keys();
            while (keys.hasNext()) {
                String bindProp;
                JSONObject childJSONObject;
                String key = (String)keys.next();
                Object obj = itemObject.get(key);
                if (!(obj instanceof JSONObject) || !(childJSONObject = (JSONObject)obj).has("bindRef") || !StringUtils.isNotBlank((CharSequence)(bindProp = childJSONObject.getString("bindRef"))) || !bindProp.startsWith("FA_ID:")) continue;
                String faId = StringUtils.substringAfter((String)bindProp, (String)"FA_ID:");
                faIds.add(faId);
                fieldValues.add(this.fieldsValueMap.get(faId));
            }
        }
        if (faIds.size() > 0) {
            String dynamicBindProp = ROW_ID_PREFIX + StringUtils.join(faIds, (String)":");
            if (this.fieldsValueMap.get(dynamicBindProp) == null) {
                List flattenedFieldValueMap = this.html5ModuleResolverService.prepareTableData(fieldValues);
                ArrayList contextValueMap = new ArrayList();
                for (List row : flattenedFieldValueMap) {
                    Iterator faIdIter = faIds.iterator();
                    HashMap<String, String> cellMap = new HashMap<String, String>();
                    for (String col : row) {
                        String faId = (String)faIdIter.next();
                        cellMap.put(faId, col);
                    }
                    contextValueMap.add(cellMap);
                }
                this.fieldsValueMap.put(dynamicBindProp, contextValueMap);
            }
            return dynamicBindProp;
        }
        return null;
    }

    protected String matchPrimitive(String relativePath, boolean isRepeatChild, boolean isRichText) throws Exception {
        return this.matchPrimitive(relativePath, isRepeatChild);
    }

    protected String matchPrimitive(String relativePath, boolean isRepeat) throws Exception {
        if (StringUtils.isNotBlank((CharSequence)relativePath) && relativePath.startsWith("FA_ID:") && this.currentContext != null) {
            String faId = StringUtils.substringAfter((String)relativePath, (String)"FA_ID:");
            Object matchedValue = this.currentContext.get(faId);
            return matchedValue != null ? String.valueOf(this.currentContext.get(faId)) : "";
        }
        return super.matchPrimitive(relativePath, isRepeat);
    }

    protected Object matchComposite(String relativePath, String fullPath) throws Exception {
        if (this.currentContext.get(relativePath) != null) {
            return this.currentContext.get(relativePath);
        }
        return super.matchComposite(relativePath, fullPath);
    }

    protected void setCurrentContext(Object ctx) {
        if (ctx instanceof Map) {
            this.currentContext = (Map)ctx;
            return;
        }
        super.setCurrentContext(ctx);
    }

    protected int getMatchCount(Object match) {
        if (match != null && match instanceof List) {
            List nodeList = (List)match;
            return nodeList.size();
        }
        return super.getMatchCount(match);
    }

    protected Object getMatchItem(Object match, int ith) {
        if (match != null && match instanceof List) {
            List nodeList = (List)match;
            return nodeList.get(ith);
        }
        return super.getMatchItem(match, ith);
    }

    protected void resetContext() {
        this.currentContext = this.fieldsValueMap;
        super.resetContext();
    }

    protected String getRelativePath(String path) {
        if (StringUtils.isNotBlank((CharSequence)path) && path.startsWith(ROW_ID_PREFIX)) {
            return path;
        }
        return super.getRelativePath(path);
    }

    protected Object getCurrentContext() {
        return this.currentContext;
    }
}