001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.NoSuchWorkflowInstanceLinkException;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.exception.SystemException;
020 import com.liferay.portal.kernel.util.LocaleUtil;
021 import com.liferay.portal.kernel.workflow.WorkflowConstants;
022 import com.liferay.portal.kernel.workflow.WorkflowHandler;
023 import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
024 import com.liferay.portal.kernel.workflow.WorkflowInstance;
025 import com.liferay.portal.kernel.workflow.WorkflowInstanceManagerUtil;
026 import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
027 import com.liferay.portal.model.User;
028 import com.liferay.portal.model.WorkflowDefinitionLink;
029 import com.liferay.portal.model.WorkflowInstanceLink;
030 import com.liferay.portal.service.base.WorkflowInstanceLinkLocalServiceBaseImpl;
031 import com.liferay.portal.util.PortalUtil;
032
033 import java.io.Serializable;
034
035 import java.util.Date;
036 import java.util.HashMap;
037 import java.util.List;
038 import java.util.Map;
039
040
045 public class WorkflowInstanceLinkLocalServiceImpl
046 extends WorkflowInstanceLinkLocalServiceBaseImpl {
047
048 public WorkflowInstanceLink addWorkflowInstanceLink(
049 long userId, long companyId, long groupId, String className,
050 long classPK, long workflowInstanceId)
051 throws PortalException, SystemException {
052
053 User user = userPersistence.findByPrimaryKey(userId);
054 long classNameId = PortalUtil.getClassNameId(className);
055 Date now = new Date();
056
057 long workflowInstanceLinkId = counterLocalService.increment();
058
059 WorkflowInstanceLink workflowInstanceLink =
060 workflowInstanceLinkPersistence.create(workflowInstanceLinkId);
061
062 workflowInstanceLink.setCreateDate(now);
063 workflowInstanceLink.setModifiedDate(now);
064 workflowInstanceLink.setUserId(userId);
065 workflowInstanceLink.setUserName(user.getFullName());
066 workflowInstanceLink.setGroupId(groupId);
067 workflowInstanceLink.setCompanyId(companyId);
068 workflowInstanceLink.setClassNameId(classNameId);
069 workflowInstanceLink.setClassPK(classPK);
070 workflowInstanceLink.setWorkflowInstanceId(workflowInstanceId);
071
072 workflowInstanceLinkPersistence.update(workflowInstanceLink, false);
073
074 return workflowInstanceLink;
075 }
076
077 public void deleteWorkflowInstanceLink(
078 long companyId, long groupId, String className, long classPK)
079 throws PortalException, SystemException {
080
081 WorkflowInstanceLink workflowInstanceLink = fetchWorkflowInstanceLink(
082 companyId, groupId, className, classPK);
083
084 if (workflowInstanceLink == null) {
085 return;
086 }
087
088 deleteWorkflowInstanceLink(workflowInstanceLink);
089 }
090
091 @Override
092 public WorkflowInstanceLink deleteWorkflowInstanceLink(
093 WorkflowInstanceLink workflowInstanceLink)
094 throws PortalException, SystemException {
095
096 super.deleteWorkflowInstanceLink(workflowInstanceLink);
097
098 subscriptionLocalService.deleteSubscriptions(
099 workflowInstanceLink.getCompanyId(),
100 WorkflowInstance.class.getName(),
101 workflowInstanceLink.getWorkflowInstanceId());
102
103 WorkflowInstanceManagerUtil.deleteWorkflowInstance(
104 workflowInstanceLink.getCompanyId(),
105 workflowInstanceLink.getWorkflowInstanceId());
106
107 return workflowInstanceLink;
108 }
109
110 public void deleteWorkflowInstanceLinks(
111 long companyId, long groupId, String className, long classPK)
112 throws PortalException, SystemException {
113
114 List<WorkflowInstanceLink> workflowInstanceLinks =
115 getWorkflowInstanceLinks(companyId, groupId, className, classPK);
116
117 for (WorkflowInstanceLink workflowInstanceLink :
118 workflowInstanceLinks) {
119
120 deleteWorkflowInstanceLink(workflowInstanceLink);
121 }
122 }
123
124 public WorkflowInstanceLink fetchWorkflowInstanceLink(
125 long companyId, long groupId, String className, long classPK)
126 throws SystemException {
127
128 List<WorkflowInstanceLink> workflowInstanceLinks =
129 getWorkflowInstanceLinks(companyId, groupId, className, classPK);
130
131 if (!workflowInstanceLinks.isEmpty()) {
132 return workflowInstanceLinks.get(0);
133 }
134 else {
135 return null;
136 }
137 }
138
139 public String getState(
140 long companyId, long groupId, String className, long classPK)
141 throws PortalException, SystemException {
142
143 WorkflowInstanceLink workflowInstanceLink = getWorkflowInstanceLink(
144 companyId, groupId, className, classPK);
145
146 WorkflowInstance workflowInstance =
147 WorkflowInstanceManagerUtil.getWorkflowInstance(
148 companyId, workflowInstanceLink.getWorkflowInstanceId());
149
150 return workflowInstance.getState();
151 }
152
153 public WorkflowInstanceLink getWorkflowInstanceLink(
154 long companyId, long groupId, String className, long classPK)
155 throws PortalException, SystemException {
156
157 List<WorkflowInstanceLink> workflowInstanceLinks =
158 getWorkflowInstanceLinks(companyId, groupId, className, classPK);
159
160 if (workflowInstanceLinks.isEmpty()) {
161 throw new NoSuchWorkflowInstanceLinkException();
162 }
163 else {
164 return workflowInstanceLinks.get(0);
165 }
166 }
167
168 public List<WorkflowInstanceLink> getWorkflowInstanceLinks(
169 long companyId, long groupId, String className, long classPK)
170 throws SystemException {
171
172 long classNameId = PortalUtil.getClassNameId(className);
173
174 return workflowInstanceLinkPersistence.findByG_C_C_C(
175 groupId, companyId, classNameId, classPK);
176 }
177
178 public boolean hasWorkflowInstanceLink(
179 long companyId, long groupId, String className, long classPK)
180 throws SystemException {
181
182 WorkflowInstanceLink workflowInstanceLink = fetchWorkflowInstanceLink(
183 companyId, groupId, className, classPK);
184
185 if (workflowInstanceLink != null) {
186 return true;
187 }
188
189 return false;
190 }
191
192 public boolean isEnded(
193 long companyId, long groupId, String className, long classPK)
194 throws PortalException, SystemException {
195
196 WorkflowInstanceLink workflowInstanceLink = fetchWorkflowInstanceLink(
197 companyId, groupId, className, classPK);
198
199 if (workflowInstanceLink == null) {
200 return false;
201 }
202
203 WorkflowInstance workflowInstance =
204 WorkflowInstanceManagerUtil.getWorkflowInstance(
205 companyId, workflowInstanceLink.getWorkflowInstanceId());
206
207 if (workflowInstance.getEndDate() != null) {
208 return true;
209 }
210
211 return false;
212 }
213
214 public void startWorkflowInstance(
215 long companyId, long groupId, long userId, String className,
216 long classPK, Map<String, Serializable> workflowContext)
217 throws PortalException, SystemException {
218
219 if (!WorkflowThreadLocal.isEnabled()) {
220 return;
221 }
222
223 if (userId == 0) {
224 userId = userLocalService.getDefaultUserId(companyId);
225 }
226
227 WorkflowHandler workflowHandler =
228 WorkflowHandlerRegistryUtil.getWorkflowHandler(className);
229
230 WorkflowDefinitionLink workflowDefinitionLink =
231 workflowHandler.getWorkflowDefinitionLink(
232 companyId, groupId, classPK);
233
234 String workflowDefinitionName =
235 workflowDefinitionLink.getWorkflowDefinitionName();
236 int workflowDefinitionVersion =
237 workflowDefinitionLink.getWorkflowDefinitionVersion();
238
239 if (workflowContext != null) {
240 workflowContext = new HashMap<String, Serializable>(
241 workflowContext);
242 }
243 else {
244 workflowContext = new HashMap<String, Serializable>();
245 }
246
247 workflowContext.put(
248 WorkflowConstants.CONTEXT_COMPANY_ID, String.valueOf(companyId));
249 workflowContext.put(
250 WorkflowConstants.CONTEXT_GROUP_ID, String.valueOf(groupId));
251 workflowContext.put(
252 WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME, className);
253 workflowContext.put(
254 WorkflowConstants.CONTEXT_ENTRY_CLASS_PK, String.valueOf(classPK));
255 workflowContext.put(
256 WorkflowConstants.CONTEXT_ENTRY_TYPE,
257 workflowHandler.getType(LocaleUtil.getDefault()));
258
259 WorkflowInstance workflowInstance =
260 WorkflowInstanceManagerUtil.startWorkflowInstance(
261 companyId, groupId, userId, workflowDefinitionName,
262 workflowDefinitionVersion, null, workflowContext);
263
264 addWorkflowInstanceLink(
265 userId, companyId, groupId, className, classPK,
266 workflowInstance.getWorkflowInstanceId());
267 }
268
269 public void updateClassPK(
270 long companyId, long groupId, String className, long oldClassPK,
271 long newClassPK)
272 throws PortalException, SystemException {
273
274 if (!WorkflowThreadLocal.isEnabled()) {
275 return;
276 }
277
278 List<WorkflowInstanceLink> workflowInstanceLinks =
279 getWorkflowInstanceLinks(companyId, groupId, className, oldClassPK);
280
281 for (WorkflowInstanceLink workflowInstanceLink :
282 workflowInstanceLinks) {
283
284 WorkflowInstance workflowInstance =
285 WorkflowInstanceManagerUtil.getWorkflowInstance(
286 workflowInstanceLink.getCompanyId(),
287 workflowInstanceLink.getWorkflowInstanceId());
288
289 workflowInstanceLink.setClassPK(newClassPK);
290
291 workflowInstanceLinkPersistence.update(workflowInstanceLink, false);
292
293 Map<String, Serializable> workflowContext =
294 new HashMap<String, Serializable>(
295 workflowInstance.getWorkflowContext());
296
297 workflowContext.put(
298 WorkflowConstants.CONTEXT_ENTRY_CLASS_PK,
299 String.valueOf(newClassPK));
300
301 WorkflowInstanceManagerUtil.updateWorkflowContext(
302 workflowInstanceLink.getCompanyId(),
303 workflowInstanceLink.getWorkflowInstanceId(), workflowContext);
304 }
305 }
306
307 }