1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.opencastproject.workflow.handler.comments;
23
24 import org.opencastproject.event.comment.EventComment;
25 import org.opencastproject.event.comment.EventCommentException;
26 import org.opencastproject.event.comment.EventCommentService;
27 import org.opencastproject.job.api.JobContext;
28 import org.opencastproject.security.api.SecurityService;
29 import org.opencastproject.security.api.User;
30 import org.opencastproject.security.api.UserDirectoryService;
31 import org.opencastproject.serviceregistry.api.ServiceRegistry;
32 import org.opencastproject.util.NotFoundException;
33 import org.opencastproject.workflow.api.AbstractWorkflowOperationHandler;
34 import org.opencastproject.workflow.api.WorkflowInstance;
35 import org.opencastproject.workflow.api.WorkflowOperationException;
36 import org.opencastproject.workflow.api.WorkflowOperationHandler;
37 import org.opencastproject.workflow.api.WorkflowOperationInstance;
38 import org.opencastproject.workflow.api.WorkflowOperationResult;
39 import org.opencastproject.workflow.api.WorkflowOperationResult.Action;
40
41 import org.apache.commons.lang3.StringUtils;
42 import org.osgi.service.component.annotations.Component;
43 import org.osgi.service.component.annotations.Reference;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 import java.util.Date;
48 import java.util.List;
49 import java.util.Optional;
50
51
52
53
54
55 @Component(
56 immediate = true,
57 service = WorkflowOperationHandler.class,
58 property = {
59 "service.description=Comment Workflow Operation Handler",
60 "workflow.operation=comment"
61 }
62 )
63 public class CommentWorkflowOperationHandler extends AbstractWorkflowOperationHandler {
64 protected static final String ACTION = "action";
65 protected static final String DESCRIPTION = "description";
66 protected static final String REASON = "reason";
67
68
69 private static final Logger logger = LoggerFactory.getLogger(CommentWorkflowOperationHandler.class);
70
71
72 private EventCommentService eventCommentService;
73 private SecurityService securityService;
74 private UserDirectoryService userDirectoryService;
75
76 public enum Operation {
77 create, resolve, delete
78 };
79
80
81
82
83 @Override
84 public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context)
85 throws WorkflowOperationException {
86 logger.debug("Running comment workflow operation on workflow {}", workflowInstance.getId());
87 try {
88 return handleCommentOperation(workflowInstance);
89 } catch (Exception e) {
90 throw new WorkflowOperationException(e);
91 }
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105 private WorkflowOperationResult handleCommentOperation(WorkflowInstance workflowInstance)
106 throws EventCommentException, NotFoundException {
107 Date date = new Date();
108 WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();
109 Operation action;
110 String inputAction = StringUtils.trimToNull(operation.getConfiguration(ACTION));
111 if (inputAction == null) {
112 action = Operation.create;
113 } else {
114 action = Operation.valueOf(inputAction.toLowerCase());
115 }
116 String reason = StringUtils.trimToNull(operation.getConfiguration(REASON));
117 String description = StringUtils.trimToNull(operation.getConfiguration(DESCRIPTION));
118 switch (action) {
119 case create:
120 createComment(workflowInstance, reason, description);
121 break;
122 case resolve:
123 resolveComment(workflowInstance, reason, description);
124 break;
125 case delete:
126 deleteComment(workflowInstance, reason, description);
127 break;
128 default:
129 logger.warn(
130 "Unknown action '{}' for comment with description '{}' and reason '{}'. It should be "
131 + "one of the following: {}",
132 inputAction, description, reason, StringUtils.join(Operation.values(), ","));
133 }
134 WorkflowOperationResult result = createResult(workflowInstance.getMediaPackage(), Action.CONTINUE,
135 (new Date().getTime()) - date.getTime());
136 return result;
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150
151 private void createComment(WorkflowInstance workflowInstance, String reason, String description)
152 throws EventCommentException {
153 String mpId = workflowInstance.getMediaPackage().getIdentifier().toString();
154 Optional<EventComment> optComment = findComment(mpId, reason, description);
155 if (optComment.isEmpty()) {
156 final User user = userDirectoryService.loadUser(workflowInstance.getCreatorName());
157 EventComment comment = EventComment.create(
158 Optional.empty(), mpId,
159 securityService.getOrganization().getId(), description, user, reason, false);
160 eventCommentService.updateComment(comment);
161 } else {
162 logger.debug("Not creating comment with '{}' text and '{}' reason as it already exists for this event.",
163 description, reason);
164 }
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178
179 private void resolveComment(WorkflowInstance workflowInstance, String reason, String description)
180 throws EventCommentException {
181 String mpId = workflowInstance.getMediaPackage().getIdentifier().toString();
182 Optional<EventComment> optComment = findComment(mpId, reason, description);
183 if (optComment.isPresent()) {
184 EventComment comment = EventComment.create(
185 optComment.get().getId(),
186 mpId,
187 securityService.getOrganization().getId(), optComment.get().getText(),
188 optComment.get().getAuthor(), optComment.get().getReason(), true, optComment.get().getCreationDate(),
189 optComment.get().getModificationDate(), optComment.get().getReplies());
190 eventCommentService.updateComment(comment);
191 } else {
192 logger.debug("Not resolving comment with '{}' text and/or '{}' reason as it doesn't exist.", description, reason);
193 }
194 }
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 private void deleteComment(WorkflowInstance workflowInstance, String reason, String description)
211 throws EventCommentException, NotFoundException {
212 String mpId = workflowInstance.getMediaPackage().getIdentifier().toString();
213 Optional<EventComment> optComment = findComment(mpId, reason, description);
214 if (optComment.isPresent()) {
215 try {
216 eventCommentService.deleteComment(optComment.get().getId().get());
217 } catch (NotFoundException e) {
218 logger.debug("Not deleting comment with '{}' text and '{}' reason and id '{}' as it doesn't exist.",
219 description, reason, optComment.get().getId());
220 }
221 } else {
222 logger.debug("Not deleting comment with '{}' text and/or '{}' reason as it doesn't exist.", description, reason);
223 }
224 }
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239 private Optional<EventComment> findComment(String eventId, String reason, String description)
240 throws EventCommentException {
241 Optional<EventComment> comment = Optional.empty();
242 List<EventComment> eventComments = eventCommentService.getComments(eventId);
243
244 for (EventComment existingComment : eventComments) {
245
246 if (reason != null && description != null
247 && reason.equals(existingComment.getReason()) && description.equals(existingComment.getText())) {
248 comment = Optional.of(existingComment);
249 break;
250 }
251
252 if (reason != null && description == null && reason.equals(existingComment.getReason())) {
253 comment = Optional.of(existingComment);
254 break;
255 }
256
257 if (reason == null && description != null && description.equals(existingComment.getText())) {
258 comment = Optional.of(existingComment);
259 break;
260 }
261 }
262 return comment;
263 }
264
265
266
267
268
269
270
271 @Reference
272 public void setEventCommentService(EventCommentService eventCommentService) {
273 this.eventCommentService = eventCommentService;
274 }
275
276
277 @Reference
278 void setSecurityService(SecurityService service) {
279 this.securityService = service;
280 }
281
282 @Reference
283 public void setUserDirectoryService(UserDirectoryService userDirectoryService) {
284 this.userDirectoryService = userDirectoryService;
285 }
286
287 @Reference
288 @Override
289 public void setServiceRegistry(ServiceRegistry serviceRegistry) {
290 super.setServiceRegistry(serviceRegistry);
291 }
292
293 }