TargetRecommendationImpl.java 6.69 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
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.targetrecommendations.impl.model;

import com.adobe.cq.targetrecommendations.api.TargetRecommendationsException;
import com.adobe.cq.targetrecommendations.api.model.TargetRecommendation;
import com.adobe.cq.targetrecommendations.impl.util.TargetRecommendationsJSONUtil;
import java.util.ArrayList;
import java.util.Collection;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TargetRecommendationImpl
implements TargetRecommendation {
    private static final Logger LOG = LoggerFactory.getLogger(TargetRecommendationImpl.class);
    private int id;
    private String name;
    private int defaultContent;
    private int catalogId;
    private TargetRecommendation.RecommendationState state;
    private String locationMBox;
    private List<String> templates;
    private List<Integer> algorithms;
    private String startDate;
    private String endDate;

    public TargetRecommendationImpl(int id, String name, String stateString, int catalogId, String locationMBox, List<String> templates, List<Integer> algorithms, String startDate, String endDate, int defaultContent) {
        this.id = id;
        this.name = name;
        this.catalogId = catalogId;
        this.locationMBox = locationMBox;
        this.templates = templates;
        this.algorithms = algorithms;
        this.startDate = startDate;
        this.endDate = endDate;
        this.defaultContent = defaultContent;
        try {
            this.state = TargetRecommendation.RecommendationState.valueOf(stateString.toUpperCase());
        }
        catch (Exception e) {
            LOG.warn("Invalid Target recommendation state string, defaulting to 'inactive'!");
            this.state = TargetRecommendation.RecommendationState.INACTIVE;
        }
    }

    @Override
    public int getId() {
        return this.id;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public int getCatalogId() {
        return this.catalogId;
    }

    @Override
    public List<String> getTemplateNames() {
        return this.templates;
    }

    @Override
    public TargetRecommendation.RecommendationState getState() {
        return this.state;
    }

    @Override
    public List<Integer> getAlgorithmIds() {
        return this.algorithms;
    }

    @Override
    public String getLocationDisplayMbox() {
        return this.locationMBox;
    }

    @Override
    public int getDefaultContentId() {
        return this.defaultContent;
    }

    @Override
    public String getStartDate() {
        return this.startDate;
    }

    @Override
    public String getEndDate() {
        return this.endDate;
    }

    @Override
    public String toJson() throws TargetRecommendationsException {
        try {
            JSONObject recJsonRepresentation = new JSONObject();
            if (this.getId() > 0) {
                recJsonRepresentation.put("id", this.getId());
            }
            recJsonRepresentation.put("name", (Object)this.getName());
            recJsonRepresentation.put("state", (Object)this.getState().toString().toLowerCase());
            recJsonRepresentation.put("catalogId", (Object)(this.getCatalogId() > 0 ? Integer.valueOf(this.getCatalogId()) : null));
            recJsonRepresentation.put("algorithmIds", this.getAlgorithmIds());
            recJsonRepresentation.put("templates", this.getTemplateNames());
            recJsonRepresentation.put("location", (Object)new JSONObject().put("displayMbox", (Object)this.getLocationDisplayMbox()));
            JSONObject configJson = new JSONObject();
            if (this.getStartDate() != null) {
                configJson.put("start", (Object)this.getStartDate());
            }
            if (this.getEndDate() != null) {
                configJson.put("end", (Object)this.getEndDate());
            }
            if (this.getDefaultContentId() >= 0) {
                configJson.put("defaultContent", (Object)new JSONObject().put("percentage", this.getDefaultContentId()));
            }
            if (configJson.length() > 0) {
                recJsonRepresentation.put("configuration", (Object)configJson);
            }
            return recJsonRepresentation.toString();
        }
        catch (Exception e) {
            throw new TargetRecommendationsException(e);
        }
    }

    public static TargetRecommendation fromJson(JSONObject recommendationJson) throws JSONException {
        int recId = -1;
        if (recommendationJson.has("id")) {
            recId = recommendationJson.getInt("id");
        }
        String recName = recommendationJson.getString("name");
        int recCatalogId = recommendationJson.has("catalogId") ? recommendationJson.getInt("catalogId") : -1;
        String recState = recommendationJson.has("state") ? recommendationJson.getString("state") : null;
        String recLocationMBox = recommendationJson.getJSONObject("location").getString("displayMbox");
        JSONObject configJson = recommendationJson.has("configuration") ? recommendationJson.getJSONObject("configuration") : new JSONObject();
        int recDefaultContent = 0;
        if (configJson.has("defaultContent")) {
            try {
                JSONObject defaultContentJson = configJson.getJSONObject("defaultContent");
                if (defaultContentJson.has("percentage")) {
                    recDefaultContent = defaultContentJson.getInt("percentage");
                }
            }
            catch (JSONException jse) {
                recDefaultContent = configJson.getInt("defaultContent");
            }
        }
        String recStartDate = configJson.has("start") ? configJson.getString("start") : null;
        String recEndDate = configJson.has("end") ? configJson.getString("end") : null;
        ArrayList<Integer> recAlgorithms = recommendationJson.has("algorithmIds") ? TargetRecommendationsJSONUtil.unmarshall(recommendationJson.getJSONArray("algorithmIds"), Integer.class) : new ArrayList<Integer>();
        ArrayList<String> recTemplates = recommendationJson.has("templates") ? TargetRecommendationsJSONUtil.unmarshall(recommendationJson.getJSONArray("templates"), String.class) : new ArrayList<String>();
        return new TargetRecommendationImpl(recId, recName, recState, recCatalogId, recLocationMBox, recTemplates, recAlgorithms, recStartDate, recEndDate, recDefaultContent);
    }
}