001/*
002 * (C) Copyright 2012 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *    mcedica@nuxeo.com
016 *
017 * $Id$
018 */
019package org.nuxeo.ecm.webapp.widgets;
020
021import java.io.Serializable;
022import java.util.Date;
023import java.util.Map;
024
025import javax.faces.application.FacesMessage;
026import javax.faces.component.UIComponent;
027import javax.faces.component.UIInput;
028import javax.faces.context.FacesContext;
029import javax.faces.validator.ValidatorException;
030
031import org.apache.commons.logging.Log;
032import org.apache.commons.logging.LogFactory;
033import org.jboss.seam.ScopeType;
034import org.jboss.seam.annotations.In;
035import org.jboss.seam.annotations.Name;
036import org.jboss.seam.annotations.Scope;
037
038/**
039 * Compares two dates in a date range widget and throws a validation error if the second date is not superior to the
040 * first date.
041 * <p>
042 * Looks up component ids by reytrieving attributes on the validated component, named "startDateComponentId" and
043 * "endDateComponentId".
044 *
045 * @since 5.7
046 */
047@Name("dateRangeValidator")
048@Scope(ScopeType.CONVERSATION)
049public class DateRangeValidator implements Serializable {
050
051    private static final long serialVersionUID = 1L;
052
053    @In(create = true)
054    protected Map<String, String> messages;
055
056    private static final Log log = LogFactory.getLog(DateRangeValidator.class);
057
058    public void validateDateRange(FacesContext context, UIComponent component, Object value) {
059        Map<String, Object> attributes = component.getAttributes();
060        String startDateComponentId = (String) attributes.get("startDateComponentId");
061        String endDateComponentId = (String) attributes.get("endDateComponentId");
062        if (startDateComponentId == null || endDateComponentId == null) {
063            return;
064        }
065
066        UIInput startDateComp = (UIInput) component.findComponent(startDateComponentId);
067        UIInput endDateComp = (UIInput) component.findComponent(endDateComponentId);
068        if (startDateComp == null) {
069            log.error("Can not find component with id " + startDateComponentId);
070            return;
071        }
072
073        if (endDateComp == null) {
074            log.error("Can not find component with id " + endDateComponentId);
075            return;
076        }
077        Date stratDate = (Date) startDateComp.getLocalValue();
078        Date endDate = (Date) endDateComp.getLocalValue();
079
080        if (stratDate != null && endDate != null && endDate.compareTo(stratDate) < 0) {
081            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, String.format(
082                    messages.get("error.dateRangeValidator.invalidDateRange"), stratDate, endDate), null);
083            throw new ValidatorException(message);
084        }
085    }
086}