001
014
015 package com.liferay.portal.lar.backgroundtask;
016
017 import com.liferay.portal.kernel.backgroundtask.BackgroundTaskConstants;
018 import com.liferay.portal.kernel.backgroundtask.BackgroundTaskResult;
019 import com.liferay.portal.kernel.backgroundtask.BackgroundTaskStatus;
020 import com.liferay.portal.kernel.backgroundtask.BackgroundTaskStatusRegistryUtil;
021 import com.liferay.portal.kernel.backgroundtask.BaseBackgroundTaskExecutor;
022 import com.liferay.portal.kernel.exception.PortalException;
023 import com.liferay.portal.kernel.json.JSONArray;
024 import com.liferay.portal.kernel.json.JSONObject;
025 import com.liferay.portal.kernel.lar.MissingReference;
026 import com.liferay.portal.kernel.lar.MissingReferences;
027 import com.liferay.portal.kernel.staging.StagingUtil;
028 import com.liferay.portal.kernel.transaction.Propagation;
029 import com.liferay.portal.kernel.util.Validator;
030 import com.liferay.portal.model.BackgroundTask;
031 import com.liferay.portal.model.LayoutSet;
032 import com.liferay.portal.service.BackgroundTaskLocalServiceUtil;
033 import com.liferay.portal.service.LayoutSetLocalServiceUtil;
034 import com.liferay.portal.service.ServiceContext;
035 import com.liferay.portal.service.ServiceContextThreadLocal;
036 import com.liferay.portal.service.UserLocalServiceUtil;
037 import com.liferay.portal.spring.transaction.TransactionAttributeBuilder;
038
039 import java.io.Serializable;
040
041 import java.util.HashMap;
042 import java.util.Map;
043
044 import org.springframework.transaction.interceptor.TransactionAttribute;
045
046
049 public abstract class BaseStagingBackgroundTaskExecutor
050 extends BaseBackgroundTaskExecutor {
051
052 public BaseStagingBackgroundTaskExecutor() {
053 setBackgroundTaskStatusMessageTranslator(
054 new DefaultExportImportBackgroundTaskStatusMessageTranslator());
055
056 setSerial(true);
057 }
058
059 @Override
060 public String handleException(BackgroundTask backgroundTask, Exception e) {
061 JSONObject jsonObject = StagingUtil.getExceptionMessagesJSONObject(
062 getLocale(backgroundTask), e, backgroundTask.getTaskContextMap());
063
064 return jsonObject.toString();
065 }
066
067 protected void clearBackgroundTaskStatus(BackgroundTask backgroundTask) {
068 BackgroundTaskStatus backgroundTaskStatus =
069 BackgroundTaskStatusRegistryUtil.getBackgroundTaskStatus(
070 backgroundTask.getBackgroundTaskId());
071
072 backgroundTaskStatus.clearAttributes();
073 }
074
075 protected void initThreadLocals(long groupId, boolean privateLayout)
076 throws PortalException {
077
078 ServiceContext serviceContext =
079 ServiceContextThreadLocal.popServiceContext();
080
081 if (serviceContext == null) {
082 serviceContext = new ServiceContext();
083 }
084
085 LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
086 groupId, privateLayout);
087
088 serviceContext.setCompanyId(layoutSet.getCompanyId());
089 serviceContext.setSignedIn(false);
090
091 long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
092 layoutSet.getCompanyId());
093
094 serviceContext.setUserId(defaultUserId);
095
096 ServiceContextThreadLocal.pushServiceContext(serviceContext);
097 }
098
099 protected void markBackgroundTask(
100 long backgroundTaskId, String backgroundTaskState) {
101
102 BackgroundTask backgroundTask =
103 BackgroundTaskLocalServiceUtil.fetchBackgroundTask(
104 backgroundTaskId);
105
106 if ((backgroundTask == null) || Validator.isNull(backgroundTaskState)) {
107 return;
108 }
109
110 Map<String, Serializable> taskContextMap =
111 backgroundTask.getTaskContextMap();
112
113 if (taskContextMap == null) {
114 taskContextMap = new HashMap<String, Serializable>();
115 }
116
117 taskContextMap.put(backgroundTaskState, Boolean.TRUE);
118
119 backgroundTask.setTaskContextMap(taskContextMap);
120
121 BackgroundTaskLocalServiceUtil.updateBackgroundTask(backgroundTask);
122 }
123
124 protected BackgroundTaskResult processMissingReferences(
125 long backgroundTaskId, MissingReferences missingReferences) {
126
127 BackgroundTaskResult backgroundTaskResult = new BackgroundTaskResult(
128 BackgroundTaskConstants.STATUS_SUCCESSFUL);
129
130 Map<String, MissingReference> weakMissingReferences =
131 missingReferences.getWeakMissingReferences();
132
133 if ((weakMissingReferences != null) &&
134 !weakMissingReferences.isEmpty()) {
135
136 BackgroundTask backgroundTask =
137 BackgroundTaskLocalServiceUtil.fetchBackgroundTask(
138 backgroundTaskId);
139
140 JSONArray jsonArray = StagingUtil.getWarningMessagesJSONArray(
141 getLocale(backgroundTask), weakMissingReferences,
142 backgroundTask.getTaskContextMap());
143
144 backgroundTaskResult.setStatusMessage(jsonArray.toString());
145 }
146
147 return backgroundTaskResult;
148 }
149
150 protected TransactionAttribute transactionAttribute =
151 TransactionAttributeBuilder.build(
152 Propagation.REQUIRED, new Class<?>[] {Exception.class});
153
154 }