001    /**
002     * Copyright (c) 2000-2010 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.journal.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.JavaConstants;
021    import com.liferay.portal.kernel.util.OrderByComparator;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portlet.PortalPreferences;
026    import com.liferay.portlet.PortletPreferencesFactoryUtil;
027    import com.liferay.portlet.journal.model.JournalArticle;
028    import com.liferay.portlet.journal.util.JournalUtil;
029    
030    import java.util.ArrayList;
031    import java.util.HashMap;
032    import java.util.List;
033    import java.util.Map;
034    
035    import javax.portlet.PortletConfig;
036    import javax.portlet.PortletRequest;
037    import javax.portlet.PortletURL;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class ArticleSearch extends SearchContainer<JournalArticle> {
043    
044            static List<String> headerNames = new ArrayList<String>();
045            static Map<String, String> orderableHeaders = new HashMap<String, String>();
046    
047            static {
048                    headerNames.add("id");
049                    headerNames.add("name");
050                    headerNames.add("version");
051                    headerNames.add("modified-date");
052                    headerNames.add("display-date");
053                    headerNames.add("author");
054    
055                    orderableHeaders.put("id", "id");
056                    orderableHeaders.put("name", "title");
057                    orderableHeaders.put("version", "version");
058                    orderableHeaders.put("modified-date", "modified-date");
059                    orderableHeaders.put("display-date", "display-date");
060            }
061    
062            public static final String EMPTY_RESULTS_MESSAGE =
063                    "no-web-content-were-found";
064    
065            public ArticleSearch(
066                    PortletRequest portletRequest, PortletURL iteratorURL) {
067    
068                    super(
069                            portletRequest, new ArticleDisplayTerms(portletRequest),
070                            new ArticleSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
071                            DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
072    
073                    PortletConfig portletConfig =
074                            (PortletConfig)portletRequest.getAttribute(
075                                    JavaConstants.JAVAX_PORTLET_CONFIG);
076    
077                    ArticleDisplayTerms displayTerms =
078                            (ArticleDisplayTerms)getDisplayTerms();
079                    ArticleSearchTerms searchTerms = (ArticleSearchTerms)getSearchTerms();
080    
081                    if (!portletConfig.getPortletName().equals(PortletKeys.JOURNAL)) {
082                            displayTerms.setStatus("approved");
083                            searchTerms.setStatus("approved");
084                    }
085    
086                    iteratorURL.setParameter(
087                            ArticleDisplayTerms.ARTICLE_ID, displayTerms.getArticleId());
088                    iteratorURL.setParameter(
089                            ArticleDisplayTerms.CONTENT, displayTerms.getContent());
090                    iteratorURL.setParameter(
091                            ArticleDisplayTerms.DESCRIPTION, displayTerms.getDescription());
092                    iteratorURL.setParameter(
093                            ArticleDisplayTerms.GROUP_ID,
094                            String.valueOf(displayTerms.getGroupId()));
095                    iteratorURL.setParameter(
096                            ArticleDisplayTerms.STATUS, displayTerms.getStatus());
097                    iteratorURL.setParameter(
098                            ArticleDisplayTerms.STRUCTURE_ID, displayTerms.getStructureId());
099                    iteratorURL.setParameter(
100                            ArticleDisplayTerms.TEMPLATE_ID, displayTerms.getTemplateId());
101                    iteratorURL.setParameter(
102                            ArticleDisplayTerms.TITLE, displayTerms.getTitle());
103                    iteratorURL.setParameter(
104                            ArticleDisplayTerms.TYPE, displayTerms.getType());
105                    iteratorURL.setParameter(
106                            ArticleDisplayTerms.VERSION,
107                            String.valueOf(displayTerms.getVersion()));
108    
109                    try {
110                            PortalPreferences preferences =
111                                    PortletPreferencesFactoryUtil.getPortalPreferences(
112                                            portletRequest);
113    
114                            String orderByCol = ParamUtil.getString(
115                                    portletRequest, "orderByCol");
116                            String orderByType = ParamUtil.getString(
117                                    portletRequest, "orderByType");
118    
119                            if (Validator.isNotNull(orderByCol) &&
120                                    Validator.isNotNull(orderByType)) {
121    
122                                    preferences.setValue(
123                                            PortletKeys.JOURNAL, "articles-order-by-col", orderByCol);
124                                    preferences.setValue(
125                                            PortletKeys.JOURNAL, "articles-order-by-type", orderByType);
126                            }
127                            else {
128                                    orderByCol = preferences.getValue(
129                                            PortletKeys.JOURNAL, "articles-order-by-col", "id");
130                                    orderByType = preferences.getValue(
131                                            PortletKeys.JOURNAL, "articles-order-by-type", "asc");
132                            }
133    
134                            OrderByComparator orderByComparator =
135                                    JournalUtil.getArticleOrderByComparator(
136                                            orderByCol, orderByType);
137    
138                            setOrderableHeaders(orderableHeaders);
139                            setOrderByCol(orderByCol);
140                            setOrderByType(orderByType);
141                            setOrderByComparator(orderByComparator);
142                    }
143                    catch (Exception e) {
144                            _log.error(e);
145                    }
146            }
147    
148            private static Log _log = LogFactoryUtil.getLog(ArticleSearch.class);
149    
150    }