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

com.adobe.aemds.guide.common.AbstractFDField Maven / Gradle / Ivy

/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2017 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.aemds.guide.common;


import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.PostConstruct;

import com.adobe.aemds.guide.utils.GuideConstants;
import com.adobe.aemds.guide.utils.GuideUtils;
import com.adobe.aemds.guide.utils.NodeStructureUtils;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.api.WCMMode;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import com.day.cq.commons.jcr.JcrConstants;
import org.osgi.annotation.versioning.ConsumerType;

/**
 * Abstract implementation of a forms and document field component
 */
@ConsumerType
public abstract class AbstractFDField implements FDField {

    @Self
    protected SlingHttpServletRequest slingRequest;

    @SlingObject
    protected Resource resource;

    @ScriptVariable
    protected ValueMap properties;

    protected I18n i18n;

    @PostConstruct
    private void initModel() {
        i18n = GuideUtils.getI18n(slingRequest, resource);
    }

    /**
     * {@inheritDoc}
     */
    @Nonnull
    @Override
    public String getTitle() {
        return externalize(properties.get(JcrConstants.JCR_TITLE, ""));
    }

    /**
     * {@inheritDoc}
     */
    @Nonnull
    @Override
    public String getPath() {
        return resource.getPath();
    }

    /**
     * {@inheritDoc}
     */
    @Nonnull
    @Override
    public String getName() {
        return StringEscapeUtils.escapeHtml4(NodeStructureUtils.getGuideName(resource));
    }

    /**
     * {@inheritDoc}
     */
    @Nullable
    @Override
    public String getDescription() {
        return externalize(properties.get(JcrConstants.JCR_DESCRIPTION, ""));
    }

    /**
     * {@inheritDoc}
     */
    @Nonnull
    @Override
    public String getId() {
        return NodeStructureUtils.getGuideNodeHtmlId(resource);
    }

    /**
     * {@inheritDoc}
     */
    @Nonnull
    @Override
    public String getGuideFieldType() {
        return properties.get(GuideConstants.GUIDE_NODE_CLASS, "");
    }

    /**
     * Externalizes/Localizes the given key
     * @param key key to externalized
     * @return externalized string
     */
    @Nullable
    protected String externalize(@Nonnull String key) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        //if author mode return key
        boolean editMode = WCMMode.fromRequest(slingRequest) == WCMMode.EDIT || WCMMode.fromRequest(slingRequest)== WCMMode.DESIGN;
        if(editMode) {
            return key;
        } else {
            return GuideUtils.translateOrReturnOriginal(key, i18n);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy