001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.search;
016    
017    import com.liferay.portal.kernel.util.StringUtil;
018    import com.liferay.portal.kernel.util.Validator;
019    
020    import java.util.List;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     * @author Raymond Aug??
025     * @author Hugo Huijser
026     */
027    public class SortFactoryImpl implements SortFactory {
028    
029            @Override
030            public Sort create(String fieldName, boolean reverse) {
031                    return new Sort(fieldName, reverse);
032            }
033    
034            @Override
035            public Sort create(String fieldName, int type, boolean reverse) {
036                    return new Sort(fieldName, type, reverse);
037            }
038    
039            @Override
040            public Sort[] getDefaultSorts() {
041                    return _DEFAULT_SORTS;
042            }
043    
044            @Override
045            public Sort getSort(
046                    Class<?> clazz, int type, String orderByCol, boolean inferSortField,
047                    String orderByType) {
048    
049                    String sortField = orderByCol;
050    
051                    if (inferSortField) {
052                            sortField = getSortField(orderByCol, type, clazz);
053                    }
054    
055                    if (Validator.isNull(orderByType)) {
056                            orderByType = "asc";
057                    }
058    
059                    return new Sort(
060                            sortField, type, !StringUtil.equalsIgnoreCase(orderByType, "asc"));
061            }
062    
063            @Override
064            public Sort getSort(
065                    Class<?> clazz, int type, String orderByCol, String orderByType) {
066    
067                    return getSort(clazz, type, orderByCol, true, orderByType);
068            }
069    
070            @Override
071            public Sort getSort(Class<?> clazz, String orderByCol, String orderByType) {
072                    return getSort(clazz, Sort.STRING_TYPE, orderByCol, orderByType);
073            }
074    
075            @Override
076            public Sort[] toArray(List<Sort> sorts) {
077                    if ((sorts == null) || sorts.isEmpty()) {
078                            return new Sort[0];
079                    }
080    
081                    Sort[] sortsArray = new Sort[sorts.size()];
082    
083                    for (int i = 0; i < sorts.size(); i++) {
084                            sortsArray[i] = sorts.get(i);
085                    }
086    
087                    return sortsArray;
088            }
089    
090            protected String getSortField(String orderByCol, int type, Class<?> clazz) {
091                    Indexer indexer = IndexerRegistryUtil.getIndexer(clazz);
092    
093                    return indexer.getSortField(orderByCol, type);
094            }
095    
096            private static final Sort[] _DEFAULT_SORTS = new Sort[] {
097                    new Sort(null, Sort.SCORE_TYPE, false),
098                    new Sort(Field.MODIFIED_DATE, Sort.LONG_TYPE, true)
099            };
100    
101    }