DDModelTransformerImpl.java 12.1 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemds.guide.service.DDModelTransformer
 *  com.adobe.aemds.guide.service.GuideException
 *  com.adobe.dct.service.DataDictionaryRegistryService
 *  com.adobe.dct.transfer.DataDictionary
 *  com.adobe.dct.transfer.DataDictionaryElement
 *  com.adobe.dct.transfer.DataDictionaryElementSubType
 *  com.adobe.dct.transfer.DataDictionaryElementType
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONObject
 */
package com.adobe.fd.adaddon.service.impl;

import com.adobe.aemds.guide.service.DDModelTransformer;
import com.adobe.aemds.guide.service.GuideException;
import com.adobe.dct.service.DataDictionaryRegistryService;
import com.adobe.dct.transfer.DataDictionary;
import com.adobe.dct.transfer.DataDictionaryElement;
import com.adobe.dct.transfer.DataDictionaryElementSubType;
import com.adobe.dct.transfer.DataDictionaryElementType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONObject;

@Component(immediate=1, label="Adaptive Document DD Json Transformer", description="Adaptive Document DD Json Transformer")
@Service(value={DDModelTransformer.class})
public class DDModelTransformerImpl
implements DDModelTransformer {
    @Reference
    private DataDictionaryRegistryService ddRegistryService;

    public String exportDDJson(String ddRef) throws GuideException {
        try {
            DataDictionary ddi;
            List ddElementsList;
            DataDictionaryElement rootElement;
            if (ddRef != null && (ddElementsList = (ddi = this.ddRegistryService.getDataDictionary(ddRef)).getDDElements()).size() != 0 && (rootElement = (DataDictionaryElement)ddElementsList.get(0)) != null) {
                JSONObject resultJSON = new JSONObject();
                resultJSON.put(rootElement.getName(), (Object)this.createJSONFromDDElement(rootElement));
                return resultJSON.toString();
            }
            return null;
        }
        catch (Exception e) {
            throw new GuideException("Error occurred while creating JSON in exportDDJson", e);
        }
    }

    private JSONObject createJSONFromDDElement(DataDictionaryElement ddElement) throws GuideException {
        try {
            JSONObject currentObj = new JSONObject();
            if (this.isPrimitive(ddElement)) {
                currentObj = this.writeAllProps(ddElement);
            } else if (this.isContainer(ddElement)) {
                currentObj = this.createContainer(ddElement);
            }
            return currentObj;
        }
        catch (Exception e) {
            throw new GuideException("Error occurred while creating JSON in createJSON", e);
        }
    }

    private JSONObject createContainer(DataDictionaryElement ddElement) {
        try {
            JSONObject jsonObj = this.writeAllProps(ddElement);
            List ddElementList = null;
            DataDictionaryElement ddElementToIterateOn = ddElement;
            if (this.isCollection(ddElement)) {
                ddElementToIterateOn = ddElement.getCollectionElement();
            }
            if ((ddElementList = ddElementToIterateOn.getChildElements()).size() > 0) {
                JSONObject childJSON = new JSONObject();
                for (DataDictionaryElement currentDDElement : ddElementList) {
                    childJSON.put(currentDDElement.getName(), (Object)this.createJSONFromDDElement(currentDDElement));
                }
                jsonObj.put("items", (Object)childJSON);
            }
            return jsonObj;
        }
        catch (Exception e) {
            throw new GuideException("Error occurred while creating JSON in createContainer", e);
        }
    }

    private boolean isPrimitive(DataDictionaryElement ddElement) {
        boolean result = false;
        if (ddElement != null) {
            switch (ddElement.getElementType()) {
                case CHAR: 
                case NUMBER: 
                case STRING: 
                case BINARY: 
                case DATE: 
                case BOOLEAN: {
                    result = true;
                    break;
                }
                case COMPOSITE: {
                    result = false;
                    break;
                }
                case COLLECTION: {
                    result = this.isPrimitive(ddElement.getCollectionElement());
                    break;
                }
                default: {
                    result = false;
                }
            }
        }
        return result;
    }

    private boolean isContainer(DataDictionaryElement ddElement) {
        return !this.isPrimitive(ddElement);
    }

    private boolean isCollection(DataDictionaryElement ddElement) {
        return ddElement.getElementType() == DataDictionaryElementType.COLLECTION;
    }

    private JSONObject writeAllProps(DataDictionaryElement ddElement) {
        try {
            JSONObject jsonObj = this.addCommonProps(ddElement);
            jsonObj = this.decideMapping(ddElement, jsonObj);
            return jsonObj;
        }
        catch (Exception e) {
            throw new GuideException("Error occurred while creating JSON in writeAllProps", e);
        }
    }

    private JSONObject decideMapping(DataDictionaryElement ddElement, JSONObject jsonObj) {
        try {
            DataDictionaryElementType ddElementType = ddElement.getElementType();
            if (ddElementType == DataDictionaryElementType.NUMBER) {
                jsonObj = ddElement.getElementSubType() == DataDictionaryElementSubType.ENUM ? this.addEnumProp(ddElement, jsonObj) : this.addNumberProps(ddElement, jsonObj);
            } else if (ddElementType == DataDictionaryElementType.CHAR) {
                jsonObj = this.addTextFieldProps(ddElement, jsonObj);
            } else if (ddElementType == DataDictionaryElementType.DATE) {
                jsonObj = this.addDateProps(ddElement, jsonObj);
            } else if (ddElementType == DataDictionaryElementType.COMPOSITE) {
                jsonObj = this.addPanelProps(ddElement, jsonObj);
            } else if (ddElementType == DataDictionaryElementType.COLLECTION) {
                jsonObj = this.addCollectionProps(ddElement, jsonObj);
            } else if (ddElementType == DataDictionaryElementType.STRING) {
                jsonObj = ddElement.getElementSubType() == DataDictionaryElementSubType.ENUM ? this.addEnumProp(ddElement, jsonObj) : this.addTextFieldProps(ddElement, jsonObj);
            } else if (ddElementType == DataDictionaryElementType.BINARY) {
                jsonObj = this.addBinaryProps(ddElement, jsonObj);
            } else if (ddElementType == DataDictionaryElementType.BOOLEAN) {
                jsonObj = this.addCheckBoxProps(ddElement, jsonObj);
            }
        }
        catch (Exception e) {
            throw new GuideException("Error occurred while mapping DataDictionary Elements to guide elements", e);
        }
        return jsonObj;
    }

    private JSONObject addCommonProps(DataDictionaryElement ddElement) {
        JSONObject jsonObj = new JSONObject();
        try {
            jsonObj.put("bindRef", (Object)ddElement.getReferenceName());
            jsonObj.put("jcr:primaryType", (Object)"nt:unstructured");
            jsonObj.put("jcr:title", (Object)ddElement.getDisplayName());
            jsonObj.put("name", (Object)ddElement.getName());
            jsonObj.put("enabledExp", (Object)"false");
            if (ddElement.isRequired()) {
                jsonObj.put("mandatory", (Object)"true");
            }
        }
        catch (Exception e) {
            throw new GuideException("Error occurred while creating JSON for common properties", e);
        }
        return jsonObj;
    }

    private JSONObject addPanelProps(DataDictionaryElement ddElement, JSONObject jsonObj) {
        try {
            jsonObj.put("guideNodeClass", (Object)"guidePanel");
            jsonObj.put("sling:resourceType", (Object)"fd/af/components/panel");
        }
        catch (Exception e) {
            throw new GuideException("Error occurred while adding panel json", e);
        }
        return jsonObj;
    }

    private JSONObject addDateProps(DataDictionaryElement ddElement, JSONObject jsonObj) {
        try {
            jsonObj.put("guideNodeClass", (Object)"guideDatePicker");
            jsonObj.put("sling:resourceType", (Object)"fd/af/components/guidedatepicker");
        }
        catch (Exception e) {
            throw new GuideException("Error occurred while adding date field json", e);
        }
        return jsonObj;
    }

    private JSONObject addNumberProps(DataDictionaryElement ddElement, JSONObject jsonObj) {
        try {
            jsonObj.put("guideNodeClass", (Object)"guideNumericBox");
            jsonObj.put("sling:resourceType", (Object)"fd/af/components/guidenumericbox");
        }
        catch (Exception e) {
            throw new GuideException("Error occurred while adding number field json", e);
        }
        return jsonObj;
    }

    private JSONObject addTextFieldProps(DataDictionaryElement ddElement, JSONObject jsonObj) {
        try {
            jsonObj.put("guideNodeClass", (Object)"guideTextBox");
            jsonObj.put("sling:resourceType", (Object)"fd/af/components/guidetextbox");
        }
        catch (Exception e) {
            throw new GuideException("Error occurred while adding number field json", e);
        }
        return jsonObj;
    }

    private JSONObject addCollectionProps(DataDictionaryElement ddElement, JSONObject jsonObj) {
        try {
            jsonObj.put("maxOccur", (Object)"-1");
            jsonObj = this.decideMapping(ddElement.getCollectionElement(), jsonObj);
        }
        catch (Exception e) {
            throw new GuideException("Error occurred while adding collection field json", e);
        }
        return jsonObj;
    }

    private JSONObject addEnumProp(DataDictionaryElement ddElement, JSONObject jsonObj) {
        try {
            jsonObj.put("guideNodeClass", (Object)"guideDropDownList");
            jsonObj.put("sling:resourceType", (Object)"fd/af/components/guidedropdownlist");
            List valueSet = ddElement.getValueSet();
            ArrayList<String> resultingOptionsArray = new ArrayList<String>();
            for (String value : valueSet) {
                resultingOptionsArray.add(value + "=" + value);
            }
            jsonObj.put("options", (Object)new JSONArray(resultingOptionsArray));
        }
        catch (Exception e) {
            throw new GuideException("Error occurred while adding enum field json", e);
        }
        return jsonObj;
    }

    private JSONObject addBinaryProps(DataDictionaryElement ddElement, JSONObject jsonObj) {
        try {
            jsonObj.put("guideNodeClass", (Object)"guideImage");
            jsonObj.put("sling:resourceType", (Object)"fd/af/components/guideimage");
        }
        catch (Exception e) {
            throw new GuideException("Error occurred while adding binary (image) field json", e);
        }
        return jsonObj;
    }

    private JSONObject addCheckBoxProps(DataDictionaryElement ddElement, JSONObject jsonObj) {
        try {
            jsonObj.put("guideNodeClass", (Object)"guideCheckBox");
            jsonObj.put("sling:resourceType", (Object)"fd/af/components/guidecheckbox");
        }
        catch (Exception e) {
            throw new GuideException("Error occurred while adding binary (image) field json", e);
        }
        return jsonObj;
    }

    protected void bindDdRegistryService(DataDictionaryRegistryService dataDictionaryRegistryService) {
        this.ddRegistryService = dataDictionaryRegistryService;
    }

    protected void unbindDdRegistryService(DataDictionaryRegistryService dataDictionaryRegistryService) {
        if (this.ddRegistryService == dataDictionaryRegistryService) {
            this.ddRegistryService = null;
        }
    }

}