001    /**
002     * Copyright (c) 2000-2013 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.search;
016    
017    import com.liferay.portal.kernel.util.Validator;
018    
019    import java.util.List;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     * @author Raymond Aug??
024     * @author Hugo Huijser
025     */
026    public class SortFactoryImpl implements SortFactory {
027    
028            @Override
029            public Sort create(String fieldName, boolean reverse) {
030                    return new Sort(fieldName, reverse);
031            }
032    
033            @Override
034            public Sort create(String fieldName, int type, boolean reverse) {
035                    return new Sort(fieldName, type, reverse);
036            }
037    
038            @Override
039            public Sort[] getDefaultSorts() {
040                    return _DEFAULT_SORTS;
041            }
042    
043            @Override
044            public Sort getSort(Class<?> clazz, String orderByCol, String orderByType) {
045                    Indexer indexer = IndexerRegistryUtil.getIndexer(clazz);
046    
047                    String sortField = indexer.getSortField(orderByCol);
048    
049                    if (Validator.isNull(orderByType)) {
050                            orderByType = "asc";
051                    }
052    
053                    return new Sort(
054                            sortField, Sort.STRING_TYPE, !orderByType.equalsIgnoreCase("asc"));
055            }
056    
057            @Override
058            public Sort[] toArray(List<Sort> sorts) {
059                    if ((sorts == null) || sorts.isEmpty()) {
060                            return new Sort[0];
061                    }
062    
063                    Sort[] sortsArray = new Sort[sorts.size()];
064    
065                    for (int i = 0; i < sorts.size(); i++) {
066                            sortsArray[i] = sorts.get(i);
067                    }
068    
069                    return sortsArray;
070            }
071    
072            private static final Sort[] _DEFAULT_SORTS = new Sort[] {
073                    new Sort(null, Sort.SCORE_TYPE, false),
074                    new Sort(Field.MODIFIED_DATE, Sort.LONG_TYPE, true)
075            };
076    
077    }