001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    @SuppressWarnings("rawtypes")
028    public abstract class OrderByComparator implements Comparator, Serializable {
029    
030            public abstract int compare(Object obj1, Object obj2);
031    
032            public String getOrderBy() {
033                    return null;
034            }
035    
036            public String[] getOrderByFields() {
037                    String orderBy = getOrderBy();
038    
039                    if (orderBy == null) {
040                            return null;
041                    }
042    
043                    String[] parts = StringUtil.split(orderBy);
044    
045                    String[] fields = new String[parts.length];
046    
047                    for (int i = 0; i < parts.length; i++) {
048                            String part = parts[i];
049    
050                            int x = part.indexOf(CharPool.PERIOD);
051                            int y = part.indexOf(CharPool.SPACE, x);
052    
053                            if (y == -1) {
054                                    y = part.length();
055                            }
056    
057                            fields[i] = part.substring(x + 1, y);
058                    }
059    
060                    return fields;
061            }
062    
063            public Object[] getOrderByValues(Object obj) {
064                    String[] fields = getOrderByFields();
065    
066                    Object[] values = new Object[fields.length];
067    
068                    for (int i = 0; i< fields.length; i++) {
069                            values[i] = BeanPropertiesUtil.getObject(obj, fields[i]);
070                    }
071    
072                    return values;
073            }
074    
075            public boolean isAscending() {
076                    String orderBy = getOrderBy();
077    
078                    if ((orderBy == null) ||
079                            (orderBy.toUpperCase().endsWith(_ORDER_BY_DESC))) {
080    
081                            return false;
082                    }
083                    else {
084                            return true;
085                    }
086            }
087    
088            @Override
089            public String toString() {
090                    return getOrderBy();
091            }
092    
093            private static final String _ORDER_BY_DESC = " DESC";
094    
095    }