All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.aemds.guide.progressive.GuideProgressiveSectionManager Maven / Gradle / Ivy

/************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 * Copyright 2014 Adobe Systems Incorporated
 * All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 *************************************************************************/

package com.adobe.aemds.guide.progressive;

import com.adobe.aemds.guide.service.GuideProgressiveStrategy;
import com.adobe.aemds.guide.service.GuideProgressiveStrategyManager;
import com.adobe.aemds.guide.utils.GuideConstants;
import com.adobe.aemds.guide.utils.GuideUtils;
import com.adobe.aemds.guide.utils.NodeStructureUtils;
import com.adobe.cq.sightly.WCMUsePojo;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestParameterMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class GuideProgressiveSectionManager extends WCMUsePojo {

    private GuideProgressiveSectionInfo guideProgressiveSectionInfo;
    private List allSections;
    private GuideProgressiveCompletionInfo guideProgressiveCompletionInfo;
    private Boolean fetchedFromService;
    private String serviceType;

    private static Logger log = LoggerFactory.getLogger(GuideProgressiveSectionManager.class);

    public Boolean getFetchedFromService() {
        return fetchedFromService;
    }

    /**
     * API to get all sections present
     * If the service is dynamic, then this API would return only one section
     * @return
     */
    public List getAllSections(){
        return allSections;
    }

    public String getSectionJSON() {
        if (this.guideProgressiveCompletionInfo == null) {
            if(this.guideProgressiveSectionInfo != null){
                return this.guideProgressiveSectionInfo.convertToJSON();
            } else {
                return "";
            }
        } else {
            return this.guideProgressiveCompletionInfo.convertToJSON();
        }
    }

    private JSONArray extractJsonArrayFromJSONObject(JSONObject jsonObject, String arg) {
        JSONArray result = new JSONArray();
        if (jsonObject.has(arg)) {
            try {
                result = (JSONArray) jsonObject.get(arg);
            } catch (Exception e) {
                log.error("Error while extracting JSON Array from JSON Object", e);
            }
        }
        return result;
    }

    private String extractFromJSONObject(JSONObject jsonObject, String arg) {
        String result = "";
        if (jsonObject.has(arg)) {
            try {
                result = (String) jsonObject.get(arg);
            } catch (Exception e) {
                log.error("Error while extracting value from JSON Object", e);
            }
        }
        return result;
    }

    @Override
    public void activate() throws Exception {
        SlingHttpServletRequest request = getRequest();
        ValueMap properties = getProperties();
        RequestParameterMap params = request.getRequestParameterMap();
        String afAssetPath = properties.get(GuideConstants.GUIDE_PROGRESSIVE_REF, "");
        String type = properties.get(GuideConstants.GUIDE_PROGRESSIVE_STRATEGY_TYPE, "");
        String progressiveStrategyName = properties.get(GuideConstants.GUIDE_PROGRESSIVE_STRATEGY_NAME, "");
        String progressiveStrategyParams = properties.get(GuideConstants.GUIDE_PROGRESSIVE_STRATEGY_PARAMS, "");
        String progressiveStrategyJcrPath = properties.get(GuideConstants.GUIDE_PROGRESSIVE_STRATEGY_JCR_PATH, "");
        fetchedFromService = Boolean.TRUE;

        Map paramMap = GuideUtils.convertStringToMap(progressiveStrategyParams);
        paramMap.put(GuideConstants.GUIDE_PROGRESSIVE_SECTION_FIELD_VALUE,
                GuideUtils.paramToString(params.getValue(GuideConstants.GUIDE_PROGRESSIVE_SECTION_FIELD_VALUE)));
        paramMap.put("strategyJcrPath",progressiveStrategyJcrPath);

        String currentSectionId = GuideUtils.paramToString(params.getValue(GuideConstants.GUIDE_PROGRESSIVE_CURRENT_SECTION_ID));
        paramMap.put(GuideConstants.GUIDE_PROGRESSIVE_RENDER_CURRENT_SECTION, GuideUtils.paramToString(params.getValue(GuideConstants.GUIDE_PROGRESSIVE_RENDER_CURRENT_SECTION)));
        // if information of the last section is available in the request then we fetch the section using that information , otherwise
        // we fetch the section from service.
        String lastSectionInfo = request.getParameter(GuideConstants.GUIDE_PROGRESSIVE_LAST_SECTION_INFO);
        JSONObject lastSectionInfoJSONObject = null;

        if (lastSectionInfo != null) {
            lastSectionInfoJSONObject = new JSONObject(lastSectionInfo);
            String sectionId = extractFromJSONObject(lastSectionInfoJSONObject, GuideConstants.GUIDE_PROGRESSIVE_SECTION_ID);

            //checking for a non empty, non null section id
            if (sectionId != null && !sectionId.isEmpty()) {
                //checking if the section info has field information
                JSONArray sectionFields = extractJsonArrayFromJSONObject(lastSectionInfoJSONObject, GuideConstants.GUIDE_PROGRESSIVE_SECTION_FIELDS);
                List sectionFieldsList = new ArrayList();
                if (sectionFields != null && sectionFields.length() > 0) {
                    for (int i = 0; i < sectionFields.length(); ++i) {
                        JSONObject field = sectionFields.getJSONObject(i);
                        String id = field.getString("id");
                        String path = field.getString("path");
                        String prefixId =null;
                        if(field.has("prefixId")) {
                            prefixId = field.getString("prefixId");
                        }
                        GuideProgressiveField progressiveField = new GuideProgressiveField(id, path, prefixId, getResourceResolver());
                        sectionFieldsList.add(progressiveField);
                    }
                }
                if (sectionFieldsList.size() > 0) {
                    fetchedFromService = Boolean.FALSE;
                    String repeatablePanelPath = extractFromJSONObject(lastSectionInfoJSONObject, GuideConstants.GUIDE_PROGRESSIVE_REPEATABLE_PANEL_PATH);
                    String repeatablePanelId = extractFromJSONObject(lastSectionInfoJSONObject, GuideConstants.GUIDE_PROGRESSIVE_REPEATABLE_PANEL_ID);
                    String sectionTitle = extractFromJSONObject(lastSectionInfoJSONObject, GuideConstants.GUIDE_PROGRESSIVE_SECTION_TITLE);
                    String sectionName = extractFromJSONObject(lastSectionInfoJSONObject, GuideConstants.GUIDE_PROGRESSIVE_SECTION_NAME);
                    if (repeatablePanelId != null && repeatablePanelId.isEmpty() &&
                            repeatablePanelPath != null && !repeatablePanelPath.isEmpty()) {
                        ResourceResolver resourceResolver = getResourceResolver();
                        Resource repeatablePanelResource = resourceResolver.getResource(repeatablePanelPath);
                        repeatablePanelId = NodeStructureUtils.getGuideNodeHtmlId(repeatablePanelResource);
                    }
                    guideProgressiveSectionInfo = new GuideProgressiveSectionInfo(sectionId,
                            sectionTitle,
                            sectionName,
                            sectionFieldsList,
                            repeatablePanelPath,
                            repeatablePanelId,
                            fetchedFromService);
                } else if (GuideProgressiveCompletionInfo.id.equals(sectionId)) { //completion section info
                    fetchedFromService = Boolean.FALSE;
                    String completionTitle = extractFromJSONObject(lastSectionInfoJSONObject, GuideConstants.GUIDE_PROGRESSIVE_COMPLETION_TITLE);
                    String completionBeforeMessage = extractFromJSONObject(lastSectionInfoJSONObject, GuideConstants.GUIDE_PROGRESSIVE_COMPLETION_BEFORE_MESSAGE);
                    String completionAfterMessage = extractFromJSONObject(lastSectionInfoJSONObject, GuideConstants.GUIDE_PROGRESSIVE_COMPLETION_AFTER_MESSAGE);
                    String completionScript = extractFromJSONObject(lastSectionInfoJSONObject, GuideConstants.GUIDE_PROGRESSIVE_COMPLETION_SCRIPT);
                    String completionButtonText = extractFromJSONObject(lastSectionInfoJSONObject, GuideConstants.GUIDE_PROGRESSIVE_COMPLETION_BUTTON_TEXT);
                    String completionSuccessScript = extractFromJSONObject(lastSectionInfoJSONObject, GuideConstants.GUIDE_PROGRESSIVE_COMPLETION_SUCCESS_SCRIPT);
                    String completionFailureScript = extractFromJSONObject(lastSectionInfoJSONObject, GuideConstants.GUIDE_PROGRESSIVE_COMPLETION_FAILURE_SCRIPT);
                    this.guideProgressiveCompletionInfo = new GuideProgressiveCompletionInfo(completionTitle,
                            completionBeforeMessage,
                            completionAfterMessage,
                            completionScript,
                            completionButtonText,
                            completionSuccessScript,
                            completionFailureScript,
                            fetchedFromService);
                }
            }
        }

        //fetching from service in case the last section info is not present in requestParameter or is having section info with no fields and is not a completion info too.
        if ("service".equals(type) && fetchedFromService) {
            paramMap.put(GuideConstants.GUIDE_PROGRESSIVE_FETCHED_FROM_SERVICE, fetchedFromService.toString());
            GuideProgressiveStrategyManager guideProgressiveStrategyManager = getSlingScriptHelper().getService(GuideProgressiveStrategyManager.class);
            GuideProgressiveStrategy guideProgressiveStrategy = guideProgressiveStrategyManager.getGuideProgressiveStrategyService(progressiveStrategyName);
            serviceType = guideProgressiveStrategy.getServiceType();
            // If sections are not present calculate it, this is a one time calculation
            if(allSections == null || allSections.size() == 0) {
                allSections = guideProgressiveStrategy.getAllSections(paramMap);
            }
            if(guideProgressiveStrategy != null) {
                guideProgressiveSectionInfo = guideProgressiveStrategy.getNextSection(afAssetPath, currentSectionId, paramMap);
                if (guideProgressiveSectionInfo == null) {
                    guideProgressiveCompletionInfo = guideProgressiveStrategy.getCompletionSection(afAssetPath, paramMap);
                }
            }
        }

    }

    /**
     * Public API to get the current Progressive Section Info Object
     * @return
     */
    public GuideProgressiveSectionInfo getGuideProgressiveSectionInfo() {
        return guideProgressiveSectionInfo;
    }

    /**
     * Public API to get the progressive completion info
     * @return
     */
    public GuideProgressiveCompletionInfo getGuideProgressiveCompletionInfo() {
        return guideProgressiveCompletionInfo;
    }

}