Search in sources :

Example 1 with InputDate

use of com.liferay.faces.alloy.component.inputdate.InputDate in project liferay-faces-alloy by liferay.

the class WeekdayValidator method validate.

@Override
public void validate(FacesContext facesContext, UIComponent uiComponent, Object object) throws ValidatorException {
    if (object != null) {
        Date date;
        InputDate inputDate = (InputDate) uiComponent;
        String timeZoneString = inputDate.getTimeZone();
        TimeZone timeZone = TimeZone.getTimeZone(timeZoneString);
        if (object instanceof Date) {
            date = (Date) object;
        } else {
            String dateString = object.toString();
            String datePattern = inputDate.getPattern();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern);
            simpleDateFormat.setTimeZone(timeZone);
            try {
                date = simpleDateFormat.parse(dateString);
            } catch (ParseException e) {
                String message = AlloyValidatorHelper.getMessage(facesContext, inputDate, "invalid-selection");
                FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message);
                throw new ValidatorException(facesMessage, e);
            }
        }
        Calendar calendar = new GregorianCalendar(timeZone);
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if ((dayOfWeek == Calendar.SATURDAY) || (dayOfWeek == Calendar.SUNDAY)) {
            String message = AlloyValidatorHelper.getMessage(facesContext, inputDate, "sat-and-sun-are-not-valid");
            FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_WARN, message, message);
            throw new ValidatorException(facesMessage);
        }
    }
}
Also used : InputDate(com.liferay.faces.alloy.component.inputdate.InputDate) TimeZone(java.util.TimeZone) ValidatorException(javax.faces.validator.ValidatorException) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) FacesMessage(javax.faces.application.FacesMessage) Date(java.util.Date) InputDate(com.liferay.faces.alloy.component.inputdate.InputDate)

Example 2 with InputDate

use of com.liferay.faces.alloy.component.inputdate.InputDate in project liferay-faces-alloy by liferay.

the class InputDateRenderer method getAlloyClassName.

@Override
public String getAlloyClassName(FacesContext facesContext, UIComponent uiComponent) {
    String alloyClassName = super.getAlloyClassName(facesContext, uiComponent);
    BrowserSniffer browserSniffer = BrowserSnifferFactory.getBrowserSnifferInstance(facesContext.getExternalContext());
    InputDate inputDate = (InputDate) uiComponent;
    if (isNative(browserSniffer, inputDate)) {
        alloyClassName = alloyClassName.concat("Native");
    }
    return alloyClassName;
}
Also used : InputDate(com.liferay.faces.alloy.component.inputdate.InputDate) BrowserSniffer(com.liferay.faces.util.client.BrowserSniffer)

Example 3 with InputDate

use of com.liferay.faces.alloy.component.inputdate.InputDate in project liferay-faces-alloy by liferay.

the class InputDateRenderer method encodeJavaScriptCustom.

