AudienceManagerFolderList.java 3.95 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.jcr.JcrUtil
 *  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.aam.client;

import com.adobe.cq.aam.client.AudienceManagerFolderImpl;
import com.adobe.cq.aam.client.RootFolder;
import com.adobe.cq.aam.client.spi.AudienceManagerFolder;
import com.adobe.cq.aam.client.spi.AudienceManagerFolders;
import com.day.cq.commons.jcr.JcrUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
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 AudienceManagerFolderList
extends ArrayList<AudienceManagerFolder>
implements AudienceManagerFolders {
    private static final long serialVersionUID = 3321320093760648696L;
    private static final Logger LOGGER = LoggerFactory.getLogger(AudienceManagerFolderList.class);
    private String template;
    private Map<Long, AudienceManagerFolder> folders = new HashMap<Long, AudienceManagerFolder>();
    private AudienceManagerFolder defaultRoot;

    public AudienceManagerFolderList(JSONArray jsonArray, String folderTemplate) throws JSONException {
        this.template = folderTemplate;
        this.add(jsonArray);
        Collections.sort(this, new Comparator<AudienceManagerFolder>(){

            @Override
            public int compare(AudienceManagerFolder before, AudienceManagerFolder after) {
                return before.getDepth() - after.getDepth();
            }
        });
        LOGGER.debug("Folder list {} ", (Object)this);
        ArrayList<AudienceManagerFolder> roots = new ArrayList<AudienceManagerFolder>();
        for (Map.Entry<Long, AudienceManagerFolder> e : this.folders.entrySet()) {
            if (this.folders.containsKey(e.getValue().getParentFolderId())) continue;
            roots.add(e.getValue());
        }
        if (roots.size() > 0) {
            this.defaultRoot = (AudienceManagerFolder)roots.get(0);
        } else if (this.size() > 0) {
            this.defaultRoot = (AudienceManagerFolder)this.get(0);
            for (AudienceManagerFolder f : this) {
                if (!(f.getParent() instanceof RootFolder)) continue;
                this.defaultRoot = f;
                break;
            }
        }
    }

    private void add(JSONArray jsonArray) throws JSONException {
        for (int i = 0; i < jsonArray.length(); ++i) {
            this.add(jsonArray.getJSONObject(i));
        }
    }

    private void add(JSONObject json) throws JSONException {
        AudienceManagerFolderImpl am = new AudienceManagerFolderImpl(this, json, this.template);
        this.add(am);
        this.folders.put(am.getFolderId(), am);
        if (json.has("subFolders")) {
            this.add(json.getJSONArray("subFolders"));
        }
    }

    @Override
    public AudienceManagerFolder getFolder(long folderId) {
        AudienceManagerFolder folder = this.folders.get(folderId);
        if (folder == null) {
            folder = this.defaultRoot;
        }
        return folder;
    }

    public String getFolderPath(String folderSubPath) {
        return this.createValidPath(folderSubPath);
    }

    private String createValidPath(String invaldPath) {
        if (invaldPath == null) {
            return null;
        }
        String[] parts = invaldPath.split("/");
        StringBuilder sb = new StringBuilder();
        for (String p : parts) {
            if (p == null || p.trim().length() <= 0) continue;
            if (sb.length() > 0) {
                sb.append("/");
            }
            sb.append(JcrUtil.createValidName((String)p));
        }
        return sb.toString();
    }

}