001    /**
002     * Copyright (c) 2000-2012 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            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    }