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 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 _documents.addAll(Arrays.asList(documents));
051
052 if (_documents.size() >= getInterval()) {
053 indexInterval();
054 }
055 }
056
057 @Override
058 public void performActions() throws PortalException {
059 if (BackgroundTaskThreadLocal.hasBackgroundTask()) {
060 _total = super.performCount();
061 }
062
063 try {
064 super.performActions();
065 }
066 finally {
067 _count = _total;
068
069 sendStatusMessage();
070 }
071 }
072
073 public void setSearchEngineId(String searchEngineId) {
074 _searchEngineId = searchEngineId;
075 }
076
077 @Override
078 protected void actionsCompleted() throws PortalException {
079 if (Validator.isNotNull(_searchEngineId)) {
080 IndexWriterHelperUtil.commit(_searchEngineId, getCompanyId());
081 }
082
083 sendStatusMessage();
084 }
085
086 @Override
087 protected long doPerformActions(long previousPrimaryKey)
088 throws PortalException {
089
090 try {
091 return super.doPerformActions(previousPrimaryKey);
092 }
093 finally {
094 indexInterval();
095 }
096 }
097
098 protected String getSearchEngineId() {
099 return _searchEngineId;
100 }
101
102 protected void indexInterval() throws PortalException {
103 if ((_documents == null) || _documents.isEmpty()) {
104 return;
105 }
106
107 if (Validator.isNull(_searchEngineId)) {
108 _searchEngineId = SearchEngineHelperUtil.getSearchEngineId(
109 _documents);
110 }
111
112 IndexWriterHelperUtil.updateDocuments(
113 _searchEngineId, getCompanyId(), new ArrayList<>(_documents),
114 false);
115
116 _count += _documents.size();
117
118 _documents.clear();
119 }
120
121 protected void sendStatusMessage() {
122 if (!BackgroundTaskThreadLocal.hasBackgroundTask()) {
123 return;
124 }
125
126 Class<?> modelClass = getModelClass();
127
128 ReindexStatusMessageSenderUtil.sendStatusMessage(
129 modelClass.getName(), _count, _total);
130 }
131
132 private long _count;
133 private Collection<Document> _documents;
134 private String _searchEngineId;
135 private long _total;
136
137 }