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.blogs.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.blogs.model.BlogsEntry;
31  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
32  import com.liferay.portlet.expando.model.ExpandoBridge;
33  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
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 Brian Wing Shun Chan
43   * @author Harry Mark
44   * @author Bruno Farache
45   * @author Raymond Augé
46   */
47  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
48  
49      public static final String PORTLET_ID = PortletKeys.BLOGS;
50  
51      public static void addEntry(
52              long companyId, long groupId, long userId, String userName,
53              long entryId, String title, String content, Date displayDate,
54              String[] tagsEntries, ExpandoBridge expandoBridge)
55          throws SearchException {
56  
57          Document doc = getEntryDocument(
58              companyId, groupId, userId, userName, entryId, title, content,
59              displayDate, tagsEntries, expandoBridge);
60  
61          SearchEngineUtil.addDocument(companyId, doc);
62      }
63  
64      public static void deleteEntry(long companyId, long entryId)
65          throws SearchException {
66  
67          SearchEngineUtil.deleteDocument(companyId, getEntryUID(entryId));
68      }
69  
70      public static Document getEntryDocument(
71          long companyId, long groupId, long userId, String userName,
72          long entryId, String title, String content, Date displayDate,
73          String[] tagsEntries, ExpandoBridge expandoBridge) {
74  
75          long scopeGroupId = groupId;
76  
77          try {
78              Group group = GroupLocalServiceUtil.getGroup(groupId);
79  
80              if (group.isLayout()) {
81                  groupId = group.getParentGroupId();
82              }
83          }
84          catch (Exception e) {
85          }
86  
87          userName = PortalUtil.getUserName(userId, userName);
88          content = HtmlUtil.extractText(content);
89  
90          Document doc = new DocumentImpl();
91  
92          doc.addUID(PORTLET_ID, entryId);
93  
94          doc.addModifiedDate(displayDate);
95  
96          doc.addKeyword(Field.COMPANY_ID, companyId);
97          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
98          doc.addKeyword(Field.GROUP_ID, groupId);
99          doc.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
100         doc.addKeyword(Field.USER_ID, userId);
101         doc.addText(Field.USER_NAME, userName);
102 
103         doc.addText(Field.TITLE, title);
104         doc.addText(Field.CONTENT, content);
105         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
106 
107         doc.addKeyword(Field.ENTRY_CLASS_NAME, BlogsEntry.class.getName());
108         doc.addKeyword(Field.ENTRY_CLASS_PK, entryId);
109 
110         ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
111 
112         return doc;
113     }
114 
115     public static String getEntryUID(long entryId) {
116         Document doc = new DocumentImpl();
117 
118         doc.addUID(PORTLET_ID, entryId);
119 
120         return doc.get(Field.UID);
121     }
122 
123     public static void updateEntry(
124             long companyId, long groupId, long userId, String userName,
125             long entryId, String title, String content, Date displayDate,
126             String[] tagsEntries, ExpandoBridge expandoBridge)
127         throws SearchException {
128 
129         Document doc = getEntryDocument(
130             companyId, groupId, userId, userName, entryId, title, content,
131             displayDate, tagsEntries, expandoBridge);
132 
133         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
134     }
135 
136     public String[] getClassNames() {
137         return _CLASS_NAMES;
138     }
139 
140     public DocumentSummary getDocumentSummary(
141         Document doc, String snippet, PortletURL portletURL) {
142 
143         // Title
144 
145         String title = doc.get(Field.TITLE);
146 
147         // Content
148 
149         String content = snippet;
150 
151         if (Validator.isNull(snippet)) {
152             content = StringUtil.shorten(doc.get(Field.CONTENT), 200);
153         }
154 
155         // Portlet URL
156 
157         String entryId = doc.get(Field.ENTRY_CLASS_PK);
158 
159         portletURL.setParameter("struts_action", "/blogs/view_entry");
160         portletURL.setParameter("entryId", entryId);
161 
162         return new DocumentSummary(title, content, portletURL);
163     }
164 
165     public void reIndex(String className, long classPK) throws SearchException {
166         try {
167             BlogsEntryLocalServiceUtil.reIndex(classPK);
168         }
169         catch (Exception e) {
170             throw new SearchException(e);
171         }
172     }
173 
174     public void reIndex(String[] ids) throws SearchException {
175         try {
176             BlogsEntryLocalServiceUtil.reIndex(ids);
177         }
178         catch (Exception e) {
179             throw new SearchException(e);
180         }
181     }
182 
183     private static final String[] _CLASS_NAMES = new String[] {
184         BlogsEntry.class.getName()
185     };
186 
187 }