LaunchesCommand.java 4.18 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.launches.api.LaunchException
 *  com.adobe.cq.launches.api.LaunchManagerFactory
 *  com.day.cq.wcm.api.commands.WCMCommand
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.joda.time.DateTime
 *  org.joda.time.format.DateTimeFormatter
 *  org.joda.time.format.ISODateTimeFormat
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.wcm.launches.impl.commands;

import com.adobe.cq.launches.api.LaunchException;
import com.adobe.cq.launches.api.LaunchManagerFactory;
import com.day.cq.wcm.api.commands.WCMCommand;
import java.util.Calendar;
import java.util.Locale;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(componentAbstract=1)
@Service
public abstract class LaunchesCommand
implements WCMCommand {
    private static final Logger log = LoggerFactory.getLogger(LaunchesCommand.class);
    @Reference
    LaunchManagerFactory launchesManagerFactory = null;
    protected static DateTimeFormatter TIME_FORMAT = ISODateTimeFormat.dateTime();
    protected static final String LIVE_DATE_PARAM = "liveDate";
    protected static final String IS_LIVE_COPY_PARAM = "isLiveCopy";
    protected static final String SOURCE_ROLLOUT_CONFIGS_PARAM = "sourceRolloutConfigs";
    protected static final String PROMOTE_ROLLOUT_CONFIGS_PARAM = "promoteRolloutConfigs";
    protected static final String TEMPLATE_PARAM = "template";

    protected final String getStringParam(SlingHttpServletRequest request, String name, String description, boolean isMandatory, String defaultValue) throws LaunchException {
        String valueAsString = request.getParameter(name);
        if (StringUtils.isNotEmpty((String)valueAsString)) {
            return valueAsString;
        }
        if (isMandatory) {
            throw new LaunchException("Error during operation. Mandatory parameter is missing: " + description);
        }
        return defaultValue;
    }

    protected final Calendar getCalendarParam(SlingHttpServletRequest request, String name, String description, boolean isMandatory, Calendar defaultValue) throws LaunchException {
        String valueAsString = request.getParameter(name);
        if (StringUtils.isNotEmpty((String)valueAsString)) {
            try {
                return TIME_FORMAT.parseDateTime(valueAsString).toCalendar(request.getLocale());
            }
            catch (IllegalArgumentException e) {
                throw new LaunchException("Error during operation. Unable to parse " + description + ": " + valueAsString);
            }
        }
        if (isMandatory) {
            throw new LaunchException("Error during operation. Mandatory parameter is missing: " + description);
        }
        return defaultValue;
    }

    protected final boolean getBooleanParam(SlingHttpServletRequest request, String name, String description, boolean isMandatory, boolean defaultValue) throws LaunchException {
        String valueAsString = request.getParameter(name);
        if (StringUtils.isNotEmpty((String)valueAsString)) {
            return "true".equals(valueAsString);
        }
        if (isMandatory) {
            throw new LaunchException("Error during operation. Mandatory parameter is missing: " + description);
        }
        return defaultValue;
    }

    protected void bindLaunchesManagerFactory(LaunchManagerFactory launchManagerFactory) {
        this.launchesManagerFactory = launchManagerFactory;
    }

    protected void unbindLaunchesManagerFactory(LaunchManagerFactory launchManagerFactory) {
        if (this.launchesManagerFactory == launchManagerFactory) {
            this.launchesManagerFactory = null;
        }
    }
}