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