1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.journal.search;
16  
17  import com.liferay.portal.kernel.dao.search.SearchContainer;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.JavaConstants;
21  import com.liferay.portal.kernel.util.OrderByComparator;
22  import com.liferay.portal.kernel.util.ParamUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.util.PortletKeys;
25  import com.liferay.portlet.PortalPreferences;
26  import com.liferay.portlet.PortletPreferencesFactoryUtil;
27  import com.liferay.portlet.journal.model.JournalArticle;
28  import com.liferay.portlet.journal.util.JournalUtil;
29  
30  import java.util.ArrayList;
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.Map;
34  
35  import javax.portlet.PortletConfig;
36  import javax.portlet.PortletRequest;
37  import javax.portlet.PortletURL;
38  
39  /**
40   * <a href="ArticleSearch.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   */
44  public class ArticleSearch extends SearchContainer<JournalArticle> {
45  
46      static List<String> headerNames = new ArrayList<String>();
47      static Map<String, String> orderableHeaders = new HashMap<String, String>();
48  
49      static {
50          headerNames.add("id");
51          headerNames.add("name");
52          headerNames.add("version");
53          headerNames.add("modified-date");
54          headerNames.add("display-date");
55          headerNames.add("author");
56  
57          orderableHeaders.put("id", "id");
58          orderableHeaders.put("name", "title");
59          orderableHeaders.put("version", "version");
60          orderableHeaders.put("modified-date", "modified-date");
61          orderableHeaders.put("display-date", "display-date");
62      }
63  
64      public static final String EMPTY_RESULTS_MESSAGE =
65          "no-web-content-were-found";
66  
67      public ArticleSearch(
68          PortletRequest portletRequest, PortletURL iteratorURL) {
69  
70          super(
71              portletRequest, new ArticleDisplayTerms(portletRequest),
72              new ArticleSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
73              DEFAULT_DELTA, iteratorURL, headerNames, EMPTY_RESULTS_MESSAGE);
74  
75          PortletConfig portletConfig =
76              (PortletConfig)portletRequest.getAttribute(
77                  JavaConstants.JAVAX_PORTLET_CONFIG);
78  
79          ArticleDisplayTerms displayTerms =
80              (ArticleDisplayTerms)getDisplayTerms();
81          ArticleSearchTerms searchTerms = (ArticleSearchTerms)getSearchTerms();
82  
83          if (!portletConfig.getPortletName().equals(PortletKeys.JOURNAL)) {
84              displayTerms.setStatus("approved");
85              searchTerms.setStatus("approved");
86          }
87  
88          iteratorURL.setParameter(
89              ArticleDisplayTerms.GROUP_ID,
90              String.valueOf(displayTerms.getGroupId()));
91          iteratorURL.setParameter(
92              ArticleDisplayTerms.ARTICLE_ID, displayTerms.getArticleId());
93          iteratorURL.setParameter(
94              ArticleDisplayTerms.VERSION,
95              String.valueOf(displayTerms.getVersion()));
96          iteratorURL.setParameter(
97              ArticleDisplayTerms.TITLE, displayTerms.getTitle());
98          iteratorURL.setParameter(
99              ArticleDisplayTerms.DESCRIPTION, displayTerms.getDescription());
100         iteratorURL.setParameter(
101             ArticleDisplayTerms.CONTENT, displayTerms.getContent());
102         iteratorURL.setParameter(
103             ArticleDisplayTerms.TYPE, displayTerms.getType());
104         iteratorURL.setParameter(
105             ArticleDisplayTerms.STRUCTURE_ID, displayTerms.getStructureId());
106         iteratorURL.setParameter(
107             ArticleDisplayTerms.TEMPLATE_ID, displayTerms.getTemplateId());
108         iteratorURL.setParameter(
109             ArticleDisplayTerms.STATUS, displayTerms.getStatus());
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 }