1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.JavaConstants;
29  import com.liferay.portal.kernel.util.OrderByComparator;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.util.PortletKeys;
33  import com.liferay.portlet.PortalPreferences;
34  import com.liferay.portlet.PortletPreferencesFactoryUtil;
35  import com.liferay.portlet.journal.model.JournalArticle;
36  import com.liferay.portlet.journal.util.JournalUtil;
37  
38  import java.util.ArrayList;
39  import java.util.HashMap;
40  import java.util.List;
41  import java.util.Map;
42  
43  import javax.portlet.PortletConfig;
44  import javax.portlet.PortletRequest;
45  import javax.portlet.PortletURL;
46  
47  /**
48   * <a href="ArticleSearch.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class ArticleSearch extends SearchContainer<JournalArticle> {
54  
55      static List<String> headerNames = new ArrayList<String>();
56      static Map<String, String> orderableHeaders = new HashMap<String, String>();
57  
58      static {
59          headerNames.add("id");
60          headerNames.add("name");
61          headerNames.add("version");
62          headerNames.add("modified-date");
63          headerNames.add("display-date");
64          headerNames.add("author");
65  
66          orderableHeaders.put("id", "id");
67          orderableHeaders.put("name", "title");
68          orderableHeaders.put("version", "version");
69          orderableHeaders.put("modified-date", "modified-date");
70          orderableHeaders.put("display-date", "display-date");
71      }
72  
73      public static final String EMPTY_RESULTS_MESSAGE =
74          "no-web-content-were-found";
75  
76      public ArticleSearch(
77          PortletRequest portletRequest, PortletURL iteratorURL) {
78  
79          super(
80              portletRequest, new ArticleDisplayTerms(portletRequest),
81              new ArticleSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
82              DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
83  
84          PortletConfig portletConfig =
85              (PortletConfig)portletRequest.getAttribute(
86                  JavaConstants.JAVAX_PORTLET_CONFIG);
87  
88          ArticleDisplayTerms displayTerms =
89              (ArticleDisplayTerms)getDisplayTerms();
90          ArticleSearchTerms searchTerms = (ArticleSearchTerms)getSearchTerms();
91  
92          if (!portletConfig.getPortletName().equals(PortletKeys.JOURNAL)) {
93              displayTerms.setStatus("approved");
94              searchTerms.setStatus("approved");
95          }
96  
97          iteratorURL.setParameter(
98              ArticleDisplayTerms.GROUP_ID,
99              String.valueOf(displayTerms.getGroupId()));
100         iteratorURL.setParameter(
101             ArticleDisplayTerms.ARTICLE_ID, displayTerms.getArticleId());
102         iteratorURL.setParameter(
103             ArticleDisplayTerms.VERSION,
104             String.valueOf(displayTerms.getVersion()));
105         iteratorURL.setParameter(
106             ArticleDisplayTerms.TITLE, displayTerms.getTitle());
107         iteratorURL.setParameter(
108             ArticleDisplayTerms.DESCRIPTION, displayTerms.getDescription());
109         iteratorURL.setParameter(
110             ArticleDisplayTerms.CONTENT, displayTerms.getContent());
111         iteratorURL.setParameter(
112             ArticleDisplayTerms.TYPE, displayTerms.getType());
113         iteratorURL.setParameter(
114             ArticleDisplayTerms.STRUCTURE_ID, displayTerms.getStructureId());
115         iteratorURL.setParameter(
116             ArticleDisplayTerms.TEMPLATE_ID, displayTerms.getTemplateId());
117         iteratorURL.setParameter(
118             ArticleDisplayTerms.STATUS, displayTerms.getStatus());
119 
120         try {
121             PortalPreferences preferences =
122                 PortletPreferencesFactoryUtil.getPortalPreferences(
123                     portletRequest);
124 
125             String orderByCol = ParamUtil.getString(
126                 portletRequest, "orderByCol");
127             String orderByType = ParamUtil.getString(
128                 portletRequest, "orderByType");
129 
130             if (Validator.isNotNull(orderByCol) &&
131                 Validator.isNotNull(orderByType)) {
132 
133                 preferences.setValue(
134                     PortletKeys.JOURNAL, "articles-order-by-col", orderByCol);
135                 preferences.setValue(
136                     PortletKeys.JOURNAL, "articles-order-by-type", orderByType);
137             }
138             else {
139                 orderByCol = preferences.getValue(
140                     PortletKeys.JOURNAL, "articles-order-by-col", "id");
141                 orderByType = preferences.getValue(
142                     PortletKeys.JOURNAL, "articles-order-by-type", "asc");
143             }
144 
145             OrderByComparator orderByComparator =
146                 JournalUtil.getArticleOrderByComparator(
147                     orderByCol, orderByType);
148 
149             setOrderableHeaders(orderableHeaders);
150             setOrderByCol(orderByCol);
151             setOrderByType(orderByType);
152             setOrderByComparator(orderByComparator);
153         }
154         catch (Exception e) {
155             _log.error(e);
156         }
157     }
158 
159     private static Log _log = LogFactoryUtil.getLog(ArticleSearch.class);
160 
161 }