1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.core.comment.service.impl; |
17 | |
|
18 | |
import java.util.List; |
19 | |
|
20 | |
import javax.jws.WebService; |
21 | |
import javax.persistence.NoResultException; |
22 | |
|
23 | |
import org.apache.log4j.Logger; |
24 | |
import org.kuali.student.common.validator.Validator; |
25 | |
import org.kuali.student.common.validator.ValidatorFactory; |
26 | |
import org.kuali.student.core.comment.dao.CommentDao; |
27 | |
import org.kuali.student.core.comment.dto.CommentInfo; |
28 | |
import org.kuali.student.core.comment.dto.CommentTypeInfo; |
29 | |
import org.kuali.student.core.comment.dto.TagInfo; |
30 | |
import org.kuali.student.core.comment.dto.TagTypeInfo; |
31 | |
import org.kuali.student.core.comment.entity.Comment; |
32 | |
import org.kuali.student.core.comment.entity.CommentType; |
33 | |
import org.kuali.student.core.comment.entity.Reference; |
34 | |
import org.kuali.student.core.comment.entity.ReferenceType; |
35 | |
import org.kuali.student.core.comment.entity.Tag; |
36 | |
import org.kuali.student.core.comment.entity.TagType; |
37 | |
import org.kuali.student.core.comment.service.CommentService; |
38 | |
import org.kuali.student.core.dictionary.dto.ObjectStructureDefinition; |
39 | |
import org.kuali.student.core.dictionary.service.DictionaryService; |
40 | |
import org.kuali.student.core.dto.ReferenceTypeInfo; |
41 | |
import org.kuali.student.core.dto.StatusInfo; |
42 | |
import org.kuali.student.core.exceptions.AlreadyExistsException; |
43 | |
import org.kuali.student.core.exceptions.DataValidationErrorException; |
44 | |
import org.kuali.student.core.exceptions.DoesNotExistException; |
45 | |
import org.kuali.student.core.exceptions.InvalidParameterException; |
46 | |
import org.kuali.student.core.exceptions.MissingParameterException; |
47 | |
import org.kuali.student.core.exceptions.OperationFailedException; |
48 | |
import org.kuali.student.core.exceptions.PermissionDeniedException; |
49 | |
import org.kuali.student.core.exceptions.VersionMismatchException; |
50 | |
import org.kuali.student.core.search.service.SearchManager; |
51 | |
import org.kuali.student.core.validation.dto.ValidationResultInfo; |
52 | |
import org.springframework.transaction.annotation.Transactional; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
@WebService(endpointInterface = "org.kuali.student.core.comment.service.CommentService", serviceName = "CommentService", portName = "CommentService", targetNamespace = "http://student.kuali.org/wsdl/commentService") |
61 | |
@Transactional(readOnly=true,noRollbackFor={DoesNotExistException.class},rollbackFor={Throwable.class}) |
62 | 2 | public class CommentServiceImpl implements CommentService { |
63 | |
|
64 | 2 | final Logger logger = Logger.getLogger(CommentServiceImpl.class); |
65 | |
|
66 | |
private CommentDao commentDao; |
67 | |
private DictionaryService dictionaryServiceDelegate; |
68 | |
private SearchManager searchManager; |
69 | |
private ValidatorFactory validatorFactory; |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
@Override |
77 | |
@Transactional(readOnly=false) |
78 | |
public CommentInfo addComment(String referenceId, String referenceTypeKey, CommentInfo commentInfo) throws DataValidationErrorException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
79 | 4 | commentInfo.setReferenceTypeKey(referenceTypeKey); |
80 | 4 | commentInfo.setReferenceId(referenceId); |
81 | |
|
82 | |
|
83 | 4 | List<ValidationResultInfo> validationResults = null; |
84 | |
try { |
85 | 4 | validationResults = validateComment("OBJECT", commentInfo); |
86 | 0 | } catch (DoesNotExistException e1) { |
87 | 0 | throw new OperationFailedException("Validation call failed." + e1.getMessage()); |
88 | 4 | } |
89 | 4 | if (null != validationResults && validationResults.size() > 0) { |
90 | 0 | throw new DataValidationErrorException("Validation error!", validationResults); |
91 | |
} |
92 | |
|
93 | 4 | Reference reference=null; |
94 | 4 | reference = commentDao.getReference(referenceId, referenceTypeKey); |
95 | 4 | if(reference==null){ |
96 | 3 | reference = new Reference(); |
97 | 3 | reference.setReferenceId(referenceId); |
98 | |
try { |
99 | 3 | ReferenceType referenceType = commentDao.fetch(ReferenceType.class, referenceTypeKey); |
100 | 3 | reference.setReferenceType(referenceType); |
101 | 3 | commentDao.create(reference); |
102 | 0 | } catch (DoesNotExistException e) { |
103 | 0 | throw new InvalidParameterException(e.getMessage()); |
104 | 3 | } |
105 | |
} |
106 | |
|
107 | 4 | Comment comment = null; |
108 | |
|
109 | |
try { |
110 | 4 | comment = CommentServiceAssembler.toComment(false, commentInfo, commentDao); |
111 | 0 | } catch (DoesNotExistException e) { |
112 | 0 | throw new InvalidParameterException(e.getMessage()); |
113 | 4 | } |
114 | |
|
115 | 4 | Comment createdComment = commentDao.create(comment); |
116 | |
|
117 | 4 | CommentInfo createdCommentInfo = CommentServiceAssembler.toCommentInfo(createdComment); |
118 | |
|
119 | 4 | return createdCommentInfo; |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
@Override |
127 | |
@Transactional(readOnly=false) |
128 | |
public TagInfo addTag(String referenceId, String referenceTypeKey, TagInfo tagInfo) throws DataValidationErrorException, AlreadyExistsException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
129 | |
|
130 | |
|
131 | 4 | List<ValidationResultInfo> validationResults = null; |
132 | |
try { |
133 | 4 | validationResults = validateTag("OBJECT", tagInfo); |
134 | 0 | } catch (DoesNotExistException e1) { |
135 | 0 | throw new OperationFailedException("Validation call failed." + e1.getMessage()); |
136 | 4 | } |
137 | 4 | if (null != validationResults && validationResults.size() > 0) { |
138 | 0 | throw new DataValidationErrorException("Validation error!", validationResults); |
139 | |
} |
140 | |
|
141 | 4 | tagInfo.setReferenceTypeKey(referenceTypeKey); |
142 | 4 | tagInfo.setReferenceId(referenceId); |
143 | 4 | Reference reference=null; |
144 | 4 | reference = commentDao.getReference(referenceId, referenceTypeKey); |
145 | 4 | if(reference==null){ |
146 | 1 | reference = new Reference(); |
147 | 1 | reference.setReferenceId(referenceId); |
148 | |
try { |
149 | 1 | ReferenceType referenceType = commentDao.fetch(ReferenceType.class, referenceTypeKey); |
150 | 1 | reference.setReferenceType(referenceType); |
151 | 1 | commentDao.create(reference); |
152 | 0 | } catch (DoesNotExistException e) { |
153 | 0 | throw new InvalidParameterException(e.getMessage()); |
154 | 1 | } |
155 | |
} |
156 | |
|
157 | 4 | Tag tag = null; |
158 | |
|
159 | |
try { |
160 | 4 | tag = CommentServiceAssembler.toTag(false, tagInfo, commentDao); |
161 | 0 | } catch (DoesNotExistException e) { |
162 | 0 | logger.error("Exception occured: ", e); |
163 | 4 | } |
164 | |
|
165 | 4 | Tag createdTag = commentDao.create(tag); |
166 | |
|
167 | 4 | TagInfo createdTagInfo = CommentServiceAssembler.toTagInfo(createdTag); |
168 | |
|
169 | 4 | return createdTagInfo; |
170 | |
} |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
@Override |
178 | |
public CommentInfo getComment(String commentId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
179 | 8 | checkForMissingParameter(commentId, "commentId"); |
180 | 7 | Comment comment = commentDao.fetch(Comment.class, commentId); |
181 | 3 | return CommentServiceAssembler.toCommentInfo(comment); |
182 | |
} |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
@Override |
190 | |
public List<CommentTypeInfo> getCommentTypes() throws OperationFailedException { |
191 | 0 | List<CommentType> commentTypes = commentDao.find(CommentType.class); |
192 | 0 | return CommentServiceAssembler.toCommentTypeInfos(commentTypes); |
193 | |
} |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
@Override |
201 | |
public List<CommentTypeInfo> getCommentTypesForReferenceType(String referenceTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException { |
202 | 1 | List<CommentType> commentTypes = commentDao.getCommentTypesByReferenceTypeId(referenceTypeKey); |
203 | 1 | return CommentServiceAssembler.toCommentTypeInfos(commentTypes); |
204 | |
} |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
@Override |
212 | |
public List<CommentInfo> getComments(String referenceId, String referenceTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
213 | 2 | List<Comment> comments = commentDao.getComments(referenceId, referenceTypeKey); |
214 | 2 | return CommentServiceAssembler.toCommentInfos(comments); |
215 | |
} |
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
@Override |
223 | |
public List<CommentInfo> getCommentsByType(String referenceId, String referenceTypeKey, String commentTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
224 | 0 | List<Comment> comments = commentDao.getCommentsByType(referenceId, referenceTypeKey, commentTypeKey); |
225 | 0 | return CommentServiceAssembler.toCommentInfos(comments); |
226 | |
} |
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
@Override |
234 | |
public List<ReferenceTypeInfo> getReferenceTypes() throws OperationFailedException { |
235 | 1 | List<ReferenceType> referenceTypes = commentDao.find(ReferenceType.class); |
236 | 1 | return CommentServiceAssembler.toReferenceTypeInfos(referenceTypes); |
237 | |
} |
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
@Override |
245 | |
public TagInfo getTag(String tagId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
246 | 4 | checkForMissingParameter(tagId, "tagId"); |
247 | 3 | Tag tag = commentDao.fetch(Tag.class, tagId); |
248 | 2 | return CommentServiceAssembler.toTagInfo(tag); |
249 | |
} |
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
@Override |
257 | |
public List<TagTypeInfo> getTagTypes() throws OperationFailedException { |
258 | 1 | List<TagType> tagTypes = commentDao.find(TagType.class); |
259 | |
|
260 | 1 | return CommentServiceAssembler.toTagTypeInfos(tagTypes); |
261 | |
} |
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
@Override |
269 | |
public List<TagInfo> getTags(String referenceId, String referenceTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
270 | |
|
271 | 2 | List<Tag> tags = commentDao.getTags(referenceId, referenceTypeKey); |
272 | |
|
273 | 2 | return CommentServiceAssembler.toTagInfos(tags); |
274 | |
} |
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
@Override |
282 | |
public List<TagInfo> getTagsByType(String referenceId, String referenceTypeKey, String tagTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
283 | |
|
284 | 1 | List<Tag> tags = commentDao.getTagsByType(referenceId, referenceTypeKey, tagTypeKey); |
285 | 1 | return CommentServiceAssembler.toTagInfos(tags); |
286 | |
} |
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
@Override |
294 | |
@Transactional(readOnly=false) |
295 | |
public StatusInfo removeComment(String commentId, String referenceId, String referenceTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
296 | |
try{ |
297 | 2 | checkForMissingParameter(commentId, "commentId"); |
298 | 2 | commentDao.delete(Comment.class, commentId); |
299 | 1 | return new StatusInfo(); |
300 | |
} |
301 | 0 | catch(MissingParameterException mpe){ |
302 | 0 | Comment comment = null; |
303 | |
try{ |
304 | 0 | comment = commentDao.getComment(referenceId, referenceTypeKey); |
305 | 0 | if(comment==null){ |
306 | 0 | throw new DoesNotExistException(); |
307 | |
} |
308 | |
} |
309 | 0 | catch(NoResultException nre){ |
310 | 0 | throw new DoesNotExistException(); |
311 | 0 | } |
312 | 0 | commentDao.delete(comment); |
313 | 0 | return new StatusInfo(); |
314 | |
} |
315 | |
|
316 | |
} |
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
@Override |
324 | |
@Transactional(readOnly=false) |
325 | |
public StatusInfo removeComments(String referenceId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
326 | 1 | List<Comment> comments = commentDao.getCommentsByRefId(referenceId); |
327 | 1 | for(Comment comment:comments){ |
328 | 2 | commentDao.delete(comment); |
329 | |
} |
330 | 1 | return new StatusInfo(); |
331 | |
} |
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
@Override |
340 | |
@Transactional(readOnly=false) |
341 | |
public StatusInfo removeTag(String tagId, String referenceId, String referenceTypeKey) throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, DoesNotExistException { |
342 | |
try{ |
343 | 3 | checkForMissingParameter(tagId, "tagId"); |
344 | 0 | commentDao.delete(Tag.class, tagId); |
345 | 0 | return new StatusInfo(); |
346 | |
} |
347 | 3 | catch(MissingParameterException mpe){ |
348 | 3 | Tag tag = null; |
349 | |
try{ |
350 | 3 | tag = commentDao.getTag(referenceId, referenceTypeKey); |
351 | 1 | if(tag==null){ |
352 | 0 | throw new DoesNotExistException(); |
353 | |
} |
354 | |
} |
355 | 2 | catch(NoResultException nre){ |
356 | 2 | throw new DoesNotExistException(); |
357 | 1 | } |
358 | 1 | commentDao.delete(tag); |
359 | 1 | return new StatusInfo(); |
360 | |
} |
361 | |
} |
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
|
368 | |
@Override |
369 | |
@Transactional(readOnly=false) |
370 | |
public StatusInfo removeTags(String tagId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException { |
371 | |
|
372 | 1 | List<Tag> tags = commentDao.getTagsByRefId(tagId); |
373 | 1 | for(Tag tag:tags){ |
374 | 3 | commentDao.delete(tag); |
375 | |
} |
376 | 1 | return new StatusInfo(); |
377 | |
} |
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
@Override |
386 | |
@Transactional(readOnly=false) |
387 | |
public CommentInfo updateComment(String referenceId, String referenceTypeKey, CommentInfo commentInfo) throws DataValidationErrorException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, DoesNotExistException, VersionMismatchException { |
388 | |
|
389 | |
|
390 | 1 | List<ValidationResultInfo> validationResults = validateComment("OBJECT", commentInfo); |
391 | 1 | if (null != validationResults && validationResults.size() > 0) { |
392 | 0 | throw new DataValidationErrorException("Validation error!", validationResults); |
393 | |
} |
394 | |
|
395 | 1 | Comment entity = commentDao.fetch(Comment.class, commentInfo.getId()); |
396 | 1 | if (!String.valueOf(entity.getVersionNumber()).equals(commentInfo.getMetaInfo().getVersionInd())){ |
397 | 0 | throw new VersionMismatchException("ResultComponent to be updated is not the current version"); |
398 | |
} |
399 | |
|
400 | 1 | CommentServiceAssembler.toComment(entity, referenceId, referenceTypeKey, commentInfo, commentDao); |
401 | 1 | commentDao.update(entity); |
402 | |
|
403 | 1 | return CommentServiceAssembler.toCommentInfo(entity); |
404 | |
} |
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
@Override |
412 | |
public List<ValidationResultInfo> validateComment(String validationType, CommentInfo commentInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException { |
413 | 5 | checkForMissingParameter(validationType, "validationType"); |
414 | 5 | checkForMissingParameter(commentInfo, "commentInfo"); |
415 | |
|
416 | 5 | ObjectStructureDefinition objStructure = this.getObjectStructure(CommentInfo.class.getName()); |
417 | 5 | Validator defaultValidator = validatorFactory.getValidator(); |
418 | 5 | List<ValidationResultInfo> validationResults = defaultValidator.validateObject(commentInfo, objStructure); |
419 | 5 | return validationResults; |
420 | |
} |
421 | |
|
422 | |
private List<ValidationResultInfo> validateTag(String validationType, TagInfo tagInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException { |
423 | 4 | checkForMissingParameter(validationType, "validationType"); |
424 | 4 | checkForMissingParameter(tagInfo, "tagInfo"); |
425 | |
|
426 | 4 | ObjectStructureDefinition objStructure = this.getObjectStructure(TagInfo.class.getName()); |
427 | 4 | Validator defaultValidator = validatorFactory.getValidator(); |
428 | 4 | List<ValidationResultInfo> validationResults = defaultValidator.validateObject(tagInfo, objStructure); |
429 | 4 | return validationResults; |
430 | |
} |
431 | |
|
432 | |
|
433 | |
|
434 | |
|
435 | |
public CommentDao getCommentDao() { |
436 | 0 | return commentDao; |
437 | |
} |
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
public void setCommentDao(CommentDao commentDao) { |
443 | 1 | this.commentDao = commentDao; |
444 | 1 | } |
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
|
452 | |
|
453 | |
private void checkForMissingParameter(Object param, String paramName) |
454 | |
throws MissingParameterException { |
455 | 35 | if (param == null) { |
456 | 5 | throw new MissingParameterException(paramName + " can not be null"); |
457 | |
} |
458 | 30 | } |
459 | |
|
460 | |
|
461 | |
|
462 | |
|
463 | |
public DictionaryService getDictionaryServiceDelegate() { |
464 | 0 | return dictionaryServiceDelegate; |
465 | |
} |
466 | |
|
467 | |
|
468 | |
|
469 | |
|
470 | |
public void setDictionaryServiceDelegate( |
471 | |
DictionaryService dictionaryServiceDelegate) { |
472 | 1 | this.dictionaryServiceDelegate = dictionaryServiceDelegate; |
473 | 1 | } |
474 | |
|
475 | |
|
476 | |
|
477 | |
public SearchManager getSearchManager() { |
478 | 0 | return searchManager; |
479 | |
} |
480 | |
|
481 | |
|
482 | |
|
483 | |
|
484 | |
public void setSearchManager(SearchManager searchManager) { |
485 | 1 | this.searchManager = searchManager; |
486 | 1 | } |
487 | |
|
488 | |
@Override |
489 | |
public ObjectStructureDefinition getObjectStructure(String objectTypeKey) { |
490 | 9 | return dictionaryServiceDelegate.getObjectStructure(objectTypeKey); |
491 | |
} |
492 | |
@Override |
493 | |
public List<String> getObjectTypes() { |
494 | 0 | return dictionaryServiceDelegate.getObjectTypes(); |
495 | |
} |
496 | |
|
497 | |
|
498 | |
|
499 | |
|
500 | |
public ValidatorFactory getValidatorFactory() { |
501 | 0 | return validatorFactory; |
502 | |
} |
503 | |
|
504 | |
|
505 | |
|
506 | |
|
507 | |
public void setValidatorFactory(ValidatorFactory validatorFactory) { |
508 | 1 | this.validatorFactory = validatorFactory; |
509 | 1 | } |
510 | |
} |