AdXmlDataProviderImpl.java 8.93 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemds.guide.utils.GuideContainerThreadLocal
 *  com.adobe.dct.exception.DCTException
 *  com.adobe.dct.service.DataDictionaryRegistryService
 *  com.adobe.dct.transfer.DataDictionary
 *  com.adobe.dct.transfer.DataDictionarySearchFilter
 *  com.adobe.forms.common.service.DataXMLOptions
 *  com.adobe.forms.common.service.DataXMLProvider
 *  com.adobe.forms.common.service.FormsException
 *  com.adobe.granite.resourceresolverhelper.ResourceResolverHelper
 *  com.adobe.icc.dbforms.obj.Letter
 *  com.adobe.icc.services.api.LetterService
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferenceCardinality
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.fd.adaddon.service.impl;

import com.adobe.aemds.guide.utils.GuideContainerThreadLocal;
import com.adobe.dct.exception.DCTException;
import com.adobe.dct.service.DataDictionaryRegistryService;
import com.adobe.dct.transfer.DataDictionary;
import com.adobe.dct.transfer.DataDictionarySearchFilter;
import com.adobe.forms.common.service.DataXMLOptions;
import com.adobe.forms.common.service.DataXMLProvider;
import com.adobe.forms.common.service.FormsException;
import com.adobe.granite.resourceresolverhelper.ResourceResolverHelper;
import com.adobe.icc.dbforms.obj.Letter;
import com.adobe.icc.services.api.LetterService;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(immediate=1, label="AdXmlDataProviderImpl", description="AdXmlDataProviderImpl")
@Service(value={DataXMLProvider.class})
public class AdXmlDataProviderImpl
implements DataXMLProvider {
    private static Logger logger = LoggerFactory.getLogger(AdXmlDataProviderImpl.class);
    @Property(intValue={2100})
    private static final String SERVICE_RANKING = "service.ranking";
    @Reference(cardinality=ReferenceCardinality.OPTIONAL_UNARY, policy=ReferencePolicy.DYNAMIC)
    private ResourceResolverHelper resolverHelper;
    @Reference(cardinality=ReferenceCardinality.OPTIONAL_UNARY, policy=ReferencePolicy.DYNAMIC)
    private LetterService letterService;
    @Reference(cardinality=ReferenceCardinality.OPTIONAL_UNARY, policy=ReferencePolicy.DYNAMIC)
    private DataDictionaryRegistryService dataDictionaryRegistryService;

    private byte[] getDataXMLForDataRef(String s) throws FormsException {
        logger.trace("getDataXMLForDataRef: url= " + s);
        if ("ad-testdata://AD_EMPTY_DATA".equals(s)) {
            return this.strToBytes("<?xml version=\"1.0\" ?><root/>");
        }
        logger.trace("getDataXMLForDataRef:resolverHelper= " + (Object)this.resolverHelper);
        if ("ad-testdata://USE_TEST_DATA".equals(s)) {
            try {
                return this.getTestData();
            }
            catch (Exception e) {
                logger.error("Error when getting test from dd", (Throwable)e);
                throw new FormsException(e);
            }
        }
        return null;
    }

    private byte[] getTestData() throws Exception {
        String guideContainerPath = GuideContainerThreadLocal.getGuideContainerPath();
        logger.trace("getTestData:guideContainerPath :" + guideContainerPath);
        ResourceResolver resourceResolver = this.resolverHelper.getResourceResolver();
        Resource guideContainerResource = resourceResolver.getResource(guideContainerPath);
        logger.trace("getTestData: guideContainerResource " + (Object)guideContainerResource);
        if (guideContainerResource != null) {
            ValueMap guideProps = guideContainerResource.getValueMap();
            String ddRef = (String)guideProps.get("ddRef", (Object)"");
            String letterRef = (String)guideProps.get("letterRef", (Object)"");
            logger.trace("getTestData: ddRef= " + ddRef);
            logger.trace("getTestData: letterRef= " + letterRef);
            if (StringUtils.isNotBlank((CharSequence)letterRef)) {
                if (this.letterService == null) {
                    logger.error("getTestData: LetterService is null, should not be null in AD with  letterRef");
                    throw new Exception(" LetterService is null");
                }
                Letter letter = this.letterService.getLetter(letterRef, false);
                if (letter == null) {
                    throw new Exception(" letter is null");
                }
                ddRef = letter.getDataDictionaryRef();
                logger.trace("getTestData: ddRef from letter : " + ddRef);
            }
            if (StringUtils.isNotBlank((CharSequence)ddRef)) {
                if (this.dataDictionaryRegistryService == null) {
                    logger.error("getTestData: dataDictionaryRegistryService is null, should not be null in AD with ddRef");
                    throw new Exception(" dataDictionaryRegistryService is null");
                }
                DataDictionary dataDictionary = null;
                dataDictionary = ddRef.contains("/") ? this.dataDictionaryRegistryService.getDataDictionary(ddRef) : this.getDataDictionaryFromName(ddRef);
                if (dataDictionary == null) {
                    throw new Exception(" DataDictionary is null");
                }
                byte[] testData = dataDictionary.getTestdata();
                if (testData != null && testData.length > 0) {
                    return testData;
                }
                return this.strToBytes("<?xml version=\"1.0\" ?><root/>");
            }
            return this.strToBytes("<?xml version=\"1.0\" ?><root/>");
        }
        throw new Exception("guideContainerResource is null");
    }

    private DataDictionary getDataDictionaryFromName(String ddName) throws DCTException {
        logger.trace("getDataDictionaryFromName : " + ddName);
        DataDictionarySearchFilter ddsf = new DataDictionarySearchFilter();
        ddsf.setName(ddName.trim());
        List dataDictionaryList = this.dataDictionaryRegistryService.getDataDictionaries(ddsf);
        logger.trace("getDataDictionaryFromName: DataDictionaries : {} ", (Object)dataDictionaryList);
        if (dataDictionaryList != null && dataDictionaryList.size() > 0) {
            return (DataDictionary)dataDictionaryList.get(0);
        }
        return null;
    }

    public InputStream getDataXMLForDataRef(DataXMLOptions dataXMLOptions) throws FormsException {
        byte[] data = this.getDataXMLForDataRef(dataXMLOptions.getDataRef());
        if (data != null) {
            return new ByteArrayInputStream(data);
        }
        return null;
    }

    private byte[] strToBytes(String str) throws FormsException {
        try {
            return str.getBytes("UTF-8");
        }
        catch (UnsupportedEncodingException e) {
            throw new FormsException((Exception)e);
        }
    }

    public String getServiceName() {
        return null;
    }

    public String getServiceDescription() {
        return null;
    }

    protected void bindResolverHelper(ResourceResolverHelper resourceResolverHelper) {
        this.resolverHelper = resourceResolverHelper;
    }

    protected void unbindResolverHelper(ResourceResolverHelper resourceResolverHelper) {
        if (this.resolverHelper == resourceResolverHelper) {
            this.resolverHelper = null;
        }
    }

    protected void bindLetterService(LetterService letterService) {
        this.letterService = letterService;
    }

    protected void unbindLetterService(LetterService letterService) {
        if (this.letterService == letterService) {
            this.letterService = null;
        }
    }

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

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