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