AudienceManagerFolderImpl.java 6.35 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.jcr.JcrUtil
 *  com.day.cq.wcm.api.Page
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.PropertyIterator
 *  javax.jcr.RepositoryException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.apache.sling.jcr.resource.JcrPropertyMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.aam.client;

import com.adobe.cq.aam.client.AudienceManagerFolderList;
import com.adobe.cq.aam.client.RootFolder;
import com.adobe.cq.aam.client.spi.AudienceManagerConfiguration;
import com.adobe.cq.aam.client.spi.AudienceManagerFolder;
import com.adobe.cq.aam.client.spi.InvalidItemPage;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.wcm.api.Page;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.jcr.resource.JcrPropertyMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AudienceManagerFolderImpl
implements AudienceManagerFolder {
    private static final String[] NODE_PROPS = new String[]{"path", "pid", "folderId", "name"};
    private static final Logger LOGGER = LoggerFactory.getLogger(AudienceManagerFolderImpl.class);
    private String title;
    private String path;
    private String name;
    private String template;
    private long folderId;
    private Map<String, Object> toNode = new HashMap<String, Object>();
    private AudienceManagerFolderList folders;
    private long parentFolderId;
    private int depth;
    private Page segmentPage;

    public AudienceManagerFolderImpl(AudienceManagerFolderList referenceFolders, JSONObject json, String folderTemplate) throws JSONException {
        this.folders = referenceFolders;
        this.path = referenceFolders.getFolderPath(json.getString("path"));
        this.depth = this.path.split("/").length;
        this.folderId = json.getLong("folderId");
        this.parentFolderId = json.getLong("parentFolderId");
        this.title = json.getString("name");
        this.name = JcrUtil.createValidName((String)this.title);
        for (String k : NODE_PROPS) {
            this.toNode.put("aam_" + k, json.get(k));
        }
        this.template = folderTemplate;
    }

    public AudienceManagerFolderImpl(Page page) throws InvalidItemPage {
        Resource resource = page.getContentResource();
        if ("cq/personalization/audiencemanager/components/segmentation/aamsegmentpage".equals(resource.getResourceType())) {
            throw new InvalidItemPage("Page is not a Audience Manager Segment, incorrect resource type. " + page.getPath());
        }
        try {
            Node node = (Node)resource.adaptTo(Node.class);
            PropertyIterator pi = node.getProperties();
            while (pi.hasNext()) {
                Property p = pi.nextProperty();
                String pname = p.getName();
                if (!pname.startsWith("aam_")) continue;
                this.toNode.put(pname, this.getValue(p));
            }
        }
        catch (RepositoryException e) {
            LOGGER.debug(e.getMessage(), (Throwable)e);
        }
        if (!this.toNode.containsKey("aam_folderId") || this.toNode.containsKey("aam_sid")) {
            throw new InvalidItemPage("Page is not a Audience Manager Segment Folder, no folderId " + page.getPath());
        }
        this.path = page.getPath();
        this.folderId = this.toLong(this.toNode.get("folderId"));
        this.title = (String)this.toNode.get("aam_name");
        this.name = JcrUtil.createValidName((String)this.title);
        this.segmentPage = page.getParent();
    }

    private long toLong(Object object) {
        if (object instanceof Long) {
            return (Long)object;
        }
        if (object instanceof Double) {
            double d = (Double)object;
            return (long)d;
        }
        LOGGER.debug("Failed, object is {} {} ", object, object.getClass());
        return Long.parseLong(String.valueOf(object));
    }

    private Object getValue(Property p) throws RepositoryException {
        if (p.getType() == 3 || p.getType() == 4) {
            return p.getLong();
        }
        return p.getString();
    }

    @Override
    public String getPath() {
        return this.path;
    }

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

    @Override
    public String getTitle() {
        return this.title;
    }

    @Override
    public String getTemplate() {
        return this.template;
    }

    @Override
    public long getFolderId() {
        return this.folderId;
    }

    @Override
    public Set<Map.Entry<String, Object>> getEntrySet() {
        return this.toNode.entrySet();
    }

    @Override
    public String getAbsolutePath(AudienceManagerConfiguration config) {
        return config.getTarget() + "/" + this.path;
    }

    @Override
    public AudienceManagerFolder getParent() {
        if (this.segmentPage != null) {
            try {
                return new AudienceManagerFolderImpl(this.segmentPage.getParent());
            }
            catch (InvalidItemPage e) {
                return new RootFolder(this.segmentPage.getParent());
            }
        }
        if (this.parentFolderId == this.folderId) {
            return new RootFolder(this.folders);
        }
        return this.folders.getFolder(this.parentFolderId);
    }

    @Override
    public long getParentFolderId() {
        if (this.segmentPage != null) {
            return (Long)new JcrPropertyMap((Node)this.segmentPage.getParent().getContentResource().adaptTo(Node.class)).get((Object)"aam_folderId");
        }
        return this.parentFolderId;
    }

    @Override
    public int getDepth() {
        return this.depth;
    }

    public String toString() {
        return this.path;
    }

    public int hashCode() {
        return this.path.hashCode();
    }

    public boolean equals(Object obj) {
        if (obj instanceof AudienceManagerFolderImpl) {
            return this.path.equals(((AudienceManagerFolderImpl)obj).path);
        }
        return super.equals(obj);
    }
}