1   /**
2    * Copyright (c) 2000-2007 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.util.PortletKeys;
28  import com.liferay.portlet.PortalPreferences;
29  import com.liferay.portlet.PortletPreferencesFactoryUtil;
30  import com.liferay.util.CollectionFactory;
31  
32  import java.util.ArrayList;
33  import java.util.List;
34  import java.util.Map;
35  
36  import javax.portlet.PortletConfig;
37  import javax.portlet.PortletURL;
38  import javax.portlet.RenderRequest;
39  
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  /**
44   * <a href="ArticleSearch.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class ArticleSearch extends SearchContainer {
50  
51      static List headerNames = new ArrayList();
52      static Map orderableHeaders = CollectionFactory.getHashMap();
53  
54      static {
55          headerNames.add("id");
56          headerNames.add("name");
57          headerNames.add("version");
58          headerNames.add("display-date");
59          headerNames.add("author");
60  
61          orderableHeaders.put("id", "id");
62          orderableHeaders.put("name", "title");
63          orderableHeaders.put("version", "version");
64          orderableHeaders.put("modified-date", "modified-date");
65          orderableHeaders.put("display-date", "display-date");
66      }
67  
68      public static final String EMPTY_RESULTS_MESSAGE =
69          "no-articles-were-found";
70  
71      public ArticleSearch(RenderRequest req, PortletURL iteratorURL) {
72          super(req, new ArticleDisplayTerms(req), new ArticleSearchTerms(req),
73                DEFAULT_CUR_PARAM, DEFAULT_DELTA, iteratorURL, headerNames,
74                EMPTY_RESULTS_MESSAGE);
75  
76          PortletConfig portletConfig = (PortletConfig)req.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 prefs =
113                 PortletPreferencesFactoryUtil.getPortalPreferences(req);
114 
115             String orderByCol = prefs.getValue(
116                 PortletKeys.JOURNAL, "articles-order-by-col", "id");
117             String orderByType = prefs.getValue(
118                 PortletKeys.JOURNAL, "articles-order-by-type", "asc");
119 
120             setOrderableHeaders(orderableHeaders);
121             setOrderByCol(orderByCol);
122             setOrderByType(orderByType);
123         }
124         catch (Exception e) {
125             _log.error(e);
126         }
127     }
128 
129     private static Log _log = LogFactory.getLog(ArticleSearch.class);
130 
131 }