Coverage Report - org.kuali.student.core.comment.service.impl.CommentServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
CommentServiceImpl
75%
114/152
64%
18/28
2.333
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 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  
  * Implementation of the Comment Search Service
 56  
  *
 57  
  * @author Kuali Rice Team (kuali-rice@googlegroups.com)
 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(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  
      * This overridden method ...
 73  
      *
 74  
      * @see org.kuali.student.core.comment.service.CommentService#addComment(java.lang.String, java.lang.String, org.kuali.student.core.comment.dto.CommentInfo)
 75  
      */
 76  
     @Override
 77  
     public CommentInfo addComment(String referenceId, String referenceTypeKey, CommentInfo commentInfo) throws DataValidationErrorException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 78  4
         commentInfo.setReferenceTypeKey(referenceTypeKey);
 79  4
         commentInfo.setReferenceId(referenceId);
 80  
             
 81  
         // Validate Comment
 82  4
         List<ValidationResultInfo> validationResults = null;
 83  
         try {
 84  4
             validationResults = validateComment("OBJECT", commentInfo);
 85  0
         } catch (DoesNotExistException e1) {
 86  0
             throw new OperationFailedException("Validation call failed." + e1.getMessage());
 87  4
         }
 88  4
         if (null != validationResults && validationResults.size() > 0) {
 89  0
             throw new DataValidationErrorException("Validation error!", validationResults);
 90  
         }
 91  
         
 92  4
         Reference reference=null;
 93  4
         reference = commentDao.getReference(referenceId, referenceTypeKey);
 94  4
         if(reference==null){
 95  3
             reference = new Reference();
 96  3
             reference.setReferenceId(referenceId);
 97  
                         try {
 98  3
                                 ReferenceType referenceType = commentDao.fetch(ReferenceType.class, referenceTypeKey);
 99  3
                     reference.setReferenceType(referenceType);
 100  3
                     commentDao.create(reference);
 101  0
                         } catch (DoesNotExistException e) {
 102  0
                                 throw new InvalidParameterException(e.getMessage());
 103  3
                         }
 104  
         }
 105  
 
 106  4
         Comment comment = null;
 107  
 
 108  
         try {
 109  4
             comment = CommentServiceAssembler.toComment(false, commentInfo, commentDao);
 110  0
         } catch (DoesNotExistException e) {
 111  0
             throw new InvalidParameterException(e.getMessage());
 112  4
         }
 113  
 
 114  4
         Comment createdComment = commentDao.create(comment);
 115  
 
 116  4
         CommentInfo createdCommentInfo = CommentServiceAssembler.toCommentInfo(createdComment);
 117  
 
 118  4
         return createdCommentInfo;
 119  
     }
 120  
 
 121  
     /**
 122  
      * This overridden method ...
 123  
      * @see org.kuali.student.core.comment.service.CommentService#addTag(java.lang.String, java.lang.String, org.kuali.student.core.comment.dto.TagInfo)
 124  
      */
 125  
     @Override
 126  
     public TagInfo addTag(String referenceId, String referenceTypeKey, TagInfo tagInfo) throws DataValidationErrorException, AlreadyExistsException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 127  
 
 128  
         // Validate Tag
 129  4
         List<ValidationResultInfo> validationResults = null;
 130  
         try {
 131  4
             validationResults = validateTag("OBJECT", tagInfo);
 132  0
         } catch (DoesNotExistException e1) {
 133  0
             throw new OperationFailedException("Validation call failed." + e1.getMessage());
 134  4
         }
 135  4
         if (null != validationResults && validationResults.size() > 0) {
 136  0
             throw new DataValidationErrorException("Validation error!", validationResults);
 137  
         }
 138  
         
 139  4
         tagInfo.setReferenceTypeKey(referenceTypeKey);
 140  4
         tagInfo.setReferenceId(referenceId);
 141  4
         Reference reference=null;
 142  4
         reference = commentDao.getReference(referenceId, referenceTypeKey);
 143  4
         if(reference==null){
 144  1
             reference = new Reference();
 145  1
             reference.setReferenceId(referenceId);
 146  
                         try {
 147  1
                                 ReferenceType referenceType = commentDao.fetch(ReferenceType.class, referenceTypeKey);
 148  1
                     reference.setReferenceType(referenceType);
 149  1
                     commentDao.create(reference);
 150  0
                         } catch (DoesNotExistException e) {
 151  0
                                 throw new InvalidParameterException(e.getMessage());
 152  1
                         }
 153  
         }
 154  
 
 155  4
         Tag tag = null;
 156  
 
 157  
         try {
 158  4
             tag = CommentServiceAssembler.toTag(false, tagInfo, commentDao);
 159  0
         } catch (DoesNotExistException e) {
 160  0
             logger.error("Exception occured: ", e);
 161  4
         }
 162  
 
 163  4
         Tag createdTag = commentDao.create(tag);
 164  
 
 165  4
         TagInfo createdTagInfo = CommentServiceAssembler.toTagInfo(createdTag);
 166  
 
 167  4
         return createdTagInfo;
 168  
     }
 169  
 
 170  
     /**
 171  
      * This overridden method ...
 172  
      *
 173  
      * @see org.kuali.student.core.comment.service.CommentService#getComment(java.lang.String)
 174  
      */
 175  
     @Override
 176  
     public CommentInfo getComment(String commentId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 177  8
         checkForMissingParameter(commentId, "commentId");
 178  7
         Comment comment = commentDao.fetch(Comment.class, commentId);
 179  3
         return CommentServiceAssembler.toCommentInfo(comment);
 180  
     }
 181  
 
 182  
     /**
 183  
      * This overridden method ...
 184  
      *
 185  
      * @see org.kuali.student.core.comment.service.CommentService#getCommentTypes()
 186  
      */
 187  
     @Override
 188  
     public List<CommentTypeInfo> getCommentTypes() throws OperationFailedException {
 189  0
         List<CommentType> commentTypes = commentDao.find(CommentType.class);
 190  0
         return CommentServiceAssembler.toCommentTypeInfos(commentTypes);
 191  
     }
 192  
 
 193  
     /**
 194  
      * This overridden method ...
 195  
      *
 196  
      * @see org.kuali.student.core.comment.service.CommentService#getCommentTypesForReferenceType(java.lang.String)
 197  
      */
 198  
     @Override
 199  
     public List<CommentTypeInfo> getCommentTypesForReferenceType(String referenceTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 200  1
         List<CommentType> commentTypes = commentDao.getCommentTypesByReferenceTypeId(referenceTypeKey);
 201  1
         return CommentServiceAssembler.toCommentTypeInfos(commentTypes);
 202  
     }
 203  
 
 204  
     /**
 205  
      * This overridden method ...
 206  
      *
 207  
      * @see org.kuali.student.core.comment.service.CommentService#getComments(java.lang.String, java.lang.String)
 208  
      */
 209  
     @Override
 210  
     public List<CommentInfo> getComments(String referenceId, String referenceTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 211  2
         List<Comment> comments = commentDao.getComments(referenceId, referenceTypeKey);
 212  2
         return CommentServiceAssembler.toCommentInfos(comments);
 213  
     }
 214  
 
 215  
     /**
 216  
      * This overridden method ...
 217  
      *
 218  
      * @see org.kuali.student.core.comment.service.CommentService#getCommentsByType(java.lang.String, java.lang.String, java.lang.String)
 219  
      */
 220  
     @Override
 221  
     public List<CommentInfo> getCommentsByType(String referenceId, String referenceTypeKey, String commentTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 222  0
         List<Comment> comments = commentDao.getCommentsByType(referenceId, referenceTypeKey, commentTypeKey);
 223  0
         return CommentServiceAssembler.toCommentInfos(comments);
 224  
     }
 225  
 
 226  
     /**
 227  
      * This overridden method ...
 228  
      *
 229  
      * @see org.kuali.student.core.comment.service.CommentService#getReferenceTypes()
 230  
      */
 231  
     @Override
 232  
     public List<ReferenceTypeInfo> getReferenceTypes() throws OperationFailedException {
 233  1
             List<ReferenceType> referenceTypes = commentDao.find(ReferenceType.class);
 234  1
         return CommentServiceAssembler.toReferenceTypeInfos(referenceTypes);
 235  
     }
 236  
 
 237  
     /**
 238  
      * This overridden method ...
 239  
      *
 240  
      * @see org.kuali.student.core.comment.service.CommentService#getTag(java.lang.String)
 241  
      */
 242  
     @Override
 243  
     public TagInfo getTag(String tagId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 244  4
         checkForMissingParameter(tagId, "tagId");
 245  3
         Tag tag = commentDao.fetch(Tag.class, tagId);
 246  2
         return CommentServiceAssembler.toTagInfo(tag);
 247  
     }
 248  
 
 249  
     /**
 250  
      * This overridden method ...
 251  
      *
 252  
      * @see org.kuali.student.core.comment.service.CommentService#getTagTypes()
 253  
      */
 254  
     @Override
 255  
     public List<TagTypeInfo> getTagTypes() throws OperationFailedException {
 256  1
         List<TagType> tagTypes = commentDao.find(TagType.class);
 257  
 
 258  1
         return CommentServiceAssembler.toTagTypeInfos(tagTypes);
 259  
     }
 260  
 
 261  
     /**
 262  
      * This overridden method ...
 263  
      *
 264  
      * @see org.kuali.student.core.comment.service.CommentService#getTags(java.lang.String, java.lang.String)
 265  
      */
 266  
     @Override
 267  
     public List<TagInfo> getTags(String referenceId, String referenceTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 268  
 
 269  2
         List<Tag> tags = commentDao.getTags(referenceId, referenceTypeKey);
 270  
 
 271  2
         return CommentServiceAssembler.toTagInfos(tags);
 272  
     }
 273  
 
 274  
     /**
 275  
      * This overridden method ...
 276  
      *
 277  
      * @see org.kuali.student.core.comment.service.CommentService#getTagsByType(java.lang.String, java.lang.String, java.lang.String)
 278  
      */
 279  
     @Override
 280  
     public List<TagInfo> getTagsByType(String referenceId, String referenceTypeKey, String tagTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 281  
 
 282  1
         List<Tag> tags = commentDao.getTagsByType(referenceId, referenceTypeKey, tagTypeKey);
 283  1
         return CommentServiceAssembler.toTagInfos(tags);
 284  
     }
 285  
 
 286  
     /**
 287  
      * This overridden method ...
 288  
      *
 289  
      * @see org.kuali.student.core.comment.service.CommentService#removeComment(java.lang.String, java.lang.String, java.lang.String)
 290  
      */
 291  
     @Override
 292  
     public StatusInfo removeComment(String commentId, String referenceId, String referenceTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 293  
         try{
 294  2
             checkForMissingParameter(commentId, "commentId");
 295  2
             commentDao.delete(Comment.class, commentId);
 296  1
             return  new StatusInfo();
 297  
         }
 298  0
         catch(MissingParameterException mpe){
 299  0
             Comment comment = null;
 300  
             try{
 301  0
                 comment = commentDao.getComment(referenceId, referenceTypeKey);
 302  0
                 if(comment==null){
 303  0
                     throw new DoesNotExistException();
 304  
                 }
 305  
             }
 306  0
             catch(NoResultException nre){
 307  0
                 throw new DoesNotExistException();
 308  0
             }
 309  0
             commentDao.delete(comment);
 310  0
             return  new StatusInfo();
 311  
         }
 312  
 
 313  
     }
 314  
 
 315  
     /**
 316  
      * This overridden method ...
 317  
      *
 318  
      * @see org.kuali.student.core.comment.service.CommentService#removeComments(java.lang.String)
 319  
      */
 320  
     @Override
 321  
     public StatusInfo removeComments(String referenceId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 322  1
         List<Comment> comments = commentDao.getCommentsByRefId(referenceId);
 323  1
         for(Comment comment:comments){
 324  2
             commentDao.delete(comment);
 325  
         }
 326  1
         return new StatusInfo();
 327  
     }
 328  
 
 329  
     /**
 330  
      * This overridden method ...
 331  
      * @throws DoesNotExistException
 332  
      *
 333  
      * @see org.kuali.student.core.comment.service.CommentService#removeTag(java.lang.String, java.lang.String, java.lang.String)
 334  
      */
 335  
     @Override
 336  
     public StatusInfo removeTag(String tagId, String referenceId, String referenceTypeKey) throws  InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, DoesNotExistException {
 337  
         try{
 338  3
             checkForMissingParameter(tagId, "tagId");
 339  0
             commentDao.delete(Tag.class, tagId);
 340  0
             return  new StatusInfo();
 341  
         }
 342  3
         catch(MissingParameterException mpe){
 343  3
             Tag tag = null;
 344  
             try{
 345  3
                 tag = commentDao.getTag(referenceId, referenceTypeKey);
 346  1
                 if(tag==null){
 347  0
                     throw new DoesNotExistException();
 348  
                 }
 349  
             }
 350  2
             catch(NoResultException nre){
 351  2
                 throw new DoesNotExistException();
 352  1
             }
 353  1
             commentDao.delete(tag);
 354  1
             return  new StatusInfo();
 355  
         }
 356  
     }
 357  
 
 358  
     /**
 359  
      * This overridden method ...
 360  
      *
 361  
      * @see org.kuali.student.core.comment.service.CommentService#removeTags(java.lang.String)
 362  
      */
 363  
     @Override
 364  
     public StatusInfo removeTags(String tagId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 365  
         //tagId sould be referenceId like in removeComments() method
 366  1
         List<Tag> tags = commentDao.getTagsByRefId(tagId);
 367  1
         for(Tag tag:tags){
 368  3
             commentDao.delete(tag);
 369  
         }
 370  1
         return new StatusInfo();
 371  
     }
 372  
 
 373  
 
 374  
     /**
 375  
      * This overridden method ...
 376  
      *
 377  
      * @see org.kuali.student.core.comment.service.CommentService#updateComment(java.lang.String, java.lang.String, org.kuali.student.core.comment.dto.CommentInfo)
 378  
      */
 379  
     @Override
 380  
     public CommentInfo updateComment(String referenceId, String referenceTypeKey, CommentInfo commentInfo) throws DataValidationErrorException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, DoesNotExistException, VersionMismatchException {
 381  
 
 382  
         // Validate Comment
 383  1
         List<ValidationResultInfo> validationResults = validateComment("OBJECT", commentInfo);
 384  1
         if (null != validationResults && validationResults.size() > 0) {
 385  0
             throw new DataValidationErrorException("Validation error!", validationResults);
 386  
         }
 387  
         
 388  1
                 Comment entity = commentDao.fetch(Comment.class, commentInfo.getId());
 389  1
                 if (!String.valueOf(entity.getVersionNumber()).equals(commentInfo.getMetaInfo().getVersionInd())){
 390  0
                         throw new VersionMismatchException("ResultComponent to be updated is not the current version");
 391  
                 }
 392  
 
 393  1
                 CommentServiceAssembler.toComment(entity, referenceId, referenceTypeKey, commentInfo, commentDao);
 394  1
             commentDao.update(entity);
 395  
 
 396  1
             return CommentServiceAssembler.toCommentInfo(entity);
 397  
     }
 398  
 
 399  
     /**
 400  
      * This overridden method ...
 401  
      *
 402  
      * @see org.kuali.student.core.comment.service.CommentService#validateComment(java.lang.String, org.kuali.student.core.comment.dto.CommentInfo)
 403  
      */
 404  
     @Override
 405  
     public List<ValidationResultInfo> validateComment(String validationType, CommentInfo commentInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 406  5
                 checkForMissingParameter(validationType, "validationType");
 407  5
                 checkForMissingParameter(commentInfo, "commentInfo");
 408  
 
 409  5
         ObjectStructureDefinition objStructure = this.getObjectStructure(CommentInfo.class.getName());
 410  5
         Validator defaultValidator = validatorFactory.getValidator();
 411  5
         List<ValidationResultInfo> validationResults = defaultValidator.validateObject(commentInfo, objStructure);
 412  5
         return validationResults;         
 413  
     }
 414  
 
 415  
     private List<ValidationResultInfo> validateTag(String validationType, TagInfo tagInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 416  4
         checkForMissingParameter(validationType, "validationType");
 417  4
         checkForMissingParameter(tagInfo, "tagInfo");
 418  
 
 419  4
         ObjectStructureDefinition objStructure = this.getObjectStructure(TagInfo.class.getName());
 420  4
         Validator defaultValidator = validatorFactory.getValidator();
 421  4
         List<ValidationResultInfo> validationResults = defaultValidator.validateObject(tagInfo, objStructure);
 422  4
         return validationResults;         
 423  
     }
 424  
     
 425  
     /**
 426  
      * @return the commentDao
 427  
      */
 428  
     public CommentDao getCommentDao() {
 429  0
         return commentDao;
 430  
     }
 431  
 
 432  
     /**
 433  
      * @param commentDao the commentDao to set
 434  
      */
 435  
     public void setCommentDao(CommentDao commentDao) {
 436  1
         this.commentDao = commentDao;
 437  1
     }
 438  
 
 439  
     /**
 440  
      * Check for missing parameter and throw localized exception if missing
 441  
      *
 442  
      * @param param
 443  
      * @param parameter name
 444  
      * @throws MissingParameterException
 445  
      */
 446  
     private void checkForMissingParameter(Object param, String paramName)
 447  
             throws MissingParameterException {
 448  35
         if (param == null) {
 449  5
             throw new MissingParameterException(paramName + " can not be null");
 450  
         }
 451  30
     }
 452  
 
 453  
         /**
 454  
          * @return the dictionaryServiceDelegate
 455  
          */
 456  
         public DictionaryService getDictionaryServiceDelegate() {
 457  0
                 return dictionaryServiceDelegate;
 458  
         }
 459  
 
 460  
         /**
 461  
          * @param dictionaryServiceDelegate the dictionaryServiceDelegate to set
 462  
          */
 463  
         public void setDictionaryServiceDelegate(
 464  
                         DictionaryService dictionaryServiceDelegate) {
 465  1
                 this.dictionaryServiceDelegate = dictionaryServiceDelegate;
 466  1
         }
 467  
     /**
 468  
          * @return the searchManager
 469  
          */
 470  
         public SearchManager getSearchManager() {
 471  0
                 return searchManager;
 472  
         }
 473  
 
 474  
         /**
 475  
          * @param searchManager the searchManager to set
 476  
          */
 477  
         public void setSearchManager(SearchManager searchManager) {
 478  1
                 this.searchManager = searchManager;
 479  1
         }
 480  
 
 481  
         @Override
 482  
         public ObjectStructureDefinition getObjectStructure(String objectTypeKey) {
 483  9
                 return dictionaryServiceDelegate.getObjectStructure(objectTypeKey);
 484  
         }
 485  
         @Override
 486  
         public List<String> getObjectTypes() {
 487  0
                 return dictionaryServiceDelegate.getObjectTypes();
 488  
         }
 489  
 
 490  
     /**
 491  
      * @return the validatorFactory
 492  
      */
 493  
     public ValidatorFactory getValidatorFactory() {
 494  0
         return validatorFactory;
 495  
     }
 496  
 
 497  
     /**
 498  
      * @param validatorFactory the validatorFactory to set
 499  
      */
 500  
     public void setValidatorFactory(ValidatorFactory validatorFactory) {
 501  1
         this.validatorFactory = validatorFactory;
 502  1
     }
 503  
 }