AbstractFDMPrefillService.java 10.5 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aem.dermis.api.IFormDataModelManager
 *  com.adobe.aem.dermis.api.bridge.DataContext
 *  com.adobe.aem.dermis.api.bridge.IDermisBridge
 *  com.adobe.aem.dermis.api.bridge.OperationOptions
 *  com.adobe.aem.dermis.api.bridge.Query
 *  com.adobe.aem.dermis.slingmodel.Entity
 *  com.adobe.aem.dermis.slingmodel.FormDataModel
 *  com.adobe.aem.dermis.util.DermisUtils
 *  com.adobe.aem.dermis.util.SlingJSONWriter
 *  com.adobe.aemds.guide.service.GuideModelTransformer
 *  com.adobe.aemds.guide.transformer.api.ResourcePropertyTransformer
 *  com.adobe.forms.common.service.DataOptions
 *  com.adobe.forms.common.service.PrefillData
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferenceCardinality
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.sling.api.resource.Resource
 *  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.adobe.aemds.guide.addon.fdm.prefill;

import com.adobe.aem.dermis.api.IFormDataModelManager;
import com.adobe.aem.dermis.api.bridge.DataContext;
import com.adobe.aem.dermis.api.bridge.IDermisBridge;
import com.adobe.aem.dermis.api.bridge.OperationOptions;
import com.adobe.aem.dermis.api.bridge.Query;
import com.adobe.aem.dermis.slingmodel.Entity;
import com.adobe.aem.dermis.slingmodel.FormDataModel;
import com.adobe.aem.dermis.util.DermisUtils;
import com.adobe.aem.dermis.util.SlingJSONWriter;
import com.adobe.aemds.guide.addon.fdm.prefill.FDMResourcePropertyTransformer;
import com.adobe.aemds.guide.addon.fdm.prefill.IFDMPrefill;
import com.adobe.aemds.guide.service.GuideModelTransformer;
import com.adobe.aemds.guide.transformer.api.ResourcePropertyTransformer;
import com.adobe.forms.common.service.DataOptions;
import com.adobe.forms.common.service.PrefillData;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.sling.api.resource.Resource;
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;

