TargetRecommendationsJSONUtil.java 5.99 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 */
package com.adobe.cq.targetrecommendations.impl.util;

import com.adobe.cq.targetrecommendations.api.model.ProductFeed;
import com.adobe.cq.targetrecommendations.api.model.RecommendationAlgorithm;
import com.adobe.cq.targetrecommendations.api.model.RecommendationTemplate;
import com.adobe.cq.targetrecommendations.api.model.TargetRecommendation;
import com.adobe.cq.targetrecommendations.impl.model.ProductFeedImpl;
import com.adobe.cq.targetrecommendations.impl.model.RecommendationAlgorithmImpl;
import com.adobe.cq.targetrecommendations.impl.model.RecommendationTemplateImpl;
import com.adobe.cq.targetrecommendations.impl.model.TargetRecommendationImpl;
import java.util.ArrayList;
import java.util.List;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;

public class TargetRecommendationsJSONUtil {
    public static List<RecommendationTemplate> parseTemplates(String rawJson) throws JSONException {
        JSONArray jsonPayload = TargetRecommendationsJSONUtil.convertToJson(rawJson);
        ArrayList<RecommendationTemplate> templates = new ArrayList<RecommendationTemplate>();
        for (int i = 0; i < jsonPayload.length(); ++i) {
            JSONObject currentTemplate = jsonPayload.getJSONObject(i);
            templates.add(RecommendationTemplateImpl.fromJson(currentTemplate));
        }
        return templates;
    }

    public static List<RecommendationAlgorithm> parseAlgorithms(String rawJson) throws JSONException {
        JSONArray jsonPayload = TargetRecommendationsJSONUtil.convertToJson(rawJson);
        ArrayList<RecommendationAlgorithm> algorithms = new ArrayList<RecommendationAlgorithm>();
        for (int i = 0; i < jsonPayload.length(); ++i) {
            JSONObject currentAlgorithm = jsonPayload.getJSONObject(i);
            algorithms.add(RecommendationAlgorithmImpl.fromJson(currentAlgorithm));
        }
        return algorithms;
    }

    public static List<TargetRecommendation> parseRecommendations(String rawJson) throws JSONException {
        JSONArray jsonPayload = TargetRecommendationsJSONUtil.convertToJson(rawJson);
        ArrayList<TargetRecommendation> recommendations = new ArrayList<TargetRecommendation>();
        for (int i = 0; i < jsonPayload.length(); ++i) {
            JSONObject currentAlgorithm = jsonPayload.getJSONObject(i);
            recommendations.add(TargetRecommendationImpl.fromJson(currentAlgorithm));
        }
        return recommendations;
    }

    public static List<ProductFeed> parseFeeds(String rawJson) throws JSONException {
        JSONArray jsonPayload = TargetRecommendationsJSONUtil.convertToJson(rawJson);
        ArrayList<ProductFeed> feeds = new ArrayList<ProductFeed>();
        for (int i = 0; i < jsonPayload.length(); ++i) {
            JSONObject currentFeed = jsonPayload.getJSONObject(i);
            feeds.add(ProductFeedImpl.fromJson(currentFeed));
        }
        return feeds;
    }

    public static ProductFeed parseFeed(String rawJson) throws JSONException {
        JSONArray jsonPayload = TargetRecommendationsJSONUtil.convertToJson(rawJson);
        if (jsonPayload.length() > 0) {
            return ProductFeedImpl.fromJson(jsonPayload.getJSONObject(0));
        }
        return null;
    }

    public static RecommendationTemplate parseTemplate(String rawJson) throws JSONException {
        JSONArray jsonPayload = TargetRecommendationsJSONUtil.convertToJson(rawJson);
        if (jsonPayload.length() > 0) {
            return RecommendationTemplateImpl.fromJson(jsonPayload.getJSONObject(0));
        }
        return null;
    }

    public static RecommendationAlgorithm parseAlgorithm(String rawJson) throws JSONException {
        JSONArray jsonPayload = TargetRecommendationsJSONUtil.convertToJson(rawJson);
        if (jsonPayload.length() > 0) {
            return RecommendationAlgorithmImpl.fromJson(jsonPayload.getJSONObject(0));
        }
        return null;
    }

    public static TargetRecommendation parseRecommendation(String rawJson) throws JSONException {
        JSONArray jsonPayload = TargetRecommendationsJSONUtil.convertToJson(rawJson);
        if (jsonPayload.length() > 0) {
            return TargetRecommendationImpl.fromJson(jsonPayload.getJSONObject(0));
        }
        return null;
    }

    public static List<String> getJsonArrayPropValues(String rawJson, String propName) throws JSONException {
        ArrayList<String> propValues = new ArrayList<String>();
        JSONArray jsonPayload = TargetRecommendationsJSONUtil.convertToJson(rawJson);
        for (int i = 0; i < jsonPayload.length(); ++i) {
            JSONObject currentObj = jsonPayload.getJSONObject(i);
            propValues.add(currentObj.getString(propName));
        }
        return propValues;
    }

    public static <T> List<T> unmarshall(JSONArray jsonArray, Class<T> convertTo) throws JSONException {
        ArrayList<Object> objArray = new ArrayList<Object>();
        if (jsonArray != null && jsonArray.length() > 0) {
            for (int idx = 0; idx < jsonArray.length(); ++idx) {
                Object arrayItem = jsonArray.get(idx);
                Object convertedItem = null;
                if (arrayItem == null || !convertTo.isAssignableFrom(arrayItem.getClass())) continue;
                convertedItem = convertTo.cast(arrayItem);
                objArray.add(convertedItem);
            }
        }
        return objArray;
    }

    public static JSONArray convertToJson(String rawJson) throws JSONException {
        JSONArray jsonPayload = null;
        if (rawJson.startsWith("{")) {
            JSONObject object = new JSONObject(rawJson);
            jsonPayload = new JSONArray();
            jsonPayload.put((Object)object);
        } else {
            jsonPayload = new JSONArray(rawJson);
        }
        return jsonPayload;
    }
}