@Override
public void encodeJavaScriptCustom(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    BrowserSniffer browserSniffer = BrowserSnifferFactory.getBrowserSnifferInstance(facesContext.getExternalContext());
    InputDate inputDate = (InputDate) uiComponent;
    String showOn = inputDate.getShowOn();
    if (isNative(browserSniffer, inputDate) || "button".equals(showOn)) {
        String clientVarName = getClientVarName(facesContext, inputDate);
        String clientKey = inputDate.getClientKey();
        if (clientKey == null) {
            clientKey = clientVarName;
        }
        JavaScriptFragment liferayComponent = new JavaScriptFragment("Liferay.component('" + clientKey + "')");
        ResponseWriter responseWriter = facesContext.getResponseWriter();
        if (isNative(browserSniffer, inputDate)) {
            String clientId = uiComponent.getClientId(facesContext);
            String inputClientId = clientId.concat(INPUT_SUFFIX);
            Object maxDateObject = inputDate.getMaxDate();
            Object minDateObject = inputDate.getMinDate();
            String maxDateString = null;
            String minDateString = null;
            if ((maxDateObject != null) || (minDateObject != null)) {
                String datePattern = inputDate.getPattern();
                String timeZoneString = inputDate.getTimeZone();
                TimeZone timeZone = TimeZone.getTimeZone(timeZoneString);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(InputDate.DEFAULT_HTML5_DATE_PATTERN, Locale.ENGLISH);
                if (maxDateObject != null) {
                    Date maxDate = inputDate.getObjectAsDate(maxDateObject, datePattern, timeZone);
                    maxDateString = simpleDateFormat.format(maxDate);
                }
                if (maxDateObject != null) {
                    Date minDate = inputDate.getObjectAsDate(minDateObject, datePattern, timeZone);
                    minDateString = simpleDateFormat.format(minDate);
                }
            }
            encodeFunctionCall(responseWriter, "LFAI.initDateTimePickerMobile", liferayComponent, inputClientId, maxDateString, minDateString);
        } else if ("button".equals(showOn)) {
            String clientId = inputDate.getClientId(facesContext);
            String inputClientId = clientId.concat(INPUT_SUFFIX);
            EscapedClientId escapedInputClientId = new EscapedClientId(inputClientId);
            encodeFunctionCall(responseWriter, "LFAI.initDatePickerShowOnButton", 'A', escapedInputClientId, liferayComponent);
        }
    }
}
Also used : InputDate(com.liferay.faces.alloy.component.inputdate.InputDate) BrowserSniffer(com.liferay.faces.util.client.BrowserSniffer) TimeZone(java.util.TimeZone) ResponseWriter(javax.faces.context.ResponseWriter) InputDateTimeResponseWriter(com.liferay.faces.alloy.component.inputdatetime.internal.InputDateTimeResponseWriter) JavaScriptFragment(com.liferay.faces.util.render.JavaScriptFragment) EscapedClientId(com.liferay.faces.alloy.render.internal.EscapedClientId) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) InputDate(com.liferay.faces.alloy.component.inputdate.InputDate)

Example 4 with InputDate

use of com.liferay.faces.alloy.component.inputdate.InputDate in project liferay-faces-alloy by liferay.

the class InputDateRenderer method getModules.

@Override
public String[] getModules(FacesContext facesContext, UIComponent uiComponent) {
    String[] modules = getModules(MODULES[0], facesContext, uiComponent);
    InputDate inputDate = (InputDate) uiComponent;
    String showOn = inputDate.getShowOn();
    if ("button".equals(showOn)) {
        modules = StringHelper.append(modules, "aui-datatype-date-parse");
    }
    return modules;
}
Also used : InputDate(com.liferay.faces.alloy.component.inputdate.InputDate)

Example 5 with InputDate

use of com.liferay.faces.alloy.component.inputdate.InputDate in project liferay-faces-alloy by liferay.

the class InputDateRenderer method encodeDate.

protected void encodeDate(ResponseWriter responseWriter, InputDate inputDate, String attributeName, Object dateObject, boolean first) throws IOException {
    String datePattern = inputDate.getPattern();
    String timeZoneString = inputDate.getTimeZone();
    TimeZone timeZone = TimeZone.getTimeZone(timeZoneString);
    Date date = inputDate.getObjectAsDate(dateObject, datePattern, timeZone);
    // Note: The JavaScript date object expects zero-based month numbers, so it is necessary to offset the month
    // by 1.
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("'new Date'(yyyy,M-1,d,0,0,0,0)");
    simpleDateFormat.setTimeZone(timeZone);
    String dateString = simpleDateFormat.format(date);
    encodeNonEscapedObject(responseWriter, attributeName, dateString, first);
}
Also used : TimeZone(java.util.TimeZone) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) InputDate(com.liferay.faces.alloy.component.inputdate.InputDate)

Aggregations

InputDate (com.liferay.faces.alloy.component.inputdate.InputDate)6 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 TimeZone (java.util.TimeZone)3 BrowserSniffer (com.liferay.faces.util.client.BrowserSniffer)2 InputDateTimeResponseWriter (com.liferay.faces.alloy.component.inputdatetime.internal.InputDateTimeResponseWriter)1 EscapedClientId (com.liferay.faces.alloy.render.internal.EscapedClientId)1 JavaScriptFragment (com.liferay.faces.util.render.JavaScriptFragment)1 ParseException (java.text.ParseException)1 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 FacesMessage (javax.faces.application.FacesMessage)1 ResponseWriter (javax.faces.context.ResponseWriter)1 ValidatorException (javax.faces.validator.ValidatorException)1