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