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