1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.calendar.util;
21  
22  import com.liferay.portal.kernel.search.Document;
23  import com.liferay.portal.kernel.search.DocumentImpl;
24  import com.liferay.portal.kernel.search.DocumentSummary;
25  import com.liferay.portal.kernel.search.Field;
26  import com.liferay.portal.kernel.search.SearchEngineUtil;
27  import com.liferay.portal.kernel.search.SearchException;
28  import com.liferay.portal.kernel.util.HtmlUtil;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portal.util.PortletKeys;
32  import com.liferay.portlet.calendar.model.CalEvent;
33  import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
34  import com.liferay.portlet.expando.model.ExpandoBridge;
35  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
36  
37  import java.util.Date;
38  
39  import javax.portlet.PortletURL;
40  
41  /**
42   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brett Swaim
45   *
46   */
47  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
48  
49      public static final String PORTLET_ID = PortletKeys.CALENDAR;
50  
51      public static void addEvent(
52              long companyId, long groupId, long userId, String userName,
53              long eventId, String title, String description, Date displayDate,
54              String[] tagsEntries, ExpandoBridge expandoBridge)
55          throws SearchException {
56  
57          Document doc = getEventDocument(
58              companyId, groupId, userId, userName, eventId, title, description,
59              displayDate, tagsEntries, expandoBridge);
60  
61          SearchEngineUtil.addDocument(companyId, doc);
62      }
63  
64      public static void deleteEvent(long companyId, long eventId)
65          throws SearchException {
66  
67          SearchEngineUtil.deleteDocument(companyId, getEventUID(eventId));
68      }
69  
70      public static Document getEventDocument(
71          long companyId, long groupId, long userId, String userName,
72          long eventId, String title, String description, Date displayDate,
73          String[] tagsEntries, ExpandoBridge expandoBridge) {
74  
75          userName = PortalUtil.getUserName(userId, userName);
76          description = HtmlUtil.extractText(description);
77  
78          Document doc = new DocumentImpl();
79  
80          doc.addUID(PORTLET_ID, eventId);
81  
82          doc.addModifiedDate(displayDate);
83  
84          doc.addKeyword(Field.COMPANY_ID, companyId);
85          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
86          doc.addKeyword(Field.GROUP_ID, groupId);
87          doc.addKeyword(Field.USER_ID, userId);
88          doc.addText(Field.USER_NAME, userName);
89  
90          doc.addText(Field.TITLE, title);
91          doc.addText(Field.DESCRIPTION, description);
92          doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
93  
94          doc.addKeyword(Field.ENTRY_CLASS_NAME, CalEvent.class.getName());
95          doc.addKeyword(Field.ENTRY_CLASS_PK, eventId);
96  
97          ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
98  
99          return doc;
100     }
101 
102     public static String getEventUID(long eventId) {
103         Document doc = new DocumentImpl();
104 
105         doc.addUID(PORTLET_ID, eventId);
106 
107         return doc.get(Field.UID);
108     }
109 
110     public static void updateEvent(
111             long companyId, long groupId, long userId, String userName,
112             long eventId, String title, String description, Date displayDate,
113             String[] tagsEntries, ExpandoBridge expandoBridge)
114         throws SearchException {
115 
116         Document doc = getEventDocument(
117             companyId, groupId, userId, userName, eventId, title, description,
118             displayDate, tagsEntries, expandoBridge);
119 
120         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
121     }
122 
123     public String[] getClassNames() {
124         return _CLASS_NAMES;
125     }
126 
127     public DocumentSummary getDocumentSummary(
128         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
129 
130         // Title
131 
132         String title = doc.get(Field.TITLE);
133 
134         // Content
135 
136         String content = doc.get(Field.DESCRIPTION);
137 
138         content = StringUtil.shorten(content, 200);
139 
140         // Portlet URL
141 
142         String eventId = doc.get(Field.ENTRY_CLASS_PK);
143 
144         portletURL.setParameter("struts_action", "/calendar/view_event");
145         portletURL.setParameter("eventId", eventId);
146 
147         return new DocumentSummary(title, content, portletURL);
148     }
149 
150     public void reIndex(String className, long classPK) throws SearchException {
151         try {
152             CalEventLocalServiceUtil.reIndex(classPK);
153         }
154         catch (Exception e) {
155             throw new SearchException(e);
156         }
157     }
158 
159     public void reIndex(String[] ids) throws SearchException {
160         try {
161             CalEventLocalServiceUtil.reIndex(ids);
162         }
163         catch (Exception e) {
164             throw new SearchException(e);
165         }
166     }
167 
168     private static final String[] _CLASS_NAMES = new String[] {
169         CalEvent.class.getName()
170     };
171 
172 }