1   /**
2    * Copyright (c) 2000-2007 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.portlet.softwarecatalog.util;
24  
25  import com.liferay.portal.kernel.search.DocumentSummary;
26  import com.liferay.portal.kernel.search.SearchException;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.lucene.LuceneFields;
29  import com.liferay.portal.lucene.LuceneUtil;
30  import com.liferay.portal.util.PortletKeys;
31  import com.liferay.portlet.softwarecatalog.service.SCProductEntryLocalServiceUtil;
32  import com.liferay.util.Html;
33  
34  import java.io.IOException;
35  
36  import java.util.Date;
37  
38  import javax.portlet.PortletURL;
39  
40  import org.apache.lucene.document.Document;
41  import org.apache.lucene.index.IndexWriter;
42  import org.apache.lucene.index.Term;
43  
44  /**
45   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Jorge Ferrer
48   * @author Brian Wing Shun Chan
49   * @author Harry Mark
50   *
51   */
52  public class Indexer
53      implements com.liferay.portal.kernel.search.Indexer {
54  
55      public static final String PORTLET_ID = PortletKeys.SOFTWARE_CATALOG;
56  
57      public static void addProductEntry(
58              long companyId, long groupId, long userId, String userName,
59              long productEntryId, String name, Date modifiedDate, String version,
60              String type, String shortDescription, String longDescription,
61              String pageURL, String repoGroupId, String repoArtifactId)
62          throws IOException {
63  
64          Document doc = getAddProductEntryDocument(
65              companyId, groupId, userId, userName, productEntryId, name,
66              modifiedDate, version, type, shortDescription, longDescription,
67              pageURL, repoGroupId, repoArtifactId);
68  
69          IndexWriter writer = null;
70  
71          try {
72              writer = LuceneUtil.getWriter(companyId);
73  
74              writer.addDocument(doc);
75          }
76          finally {
77              if (writer != null) {
78                  LuceneUtil.write(companyId);
79              }
80          }
81      }
82  
83      public static void deleteProductEntry(long companyId, long productEntryId)
84          throws IOException {
85  
86          LuceneUtil.deleteDocuments(
87              companyId,
88              new Term(
89                  LuceneFields.UID,
90                  LuceneFields.getUID(PORTLET_ID, productEntryId)));
91      }
92  
93      public static Document getAddProductEntryDocument(
94          long companyId, long groupId, long userId, String userName,
95          long productEntryId, String name, Date modifiedDate, String version,
96          String type, String shortDescription, String longDescription,
97          String pageURL, String repoGroupId, String repoArtifactId) {
98  
99          shortDescription = Html.stripHtml(shortDescription);
100         longDescription = Html.stripHtml(longDescription);
101 
102         String content =
103             userId + " " + userName + " " + type + " " + shortDescription +
104                 " " + longDescription + " " + pageURL + repoGroupId + " " +
105                     repoArtifactId;
106 
107         Document doc = new Document();
108 
109         doc.add(
110             LuceneFields.getKeyword(
111                 LuceneFields.UID,
112                 LuceneFields.getUID(PORTLET_ID, productEntryId)));
113 
114         doc.add(LuceneFields.getKeyword(LuceneFields.COMPANY_ID, companyId));
115         doc.add(LuceneFields.getKeyword(LuceneFields.PORTLET_ID, PORTLET_ID));
116         doc.add(LuceneFields.getKeyword(LuceneFields.GROUP_ID, groupId));
117         doc.add(LuceneFields.getKeyword(LuceneFields.USER_ID, userId));
118 
119         doc.add(LuceneFields.getText(LuceneFields.TITLE, name));
120         doc.add(LuceneFields.getText(LuceneFields.CONTENT, content));
121 
122         doc.add(LuceneFields.getDate(LuceneFields.MODIFIED));
123 
124         doc.add(LuceneFields.getKeyword("productEntryId", productEntryId));
125         doc.add(LuceneFields.getDate("modified-date", modifiedDate));
126         doc.add(LuceneFields.getText("version", version));
127         doc.add(LuceneFields.getKeyword("type", type));
128         doc.add(LuceneFields.getKeyword("repoGroupId", repoGroupId));
129         doc.add(LuceneFields.getKeyword("repoArtifactId", repoArtifactId));
130 
131         return doc;
132     }
133 
134     public static void updateProductEntry(
135             long companyId, long groupId, long userId, String userName,
136             long productEntryId, String name, Date modifiedDate, String version,
137             String type, String shortDescription, String longDescription,
138             String pageURL, String repoGroupId, String repoArtifactId)
139         throws IOException {
140 
141         try {
142             deleteProductEntry(companyId, productEntryId);
143         }
144         catch (IOException ioe) {
145         }
146 
147         addProductEntry(
148             companyId, groupId, userId, userName, productEntryId, name,
149             modifiedDate, version, type, shortDescription, longDescription,
150             pageURL, repoGroupId, repoArtifactId);
151     }
152 
153     public DocumentSummary getDocumentSummary(
154         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
155 
156         // Title
157 
158         String title = doc.get(LuceneFields.TITLE);
159 
160         // Content
161 
162         String content = doc.get(LuceneFields.CONTENT);
163 
164         content = StringUtil.shorten(content, 200);
165 
166         // URL
167 
168         String productEntryId = doc.get("productEntryId");
169 
170         portletURL.setParameter(
171             "struts_action", "/software_catalog/view_product_entry");
172         portletURL.setParameter("productEntryId", productEntryId);
173 
174         return new DocumentSummary(title, content, portletURL);
175     }
176 
177     public void reIndex(String[] ids) throws SearchException {
178         try {
179             SCProductEntryLocalServiceUtil.reIndex(ids);
180         }
181         catch (Exception e) {
182             throw new SearchException(e);
183         }
184     }
185 
186 }