001
014
015 package com.liferay.portal.kernel.search;
016
017 import com.liferay.portal.kernel.util.Validator;
018
019 import java.util.List;
020
021
026 public class SortFactoryImpl implements SortFactory {
027
028 public Sort create(String fieldName, boolean reverse) {
029 return new Sort(fieldName, reverse);
030 }
031
032 public Sort create(String fieldName, int type, boolean reverse) {
033 return new Sort(fieldName, type, reverse);
034 }
035
036 public Sort[] getDefaultSorts() {
037 return _DEFAULT_SORTS;
038 }
039
040 public Sort getSort(Class<?> clazz, String orderByCol, String orderByType) {
041 Indexer indexer = IndexerRegistryUtil.getIndexer(clazz);
042
043 String sortField = indexer.getSortField(orderByCol);
044
045 if (Validator.isNull(orderByType)) {
046 orderByType = "asc";
047 }
048
049 return new Sort(
050 sortField, Sort.STRING_TYPE, !orderByType.equalsIgnoreCase("asc"));
051 }
052
053 public Sort[] toArray(List<Sort> sorts) {
054 if ((sorts == null) || sorts.isEmpty()) {
055 return new Sort[0];
056 }
057
058 Sort[] sortsArray = new Sort[sorts.size()];
059
060 for (int i = 0; i < sorts.size(); i++) {
061 sortsArray[i] = sorts.get(i);
062 }
063
064 return sortsArray;
065 }
066
067 private static final Sort[] _DEFAULT_SORTS = new Sort[] {
068 new Sort(null, Sort.SCORE_TYPE, false),
069 new Sort(Field.MODIFIED_DATE, Sort.LONG_TYPE, true)
070 };
071
072 }