1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.blogs.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.blogs.model.BlogsEntry;
39  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
40  import com.liferay.portlet.expando.model.ExpandoBridge;
41  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
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 Brian Wing Shun Chan
51   * @author Harry Mark
52   * @author Bruno Farache
53   * @author Raymond Augé
54   */
55  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
56  
57      public static final String PORTLET_ID = PortletKeys.BLOGS;
58  
59      public static void addEntry(
60              long companyId, long groupId, long userId, String userName,
61              long entryId, String title, String content, Date displayDate,
62              String[] tagsEntries, ExpandoBridge expandoBridge)
63          throws SearchException {
64  
65          Document doc = getEntryDocument(
66              companyId, groupId, userId, userName, entryId, title, content,
67              displayDate, tagsEntries, expandoBridge);
68  
69          SearchEngineUtil.addDocument(companyId, doc);
70      }
71  
72      public static void deleteEntry(long companyId, long entryId)
73          throws SearchException {
74  
75          SearchEngineUtil.deleteDocument(companyId, getEntryUID(entryId));
76      }
77  
78      public static Document getEntryDocument(
79          long companyId, long groupId, long userId, String userName,
80          long entryId, String title, String content, Date displayDate,
81          String[] tagsEntries, ExpandoBridge expandoBridge) {
82  
83          long scopeGroupId = groupId;
84  
85          try {
86              Group group = GroupLocalServiceUtil.getGroup(groupId);
87  
88              if (group.isLayout()) {
89                  groupId = group.getParentGroupId();
90              }
91          }
92          catch (Exception e) {
93          }
94  
95          userName = PortalUtil.getUserName(userId, userName);
96          content = HtmlUtil.extractText(content);
97  
98          Document doc = new DocumentImpl();
99  
100         doc.addUID(PORTLET_ID, entryId);
101 
102         doc.addModifiedDate(displayDate);
103 
104         doc.addKeyword(Field.COMPANY_ID, companyId);
105         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
106         doc.addKeyword(Field.GROUP_ID, groupId);
107         doc.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
108         doc.addKeyword(Field.USER_ID, userId);
109         doc.addText(Field.USER_NAME, userName);
110 
111         doc.addText(Field.TITLE, title);
112         doc.addText(Field.CONTENT, content);
113         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
114 
115         doc.addKeyword(Field.ENTRY_CLASS_NAME, BlogsEntry.class.getName());
116         doc.addKeyword(Field.ENTRY_CLASS_PK, entryId);
117 
118         ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
119 
120         return doc;
121     }
122 
123     public static String getEntryUID(long entryId) {
124         Document doc = new DocumentImpl();
125 
126         doc.addUID(PORTLET_ID, entryId);
127 
128         return doc.get(Field.UID);
129     }
130 
131     public static void updateEntry(
132             long companyId, long groupId, long userId, String userName,
133             long entryId, String title, String content, Date displayDate,
134             String[] tagsEntries, ExpandoBridge expandoBridge)
135         throws SearchException {
136 
137         Document doc = getEntryDocument(
138             companyId, groupId, userId, userName, entryId, title, content,
139             displayDate, tagsEntries, expandoBridge);
140 
141         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
142     }
143 
144     public String[] getClassNames() {
145         return _CLASS_NAMES;
146     }
147 
148     public DocumentSummary getDocumentSummary(
149         Document doc, String snippet, PortletURL portletURL) {
150 
151         // Title
152 
153         String title = doc.get(Field.TITLE);
154 
155         // Content
156 
157         String content = snippet;
158 
159         if (Validator.isNull(snippet)) {
160             content = StringUtil.shorten(doc.get(Field.CONTENT), 200);
161         }
162 
163         // Portlet URL
164 
165         String entryId = doc.get(Field.ENTRY_CLASS_PK);
166 
167         portletURL.setParameter("struts_action", "/blogs/view_entry");
168         portletURL.setParameter("entryId", entryId);
169 
170         return new DocumentSummary(title, content, portletURL);
171     }
172 
173     public void reIndex(String className, long classPK) throws SearchException {
174         try {
175             BlogsEntryLocalServiceUtil.reIndex(classPK);
176         }
177         catch (Exception e) {
178             throw new SearchException(e);
179         }
180     }
181 
182     public void reIndex(String[] ids) throws SearchException {
183         try {
184             BlogsEntryLocalServiceUtil.reIndex(ids);
185         }
186         catch (Exception e) {
187             throw new SearchException(e);
188         }
189     }
190 
191     private static final String[] _CLASS_NAMES = new String[] {
192         BlogsEntry.class.getName()
193     };
194 
195 }