001    /**
002     * Copyright (c) 2000-present 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.portal.kernel.dao.orm;
016    
017    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskThreadLocal;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.search.Document;
020    import com.liferay.portal.kernel.search.IndexWriterHelperUtil;
021    import com.liferay.portal.kernel.search.SearchEngineHelperUtil;
022    import com.liferay.portal.kernel.search.background.task.ReindexStatusMessageSenderUtil;
023    import com.liferay.portal.kernel.util.ArrayUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    
026    import java.util.ArrayList;
027    import java.util.Collection;
028    import java.util.concurrent.ConcurrentLinkedDeque;
029    
030    /**
031     * @author Andrew Betts
032     */
033    public class IndexableActionableDynamicQuery
034            extends DefaultActionableDynamicQuery {
035    
036            public void addDocuments(Document... documents) throws PortalException {
037                    if (ArrayUtil.isEmpty(documents)) {
038                            return;
039                    }
040    
041                    if (_documents == null) {
042                            if (isParallel()) {
043                                    _documents = new ConcurrentLinkedDeque<>();
044                            }
045                            else {
046                                    _documents = new ArrayList<>();
047                            }
048                    }
049    
050                    for (Document document : documents) {
051                            if (document != null) {
052                                    _documents.add(document);
053                            }
054                    }
055    
056                    if (_documents.size() >= getInterval()) {
057                            indexInterval();
058                    }
059            }
060    
061            @Override
062            public void performActions() throws PortalException {
063                    if (BackgroundTaskThreadLocal.hasBackgroundTask()) {
064                            _total = super.performCount();
065                    }
066    
067                    try {
068                            super.performActions();
069                    }
070                    finally {
071                            _count = _total;
072    
073                            sendStatusMessage();
074                    }
075            }
076    
077            public void setSearchEngineId(String searchEngineId) {
078                    _searchEngineId = searchEngineId;
079            }
080    
081            @Override
082            protected void actionsCompleted() throws PortalException {
083                    if (Validator.isNotNull(_searchEngineId)) {
084                            IndexWriterHelperUtil.commit(_searchEngineId, getCompanyId());
085                    }
086    
087                    sendStatusMessage();
088            }
089    
090            @Override
091            protected long doPerformActions(long previousPrimaryKey)
092                    throws PortalException {
093    
094                    try {
095                            return super.doPerformActions(previousPrimaryKey);
096                    }
097                    finally {
098                            indexInterval();
099                    }
100            }
101    
102            protected String getSearchEngineId() {
103                    return _searchEngineId;
104            }
105    
106            protected void indexInterval() throws PortalException {
107                    if ((_documents == null) || _documents.isEmpty()) {
108                            return;
109                    }
110    
111                    if (Validator.isNull(_searchEngineId)) {
112                            _searchEngineId = SearchEngineHelperUtil.getSearchEngineId(
113                                    _documents);
114                    }
115    
116                    IndexWriterHelperUtil.updateDocuments(
117                            _searchEngineId, getCompanyId(), new ArrayList<>(_documents),
118                            false);
119    
120                    _count += _documents.size();
121    
122                    _documents.clear();
123            }
124    
125            protected void sendStatusMessage() {
126                    if (!BackgroundTaskThreadLocal.hasBackgroundTask()) {
127                            return;
128                    }
129    
130                    Class<?> modelClass = getModelClass();
131    
132                    ReindexStatusMessageSenderUtil.sendStatusMessage(
133                            modelClass.getName(), _count, _total);
134            }
135    
136            private long _count;
137            private Collection<Document> _documents;
138            private String _searchEngineId;
139            private long _total;
140    
141    }