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