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

import com.adobe.cq.targetrecommendations.api.TargetRecommendationsException;
import com.adobe.cq.targetrecommendations.api.model.RecommendationTemplate;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;

public class RecommendationTemplateImpl
implements RecommendationTemplate {
    private int id;
    private String name;
    private String script;

    public RecommendationTemplateImpl(int id, String name, String script) {
        this.id = id;
        this.name = name;
        this.script = script;
    }

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

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

    @Override
    public String getScript() {
        return this.script;
    }

    public static RecommendationTemplate fromJson(JSONObject recTemplateJson) throws JSONException {
        int id = -1;
        if (recTemplateJson.has("id")) {
            id = recTemplateJson.getInt("id");
        }
        String name = recTemplateJson.getString("name");
        String script = recTemplateJson.getString("script");
        return new RecommendationTemplateImpl(id, name, script);
    }

    @Override
    public String toJson() throws TargetRecommendationsException {
        try {
            JSONObject templateJson = new JSONObject();
            if (this.getId() > 0) {
                templateJson.put("id", this.getId());
            }
            templateJson.put("name", (Object)this.getName());
            templateJson.put("script", (Object)this.getScript());
            return templateJson.toString();
        }
        catch (Exception e) {
            throw new TargetRecommendationsException(e);
        }
    }
}