001    /**
002     * Copyright (c) 2000-present 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.portlet.trash.search;
016    
017    import com.liferay.portal.kernel.dao.search.SearchContainer;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.OrderByComparator;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.util.PortletKeys;
024    import com.liferay.portlet.PortalPreferences;
025    import com.liferay.portlet.PortletPreferencesFactoryUtil;
026    import com.liferay.portlet.trash.model.TrashEntry;
027    import com.liferay.portlet.trash.util.TrashUtil;
028    
029    import java.util.ArrayList;
030    import java.util.HashMap;
031    import java.util.List;
032    import java.util.Map;
033    
034    import javax.portlet.PortletRequest;
035    import javax.portlet.PortletURL;
036    
037    /**
038     * @author Sergio Gonz??lez
039     */
040    public class EntrySearch extends SearchContainer<TrashEntry> {
041    
042            public static final String EMPTY_RESULTS_MESSAGE =
043                    "the-recycle-bin-is-empty";
044    
045            public static List<String> headerNames = new ArrayList<String>();
046            public static Map<String, String> orderableHeaders =
047                    new HashMap<String, String>();
048    
049            static {
050                    headerNames.add("name");
051                    headerNames.add("type");
052                    headerNames.add("removed-date");
053                    headerNames.add("removed-by");
054    
055                    orderableHeaders.put("name", "name");
056                    orderableHeaders.put("type", "type");
057                    orderableHeaders.put("removed-date", "removed-date");
058                    orderableHeaders.put("removed-by", "removed-by");
059            }
060    
061            public EntrySearch(PortletRequest portletRequest, PortletURL iteratorURL) {
062                    super(
063                            portletRequest, new EntryDisplayTerms(portletRequest),
064                            new EntrySearchTerms(portletRequest), DEFAULT_CUR_PARAM,
065                            DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
066    
067                    try {
068                            PortalPreferences preferences =
069                                    PortletPreferencesFactoryUtil.getPortalPreferences(
070                                            portletRequest);
071    
072                            String orderByCol = ParamUtil.getString(
073                                    portletRequest, "orderByCol");
074                            String orderByType = ParamUtil.getString(
075                                    portletRequest, "orderByType");
076    
077                            if (Validator.isNotNull(orderByCol) &&
078                                    Validator.isNotNull(orderByType)) {
079    
080                                    preferences.setValue(
081                                            PortletKeys.USERS_ADMIN, "entries-order-by-col",
082                                            orderByCol);
083                                    preferences.setValue(
084                                            PortletKeys.USERS_ADMIN, "entries-order-by-type",
085                                            orderByType);
086                            }
087                            else {
088                                    orderByCol = preferences.getValue(
089                                            PortletKeys.USERS_ADMIN, "entries-order-by-col",
090                                            "removed-date");
091                                    orderByType = preferences.getValue(
092                                            PortletKeys.USERS_ADMIN, "entries-order-by-type", "asc");
093                            }
094    
095                            OrderByComparator<TrashEntry> orderByComparator =
096                                    TrashUtil.getEntryOrderByComparator(orderByCol, orderByType);
097    
098                            setOrderableHeaders(orderableHeaders);
099                            setOrderByCol(orderByCol);
100                            setOrderByType(orderByType);
101                            setOrderByComparator(orderByComparator);
102                    }
103                    catch (Exception e) {
104                            _log.error(e);
105                    }
106            }
107    
108            private static final Log _log = LogFactoryUtil.getLog(EntrySearch.class);
109    
110    }