001/*
002 * (C) Copyright 2010 Nuxeo SA (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 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.ui.web.validator;
018
019import java.util.ArrayList;
020import java.util.List;
021import java.util.Map;
022
023import javax.faces.component.UIComponent;
024import javax.faces.context.FacesContext;
025import javax.faces.validator.Validator;
026import javax.faces.validator.ValidatorException;
027
028import org.apache.commons.lang.StringUtils;
029import org.nuxeo.ecm.core.api.SortInfo;
030
031import com.sun.faces.util.MessageFactory;
032
033/**
034 * Validator for a list of {@link SortInfo} elements, checking that there is no conflicting sort information (several
035 * sorts on same criterion)
036 *
037 * @author Anahide Tchertchian
038 */
039public class SortInfoListValidator implements Validator {
040
041    public static final String VALIDATOR_ID = "SortInfoListValidator";
042
043    /**
044     * The message identifier of the {@link javax.faces.application.FacesMessage} to be created if the value to validate
045     * is not a list of sort infos.
046     */
047    public static final String INVALID_VALUE_MESSAGE_ID = "error.sortInfoValidator.invalidValue";
048
049    /**
050     * The message identifier of the {@link javax.faces.application.FacesMessage} to be created if the value to validate
051     * is a list of sort infos with conflicting criteria (several sorts on the same criterion).
052     * <p>
053     * The message format string for this message may optionally include the following placeholders:
054     * <ul>
055     * <li><code>{0}</code> replaced by the first found duplicate criterion.</li>
056     * </ul>
057     */
058    public static final String CONFLICTING_CRITERIA_MESSAGE_ID = "error.sortInfoValidator.conflictingCriteria";
059
060    /**
061     * The message identifier of the {@link javax.faces.application.FacesMessage} to be created if the value to validate
062     * contains a sort info with an empty sort criterion.
063     */
064    public static final String EMPTY_CRITERION_MESSAGE_ID = "error.sortInfoValidator.emptyCriterion";
065
066    @Override
067    @SuppressWarnings({ "unchecked", "rawtypes" })
068    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
069        if (context == null || component == null) {
070            throw new IllegalArgumentException();
071        }
072        if (value != null) {
073            if (value instanceof List) {
074                try {
075                    List sortInfos = (List) value;
076                    List<String> criteria = new ArrayList<String>();
077                    for (Object sortInfo : sortInfos) {
078                        String criterion = null;
079                        if (sortInfo instanceof SortInfo) {
080                            criterion = ((SortInfo) sortInfo).getSortColumn();
081                        } else {
082                            // assume it's a map
083                            SortInfo sortInfoValue = SortInfo.asSortInfo((Map) sortInfo);
084                            if (sortInfoValue == null) {
085                                throw new ValidatorException(MessageFactory.getMessage(context,
086                                        INVALID_VALUE_MESSAGE_ID));
087                            }
088                            criterion = sortInfoValue.getSortColumn();
089                        }
090                        if (criterion == null || StringUtils.isEmpty(criterion.trim())) {
091                            throw new ValidatorException(MessageFactory.getMessage(context, EMPTY_CRITERION_MESSAGE_ID));
092                        }
093                        if (criteria.contains(criterion)) {
094                            throw new ValidatorException(MessageFactory.getMessage(context,
095                                    CONFLICTING_CRITERIA_MESSAGE_ID, criterion));
096                        } else {
097                            criteria.add(criterion);
098                        }
099                    }
100                } catch (ClassCastException e) {
101                    throw new ValidatorException(MessageFactory.getMessage(context, INVALID_VALUE_MESSAGE_ID));
102                }
103            }
104        }
105    }
106}