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

org.dd4t.mvc.utils.XPMRenderer Maven / Gradle / Ivy

There is a newer version: 2.1.12
Show newest version
package org.dd4t.mvc.utils;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Required;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.util.Arrays;
import java.util.GregorianCalendar;
import java.util.Map;

/**
 * dd4t-parent
 *
 * @author M.Cadariu, R. Kempees
 */
public class XPMRenderer {

    private static final XPMRenderer INSTANCE = new XPMRenderer();
    private static final Logger LOG = LoggerFactory.getLogger(XPMRenderer.class);
    public static final String XPM_BOOTSTRAP_PATH = "WebUI/Editors/SiteEdit/Views/Bootstrap/Bootstrap.aspx?mode=js";

    private boolean enabled;
    private String cmsURL;

    private XPMRenderer() {
    }

    public static XPMRenderer getInstance() {
        return INSTANCE;
    }

    public static boolean isXPMEnabled() {
        return INSTANCE.enabled;
    }

    public static boolean isXPMActive() {
        // for now we don't have a good server-side check if xpm editing is really active.
        return isXPMEnabled();
    }

    public void init(final boolean enabled, final String cmsURL) {
        this.enabled = enabled;
        this.cmsURL = cmsURL;
    }

    /**
     * Removes the type identifier from the id if present
     */
    private static String stripTypeIdentifier(String componentId) {
        if (componentId.endsWith("-16")) {
            return componentId.substring(0, componentId.length() - 3);
        } else {
            return componentId;
        }
    }

    /**
     * 
     */
    public String componentPresentation(String componentId, DateTime lastModified, String componentTemplateId,
                                        boolean isDynamic) {
        if (isXPMEnabled()) {
            return "";
        }
        return "";
    }

    /**
     * 
     */
    public String region(final String title, Map allowedComponentTypes, int minOccurs, int maxOccurs) {
        if (isXPMEnabled()) {
            return "";
        }

        return "";
    }

    /**
     * 
     */
    public String componentField(String xPath, boolean multiValued, int index) {
        if (isXPMEnabled()) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("");

            return stringBuilder.toString();
        }

        return "";
    }

    /**
     * 
     */
    public String initPage(String pageId, DateTime revisionDate, String pageTemplateId) {
        if (isXPMEnabled()) {
            return "\n" + getTag();
        }
        return "";
    }

    public String getTag() {
        return "";
    }

    public String getTagLazyLoaded() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("\n");
        return stringBuilder.toString();

    }

    private String formatAllowedComponentTypes(Map allowedComponentTypes) {
        final String format = "{ \"schema\": \"%s\", \"template\": \"%s\" }";
        if (null != allowedComponentTypes) {
            String[] allowed = new String[allowedComponentTypes.keySet().size()];
            int i = 0;
            for (Map.Entry entry : allowedComponentTypes.entrySet()) {
                allowed[i++] = String.format(format, entry.getKey(), entry.getValue());
            }
            return Arrays.toString(allowed);
        }

        return "";
    }

    public String getXMLDateAsString(DateTime date) {
        try {
            GregorianCalendar c = date.toGregorianCalendar();
            XMLGregorianCalendar xmlDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
            xmlDate.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
            xmlDate.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
            return xmlDate.toXMLFormat();
        } catch (DatatypeConfigurationException e) {
            LOG.error(e.getLocalizedMessage(), e);
        }
        return "";
    }

    @Required
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    @Required
    public void setCmsUrl(String cmsURL) {
        this.cmsURL = cmsURL;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy