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