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