001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
018    
019    import java.io.Serializable;
020    
021    import java.util.Comparator;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     * @author Shuyang Zhou
026     */
027    public abstract class OrderByComparator implements Comparator, Serializable {
028    
029            public abstract int compare(Object obj1, Object obj2);
030    
031            public String getOrderBy() {
032                    return null;
033            }
034    
035            public String[] getOrderByFields() {
036                    String orderBy = getOrderBy();
037    
038                    if (orderBy == null) {
039                            return null;
040                    }
041    
042                    String[] parts = StringUtil.split(orderBy);
043    
044                    String[] fields = new String[parts.length];
045    
046                    for (int i = 0; i < parts.length; i++) {
047                            String part = parts[i];
048    
049                            int x = part.indexOf(CharPool.PERIOD);
050                            int y = part.indexOf(CharPool.SPACE, x);
051    
052                            if (y == -1) {
053                                    y = part.length();
054                            }
055    
056                            fields[i] = part.substring(x + 1, y);
057                    }
058    
059                    return fields;
060            }
061    
062            public Object[] getOrderByValues(Object obj) {
063                    String[] fields = getOrderByFields();
064    
065                    Object[] values = new Object[fields.length];
066    
067                    for (int i = 0; i< fields.length; i++) {
068                            values[i] = BeanPropertiesUtil.getObject(obj, fields[i]);
069                    }
070    
071                    return values;
072            }
073    
074            public boolean isAscending() {
075                    String orderBy = getOrderBy();
076    
077                    if ((orderBy == null) ||
078                            (orderBy.toUpperCase().endsWith(_ORDER_BY_DESC))) {
079    
080                            return false;
081                    }
082                    else {
083                            return true;
084                    }
085            }
086    
087            public String toString() {
088                    return getOrderBy();
089            }
090    
091            private static final String _ORDER_BY_DESC = " DESC";
092    
093    }