1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journal.search;
24  
25  import com.liferay.portal.kernel.dao.search.SearchContainer;
26  import com.liferay.portal.kernel.util.JavaConstants;
27  import com.liferay.portal.kernel.util.OrderByComparator;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.util.PortletKeys;
31  import com.liferay.portlet.PortalPreferences;
32  import com.liferay.portlet.PortletPreferencesFactoryUtil;
33  import com.liferay.portlet.journal.model.JournalArticle;
34  import com.liferay.portlet.journal.util.JournalUtil;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  import javax.portlet.PortletConfig;
42  import javax.portlet.PortletURL;
43  import javax.portlet.RenderRequest;
44  
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
47  
48  /**
49   * <a href="ArticleSearch.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   *
53   */
54  public class ArticleSearch extends SearchContainer<JournalArticle> {
55  
56      static List<String> headerNames = new ArrayList<String>();
57      static Map<String, String> orderableHeaders = new HashMap<String, String>();
58  
59      static {
60          headerNames.add("id");
61          headerNames.add("name");
62          headerNames.add("version");
63          headerNames.add("modified-date");
64          headerNames.add("display-date");
65          headerNames.add("author");
66  
67          orderableHeaders.put("id", "id");
68          orderableHeaders.put("name", "title");
69          orderableHeaders.put("version", "version");
70          orderableHeaders.put("modified-date", "modified-date");
71          orderableHeaders.put("display-date", "display-date");
72      }
73  
74      public static final String EMPTY_RESULTS_MESSAGE =
75          "no-articles-were-found";
76  
77      public ArticleSearch(RenderRequest renderRequest, PortletURL iteratorURL) {
78          super(
79              renderRequest, new ArticleDisplayTerms(renderRequest),
80              new ArticleSearchTerms(renderRequest), DEFAULT_CUR_PARAM,
81              DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
82  
83          PortletConfig portletConfig = (PortletConfig)renderRequest.getAttribute(
84              JavaConstants.JAVAX_PORTLET_CONFIG);
85  
86          ArticleDisplayTerms displayTerms =
87              (ArticleDisplayTerms)getDisplayTerms();
88          ArticleSearchTerms searchTerms = (ArticleSearchTerms)getSearchTerms();
89  
90          if (!portletConfig.getPortletName().equals(PortletKeys.JOURNAL)) {
91              displayTerms.setStatus("approved");
92              searchTerms.setStatus("approved");
93          }
94  
95          iteratorURL.setParameter(
96              ArticleDisplayTerms.GROUP_ID,
97              String.valueOf(displayTerms.getGroupId()));
98          iteratorURL.setParameter(
99              ArticleDisplayTerms.ARTICLE_ID, displayTerms.getArticleId());
100         iteratorURL.setParameter(
101             ArticleDisplayTerms.VERSION,
102             String.valueOf(displayTerms.getVersion()));
103         iteratorURL.setParameter(
104             ArticleDisplayTerms.TITLE, displayTerms.getTitle());
105         iteratorURL.setParameter(
106             ArticleDisplayTerms.DESCRIPTION, displayTerms.getDescription());
107         iteratorURL.setParameter(
108             ArticleDisplayTerms.CONTENT, displayTerms.getContent());
109         iteratorURL.setParameter(
110             ArticleDisplayTerms.TYPE, displayTerms.getType());
111         iteratorURL.setParameter(
112             ArticleDisplayTerms.STRUCTURE_ID, displayTerms.getStructureId());
113         iteratorURL.setParameter(
114             ArticleDisplayTerms.TEMPLATE_ID, displayTerms.getTemplateId());
115         iteratorURL.setParameter(
116             ArticleDisplayTerms.STATUS, displayTerms.getStatus());
117 
118         try {
119             PortalPreferences prefs =
120                 PortletPreferencesFactoryUtil.getPortalPreferences(
121                     renderRequest);
122 
123             String orderByCol = ParamUtil.getString(
124                 renderRequest, "orderByCol");
125             String orderByType = ParamUtil.getString(
126                 renderRequest, "orderByType");
127 
128             if (Validator.isNotNull(orderByCol) &&
129                 Validator.isNotNull(orderByType)) {
130 
131                 prefs.setValue(
132                     PortletKeys.JOURNAL, "articles-order-by-col", orderByCol);
133                 prefs.setValue(
134                     PortletKeys.JOURNAL, "articles-order-by-type", orderByType);
135             }
136             else {
137                 orderByCol = prefs.getValue(
138                     PortletKeys.JOURNAL, "articles-order-by-col", "id");
139                 orderByType = prefs.getValue(
140                     PortletKeys.JOURNAL, "articles-order-by-type", "asc");
141             }
142 
143             OrderByComparator orderByComparator =
144                 JournalUtil.getArticleOrderByComparator(
145                     orderByCol, orderByType);
146 
147             setOrderableHeaders(orderableHeaders);
148             setOrderByCol(orderByCol);
149             setOrderByType(orderByType);
150             setOrderByComparator(orderByComparator);
151         }
152         catch (Exception e) {
153             _log.error(e);
154         }
155     }
156 
157     private static Log _log = LogFactory.getLog(ArticleSearch.class);
158 
159 }