1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.journal.util;
21  
22  import com.liferay.portal.kernel.search.Document;
23  import com.liferay.portal.kernel.search.Field;
24  import com.liferay.portal.kernel.search.Hits;
25  import com.liferay.portal.model.Layout;
26  import com.liferay.portal.search.HitsOpenSearchImpl;
27  import com.liferay.portal.service.LayoutLocalServiceUtil;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
31  import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
32  
33  import java.util.List;
34  
35  import javax.portlet.PortletURL;
36  
37  /**
38   * <a href="JournalOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class JournalOpenSearchImpl extends HitsOpenSearchImpl {
44  
45      public static final String SEARCH_PATH = "/c/journal/open_search";
46  
47      public static final String TITLE = "Liferay Journal Search: ";
48  
49      public Hits getHits(
50              long companyId, long groupId, long userId, String keywords,
51              int start, int end)
52          throws Exception {
53  
54          return JournalArticleLocalServiceUtil.search(
55              companyId, groupId, keywords, start, end);
56      }
57  
58      public String getSearchPath() {
59          return SEARCH_PATH;
60      }
61  
62      public String getTitle(String keywords) {
63          return TITLE + keywords;
64      }
65  
66      protected String getURL(
67              ThemeDisplay themeDisplay, long groupId, Document result,
68              PortletURL portletURL)
69          throws Exception {
70  
71          Layout layout = themeDisplay.getLayout();
72  
73          String articleId = result.get(Field.ENTRY_CLASS_PK);
74          String version = result.get("version");
75  
76          List<Long> hitLayoutIds =
77              JournalContentSearchLocalServiceUtil.getLayoutIds(
78                  layout.getGroupId(), layout.isPrivateLayout(), articleId);
79  
80          if (hitLayoutIds.size() > 0) {
81              Long hitLayoutId = hitLayoutIds.get(0);
82  
83              Layout hitLayout = LayoutLocalServiceUtil.getLayout(
84                  layout.getGroupId(), layout.isPrivateLayout(),
85                  hitLayoutId.longValue());
86  
87              return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
88          }
89          else {
90              StringBuilder sb = new StringBuilder();
91  
92              sb.append(themeDisplay.getPathMain());
93              sb.append("/journal/view_article_content?groupId=");
94              sb.append(groupId);
95              sb.append("&articleId=");
96              sb.append(articleId);
97              sb.append("&version=");
98              sb.append(version);
99  
100             return sb.toString();
101         }
102     }
103 
104 }