001
014
015 package com.liferay.portal.lar.backgroundtask;
016
017 import com.liferay.portal.kernel.backgroundtask.BackgroundTaskResult;
018 import com.liferay.portal.kernel.backgroundtask.BaseBackgroundTaskExecutor;
019 import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
020 import com.liferay.portal.kernel.dao.orm.DynamicQuery;
021 import com.liferay.portal.kernel.dao.orm.Property;
022 import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
023 import com.liferay.portal.kernel.exception.PortalException;
024 import com.liferay.portal.kernel.lar.PortletDataContext;
025 import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
026 import com.liferay.portal.kernel.log.Log;
027 import com.liferay.portal.kernel.log.LogFactoryUtil;
028 import com.liferay.portal.kernel.search.Indexer;
029 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
030 import com.liferay.portal.kernel.util.ArrayUtil;
031 import com.liferay.portal.kernel.util.GetterUtil;
032 import com.liferay.portal.kernel.util.MapUtil;
033 import com.liferay.portal.kernel.util.ReflectionUtil;
034 import com.liferay.portal.kernel.util.StringBundler;
035 import com.liferay.portal.kernel.util.StringPool;
036 import com.liferay.portal.model.BackgroundTask;
037 import com.liferay.portal.model.User;
038 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
039 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
040 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
041 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
042 import com.liferay.portlet.journal.model.JournalArticle;
043 import com.liferay.portlet.journal.service.persistence.JournalArticleActionableDynamicQuery;
044 import com.liferay.portlet.journal.util.JournalArticleIndexer;
045
046 import java.io.Serializable;
047
048 import java.lang.reflect.Method;
049
050 import java.util.ArrayList;
051 import java.util.List;
052 import java.util.Map;
053
054
057 public class StagingIndexingBackgroundTaskExecutor
058 extends BaseBackgroundTaskExecutor {
059
060 public StagingIndexingBackgroundTaskExecutor() {
061 setSerial(true);
062 }
063
064 @Override
065 public BackgroundTaskResult execute(BackgroundTask backgroundTask)
066 throws Exception {
067
068 Map<String, Serializable> taskContextMap =
069 backgroundTask.getTaskContextMap();
070
071 PortletDataContext portletDataContext =
072 (PortletDataContext)taskContextMap.get("portletDataContext");
073
074 boolean importPermissions = MapUtil.getBoolean(
075 portletDataContext.getParameterMap(),
076 PortletDataHandlerKeys.PERMISSIONS);
077
078 if (importPermissions) {
079 long userId = MapUtil.getLong(taskContextMap, "userId");
080
081 if (userId > 0) {
082 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
083 User.class);
084
085 indexer.reindex(userId);
086 }
087 }
088
089 Map<String, Map<?, ?>> newPrimaryKeysMaps =
090 portletDataContext.getNewPrimaryKeysMaps();
091
092 for (Map.Entry<String, Map<?, ?>> newPrimaryKeysMapsEntry :
093 newPrimaryKeysMaps.entrySet()) {
094
095 String className = newPrimaryKeysMapsEntry.getKey();
096
097 Indexer indexer = IndexerRegistryUtil.getIndexer(className);
098
099 if ((indexer == null) &&
100 !className.equals(DDMStructure.class.getName())) {
101
102 continue;
103 }
104
105 Map<?, ?> newPrimaryKeysMap = newPrimaryKeysMapsEntry.getValue();
106
107 List<Long> newPrimaryKeys = new ArrayList<Long>();
108
109 for (Object object : newPrimaryKeysMap.values()) {
110 long classPK = GetterUtil.getLong(object);
111
112 if (classPK > 0) {
113 newPrimaryKeys.add(classPK);
114 }
115 }
116
117 if (className.equals(DDMStructure.class.getName())) {
118 reindexDDMStructures(
119 newPrimaryKeys, newPrimaryKeysMaps,
120 portletDataContext.getGroupId());
121 }
122 else {
123 for (Long classPK : newPrimaryKeys) {
124 indexer.reindex(className, classPK);
125 }
126 }
127 }
128
129 return BackgroundTaskResult.SUCCESS;
130 }
131
132 protected ActionableDynamicQuery getJournalArticleActionableDynamicQuery(
133 long groupId, final Map<?, ?> journalArticleIds,
134 final String[] ddmStructureKeys)
135 throws Exception {
136
137 ActionableDynamicQuery journalArticleActionableDynamicQuery =
138 new JournalArticleActionableDynamicQuery() {
139
140 @Override
141 protected void addCriteria(DynamicQuery dynamicQuery) {
142 Property structureIdProperty = PropertyFactoryUtil.forName(
143 "structureId");
144
145 dynamicQuery.add(structureIdProperty.in(ddmStructureKeys));
146 }
147
148 @Override
149 protected void performAction(Object object) throws PortalException {
150 JournalArticle article = (JournalArticle)object;
151
152 if (containsValue(
153 journalArticleIds, article.getResourcePrimKey())) {
154
155 return;
156 }
157
158 try {
159 _journalArticleIndexer.doReindex(article, false);
160 }
161 catch (Exception e) {
162 throw new PortalException(e);
163 }
164 }
165
166 private final JournalArticleIndexer _journalArticleIndexer =
167 (JournalArticleIndexer)IndexerRegistryUtil.getIndexer(
168 JournalArticle.class);
169
170 };
171
172 journalArticleActionableDynamicQuery.setGroupId(groupId);
173
174 return journalArticleActionableDynamicQuery;
175 }
176
177 protected void reindexDDMStructures(
178 List<Long> ddmStructureIds,
179 final Map<String, Map<?, ?>> newPrimaryKeysMaps, long groupId)
180 throws Exception {
181
182 if ((ddmStructureIds == null) || ddmStructureIds.isEmpty()) {
183 return;
184 }
185
186 final String[] ddmStructureKeys = new String[ddmStructureIds.size()];
187
188 for (int i = 0; i < ddmStructureIds.size(); i++) {
189 long structureId = ddmStructureIds.get(i);
190
191 DDMStructure ddmStructure =
192 DDMStructureLocalServiceUtil.getDDMStructure(structureId);
193
194 ddmStructureKeys[i] = ddmStructure.getStructureKey();
195 }
196
197
198
199 Map<?, ?> articleIds = (Map<?, ?>)newPrimaryKeysMaps.get(
200 JournalArticle.class.getName());
201
202 ActionableDynamicQuery journalArticleActionableDynamicQuery =
203 getJournalArticleActionableDynamicQuery(
204 groupId, articleIds, ddmStructureKeys);
205
206 journalArticleActionableDynamicQuery.performActions();
207
208
209
210 List<DLFileEntry> dlFileEntries = new ArrayList<DLFileEntry>();
211
212 try {
213 Method method = ReflectionUtil.getDeclaredMethod(
214 DLFileEntryLocalServiceUtil.class, "getDDMStructureFileEntries",
215 long.class, long[].class);
216
217 Object object = method.invoke(
218 DLFileEntryLocalServiceUtil.class, groupId,
219 ArrayUtil.toLongArray(ddmStructureIds));
220
221 if (object != null) {
222 dlFileEntries = (List<DLFileEntry>)object;
223 }
224 }
225 catch (Exception e) {
226 List<DLFileEntry> allDlFileEntries =
227 DLFileEntryLocalServiceUtil.getDDMStructureFileEntries(
228 ArrayUtil.toLongArray(ddmStructureIds));
229
230 for (DLFileEntry dlFileEntry : allDlFileEntries) {
231 if (groupId == dlFileEntry.getGroupId()) {
232 dlFileEntries.add(dlFileEntry);
233 }
234 }
235 }
236
237 Map<?, ?> dlFileEntryPrimaryKeysMap = newPrimaryKeysMaps.get(
238 DLFileEntry.class.getName());
239
240 Indexer dlFileEntryIndexer = IndexerRegistryUtil.getIndexer(
241 DLFileEntry.class);
242
243 for (DLFileEntry dlFileEntry : dlFileEntries) {
244 if (containsValue(
245 dlFileEntryPrimaryKeysMap, dlFileEntry.getFileEntryId())) {
246
247 continue;
248 }
249
250 dlFileEntryIndexer.reindex(dlFileEntry);
251 }
252 }
253
254 protected boolean containsValue(Map<?, ?> map, long value) {
255 if ((map == null) || map.isEmpty() || (value <= 0)) {
256 return false;
257 }
258
259 for (Object object : map.values()) {
260 if (GetterUtil.getLong(object) == value) {
261 return true;
262 }
263 }
264
265 return false;
266 }
267
268 @Override
269 public String handleException(BackgroundTask backgroundTask, Exception e) {
270 StringBundler sb = new StringBundler(4);
271
272 sb.append("Indexing failed after importing with the following error: ");
273 sb.append(e.getMessage());
274 sb.append(StringPool.PERIOD);
275 sb.append(StringPool.SPACE);
276 sb.append("Please reindex site manually.");
277
278 String message = sb.toString();
279
280 if (_log.isInfoEnabled()) {
281 _log.info(message);
282 }
283
284 return message;
285 }
286
287 private static Log _log = LogFactoryUtil.getLog(
288 StagingIndexingBackgroundTaskExecutor.class);
289
290 }