001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.dynamicdatalists.util;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.search.BaseIndexer;
021    import com.liferay.portal.kernel.search.BooleanClauseOccur;
022    import com.liferay.portal.kernel.search.BooleanQuery;
023    import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
024    import com.liferay.portal.kernel.search.Document;
025    import com.liferay.portal.kernel.search.Field;
026    import com.liferay.portal.kernel.search.SearchContext;
027    import com.liferay.portal.kernel.search.SearchEngineUtil;
028    import com.liferay.portal.kernel.search.Summary;
029    import com.liferay.portal.kernel.util.ArrayUtil;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.StringPool;
032    import com.liferay.portal.kernel.workflow.WorkflowConstants;
033    import com.liferay.portal.util.PortletKeys;
034    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
035    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
036    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSetConstants;
037    import com.liferay.portlet.dynamicdatalists.model.DDLRecordVersion;
038    import com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalServiceUtil;
039    import com.liferay.portlet.dynamicdatalists.service.DDLRecordSetLocalServiceUtil;
040    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
041    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
042    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
043    import com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil;
044    import com.liferay.portlet.dynamicdatamapping.util.DDMIndexerUtil;
045    
046    import java.util.ArrayList;
047    import java.util.Collection;
048    import java.util.List;
049    import java.util.Locale;
050    import java.util.Set;
051    import java.util.TreeSet;
052    
053    import javax.portlet.PortletURL;
054    
055    /**
056     * @author Marcellus Tavares
057     */
058    public class DDLIndexer extends BaseIndexer {
059    
060            public static final String[] CLASS_NAMES = {DDLRecord.class.getName()};
061    
062            public static final String PORTLET_ID = PortletKeys.DYNAMIC_DATA_LISTS;
063    
064            public DDLIndexer() {
065                    setFilterSearch(true);
066            }
067    
068            @Override
069            public String[] getClassNames() {
070                    return CLASS_NAMES;
071            }
072    
073            @Override
074            public String getPortletId() {
075                    return PORTLET_ID;
076            }
077    
078            @Override
079            public void postProcessContextQuery(
080                            BooleanQuery contextQuery, SearchContext searchContext)
081                    throws Exception {
082    
083                    int status = GetterUtil.getInteger(
084                            searchContext.getAttribute(Field.STATUS),
085                            WorkflowConstants.STATUS_APPROVED);
086    
087                    if (status != WorkflowConstants.STATUS_ANY) {
088                            contextQuery.addRequiredTerm(Field.STATUS, status);
089                    }
090    
091                    long recordSetId = GetterUtil.getLong(
092                            searchContext.getAttribute("recordSetId"));
093    
094                    if (recordSetId > 0) {
095                            contextQuery.addRequiredTerm("recordSetId", recordSetId);
096                    }
097            }
098    
099            @Override
100            public void postProcessSearchQuery(
101                            BooleanQuery searchQuery, SearchContext searchContext)
102                    throws Exception {
103    
104                    Set<DDMStructure> ddmStructuresSet = new TreeSet<DDMStructure>();
105    
106                    long recordSetId = GetterUtil.getLong(
107                            searchContext.getAttribute("recordSetId"));
108    
109                    if (recordSetId > 0) {
110                            DDLRecordSet recordSet = DDLRecordSetLocalServiceUtil.getRecordSet(
111                                    recordSetId);
112    
113                            ddmStructuresSet.add(recordSet.getDDMStructure());
114                    }
115                    else {
116                            long[] groupIds = searchContext.getGroupIds();
117    
118                            if (ArrayUtil.isNotEmpty(groupIds)) {
119                                    List<DDMStructure> ddmStructures =
120                                            DDMStructureLocalServiceUtil.getStructures(groupIds);
121    
122                                    ddmStructuresSet.addAll(ddmStructures);
123                            }
124                    }
125    
126                    BooleanQuery ddmStructureQuery = BooleanQueryFactoryUtil.create(
127                            searchContext);
128    
129                    for (DDMStructure ddmStructure : ddmStructuresSet) {
130                            addSearchDDMStruture(searchQuery, searchContext, ddmStructure);
131                    }
132    
133                    if (ddmStructureQuery.hasClauses()) {
134                            searchQuery.add(ddmStructureQuery, BooleanClauseOccur.MUST);
135                    }
136    
137                    addSearchTerm(searchQuery, searchContext, Field.USER_NAME, false);
138            }
139    
140            @Override
141            protected void doDelete(Object obj) throws Exception {
142                    DDLRecord record = (DDLRecord)obj;
143    
144                    deleteDocument(record.getCompanyId(), record.getRecordId());
145            }
146    
147            @Override
148            protected Document doGetDocument(Object obj) throws Exception {
149                    DDLRecord record = (DDLRecord)obj;
150    
151                    Document document = getBaseModelDocument(PORTLET_ID, record);
152    
153                    DDLRecordVersion recordVersion = record.getRecordVersion();
154    
155                    document.addKeyword(Field.STATUS, recordVersion.getStatus());
156                    document.addKeyword(Field.VERSION, recordVersion.getVersion());
157    
158                    document.addKeyword("recordSetId", recordVersion.getRecordSetId());
159    
160                    DDLRecordSet recordSet = recordVersion.getRecordSet();
161    
162                    DDMStructure ddmStructure = recordSet.getDDMStructure();
163    
164                    Fields fields = StorageEngineUtil.getFields(
165                            recordVersion.getDDMStorageId());
166    
167                    DDMIndexerUtil.addAttributes(document, ddmStructure, fields);
168    
169                    return document;
170            }
171    
172            @Override
173            protected Summary doGetSummary(
174                    Document document, Locale locale, String snippet,
175                    PortletURL portletURL) {
176    
177                    long recordSetId = GetterUtil.getLong(document.get("recordSetId"));
178    
179                    String title = getTitle(recordSetId, locale);
180    
181                    String recordId = document.get(Field.ENTRY_CLASS_PK);
182    
183                    portletURL.setParameter(
184                            "struts_action", "/dynamic_data_lists/view_record");
185                    portletURL.setParameter("recordId", recordId);
186    
187                    Summary summary = createSummary(
188                            document, Field.TITLE, Field.DESCRIPTION);
189    
190                    summary.setMaxContentLength(200);
191                    summary.setPortletURL(portletURL);
192                    summary.setTitle(title);
193    
194                    return summary;
195            }
196    
197            @Override
198            protected void doReindex(Object obj) throws Exception {
199                    DDLRecord record = (DDLRecord)obj;
200    
201                    DDLRecordVersion recordVersion = record.getRecordVersion();
202    
203                    if (!recordVersion.isApproved()) {
204                            return;
205                    }
206    
207                    Document document = getDocument(record);
208    
209                    if (document != null) {
210                            SearchEngineUtil.updateDocument(
211                                    getSearchEngineId(), record.getCompanyId(), document);
212                    }
213            }
214    
215            @Override
216            protected void doReindex(String className, long classPK) throws Exception {
217                    DDLRecord record = DDLRecordLocalServiceUtil.getRecord(classPK);
218    
219                    doReindex(record);
220            }
221    
222            @Override
223            protected void doReindex(String[] ids) throws Exception {
224                    long companyId = GetterUtil.getLong(ids[0]);
225    
226                    reindexRecords(companyId);
227            }
228    
229            @Override
230            protected String getPortletId(SearchContext searchContext) {
231                    return PORTLET_ID;
232            }
233    
234            protected String getTitle(long recordSetId, Locale locale) {
235                    try {
236                            DDLRecordSet recordSet = DDLRecordSetLocalServiceUtil.getRecordSet(
237                                    recordSetId);
238    
239                            DDMStructure ddmStructure = recordSet.getDDMStructure();
240    
241                            String ddmStructureName = ddmStructure.getName(locale);
242    
243                            String recordSetName = recordSet.getName(locale);
244    
245                            return LanguageUtil.format(
246                                    locale, "new-x-for-list-x",
247                                    new Object[] {ddmStructureName, recordSetName});
248                    }
249                    catch (Exception e) {
250                            _log.error(e, e);
251                    }
252    
253                    return StringPool.BLANK;
254            }
255    
256            protected void reindexRecords(long companyId) throws Exception {
257                    Long[] minAndMaxRecordIds =
258                            DDLRecordLocalServiceUtil.getMinAndMaxCompanyRecordIds(
259                                    companyId, WorkflowConstants.STATUS_APPROVED,
260                                    DDLRecordSetConstants.SCOPE_DYNAMIC_DATA_LISTS);
261    
262                    if ((minAndMaxRecordIds[0] == null) ||
263                            (minAndMaxRecordIds[1] == null)) {
264    
265                            return;
266                    }
267    
268                    long minRecordId = minAndMaxRecordIds[0];
269                    long maxRecordId = minAndMaxRecordIds[1];
270    
271                    long startRecordId = minRecordId;
272                    long endRecordId = startRecordId + DEFAULT_INTERVAL;
273    
274                    while (startRecordId <= maxRecordId) {
275                            reindexRecords(companyId, startRecordId, endRecordId);
276    
277                            startRecordId = endRecordId;
278                            endRecordId += DEFAULT_INTERVAL;
279                    }
280            }
281    
282            protected void reindexRecords(
283                            long companyId, long startRecordId, long endRecordId)
284                    throws Exception {
285    
286                    List<DDLRecord> records =
287                            DDLRecordLocalServiceUtil.getMinAndMaxCompanyRecords(
288                                    companyId, WorkflowConstants.STATUS_APPROVED,
289                                    DDLRecordSetConstants.SCOPE_DYNAMIC_DATA_LISTS, startRecordId,
290                                    endRecordId);
291    
292                    Collection<Document> documents = new ArrayList<Document>(
293                            records.size());
294    
295                    for (DDLRecord record : records) {
296                            Document document = getDocument(record);
297    
298                            documents.add(document);
299                    }
300    
301                    SearchEngineUtil.updateDocuments(
302                            getSearchEngineId(), companyId, documents);
303            }
304    
305            private static Log _log = LogFactoryUtil.getLog(DDLIndexer.class);
306    
307    }