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