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[] getOrderByConditionFields() {
037                    return getOrderByFields();
038            }
039    
040            public Object[] getOrderByConditionValues(Object obj) {
041                    String[] fields = getOrderByConditionFields();
042    
043                    Object[] values = new Object[fields.length];
044    
045                    for (int i = 0; i < fields.length; i++) {
046                            values[i] = BeanPropertiesUtil.getObject(obj, fields[i]);
047                    }
048    
049                    return values;
050            }
051    
052            public String[] getOrderByFields() {
053                    String orderBy = getOrderBy();
054    
055                    if (orderBy == null) {
056                            return null;
057                    }
058    
059                    String[] parts = StringUtil.split(orderBy);
060    
061                    String[] fields = new String[parts.length];
062    
063                    for (int i = 0; i < parts.length; i++) {
064                            String part = parts[i];
065    
066                            int x = part.indexOf(CharPool.PERIOD);
067                            int y = part.indexOf(CharPool.SPACE, x);
068    
069                            if (y == -1) {
070                                    y = part.length();
071                            }
072    
073                            fields[i] = part.substring(x + 1, y);
074                    }
075    
076                    return fields;
077            }
078    
079            public boolean isAscending() {
080                    String orderBy = getOrderBy();
081    
082                    if ((orderBy == null) ||
083                            (orderBy.toUpperCase().endsWith(_ORDER_BY_DESC))) {
084    
085                            return false;
086                    }
087                    else {
088                            return true;
089                    }
090            }
091    
092            @Override
093            public String toString() {
094                    return getOrderBy();
095            }
096    
097            private static final String _ORDER_BY_DESC = " DESC";
098    
099    }