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.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    }