1   /**
2    * Copyright (c) 2000-2009 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.Document;
26  import com.liferay.portal.kernel.search.DocumentImpl;
27  import com.liferay.portal.kernel.search.DocumentSummary;
28  import com.liferay.portal.kernel.search.Field;
29  import com.liferay.portal.kernel.search.SearchEngineUtil;
30  import com.liferay.portal.kernel.search.SearchException;
31  import com.liferay.portal.kernel.util.HtmlUtil;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.portal.model.Group;
35  import com.liferay.portal.service.GroupLocalServiceUtil;
36  import com.liferay.portal.util.PortalUtil;
37  import com.liferay.portal.util.PortletKeys;
38  import com.liferay.portlet.expando.model.ExpandoBridge;
39  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
40  import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
41  import com.liferay.portlet.softwarecatalog.service.SCProductEntryLocalServiceUtil;
42  
43  import java.util.Date;
44  
45  import javax.portlet.PortletURL;
46  
47  /**
48   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Jorge Ferrer
51   * @author Brian Wing Shun Chan
52   * @author Harry Mark
53   * @author Bruno Farache
54   * @author Raymond Augé
55   */
56  public class Indexer
57      implements com.liferay.portal.kernel.search.Indexer {
58  
59      public static final String PORTLET_ID = PortletKeys.SOFTWARE_CATALOG;
60  
61      public static void addProductEntry(
62              long companyId, long groupId, long userId, String userName,
63              long productEntryId, String name, Date modifiedDate, String version,
64              String type, String shortDescription, String longDescription,
65              String pageURL, String repoGroupId, String repoArtifactId,
66              ExpandoBridge expandoBridge)
67          throws SearchException {
68  
69          Document doc = getProductEntryDocument(
70              companyId, groupId, userId, userName, productEntryId, name,
71              modifiedDate, version, type, shortDescription, longDescription,
72              pageURL, repoGroupId, repoArtifactId, expandoBridge);
73  
74          SearchEngineUtil.addDocument(companyId, doc);
75      }
76  
77      public static void deleteProductEntry(long companyId, long productEntryId)
78          throws SearchException {
79  
80          SearchEngineUtil.deleteDocument(companyId, getEntryUID(productEntryId));
81      }
82  
83      public static Document getProductEntryDocument(
84          long companyId, long groupId, long userId, String userName,
85          long productEntryId, String name, Date modifiedDate, String version,
86          String type, String shortDescription, String longDescription,
87          String pageURL, String repoGroupId, String repoArtifactId,
88          ExpandoBridge expandoBridge) {
89  
90          long scopeGroupId = groupId;
91  
92          try {
93              Group group = GroupLocalServiceUtil.getGroup(groupId);
94  
95              if (group.isLayout()) {
96                  groupId = group.getParentGroupId();
97              }
98          }
99          catch (Exception e) {
100         }
101 
102         userName = PortalUtil.getUserName(userId, userName);
103         shortDescription = HtmlUtil.extractText(shortDescription);
104         longDescription = HtmlUtil.extractText(longDescription);
105 
106         String content =
107             userId + " " + userName + " " + type + " " + shortDescription +
108                 " " + longDescription + " " + pageURL + repoGroupId + " " +
109                     repoArtifactId;
110 
111         Document doc = new DocumentImpl();
112 
113         doc.addUID(PORTLET_ID, productEntryId);
114 
115         doc.addModifiedDate(modifiedDate);
116 
117         doc.addKeyword(Field.COMPANY_ID, companyId);
118         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
119         doc.addKeyword(Field.GROUP_ID, groupId);
120         doc.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
121         doc.addKeyword(Field.USER_ID, userId);
122         doc.addText(Field.USER_NAME, userName);
123 
124         doc.addText(Field.TITLE, name);
125         doc.addText(Field.CONTENT, content);
126 
127         doc.addKeyword(Field.ENTRY_CLASS_NAME, SCProductEntry.class.getName());
128         doc.addKeyword(Field.ENTRY_CLASS_PK, productEntryId);
129         doc.addKeyword("version", version);
130         doc.addKeyword("type", type);
131         doc.addText("shortDescription", shortDescription);
132         doc.addText("longDescription", longDescription);
133         doc.addText("pageURL", pageURL);
134         doc.addKeyword("repoGroupId", repoGroupId);
135         doc.addKeyword("repoArtifactId", repoArtifactId);
136 
137         ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
138 
139         return doc;
140     }
141 
142     public static String getEntryUID(long productEntryId) {
143         Document doc = new DocumentImpl();
144 
145         doc.addUID(PORTLET_ID, productEntryId);
146 
147         return doc.get(Field.UID);
148     }
149 
150     public static void updateProductEntry(
151             long companyId, long groupId, long userId, String userName,
152             long productEntryId, String name, Date modifiedDate, String version,
153             String type, String shortDescription, String longDescription,
154             String pageURL, String repoGroupId, String repoArtifactId,
155             ExpandoBridge expandoBridge)
156         throws SearchException {
157 
158         Document doc = getProductEntryDocument(
159             companyId, groupId, userId, userName, productEntryId, name,
160             modifiedDate, version, type, shortDescription, longDescription,
161             pageURL, repoGroupId, repoArtifactId, expandoBridge);
162 
163         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
164     }
165 
166     public String[] getClassNames() {
167         return _CLASS_NAMES;
168     }
169 
170     public DocumentSummary getDocumentSummary(
171         Document doc, String snippet, PortletURL portletURL) {
172 
173         // Title
174 
175         String title = doc.get(Field.TITLE);
176 
177         // Content
178 
179         String content = snippet;
180 
181         if (Validator.isNull(snippet)) {
182             content = StringUtil.shorten(doc.get(Field.CONTENT), 200);
183         }
184 
185         // Portlet URL
186 
187         String productEntryId = doc.get(Field.ENTRY_CLASS_PK);
188 
189         portletURL.setParameter(
190             "struts_action", "/software_catalog/view_product_entry");
191         portletURL.setParameter("productEntryId", productEntryId);
192 
193         return new DocumentSummary(title, content, portletURL);
194     }
195 
196     public void reIndex(String className, long classPK) throws SearchException {
197         try {
198             SCProductEntryLocalServiceUtil.reIndex(classPK);
199         }
200         catch (Exception e) {
201             throw new SearchException(e);
202         }
203     }
204 
205     public void reIndex(String[] ids) throws SearchException {
206         try {
207             SCProductEntryLocalServiceUtil.reIndex(ids);
208         }
209         catch (Exception e) {
210             throw new SearchException(e);
211         }
212     }
213 
214     private static final String[] _CLASS_NAMES = new String[] {
215         SCProductEntry.class.getName()
216     };
217 
218 }