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.portal.search;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.dao.search.SearchContainer;
19  import com.liferay.portal.kernel.search.OpenSearch;
20  import com.liferay.portal.kernel.search.SearchException;
21  import com.liferay.portal.kernel.util.GetterUtil;
22  import com.liferay.portal.kernel.util.HttpUtil;
23  import com.liferay.portal.kernel.util.StringUtil;
24  import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
25  import com.liferay.portal.kernel.xml.Document;
26  import com.liferay.portal.kernel.xml.Element;
27  import com.liferay.portal.kernel.xml.SAXReaderUtil;
28  import com.liferay.portal.model.Layout;
29  import com.liferay.portal.service.LayoutLocalServiceUtil;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.PropsUtil;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.portlet.PortletURLImpl;
34  
35  import java.util.Date;
36  
37  import javax.portlet.PortletMode;
38  import javax.portlet.PortletModeException;
39  import javax.portlet.PortletRequest;
40  import javax.portlet.PortletURL;
41  import javax.portlet.WindowState;
42  import javax.portlet.WindowStateException;
43  
44  import javax.servlet.http.HttpServletRequest;
45  
46  /**
47   * <a href="BaseOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Charles May
50   * @author Brian Wing Shun Chan
51   */
52  public abstract class BaseOpenSearchImpl implements OpenSearch {
53  
54      public boolean isEnabled() {
55          return _enabled;
56      }
57  
58      public String search(HttpServletRequest request, String url)
59          throws SearchException {
60  
61          String keywords = GetterUtil.getString(
62              HttpUtil.getParameter(url, "keywords", false));
63          int startPage = GetterUtil.getInteger(
64              HttpUtil.getParameter(url, "p", false), 1);
65          int itemsPerPage = GetterUtil.getInteger(
66              HttpUtil.getParameter(url, "c", false),
67              SearchContainer.DEFAULT_DELTA);
68  
69          return search(request, keywords, startPage, itemsPerPage);
70      }
71  
72      public abstract String search(
73              HttpServletRequest request, String keywords, int startPage,
74              int itemsPerPage)
75          throws SearchException;
76  
77      protected void addSearchResult(
78          Element root, String title, String link, Date updated,
79          String summary, double score) {
80  
81          addSearchResult(
82              root, title, link, updated, summary, new String[0], 0, score);
83      }
84  
85      protected void addSearchResult(
86          Element root, String title, String link, Date updated, String summary,
87          String[] tags, double ratings, double score) {
88  
89          // entry
90  
91          Element entry = OpenSearchUtil.addElement(
92              root, "entry", OpenSearchUtil.DEFAULT_NAMESPACE);
93  
94          // title
95  
96          OpenSearchUtil.addElement(
97              entry, "title", OpenSearchUtil.DEFAULT_NAMESPACE, title);
98  
99          // link
100 
101         Element entryLink = OpenSearchUtil.addElement(
102             entry, "link", OpenSearchUtil.DEFAULT_NAMESPACE);
103 
104         entryLink.addAttribute("href", link);
105 
106         // id
107 
108         OpenSearchUtil.addElement(
109             entry, "id", OpenSearchUtil.DEFAULT_NAMESPACE,
110             "urn:uuid:" + PortalUUIDUtil.generate());
111 
112         // updated
113 
114         OpenSearchUtil.addElement(
115             entry, "updated", OpenSearchUtil.DEFAULT_NAMESPACE, updated);
116 
117         // summary
118 
119         OpenSearchUtil.addElement(
120             entry, "summary", OpenSearchUtil.DEFAULT_NAMESPACE, summary);
121 
122         // tags
123 
124         OpenSearchUtil.addElement(
125             entry, "tags", OpenSearchUtil.DEFAULT_NAMESPACE,
126             StringUtil.merge(tags));
127 
128         // ratings
129 
130         OpenSearchUtil.addElement(
131             entry, "ratings", OpenSearchUtil.DEFAULT_NAMESPACE, ratings);
132 
133         // relevance:score
134 
135         OpenSearchUtil.addElement(
136             entry, "score", OpenSearchUtil.RELEVANCE_NAMESPACE, score);
137     }
138 
139     protected Object[] addSearchResults(
140         String keywords, int startPage, int itemsPerPage, int total, int start,
141         String title, String searchPath, ThemeDisplay themeDisplay) {
142 
143         int totalPages = 0;
144 
145         if (total % itemsPerPage == 0) {
146             totalPages = total / itemsPerPage;
147         }
148         else {
149             totalPages = (total / itemsPerPage) + 1;
150         }
151 
152         int previousPage = startPage - 1;
153         int nextPage = startPage + 1;
154 
155         // Create document
156 
157         Document doc = SAXReaderUtil.createDocument();
158 
159         // feed
160 
161         Element root = doc.addElement("feed");
162 
163         root.add(OpenSearchUtil.getNamespace(OpenSearchUtil.DEFAULT_NAMESPACE));
164         root.add(OpenSearchUtil.getNamespace(OpenSearchUtil.OS_NAMESPACE));
165         root.add(
166             OpenSearchUtil.getNamespace(OpenSearchUtil.RELEVANCE_NAMESPACE));
167 
168         // title
169 
170         OpenSearchUtil.addElement(
171             root, "title", OpenSearchUtil.DEFAULT_NAMESPACE, title);
172 
173         // updated
174 
175         OpenSearchUtil.addElement(
176             root, "updated", OpenSearchUtil.DEFAULT_NAMESPACE, new Date());
177 
178         // author
179 
180         Element author = OpenSearchUtil.addElement(
181             root, "author", OpenSearchUtil.DEFAULT_NAMESPACE);
182 
183         // name
184 
185         OpenSearchUtil.addElement(
186             author, "name", OpenSearchUtil.DEFAULT_NAMESPACE,
187             themeDisplay.getUserId());
188 
189         // id
190 
191         OpenSearchUtil.addElement(
192             root, "id", OpenSearchUtil.DEFAULT_NAMESPACE,
193             "urn:uuid:" + PortalUUIDUtil.generate());
194 
195         // opensearch:totalResults
196 
197         OpenSearchUtil.addElement(
198             root, "totalResults", OpenSearchUtil.OS_NAMESPACE, total);
199 
200         // opensearch:startIndex
201 
202         OpenSearchUtil.addElement(
203             root, "startIndex", OpenSearchUtil.OS_NAMESPACE, start + 1);
204 
205         // opensearch:itemsPerPage
206 
207         OpenSearchUtil.addElement(
208             root, "itemsPerPage", OpenSearchUtil.OS_NAMESPACE, itemsPerPage);
209 
210         // opensearch:Query
211 
212         Element query = OpenSearchUtil.addElement(
213             root, "Query", OpenSearchUtil.OS_NAMESPACE);
214 
215         query.addAttribute("role", "request");
216         query.addAttribute("searchTerms", keywords);
217         query.addAttribute("startPage", String.valueOf(startPage));
218 
219         // links
220 
221         String searchURL = themeDisplay.getURLPortal() + searchPath;
222 
223         OpenSearchUtil.addLink(
224             root, searchURL, "self", keywords, startPage, itemsPerPage);
225         OpenSearchUtil.addLink(
226             root, searchURL, "first", keywords, 1, itemsPerPage);
227 
228         if (previousPage > 0) {
229             OpenSearchUtil.addLink(
230                 root, searchURL, "previous", keywords, previousPage,
231                 itemsPerPage);
232         }
233 
234         if (nextPage <= totalPages) {
235             OpenSearchUtil.addLink(
236                 root, searchURL, "next", keywords, nextPage, itemsPerPage);
237         }
238 
239         OpenSearchUtil.addLink(
240             root, searchURL, "last", keywords, totalPages, itemsPerPage);
241 
242         Element link = OpenSearchUtil.addElement(
243             root, "link", OpenSearchUtil.DEFAULT_NAMESPACE);
244 
245         link.addAttribute("rel", "search");
246         link.addAttribute("href", searchPath + "_description.xml");
247         link.addAttribute("type", "application/opensearchdescription+xml");
248 
249         return new Object[] {doc, root};
250     }
251 
252     protected PortletURL getPortletURL(
253             HttpServletRequest request, String portletId)
254         throws PortletModeException, SystemException, WindowStateException {
255 
256         return getPortletURL(request, portletId, 0);
257     }
258 
259     protected PortletURL getPortletURL(
260             HttpServletRequest request, String portletId, long groupId)
261         throws PortletModeException, SystemException, WindowStateException {
262 
263         long plid = LayoutLocalServiceUtil.getDefaultPlid(
264             groupId, false, portletId);
265 
266         if (plid == 0) {
267             plid = LayoutLocalServiceUtil.getDefaultPlid(
268                 groupId, true, portletId);
269         }
270 
271         if (plid == 0) {
272             Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
273 
274             if (layout != null) {
275                 plid = layout.getPlid();
276             }
277         }
278 
279         PortletURL portletURL = new PortletURLImpl(
280             request, portletId, plid, PortletRequest.RENDER_PHASE);
281 
282         portletURL.setWindowState(WindowState.MAXIMIZED);
283         portletURL.setPortletMode(PortletMode.VIEW);
284 
285         return portletURL;
286     }
287 
288     private boolean _enabled = GetterUtil.getBoolean(
289         PropsUtil.get(getClass().getName()), true);
290 
291 }