001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.ui.web.directory;
023
024import java.io.Serializable;
025import java.text.Collator;
026import java.util.Comparator;
027import java.util.Locale;
028
029import javax.faces.context.FacesContext;
030
031import org.apache.commons.lang.StringUtils;
032
033/**
034 * @author <a href="mailto:glefter@nuxeo.com">George Lefter</a>
035 */
036public class DirectorySelectItemComparator implements Comparator<DirectorySelectItem>, Serializable {
037
038    private static final long serialVersionUID = 1L;
039
040    private final String[] ordering;
041
042    private Boolean caseSensitive;
043
044    private Collator collator;
045
046    private Locale locale;
047
048    public DirectorySelectItemComparator(String ordering, Boolean caseSensitive, Locale locale) {
049        this.ordering = StringUtils.split(ordering, ",");
050        this.caseSensitive = caseSensitive;
051        if (locale == null) {
052            FacesContext context = FacesContext.getCurrentInstance();
053            this.locale = context.getViewRoot().getLocale();
054        } else {
055            this.locale = locale;
056        }
057        collator = Collator.getInstance(this.locale);
058        if (Boolean.TRUE.equals(this.caseSensitive)) {
059            collator.setStrength(Collator.TERTIARY); // TERTIARY will make a
060                                                     // difference between 'a'
061                                                     // and 'A'
062        } else {
063            collator.setStrength(Collator.SECONDARY);
064        }
065    }
066
067    public DirectorySelectItemComparator(String ordering, Boolean caseSentitive) {
068        this(ordering, caseSentitive, null);
069    }
070
071    public DirectorySelectItemComparator(String ordering) {
072        this(ordering, Boolean.FALSE);
073    }
074
075    protected int compareField(String field, DirectorySelectItem item1, DirectorySelectItem item2) {
076        if ("label".equals(field)) {
077            String label1 = item1.getLabel();
078            String label2 = item2.getLabel();
079            return collator.compare(label1, label2);
080        } else if ("id".equals(field)) {
081            String value1 = String.valueOf(item1.getValue());
082            String value2 = String.valueOf(item2.getValue());
083            // TODO: maybe deal with numbers comparisons (?)
084            return collator.compare(value1, value2);
085        } else if ("ordering".equals(field)) {
086            long orderItem1 = item1.getOrdering();
087            long orderItem2 = item2.getOrdering();
088            return Long.valueOf(orderItem1).compareTo(Long.valueOf(orderItem2));
089        } else {
090            throw new RuntimeException("Invalid sort criteria " + field);
091        }
092    }
093
094    @Override
095    public int compare(DirectorySelectItem item1, DirectorySelectItem item2) {
096        for (String field : ordering) {
097            int compare = compareField(field, item1, item2);
098            if (compare != 0) {
099                return compare;
100            }
101        }
102        return 0;
103    }
104
105}