ProductFeedImpl.java 7.92 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.ProductFeed;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;

public class ProductFeedImpl
implements ProductFeed {
    private static final String GOOGLE_FEED_TYPE_STR = "google-atom-1";
    private static final String CSV_FEED_TYPE_STR = "csv";
    private static final String SAINT_FEED_TYPE_STR = "saint";
    private static final Map<String, ProductFeed.FeedType> FEED_MAPPING = new HashMap<String, ProductFeed.FeedType>();
    private static final Map<ProductFeed.FeedSchedule, String> FEED_FREQUENCY_MAPPING;
    private int id;
    private String name;
    private ProductFeed.FeedType type;
    private ProductFeed.FeedSchedule updateSchedule;
    private int environmentId;
    private Map<String, String> attributeMapping;
    private Map<String, String> connectionProperties;

    public ProductFeedImpl(int id, String name, ProductFeed.FeedType type, ProductFeed.FeedSchedule updateSchedule, int environmentId, Map<String, String> attributeMapping, Map<String, String> connectionProperties) {
        this.id = id;
        this.name = name;
        this.type = type;
        this.updateSchedule = updateSchedule;
        this.environmentId = environmentId;
        this.attributeMapping = attributeMapping;
        this.connectionProperties = connectionProperties;
    }

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

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

    @Override
    public int getEnvironmentId() {
        return this.environmentId;
    }

    @Override
    public ProductFeed.FeedType getType() {
        return this.type;
    }

    @Override
    public ProductFeed.FeedSchedule getSchedule() {
        return this.updateSchedule;
    }

    @Override
    public Map<String, String> getAttributeMapping() {
        return this.attributeMapping;
    }

    @Override
    public Map<String, String> getConnectionProperties() {
        return this.connectionProperties;
    }

    @Override
    public String toJson() throws TargetRecommendationsException {
        try {
            JSONObject feedJsonRepresentation = new JSONObject();
            if (this.getId() > 0) {
                feedJsonRepresentation.put("id", this.getId());
            }
            feedJsonRepresentation.put("environmentId", this.getEnvironmentId());
            feedJsonRepresentation.put("name", (Object)this.getName());
            feedJsonRepresentation.put("scheduleType", (Object)FEED_FREQUENCY_MAPPING.get((Object)this.getSchedule()));
            feedJsonRepresentation.put("feedType", (Object)ProductFeedImpl.getRecsFeedTypeString(this.getType()));
            JSONObject attributeMappingsJson = new JSONObject();
            for (Map.Entry<String, String> attributeEntry : this.getAttributeMapping().entrySet()) {
                attributeMappingsJson.put(attributeEntry.getKey(), (Object)attributeEntry.getValue());
            }
            feedJsonRepresentation.put("attributeMapping", (Object)attributeMappingsJson);
            JSONObject connectionPropertiesJson = new JSONObject();
            String connectionType = this.getConnectionProperties().get("type");
            for (Map.Entry<String, String> connectionPropEntry : this.getConnectionProperties().entrySet()) {
                if ("type".equalsIgnoreCase(connectionPropEntry.getKey())) continue;
                connectionPropertiesJson.put(connectionPropEntry.getKey(), (Object)connectionPropEntry.getValue());
            }
            JSONObject connectionJson = new JSONObject();
            connectionJson.put("type", (Object)connectionType);
            connectionJson.put("attributes", (Object)connectionPropertiesJson);
            feedJsonRepresentation.put("connection", (Object)connectionJson);
            return feedJsonRepresentation.toString();
        }
        catch (Exception e) {
            throw new TargetRecommendationsException(e);
        }
    }

    public static ProductFeed fromJson(JSONObject feedJson) throws JSONException {
        int feedId = -1;
        if (feedJson.has("id")) {
            feedId = feedJson.getInt("id");
        }
        String feedName = feedJson.getString("name");
        int feedEnvironmentId = -1;
        if (feedJson.has("environmentId")) {
            feedEnvironmentId = feedJson.getInt("environmentId");
        }
        String feedTypeStr = feedJson.getString("feedType");
        ProductFeed.FeedType feedType = FEED_MAPPING.get(feedTypeStr);
        String feedUpdateFrequencyStr = feedJson.getString("scheduleType");
        ProductFeed.FeedSchedule feedUpdateSchedule = ProductFeedImpl.getFeedScheduleFromString(feedUpdateFrequencyStr);
        HashMap<String, String> attributeMappings = new HashMap<String, String>();
        HashMap<String, String> connectionConfig = new HashMap<String, String>();
        if (feedJson.has("attributeMapping")) {
            JSONObject attributeMapJson = feedJson.getJSONObject("attributeMapping");
            Iterator attributeIterator = attributeMapJson.keys();
            while (attributeIterator.hasNext()) {
                String attributeName = (String)attributeIterator.next();
                String attributeMapValue = attributeMapJson.getString(attributeName);
                attributeMappings.put(attributeName, attributeMapValue);
            }
        }
        if (feedJson.has("connection")) {
            JSONObject connectionJson = feedJson.getJSONObject("connection");
            connectionConfig.put("type", connectionJson.getString("type"));
            JSONObject connectionConfigJson = connectionJson.getJSONObject("attributes");
            Iterator connectionConfigIterator = connectionConfigJson.keys();
            while (connectionConfigIterator.hasNext()) {
                String attributeName = (String)connectionConfigIterator.next();
                String attributeMapValue = connectionConfigJson.getString(attributeName);
                connectionConfig.put(attributeName, attributeMapValue);
            }
        }
        return new ProductFeedImpl(feedId, feedName, feedType, feedUpdateSchedule, feedEnvironmentId, attributeMappings, connectionConfig);
    }

    private static String getRecsFeedTypeString(ProductFeed.FeedType feedType) {
        switch (feedType) {
            case GOOGLE_PRODUCT: {
                return "google-atom-1";
            }
            case CSV: {
                return "csv";
            }
            case SITECATALYST_SAINT: {
                return "saint";
            }
        }
        return "";
    }

    private static ProductFeed.FeedSchedule getFeedScheduleFromString(String feedScheduleStr) {
        for (Map.Entry<ProductFeed.FeedSchedule, String> feedFrequencyEntry : FEED_FREQUENCY_MAPPING.entrySet()) {
            if (!feedScheduleStr.equalsIgnoreCase(feedFrequencyEntry.getValue())) continue;
            return feedFrequencyEntry.getKey();
        }
        return ProductFeed.FeedSchedule.NEVER;
    }

    static {
        FEED_MAPPING.put("google-atom-1", ProductFeed.FeedType.GOOGLE_PRODUCT);
        FEED_MAPPING.put("csv", ProductFeed.FeedType.CSV);
        FEED_MAPPING.put("saint", ProductFeed.FeedType.SITECATALYST_SAINT);
        FEED_FREQUENCY_MAPPING = new HashMap<ProductFeed.FeedSchedule, String>();
        FEED_FREQUENCY_MAPPING.put(ProductFeed.FeedSchedule.WEEKLY, "weekly");
        FEED_FREQUENCY_MAPPING.put(ProductFeed.FeedSchedule.BI_WEEKLY, "biweekly");
        FEED_FREQUENCY_MAPPING.put(ProductFeed.FeedSchedule.DAILY, "daily");
        FEED_FREQUENCY_MAPPING.put(ProductFeed.FeedSchedule.NEVER, "never");
    }

}