All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.cq.editor.model.asset.mapping.ComponentDropTarget Maven / Gradle / Ivy

/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2016 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 *
 **************************************************************************/
package com.adobe.cq.editor.model.asset.mapping;

import java.io.StringWriter;
import java.util.Collection;
import java.util.Iterator;

import com.day.cq.wcm.api.components.Component;
import com.day.cq.wcm.api.components.DropTarget;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.json.io.JSONWriter;

/**
 * Represents a component and its drop targets
 *
 * This object is suitable for being used by list as it can be flagged as selected
 */
public final class ComponentDropTarget {

    private Component component;

    private Collection dropTargets;

    private boolean selected;

    private String[] searchPaths = {};

    public ComponentDropTarget(Component component, Collection dropTargets) {
        this.component = component;

        if (component != null) {
            Resource componentResource = component.adaptTo(Resource.class);
            searchPaths = componentResource.getResourceResolver().getSearchPath();
        }

        this.dropTargets = dropTargets;
    }

    /**
     * Returns a relative path extracted from a given absolute path
     *
     * @param path      - Absolute path
     * @return
     */
    private String getRelativePath(String path) {
        for (String searchPath : searchPaths) {
            if (path.startsWith(searchPath)) {
                return path.replace(searchPath, "");
            }
        }

        return path;
    }

    /**
     * The {@link Component} object
     *
     * @return
     */
    Component getComponent() {
        return component;
    }

    /**
     * Set the {@link Component} selected
     *
     * @param selected
     */
    void setSelected(boolean selected) {
        this.selected = selected;
    }

    /**
     * Is the {@link Component} selected ?
     *
     * @return
     */
    public boolean isSelected() {
        return selected;
    }

    /**
     * The title of the {@link Component}
     * @return
     */
    public String getTitle() {
        return component.getTitle();
    }

    /**
     * The relative resource type of the {@link Component}
     *
     * @return
     */
    public String getResourceType() {
        return getRelativePath(component.getResourceType());
    }

    /**
     * The component group of the {@link Component}
     *
     * @return
     */
    public String getGroup() {
        return component.getComponentGroup();
    }

    /**
     * Returns the list of {@link DropTarget}s of the current {@link Component}
     * @return
     */
    public Collection getDropTargets() {
        return dropTargets;
    }

    /**
     * Returns the JSON formatted list of drop targets
     *
     * @return
     * @throws JSONException
     */
    public String getDropTargetsData() throws JSONException {
        StringWriter sw = new StringWriter();
        JSONWriter jw = new JSONWriter(sw);

        jw.object();
        JSONArray dropTargetsJs = new JSONArray();

        Iterator dropTargetIterator = dropTargets.iterator();
        while (dropTargetIterator.hasNext()) {
            DropTarget dropTarget = dropTargetIterator.next();

            JSONObject dropTargetJs = new JSONObject();

            dropTargetJs.put("name", dropTarget.getName());


            if (dropTarget.getAccept() != null) {
                JSONArray acceptsJs = new JSONArray();

                for (String accept : dropTarget.getAccept()) {
                    acceptsJs.put(accept);
                }

                dropTargetJs.put("accepts", acceptsJs);
            }

            if (dropTarget.getGroups() != null) {
                JSONArray groupsJs = new JSONArray();

                for (String group : dropTarget.getGroups()) {
                    groupsJs.put(group);
                }

                dropTargetJs.put("groups", groupsJs);
            }

            dropTargetsJs.put(dropTargetJs);
        }

        jw.key("dropTargets").value(dropTargetsJs);
        jw.endObject();
        return sw.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy