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.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
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 for (Document document : documents) {
042 if (document != null) {
043 _documents.add(document);
044 }
045 }
046
047 long size = _documents.size();
048
049 if (size >= getInterval()) {
050 indexInterval();
051 }
052 else if ((size % _STATUS_INTERVAL) == 0) {
053 sendStatusMessage(size);
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 @Override
074 public void setParallel(boolean parallel) {
075 if (isParallel() == parallel) {
076 return;
077 }
078
079 super.setParallel(parallel);
080
081 if (parallel) {
082 _documents = new ConcurrentLinkedDeque<>();
083 }
084 }
085
086 public void setSearchEngineId(String searchEngineId) {
087 _searchEngineId = searchEngineId;
088 }
089
090 @Override
091 protected void actionsCompleted() throws PortalException {
092 if (Validator.isNotNull(_searchEngineId)) {
093 IndexWriterHelperUtil.commit(_searchEngineId, getCompanyId());
094 }
095 }
096
097 @Override
098 protected long doPerformActions(long previousPrimaryKey)
099 throws PortalException {
100
101 try {
102 return super.doPerformActions(previousPrimaryKey);
103 }
104 finally {
105 indexInterval();
106 }
107 }
108
109 protected String getSearchEngineId() {
110 return _searchEngineId;
111 }
112
113 protected void indexInterval() throws PortalException {
114 if ((_documents == null) || _documents.isEmpty()) {
115 return;
116 }
117
118 if (Validator.isNull(_searchEngineId)) {
119 _searchEngineId = SearchEngineHelperUtil.getSearchEngineId(
120 _documents);
121 }
122
123 IndexWriterHelperUtil.updateDocuments(
124 _searchEngineId, getCompanyId(), new ArrayList<>(_documents),
125 false);
126
127 _count += _documents.size();
128
129 _documents.clear();
130
131 sendStatusMessage();
132 }
133
134 protected void sendStatusMessage() {
135 sendStatusMessage(0);
136 }
137
138 protected void sendStatusMessage(long documentIntervalCount) {
139 if (!BackgroundTaskThreadLocal.hasBackgroundTask()) {
140 return;
141 }
142
143 Class<?> modelClass = getModelClass();
144
145 ReindexStatusMessageSenderUtil.sendStatusMessage(
146 modelClass.getName(), _count + documentIntervalCount, _total);
147 }
148
149 private static final long _STATUS_INTERVAL = 1000;
150
151 private long _count;
152 private Collection<Document> _documents = new ArrayList<>();
153 private String _searchEngineId;
154 private long _total;
155
156 }