001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.search;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.dao.search.SearchContainer;
026: import com.liferay.portal.kernel.search.Hits;
027: import com.liferay.portal.kernel.search.OpenSearch;
028: import com.liferay.portal.kernel.search.SearchException;
029: import com.liferay.portal.kernel.util.GetterUtil;
030: import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
031: import com.liferay.portal.model.Layout;
032: import com.liferay.portal.service.LayoutLocalServiceUtil;
033: import com.liferay.portal.theme.ThemeDisplay;
034: import com.liferay.portal.util.WebKeys;
035: import com.liferay.portlet.PortletURLImpl;
036: import com.liferay.util.Http;
037:
038: import java.util.Date;
039:
040: import javax.portlet.PortletMode;
041: import javax.portlet.PortletModeException;
042: import javax.portlet.PortletURL;
043: import javax.portlet.WindowState;
044: import javax.portlet.WindowStateException;
045:
046: import javax.servlet.http.HttpServletRequest;
047:
048: import org.dom4j.DocumentHelper;
049: import org.dom4j.Element;
050:
051: /**
052: * <a href="BaseOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
053: *
054: * @author Charles May
055: * @author Brian Wing Shun Chan
056: *
057: */
058: public abstract class BaseOpenSearchImpl implements OpenSearch {
059:
060: public boolean isEnabled() {
061: return true;
062: }
063:
064: public String search(HttpServletRequest req, String url)
065: throws SearchException {
066:
067: String keywords = GetterUtil.getString(Http.getParameter(url,
068: "keywords", false));
069: int startPage = GetterUtil.getInteger(Http.getParameter(url,
070: "p", false), 1);
071: int itemsPerPage = GetterUtil.getInteger(Http.getParameter(url,
072: "c", false), SearchContainer.DEFAULT_DELTA);
073:
074: return search(req, keywords, startPage, itemsPerPage);
075: }
076:
077: public abstract String search(HttpServletRequest req,
078: String keywords, int startPage, int itemsPerPage)
079: throws SearchException;
080:
081: protected void addSearchResult(Element root, String title,
082: String link, Date updated, String summary, double score) {
083:
084: // entry
085:
086: Element entry = OpenSearchUtil.addElement(root, "entry",
087: OpenSearchUtil.DEFAULT_NAMESPACE);
088:
089: // title
090:
091: OpenSearchUtil.addElement(entry, "title",
092: OpenSearchUtil.DEFAULT_NAMESPACE, title);
093:
094: // link
095:
096: Element entryLink = OpenSearchUtil.addElement(entry, "link",
097: OpenSearchUtil.DEFAULT_NAMESPACE);
098:
099: entryLink.addAttribute("href", link);
100:
101: // id
102:
103: OpenSearchUtil.addElement(entry, "id",
104: OpenSearchUtil.DEFAULT_NAMESPACE, "urn:uuid:"
105: + PortalUUIDUtil.generate());
106:
107: // updated
108:
109: OpenSearchUtil.addElement(entry, "updated",
110: OpenSearchUtil.DEFAULT_NAMESPACE, updated);
111:
112: // summary
113:
114: OpenSearchUtil.addElement(entry, "summary",
115: OpenSearchUtil.DEFAULT_NAMESPACE, summary);
116:
117: // relevance:score
118:
119: OpenSearchUtil.addElement(entry, "score",
120: OpenSearchUtil.RELEVANCE_NAMESPACE, score);
121: }
122:
123: protected Object[] addSearchResults(String keywords, int startPage,
124: int itemsPerPage, int total, String title,
125: String searchPath, ThemeDisplay themeDisplay) {
126:
127: return addSearchResults(keywords, startPage, itemsPerPage,
128: null, title, searchPath, themeDisplay);
129: }
130:
131: protected Object[] addSearchResults(String keywords, int startPage,
132: int itemsPerPage, Hits hits, String title,
133: String searchPath, ThemeDisplay themeDisplay) {
134:
135: return addSearchResults(keywords, startPage, itemsPerPage, 0,
136: hits, title, searchPath, themeDisplay);
137: }
138:
139: protected Object[] addSearchResults(String keywords, int startPage,
140: int itemsPerPage, int total, Hits hits, String title,
141: String searchPath, ThemeDisplay themeDisplay) {
142:
143: int begin = (startPage * itemsPerPage) - itemsPerPage;
144:
145: if (hits != null) {
146: int end = startPage * itemsPerPage;
147:
148: total = hits.getLength();
149:
150: if (end > total) {
151: end = total;
152: }
153:
154: hits = hits.subset(begin, end);
155: }
156:
157: int totalPages = 0;
158:
159: if (total % itemsPerPage == 0) {
160: totalPages = total / itemsPerPage;
161: } else {
162: totalPages = (total / itemsPerPage) + 1;
163: }
164:
165: int previousPage = startPage - 1;
166: int nextPage = startPage + 1;
167:
168: // Create document
169:
170: org.dom4j.Document doc = DocumentHelper.createDocument();
171:
172: // feed
173:
174: Element root = doc.addElement("feed");
175:
176: root.add(OpenSearchUtil
177: .getNamespace(OpenSearchUtil.DEFAULT_NAMESPACE));
178: root.add(OpenSearchUtil
179: .getNamespace(OpenSearchUtil.OS_NAMESPACE));
180: root.add(OpenSearchUtil
181: .getNamespace(OpenSearchUtil.RELEVANCE_NAMESPACE));
182:
183: // title
184:
185: OpenSearchUtil.addElement(root, "title",
186: OpenSearchUtil.DEFAULT_NAMESPACE, title);
187:
188: // updated
189:
190: OpenSearchUtil.addElement(root, "updated",
191: OpenSearchUtil.DEFAULT_NAMESPACE, new Date());
192:
193: // author
194:
195: Element author = OpenSearchUtil.addElement(root, "author",
196: OpenSearchUtil.DEFAULT_NAMESPACE);
197:
198: // name
199:
200: OpenSearchUtil.addElement(author, "name",
201: OpenSearchUtil.DEFAULT_NAMESPACE, themeDisplay
202: .getUserId());
203:
204: // id
205:
206: OpenSearchUtil.addElement(root, "id",
207: OpenSearchUtil.DEFAULT_NAMESPACE, "urn:uuid:"
208: + PortalUUIDUtil.generate());
209:
210: // opensearch:totalResults
211:
212: OpenSearchUtil.addElement(root, "totalResults",
213: OpenSearchUtil.OS_NAMESPACE, total);
214:
215: // opensearch:startIndex
216:
217: OpenSearchUtil.addElement(root, "startIndex",
218: OpenSearchUtil.OS_NAMESPACE, begin + 1);
219:
220: // opensearch:itemsPerPage
221:
222: OpenSearchUtil.addElement(root, "itemsPerPage",
223: OpenSearchUtil.OS_NAMESPACE, itemsPerPage);
224:
225: // opensearch:Query
226:
227: Element query = OpenSearchUtil.addElement(root, "Query",
228: OpenSearchUtil.OS_NAMESPACE);
229:
230: query.addAttribute("role", "request");
231: query.addAttribute("searchTerms", keywords);
232: query.addAttribute("startPage", String.valueOf(startPage));
233:
234: // links
235:
236: String searchURL = themeDisplay.getURLPortal() + searchPath;
237:
238: OpenSearchUtil.addLink(root, searchURL, "self", keywords,
239: startPage, itemsPerPage);
240: OpenSearchUtil.addLink(root, searchURL, "first", keywords, 1,
241: itemsPerPage);
242:
243: if (previousPage > 0) {
244: OpenSearchUtil.addLink(root, searchURL, "previous",
245: keywords, previousPage, itemsPerPage);
246: }
247:
248: if (nextPage <= totalPages) {
249: OpenSearchUtil.addLink(root, searchURL, "next", keywords,
250: nextPage, itemsPerPage);
251: }
252:
253: OpenSearchUtil.addLink(root, searchURL, "last", keywords,
254: totalPages, itemsPerPage);
255:
256: Element link = OpenSearchUtil.addElement(root, "link",
257: OpenSearchUtil.DEFAULT_NAMESPACE);
258:
259: link.addAttribute("rel", "search");
260: link.addAttribute("href", searchPath + "_description.xml");
261: link.addAttribute("type",
262: "application/opensearchdescription+xml");
263:
264: return new Object[] { hits, doc, root };
265: }
266:
267: protected PortletURL getPortletURL(HttpServletRequest req,
268: String portletId) throws PortalException,
269: PortletModeException, SystemException, WindowStateException {
270:
271: return getPortletURL(req, portletId, 0);
272: }
273:
274: protected PortletURL getPortletURL(HttpServletRequest req,
275: String portletId, long groupId) throws PortalException,
276: PortletModeException, SystemException, WindowStateException {
277:
278: long plid = LayoutLocalServiceUtil
279: .getDefaultPlid(groupId, true);
280:
281: if (plid == 0) {
282: plid = LayoutLocalServiceUtil
283: .getDefaultPlid(groupId, false);
284: }
285:
286: if (plid == 0) {
287: Layout layout = (Layout) req.getAttribute(WebKeys.LAYOUT);
288:
289: if (layout != null) {
290: plid = layout.getPlid();
291: }
292: }
293:
294: PortletURL portletURL = new PortletURLImpl(req, portletId,
295: plid, false);
296:
297: portletURL.setWindowState(WindowState.MAXIMIZED);
298: portletURL.setPortletMode(PortletMode.VIEW);
299:
300: return portletURL;
301: }
302:
303: }
|