@Component(immediate=1, metatype=0)
public abstract class AbstractFDMPrefillService
implements IFDMPrefill {
    @Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.OPTIONAL_UNARY)
    private IDermisBridge dermisBridge;
    @Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.OPTIONAL_UNARY)
    protected IFormDataModelManager formDataModelManager;
    @Reference
    private GuideModelTransformer guideModelTransformer;
    private final Logger logger;
    private Map<String, Object> extraParamMap;

    public AbstractFDMPrefillService() {
        this.logger = LoggerFactory.getLogger(this.getClass());
    }

    public abstract PrefillData getPrefillData(DataOptions var1);

    protected String getFormDataModelDamAssetPath(Resource formResource) {
        if (formResource != null) {
            ValueMap formResourceValueMap = null;
            formResourceValueMap = (ValueMap)formResource.adaptTo(ValueMap.class);
            if (formResourceValueMap != null) {
                return (String)formResourceValueMap.get("schemaRef", (Object)"");
            }
        }
        return "";
    }

    protected InputStream getData(DataOptions options) throws Exception {
        String dataRef = options.getDataRef();
        Resource formResource = options.getFormResource();
        this.extraParamMap = options.getExtras();
        String formDataModelDamAssetPath = this.getFormDataModelDamAssetPath(formResource);
        FormDataModel formDataModel = null;
        if (StringUtils.isBlank((CharSequence)formDataModelDamAssetPath)) {
            this.logger.warn("No form data model associated with adaptive form");
            return null;
        }
        try {
            formDataModel = this.formDataModelManager.getFormDataModel(formDataModelDamAssetPath);
        }
        catch (Exception ex) {
            this.logger.warn("Incorrect form data model path set " + formDataModelDamAssetPath, (Throwable)ex);
            return null;
        }
        ByteArrayInputStream result = null;
        if (StringUtils.isEmpty((CharSequence)dataRef) || StringUtils.startsWith((CharSequence)dataRef, (CharSequence)"service")) {
            if (StringUtils.isNotBlank((CharSequence)formDataModelDamAssetPath)) {
                String selectionPoints = this.getSelectionPoints(formResource);
                if (selectionPoints == null) {
                    this.logger.warn("No selection points configured for the adaptive form: " + formResource.getPath());
                    return null;
                }
                OperationOptions operationOptions = new OperationOptions(selectionPoints, this.getEntityPrefillOperationMap(formResource));
                Query query = new Query(formDataModelDamAssetPath, this.getDataContext(), operationOptions);
                if (this.dermisBridge != null) {
                    Object data = this.dermisBridge.executeQuery(query);
                    if (data != null) {
                        data = this.marshallDataWithAFWrapper(data);
                    }
                    if (data instanceof String) {
                        result = new ByteArrayInputStream(((String)data).getBytes(StandardCharsets.UTF_8));
                    }
                }
            } else {
                throw new Exception("No form data model associated with adaptive form " + formResource.getPath());
            }
        }
        return result;
    }

    protected Object marshallDataWithAFWrapper(Object data) {
        HashMap<String, Map> jsonData;
        if (data instanceof Map && (jsonData = (HashMap<String, Map>)data).get("afData") == null) {
            HashMap<K, V> afBoundData = new HashMap<K, V>();
            HashMap<K, V> afUnboundData = new HashMap<K, V>();
            HashMap<String, Map> boundDataObject = new HashMap<String, Map>();
            HashMap<String, HashMap<K, V>> unboundDataObject = new HashMap<String, HashMap<K, V>>();
            for (Map.Entry<K, V> thisEntry : jsonData.entrySet()) {
                String entityName = (String)thisEntry.getKey();
                if (!(jsonData.get(entityName) instanceof List)) continue;
                List jsonList = (List)jsonData.get(entityName);
                Map arrayData = (Map)jsonList.get(0);
                jsonData = new HashMap<String, Map>();
                jsonData.put(entityName, arrayData);
            }
            boundDataObject.put("data", jsonData);
            unboundDataObject.put("data", new HashMap<K, V>());
            HashMap<String, HashMap<K, V>> afData = new HashMap<String, HashMap<K, V>>();
            HashMap<String, HashMap> afDataChildren = new HashMap<String, HashMap>();
            afDataChildren.put("afBoundData", boundDataObject);
            afDataChildren.put("afUnboundData", unboundDataObject);
            afData.put("afData", afDataChildren);
            StringWriter writer = new StringWriter();
            SlingJSONWriter slingJSONWriter = new SlingJSONWriter((Writer)writer);
            slingJSONWriter.write(afData);
            data = writer.toString();
        }
        return data;
    }

    @Override
    public String getSelectionPoints(Resource formResource) throws JSONException {
        String selectionPoints = null;
        FDMResourcePropertyTransformer transformer = new FDMResourcePropertyTransformer();
        this.guideModelTransformer.getAdaptiveFormTreeJSON(formResource, (ResourcePropertyTransformer)transformer);
        JSONObject result = transformer.getResult();
        if (result != null) {
            selectionPoints = result.toString();
        }
        return selectionPoints;
    }

    @Override
    public Map<String, String> getEntityPrefillOperationMap(Resource formResource) throws Exception {
        if (formResource == null) {
            throw new Exception("Form resource cannot be null");
        }
        HashMap<String, String> entityPrefillOperationMap = new HashMap<String, String>();
        String formDataModelDamAssetPath = this.getFormDataModelDamAssetPath(formResource);
        if (StringUtils.isNotEmpty((CharSequence)formDataModelDamAssetPath)) {
            FormDataModel formDataModel = this.formDataModelManager.getFormDataModel(formDataModelDamAssetPath);
            List entityList = formDataModel.getDataModels();
            for (Entity entity : entityList) {
                entityPrefillOperationMap.put(entity.getId(), entity.getDefaultReadOperation());
            }
        }
        return entityPrefillOperationMap;
    }

    @Override
    public DataContext getDataContext() {
        Map wrapperMap = DermisUtils.createRequestDataContext(this.extraParamMap);
        return new DataContext(wrapperMap);
    }

    public abstract String getServiceName();

    public abstract String getServiceDescription();

    protected void bindDermisBridge(IDermisBridge iDermisBridge) {
        this.dermisBridge = iDermisBridge;
    }

    protected void unbindDermisBridge(IDermisBridge iDermisBridge) {
        if (this.dermisBridge == iDermisBridge) {
            this.dermisBridge = null;
        }
    }

    protected void bindFormDataModelManager(IFormDataModelManager iFormDataModelManager) {
        this.formDataModelManager = iFormDataModelManager;
    }

    protected void unbindFormDataModelManager(IFormDataModelManager iFormDataModelManager) {
        if (this.formDataModelManager == iFormDataModelManager) {
            this.formDataModelManager = null;
        }
    }

    protected void bindGuideModelTransformer(GuideModelTransformer guideModelTransformer) {
        this.guideModelTransformer = guideModelTransformer;
    }

    protected void unbindGuideModelTransformer(GuideModelTransformer guideModelTransformer) {
        if (this.guideModelTransformer == guideModelTransformer) {
            this.guideModelTransformer = null;
        }
    }
}