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

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

/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2014 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 may be covered by U.S. and Foreign Patents,
 * patents in process, 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 com.adobe.aemds.guide.utils.GuideConstants;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.commons.json.io.JSONWriter;
import java.io.StringWriter;
import java.lang.Boolean;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParseException;
import java.util.Calendar;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.aemds.guide.service.GuideException;

import javax.jcr.RepositoryException;

/**
 * GuideDatePicker encapsulates basic properties of the adaptive forms DatePicker Component.
 */
public class GuideDatePicker extends GuideField{

    protected Logger logger = LoggerFactory.getLogger(GuideDatePicker.class);
    /**
     * Returns the field type of the Adaptive Forms Component
     * @return String representing guide field type
     */
    public String getGuideFieldType(){
        return GuideConstants.GUIDE_FIELD_DATEPICKER;
    }

    /**
     * Returns the options for passing to Adaptive Forms Date Input Component
     * @return String returning options
     */
    public String getDateInputOptions(){
        StringWriter jsonStringWriter = new StringWriter();
        JSONWriter writer = new JSONWriter(jsonStringWriter);
        try {
          writer.object();
          writer.key("dateFormat").value(resourceProps.get(GuideConstants.DATE_DISPLAY_FORMAT, "date{M/D/YYYY}"));
          writer.key("placeholderMonth").value(externalize(resourceProps.get(GuideConstants.PLACEHOLDER_MONTH,"")));
          writer.key("placeholderDay").value(externalize(resourceProps.get(GuideConstants.PLACEHOLDER_DAY,"")));
          writer.key("placeholderYear").value(externalize(resourceProps.get(GuideConstants.PLACEHOLDER_YEAR,"")));
          writer.key("labelMonth").value(externalize(resourceProps.get(GuideConstants.TITLE_MONTH,"")));
          writer.key("labelDay").value(externalize(resourceProps.get(GuideConstants.TITLE_DAY,"")));
          writer.key("labelYear").value(externalize(resourceProps.get(GuideConstants.TITLE_YEAR,"")));
          writer.key("hideLabels").value(checkIfDateInputTitleHidden());
          writer.endObject();
        } catch (Exception e) {
            throw new GuideException(e);
        }
        return jsonStringWriter.toString();
    }
    /**
     * Returns if labels to each date Input are necessary
     * @return Boolean representing if labels to the date inputs are required
     */
    public Boolean checkIfDateInputTitleHidden(){
        return resourceProps.get("hideTitleDate", false);
    }

    /**
     * Returns the class to be used on the wrapper depending if labels are required
     * @return String representing the class
     */
    public String getHeightOfDiv(){
        if(!checkIfDateInputTitleHidden()){
           return "showDateInputLabels";
        }else {
           return "";
        }
    }

    /**
     * Returns boolean if maximum date is excluded from range of valid dates.
     * @return Boolean if maximum date is excluded from range of valid dates.
     */
    public Boolean isMaxDateExcluded() {
        return resourceProps.get("exclusiveMaximum", false);
    }

    /**
     * Returns boolean if minimum date is excluded from range of valid dates.
     * @return Boolean if minimum date is excluded from range of valid dates.
     */
    public Boolean isMinDateExcluded() {
        return resourceProps.get("exclusiveMinimum", false);
    }

    /**
     * Returns maximum valid date.
     * @return String maximum date which is valid.
     */
    public String getMaximumDate() {
        String maxDateInString = resourceProps.get("maximum", "");
        if (!maxDateInString.isEmpty() && isMaxDateExcluded()) {
            Date maxDate = maxDate = convertStringToDate(maxDateInString);
            maxDate = addDaysToDate(maxDate, -1);
            maxDateInString = convertDateToString(maxDate);
        }
        return maxDateInString;
    }

    /**
     * Returns minimum valid date.
     * @return String minimum date which is valid.
     */
    public String getMinimumDate() {
        String minDateInString = resourceProps.get("minimum", "");
        if (!minDateInString.isEmpty() && isMinDateExcluded()) {
            Date minDate = convertStringToDate(minDateInString);
            minDate = addDaysToDate(minDate, 1);
            minDateInString = convertDateToString(minDate);
        }
        return minDateInString;
    }

    /**
     * Utility function to convert string value to date.
     * @param dateInString value of date in String.
     * @return date string converted to date format.
     */
    private Date convertStringToDate(String dateInString) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date = formatter.parse(dateInString);
        } catch (ParseException e) {
            logger.error("Error in converting date picker string to date" + e.getMessage());
        }
        return date;
    }

    /**
     * Utility function to convert date to string.
     * @param date value of date in date format.
     * @return dateInString date converted to string
     */
    private String convertDateToString(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateInString;
        dateInString = formatter.format(date);
        return dateInString;
    }

    /**
     * Utility function to add or subtract days in date.
     * @param date the base date to which days are to be added.
     * @param days number of days to be added or substracted from date.
     * @return newDate which is computed after adding/substracting given days.
     */
    private Date addDaysToDate(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
        return cal.getTime();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy