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