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.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("title");
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("title", "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                    String portletName = portletConfig.getPortletName();
082    
083                    if (!portletName.equals(PortletKeys.JOURNAL)) {
084                            displayTerms.setStatus("approved");
085                            searchTerms.setStatus("approved");
086                    }
087    
088                    iteratorURL.setParameter(
089                            ArticleDisplayTerms.ARTICLE_ID, displayTerms.getArticleId());
090                    iteratorURL.setParameter(
091                            ArticleDisplayTerms.CONTENT, displayTerms.getContent());
092                    iteratorURL.setParameter(
093                            ArticleDisplayTerms.DESCRIPTION, displayTerms.getDescription());
094                    iteratorURL.setParameter(
095                            ArticleDisplayTerms.GROUP_ID,
096                            String.valueOf(displayTerms.getGroupId()));
097                    iteratorURL.setParameter(
098                            ArticleDisplayTerms.STATUS, displayTerms.getStatus());
099                    iteratorURL.setParameter(
100                            ArticleDisplayTerms.STRUCTURE_ID, displayTerms.getStructureId());
101                    iteratorURL.setParameter(
102                            ArticleDisplayTerms.TEMPLATE_ID, displayTerms.getTemplateId());
103                    iteratorURL.setParameter(
104                            ArticleDisplayTerms.TITLE, displayTerms.getTitle());
105                    iteratorURL.setParameter(
106                            ArticleDisplayTerms.TYPE, displayTerms.getType());
107                    iteratorURL.setParameter(
108                            ArticleDisplayTerms.VERSION,
109                            String.valueOf(displayTerms.getVersion()));
110    
111                    try {
112                            PortalPreferences preferences =
113                                    PortletPreferencesFactoryUtil.getPortalPreferences(
114                                            portletRequest);
115    
116                            String orderByCol = ParamUtil.getString(
117                                    portletRequest, "orderByCol");
118                            String orderByType = ParamUtil.getString(
119                                    portletRequest, "orderByType");
120    
121                            if (Validator.isNotNull(orderByCol) &&
122                                    Validator.isNotNull(orderByType)) {
123    
124                                    preferences.setValue(
125                                            PortletKeys.JOURNAL, "articles-order-by-col", orderByCol);
126                                    preferences.setValue(
127                                            PortletKeys.JOURNAL, "articles-order-by-type", orderByType);
128                            }
129                            else {
130                                    orderByCol = preferences.getValue(
131                                            PortletKeys.JOURNAL, "articles-order-by-col", "id");
132                                    orderByType = preferences.getValue(
133                                            PortletKeys.JOURNAL, "articles-order-by-type", "asc");
134                            }
135    
136                            OrderByComparator orderByComparator =
137                                    JournalUtil.getArticleOrderByComparator(
138                                            orderByCol, orderByType);
139    
140                            setOrderableHeaders(orderableHeaders);
141                            setOrderByCol(orderByCol);
142                            setOrderByType(orderByType);
143                            setOrderByComparator(orderByComparator);
144                    }
145                    catch (Exception e) {
146                            _log.error(e);
147                    }
148            }
149    
150            private static Log _log = LogFactoryUtil.getLog(ArticleSearch.class);
151    
152    }