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

com.adobe.cq.editor.model.asset.mapping.AssetToComponentMapping 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.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import com.day.cq.wcm.api.components.DropTarget;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;

/**
 * Mapping for allowing the creation of a component from an asset
 */
public final class AssetToComponentMapping {

    private static final String ASSET_TO_COMPONENT_MAPPING_RELATIVE_PATH = "./cq:authoring/assetToComponentMapping/";

    private String name;

    private ComponentDropTarget selectedComponentDropTarget;

    private String resourceType;

    private String assetGroup;

    private DropTarget selectedDropTarget;

    private String[] mimeTypes;

    private List componentDropTargets;

    public AssetToComponentMapping(Resource resource, List componentDropTargets) {
        ValueMap properties = resource.getValueMap();

        this.name = resource.getName();
        this.componentDropTargets = componentDropTargets;

        this.resourceType = properties.get("resourceType", String.class);
        this.assetGroup = properties.get("assetGroup", String.class);
        this.mimeTypes = properties.get("assetMimetype", String[].class);

        String selectedDropTargetValue = properties.get("droptarget", String.class);

        for (ComponentDropTarget componentDropTarget : componentDropTargets) {
            Collection dropTargets = componentDropTarget.getDropTargets();
            Iterator dropTargetIterator = dropTargets.iterator();

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

                String providedResourceType = componentDropTarget.getResourceType();
                List providedAssetGroups = Arrays.asList(dropTarget.getGroups());
                String providedDropTargetName = dropTarget.getName();

                if (StringUtils.isEmpty(providedResourceType) || providedAssetGroups.isEmpty() || StringUtils.isEmpty(providedDropTargetName)) {
                    continue;
                }

                // Mark the current component drop target as selected if it has the corresponding name, group and resource type
                if (providedResourceType.equals(this.resourceType) &&
                        providedAssetGroups.indexOf(this.assetGroup) > -1 &&
                        providedDropTargetName.equals(selectedDropTargetValue)) {
                    this.selectedComponentDropTarget = componentDropTarget;
                    this.selectedDropTarget = dropTarget;
                    componentDropTarget.setSelected(true);
                    break;
                }
            }
        }
    }

    /**
     * Name of the mapping correspond to the name of the node of type {@code cq:DropTargetConfig}
     *
     * @return
     */
    public String getName() {
        return name;
    }

    /**
     * Human readable label of the mapping
     *
     * @return
     */
    public String getLabel() {
        if (selectedComponentDropTarget == null) {
            return null;
        }

        return selectedComponentDropTarget.getTitle() + " - " + selectedComponentDropTarget.getGroup();
    }

    /**
     * Relative path of the mapping
     *
     * @return
     */
    public String getRelativePath() {
        return ASSET_TO_COMPONENT_MAPPING_RELATIVE_PATH + name;
    }

    /**
     * Resource type referenced by the mapping
     * @return
     */
    public String getResourceType() {
        return resourceType;
    }

    /**
     * Asset group to which belongs the drop target
     *
     * @return
     */
    public String getAssetGroup() {
        return assetGroup;
    }

    /**
     * List of all the {@link ComponentDropTarget}s
     *
     * @return
     */
    public Collection getComponentDropTargets() {
        return componentDropTargets;
    }

    /**
     * Selected {@link DropTarget}
     *
     * @return
     */
    public DropTarget getSelectedDropTarget() {
        return selectedDropTarget;
    }

    /**
     * Selected {@link ComponentDropTarget}
     *
     * @return
     */
    public ComponentDropTarget getSelectedComponentDropTarget() {
        return selectedComponentDropTarget;
    }

    /**
     * List of all the defined mime types for the current mapping
     *
     * @return
     */
    public String[] getMimeTypes() {
        return mimeTypes;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy