001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.calendar.util;
016    
017    import com.liferay.portal.kernel.search.BaseIndexer;
018    import com.liferay.portal.kernel.search.Document;
019    import com.liferay.portal.kernel.search.DocumentImpl;
020    import com.liferay.portal.kernel.search.Field;
021    import com.liferay.portal.kernel.search.Indexer;
022    import com.liferay.portal.kernel.search.SearchContext;
023    import com.liferay.portal.kernel.search.SearchEngineUtil;
024    import com.liferay.portal.kernel.search.Summary;
025    import com.liferay.portal.kernel.util.GetterUtil;
026    import com.liferay.portal.kernel.util.HtmlUtil;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.PortletKeys;
031    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
032    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
033    import com.liferay.portlet.calendar.model.CalEvent;
034    import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
035    import com.liferay.portlet.expando.model.ExpandoBridge;
036    import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
037    
038    import java.util.ArrayList;
039    import java.util.Collection;
040    import java.util.Date;
041    import java.util.List;
042    
043    import javax.portlet.PortletURL;
044    
045    /**
046     * @author Brett Swaim
047     */
048    public class CalIndexer extends BaseIndexer {
049    
050            public static final String[] CLASS_NAMES = {CalEvent.class.getName()};
051    
052            public static final String PORTLET_ID = PortletKeys.CALENDAR;
053    
054            public String[] getClassNames() {
055                    return CLASS_NAMES;
056            }
057    
058            public Summary getSummary(
059                    Document document, String snippet, PortletURL portletURL) {
060    
061                    String title = document.get(Field.TITLE);
062    
063                    String content = snippet;
064    
065                    if (Validator.isNull(snippet)) {
066                            content = StringUtil.shorten(document.get(Field.DESCRIPTION), 200);
067                    }
068    
069                    String eventId = document.get(Field.ENTRY_CLASS_PK);
070    
071                    portletURL.setParameter("struts_action", "/calendar/view_event");
072                    portletURL.setParameter("eventId", eventId);
073    
074                    return new Summary(title, content, portletURL);
075            }
076    
077            protected void doDelete(Object obj) throws Exception {
078                    CalEvent event = (CalEvent)obj;
079    
080                    Document document = new DocumentImpl();
081    
082                    document.addUID(PORTLET_ID, event.getEventId());
083    
084                    SearchEngineUtil.deleteDocument(
085                            event.getCompanyId(), document.get(Field.UID));
086            }
087    
088            protected Document doGetDocument(Object obj) throws Exception {
089                    CalEvent event = (CalEvent)obj;
090    
091                    long companyId = event.getCompanyId();
092                    long groupId = getParentGroupId(event.getGroupId());
093                    long scopeGroupId = event.getGroupId();
094                    long userId = event.getUserId();
095                    long eventId = event.getEventId();
096                    String userName = PortalUtil.getUserName(userId, event.getUserName());
097                    String title = event.getTitle();
098                    String description = HtmlUtil.extractText(event.getDescription());
099                    Date modifiedDate = event.getModifiedDate();
100    
101                    long[] assetCategoryIds = AssetCategoryLocalServiceUtil.getCategoryIds(
102                            CalEvent.class.getName(), eventId);
103                    String[] assetCategoryNames =
104                            AssetCategoryLocalServiceUtil.getCategoryNames(
105                                    CalEvent.class.getName(), eventId);
106                    String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
107                            CalEvent.class.getName(), eventId);
108    
109                    ExpandoBridge expandoBridge = event.getExpandoBridge();
110    
111                    Document document = new DocumentImpl();
112    
113                    document.addUID(PORTLET_ID, eventId);
114    
115                    document.addModifiedDate(modifiedDate);
116    
117                    document.addKeyword(Field.COMPANY_ID, companyId);
118                    document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
119                    document.addKeyword(Field.GROUP_ID, groupId);
120                    document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
121                    document.addKeyword(Field.USER_ID, userId);
122                    document.addText(Field.USER_NAME, userName);
123    
124                    document.addText(Field.TITLE, title);
125                    document.addText(Field.DESCRIPTION, description);
126                    document.addKeyword(Field.ASSET_CATEGORY_IDS, assetCategoryIds);
127                    document.addKeyword(Field.ASSET_CATEGORY_NAMES, assetCategoryNames);
128                    document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
129    
130                    document.addKeyword(Field.ENTRY_CLASS_NAME, CalEvent.class.getName());
131                    document.addKeyword(Field.ENTRY_CLASS_PK, eventId);
132    
133                    ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
134    
135                    return document;
136            }
137    
138            protected void doReindex(Object obj) throws Exception {
139                    CalEvent event = (CalEvent)obj;
140    
141                    Document document = getDocument(event);
142    
143                    SearchEngineUtil.updateDocument(event.getCompanyId(), document);
144            }
145    
146            protected void doReindex(String className, long classPK) throws Exception {
147                    CalEvent event = CalEventLocalServiceUtil.getEvent(classPK);
148    
149                    doReindex(event);
150            }
151    
152            protected void doReindex(String[] ids) throws Exception {
153                    long companyId = GetterUtil.getLong(ids[0]);
154    
155                    reindexEvents(companyId);
156            }
157    
158            protected String getPortletId(SearchContext searchContext) {
159                    return PORTLET_ID;
160            }
161    
162            protected void reindexEvents(long companyId) throws Exception {
163                    int count = CalEventLocalServiceUtil.getCompanyEventsCount(companyId);
164    
165                    int pages = count / Indexer.DEFAULT_INTERVAL;
166    
167                    for (int i = 0; i <= pages; i++) {
168                            int start = (i * Indexer.DEFAULT_INTERVAL);
169                            int end = start + Indexer.DEFAULT_INTERVAL;
170    
171                            reindexEvents(companyId, start, end);
172                    }
173            }
174    
175            protected void reindexEvents(long companyId, int start, int end)
176                    throws Exception {
177    
178                    List<CalEvent> events = CalEventLocalServiceUtil.getCompanyEvents(
179                            companyId, start, end);
180    
181                    if (events.isEmpty()) {
182                            return;
183                    }
184    
185                    Collection<Document> documents = new ArrayList<Document>();
186    
187                    for (CalEvent event : events) {
188                            Document document = getDocument(event);
189    
190                            documents.add(document);
191                    }
192    
193                    SearchEngineUtil.updateDocuments(companyId, documents);
194            }
195    
196    }