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.calendar.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.util.PortalUtil;
34  import com.liferay.portal.util.PortletKeys;
35  import com.liferay.portlet.calendar.model.CalEvent;
36  import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
37  import com.liferay.portlet.expando.model.ExpandoBridge;
38  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
39  
40  import java.util.Date;
41  
42  import javax.portlet.PortletURL;
43  
44  /**
45   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brett Swaim
48   *
49   */
50  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
51  
52      public static final String PORTLET_ID = PortletKeys.CALENDAR;
53  
54      public static void addEvent(
55              long companyId, long groupId, long userId, String userName,
56              long eventId, String title, String description, Date displayDate,
57              String[] tagsEntries, ExpandoBridge expandoBridge)
58          throws SearchException {
59  
60          Document doc = getEventDocument(
61              companyId, groupId, userId, userName, eventId, title, description,
62              displayDate, tagsEntries, expandoBridge);
63  
64          SearchEngineUtil.addDocument(companyId, doc);
65      }
66  
67      public static void deleteEvent(long companyId, long eventId)
68          throws SearchException {
69  
70          SearchEngineUtil.deleteDocument(companyId, getEventUID(eventId));
71      }
72  
73      public static Document getEventDocument(
74          long companyId, long groupId, long userId, String userName,
75          long eventId, String title, String description, Date displayDate,
76          String[] tagsEntries, ExpandoBridge expandoBridge) {
77  
78          userName = PortalUtil.getUserName(userId, userName);
79          description = HtmlUtil.extractText(description);
80  
81          Document doc = new DocumentImpl();
82  
83          doc.addUID(PORTLET_ID, eventId);
84  
85          doc.addModifiedDate(displayDate);
86  
87          doc.addKeyword(Field.COMPANY_ID, companyId);
88          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
89          doc.addKeyword(Field.GROUP_ID, groupId);
90          doc.addKeyword(Field.USER_ID, userId);
91          doc.addText(Field.USER_NAME, userName);
92  
93          doc.addText(Field.TITLE, title);
94          doc.addText(Field.DESCRIPTION, description);
95          doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
96  
97          doc.addKeyword(Field.ENTRY_CLASS_NAME, CalEvent.class.getName());
98          doc.addKeyword(Field.ENTRY_CLASS_PK, eventId);
99  
100         ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
101 
102         return doc;
103     }
104 
105     public static String getEventUID(long eventId) {
106         Document doc = new DocumentImpl();
107 
108         doc.addUID(PORTLET_ID, eventId);
109 
110         return doc.get(Field.UID);
111     }
112 
113     public static void updateEvent(
114             long companyId, long groupId, long userId, String userName,
115             long eventId, String title, String description, Date displayDate,
116             String[] tagsEntries, ExpandoBridge expandoBridge)
117         throws SearchException {
118 
119         Document doc = getEventDocument(
120             companyId, groupId, userId, userName, eventId, title, description,
121             displayDate, tagsEntries, expandoBridge);
122 
123         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
124     }
125 
126     public String[] getClassNames() {
127         return _CLASS_NAMES;
128     }
129 
130     public DocumentSummary getDocumentSummary(
131         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
132 
133         // Title
134 
135         String title = doc.get(Field.TITLE);
136 
137         // Content
138 
139         String content = doc.get(Field.DESCRIPTION);
140 
141         content = StringUtil.shorten(content, 200);
142 
143         // Portlet URL
144 
145         String eventId = doc.get(Field.ENTRY_CLASS_PK);
146 
147         portletURL.setParameter("struts_action", "/calendar/view_event");
148         portletURL.setParameter("eventId", eventId);
149 
150         return new DocumentSummary(title, content, portletURL);
151     }
152 
153     public void reIndex(String className, long classPK) throws SearchException {
154         try {
155             CalEventLocalServiceUtil.reIndex(classPK);
156         }
157         catch (Exception e) {
158             throw new SearchException(e);
159         }
160     }
161 
162     public void reIndex(String[] ids) throws SearchException {
163         try {
164             CalEventLocalServiceUtil.reIndex(ids);
165         }
166         catch (Exception e) {
167             throw new SearchException(e);
168         }
169     }
170 
171     private static final String[] _CLASS_NAMES = new String[] {
172         CalEvent.class.getName()
173     };
174 
175 }