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