Coverage Report - org.kuali.student.core.document.service.impl.DocumentServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentServiceImpl
84%
119/141
57%
16/28
1.789
 
 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.document.service.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Arrays;
 20  
 import java.util.List;
 21  
 
 22  
 import javax.jws.WebService;
 23  
 
 24  
 import org.kuali.student.common.validator.Validator;
 25  
 import org.kuali.student.common.validator.ValidatorFactory;
 26  
 import org.kuali.student.core.dictionary.dto.ObjectStructureDefinition;
 27  
 import org.kuali.student.core.dictionary.service.DictionaryService;
 28  
 import org.kuali.student.core.document.dao.DocumentDao;
 29  
 import org.kuali.student.core.document.dto.DocumentCategoryInfo;
 30  
 import org.kuali.student.core.document.dto.DocumentInfo;
 31  
 import org.kuali.student.core.document.dto.DocumentTypeInfo;
 32  
 import org.kuali.student.core.document.dto.RefDocRelationInfo;
 33  
 import org.kuali.student.core.document.dto.RefDocRelationTypeInfo;
 34  
 import org.kuali.student.core.document.entity.Document;
 35  
 import org.kuali.student.core.document.entity.DocumentCategory;
 36  
 import org.kuali.student.core.document.entity.DocumentType;
 37  
 import org.kuali.student.core.document.entity.RefDocRelation;
 38  
 import org.kuali.student.core.document.entity.RefDocRelationType;
 39  
 import org.kuali.student.core.document.entity.RefObjectSubType;
 40  
 import org.kuali.student.core.document.entity.RefObjectType;
 41  
 import org.kuali.student.core.document.service.DocumentService;
 42  
 import org.kuali.student.core.dto.StatusInfo;
 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  
  * This is a description of what this class does - lindholm don't forget to fill this in.
 56  
  *
 57  
  * @author Kuali Rice Team (kuali-rice@googlegroups.com)
 58  
  *
 59  
  */
 60  
 @WebService(endpointInterface = "org.kuali.student.core.document.service.DocumentService", serviceName = "DocumentService", portName = "DocumentService", targetNamespace = "http://student.kuali.org/wsdl/documentService")
 61  
 @Transactional(noRollbackFor={DoesNotExistException.class},rollbackFor={Throwable.class})
 62  2
 public class DocumentServiceImpl implements DocumentService {
 63  
     private DocumentDao dao;
 64  
     private DictionaryService dictionaryServiceDelegate;
 65  
     private ValidatorFactory validatorFactory;
 66  
     private SearchManager searchManager;
 67  
     
 68  
     public DocumentDao getDocumentDao(){
 69  0
         return dao;
 70  
     }
 71  
     
 72  
     public void setDocumentDao(DocumentDao dao){
 73  1
         this.dao=dao;
 74  1
     }
 75  
     
 76  
     
 77  
     @Override
 78  
     public StatusInfo addDocumentCategoryToDocument(String documentId, String documentCategoryKey) throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, VersionMismatchException {
 79  3
         checkForMissingParameter(documentId, "documentId");
 80  3
         checkForMissingParameter(documentCategoryKey, "documentCategoryKey");
 81  3
         StatusInfo statusInfo = new StatusInfo();
 82  3
         statusInfo.setSuccess(dao.addDocumentCategoryToDocument(documentId, documentCategoryKey));
 83  
         
 84  3
         return statusInfo;
 85  
     }
 86  
 
 87  
     @Override
 88  
     public DocumentInfo createDocument(String documentTypeKey, String documentCategoryKey, DocumentInfo documentInfo) throws DataValidationErrorException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 89  3
         checkForMissingParameter(documentTypeKey, "documentTypeKey");
 90  3
         checkForMissingParameter(documentCategoryKey, "documentCategoryKey");
 91  3
         checkForMissingParameter(documentInfo, "documentInfo");
 92  
         
 93  
         DocumentType type;
 94  
         DocumentCategory  category;
 95  
         
 96  
     // Validate
 97  
     List<ValidationResultInfo> validationResults;
 98  
     try {
 99  3
         validationResults = validateDocument("OBJECT", documentInfo);
 100  0
     } catch (DoesNotExistException e) {
 101  0
         throw new OperationFailedException("Validation call failed." + e.getMessage());
 102  3
     }
 103  3
     if (null != validationResults && validationResults.size() > 0) {
 104  0
         throw new DataValidationErrorException("Validation error!", validationResults);
 105  
     }
 106  
         
 107  
         try {
 108  3
             type = dao.fetch(DocumentType.class, documentTypeKey);
 109  3
             category = dao.fetch(DocumentCategory.class, documentCategoryKey);
 110  0
         } catch (DoesNotExistException dnee) {
 111  0
             throw new OperationFailedException("error fetching document keys", dnee);
 112  3
     }
 113  
         
 114  3
         Document doc = DocumentServiceAssembler.toDocument(new Document(), documentInfo, dao);
 115  3
         doc.setType(type);
 116  3
         doc.setCategoryList(Arrays.asList(category));
 117  3
         dao.create(doc);
 118  3
         return DocumentServiceAssembler.toDocumentInfo(doc);
 119  
     }
 120  
 
 121  
     @Override
 122  
     public StatusInfo deleteDocument(String documentId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 123  1
         checkForMissingParameter(documentId, "documentId");
 124  1
         dao.delete(Document.class, documentId);
 125  1
         return new StatusInfo();
 126  
     }
 127  
     @Override
 128  
     public List<DocumentCategoryInfo> getCategoriesByDocument(String documentId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 129  6
         checkForMissingParameter(documentId, "documentId");
 130  6
         List<DocumentCategory> categories = dao.getCategoriesByDocument(documentId);
 131  6
         return DocumentServiceAssembler.toDocumentCategoryInfos(categories);
 132  
     }
 133  
     @Override
 134  
     public DocumentInfo getDocument(String documentId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 135  5
         checkForMissingParameter(documentId, "documentId");
 136  5
         return DocumentServiceAssembler.toDocumentInfo(dao.fetch(Document.class, documentId));
 137  
     }
 138  
     @Override
 139  
     public List<DocumentCategoryInfo> getDocumentCategories() throws OperationFailedException {
 140  1
         List<DocumentCategory> categories = dao.find(DocumentCategory.class);
 141  1
         return DocumentServiceAssembler.toDocumentCategoryInfos(categories);
 142  
     }
 143  
     
 144  
     @Override
 145  
     public DocumentCategoryInfo getDocumentCategory(String documentCategoryKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 146  1
         checkForMissingParameter(documentCategoryKey, "documentCategoryKey");
 147  1
         return DocumentServiceAssembler.toDocumentCategoryInfo(dao.fetch(DocumentCategory.class, documentCategoryKey));
 148  
     }
 149  
     
 150  
     @Override
 151  
     public DocumentTypeInfo getDocumentType(String documentTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 152  1
         checkForMissingParameter(documentTypeKey, "documentTypeKey");
 153  1
         return DocumentServiceAssembler.toGenericTypeInfo(DocumentTypeInfo.class,(dao.fetch(DocumentType.class, documentTypeKey)));
 154  
     }
 155  
     
 156  
     @Override
 157  
     public List<DocumentTypeInfo> getDocumentTypes() throws OperationFailedException {
 158  1
         return DocumentServiceAssembler.toGenericTypeInfoList(DocumentTypeInfo.class,dao.find(DocumentType.class));
 159  
     }
 160  
     
 161  
     @Override
 162  
     public List<String> getRefObjectTypes() throws OperationFailedException {
 163  1
         return DocumentServiceAssembler.toGenericTypeKeyList(dao.find(RefObjectType.class));
 164  
     }
 165  
     
 166  
     @Override
 167  
     public List<String> getRefObjectSubTypes(String refObjectTypeKey) throws MissingParameterException, OperationFailedException {
 168  1
         checkForMissingParameter(refObjectTypeKey, "refObjectTypeKey");
 169  
         RefObjectType refOjectType;
 170  
                 try {
 171  1
                         refOjectType = dao.fetch(RefObjectType.class, refObjectTypeKey);
 172  0
                 } catch (DoesNotExistException e) {
 173  0
                         return new ArrayList<String>(0);
 174  1
                 }
 175  1
         return DocumentServiceAssembler.toGenericTypeKeyList(refOjectType.getRefObjectSubTypes());
 176  
     }
 177  
     
 178  
     @Override
 179  
     public List<RefDocRelationTypeInfo> getRefDocRelationTypes() throws OperationFailedException {
 180  1
         return DocumentServiceAssembler.toGenericTypeInfoList(RefDocRelationTypeInfo.class, dao.find(RefDocRelationType.class));
 181  
     }
 182  
     
 183  
     @Override
 184  
         public List<RefDocRelationTypeInfo> getRefDocRelationTypesForRefObjectSubType(String refSubTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 185  1
         checkForMissingParameter(refSubTypeKey, "refSubTypeKey");
 186  
         
 187  1
         RefObjectSubType refObjectSubType = dao.fetch(RefObjectSubType.class, refSubTypeKey);
 188  1
         return DocumentServiceAssembler.toGenericTypeInfoList(RefDocRelationTypeInfo.class, refObjectSubType.getRefDocRelationTypes());
 189  
     }
 190  
     @Override
 191  
     public List<DocumentInfo> getDocumentsByIdList(List<String> documentIdList) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 192  1
         checkForMissingParameter(documentIdList, "documentIdList");
 193  1
         checkForEmptyList(documentIdList, "documentIdList");
 194  1
         List<Document> documents = dao.getDocumentsByIdList(documentIdList);
 195  1
         return DocumentServiceAssembler.toDocumentInfos(documents);
 196  
     }
 197  
     @Override
 198  
     public RefDocRelationInfo getRefDocRelation(String refDocRelationId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 199  2
         checkForMissingParameter(refDocRelationId, "refDocRelationId");
 200  2
         return DocumentServiceAssembler.toRefDocRelationInfo(dao.fetch(RefDocRelation.class, refDocRelationId));
 201  
     }
 202  
     @Override
 203  
         public List<RefDocRelationInfo> getRefDocRelationsByRef(String refObjectTypeKey, String refObjectId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 204  1
         checkForMissingParameter(refObjectTypeKey, "refObjectTypeKey");
 205  1
         checkForMissingParameter(refObjectId, "refObjectId");
 206  1
         return DocumentServiceAssembler.toRefDocRelationInfos(dao.getRefDocRelationsByRef(refObjectTypeKey, refObjectId));       
 207  
     }
 208  
     @Override
 209  
     public List<RefDocRelationInfo> getRefDocRelationsByDoc(String documentId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 210  1
         checkForMissingParameter(documentId, "documentId");
 211  1
         return DocumentServiceAssembler.toRefDocRelationInfos(dao.getRefDocRelationsByDoc(documentId));       
 212  
     }
 213  
     
 214  
     @Override
 215  
     public StatusInfo removeDocumentCategoryFromDocument(String documentId, String documentCategoryKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 216  3
         checkForMissingParameter(documentId, "documentId");
 217  3
         checkForMissingParameter(documentCategoryKey, "documentCategoryKey");
 218  3
         StatusInfo statusInfo = new StatusInfo();
 219  3
         statusInfo.setSuccess(dao.removeDocumentCategoryFromDocument(documentId, documentCategoryKey));
 220  3
         return statusInfo;
 221  
     }
 222  
 
 223  
     /**
 224  
      * Does not update Type or categories
 225  
      */
 226  
     @Override
 227  
     public DocumentInfo updateDocument(String documentId, DocumentInfo documentInfo) throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, VersionMismatchException {
 228  2
         checkForMissingParameter(documentId, "documentId");
 229  2
         checkForMissingParameter(documentInfo, "documentInfo");
 230  
         
 231  2
         List<ValidationResultInfo> validationResults = validateDocument("OBJECT", documentInfo);
 232  2
         if (null != validationResults && validationResults.size() > 0) {
 233  0
             throw new DataValidationErrorException("Validation error!", validationResults);
 234  
         }
 235  
         
 236  2
         Document document = dao.fetch(Document.class, documentId);
 237  
         
 238  2
         if (!String.valueOf(document.getVersionNumber()).equals(documentInfo.getMetaInfo().getVersionInd())){
 239  1
             throw new VersionMismatchException("Document to be updated is not the current version");
 240  
         }
 241  
         
 242  1
         document = DocumentServiceAssembler.toDocument(document, documentInfo, dao);
 243  
                 
 244  1
         return DocumentServiceAssembler.toDocumentInfo(dao.update(document));
 245  
     }
 246  
     
 247  
 
 248  
     @Override
 249  
     public List<ValidationResultInfo> validateDocument(String validationType, DocumentInfo documentInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 250  5
         checkForMissingParameter(validationType, "validationType");
 251  5
         checkForMissingParameter(documentInfo, "documentInfo");
 252  
         
 253  5
         ObjectStructureDefinition objStructure = this.getObjectStructure(DocumentInfo.class.getName());
 254  5
         Validator defaultValidator = validatorFactory.getValidator();
 255  5
         List<ValidationResultInfo> validationResults = defaultValidator.validateObject(documentInfo, objStructure);
 256  5
         return validationResults;        
 257  
     }
 258  
 
 259  
 
 260  
     @Override
 261  
     public List<ValidationResultInfo> validateRefDocRelation(String validationType, RefDocRelationInfo refDocRelationInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 262  5
         checkForMissingParameter(validationType, "validationType");
 263  5
         checkForMissingParameter(refDocRelationInfo, "refDocRelationInfo");
 264  
 
 265  5
         ObjectStructureDefinition objStructure = this.getObjectStructure(RefDocRelationInfo.class.getName());
 266  5
         Validator defaultValidator = validatorFactory.getValidator();
 267  5
         List<ValidationResultInfo> validationResults = defaultValidator.validateObject(refDocRelationInfo, objStructure);
 268  5
         return validationResults;        
 269  
     }
 270  
 
 271  
     @Override
 272  
     public RefDocRelationInfo createRefDocRelation(String refObjectTypeKey, String refObjectId, String documentId, String refDocRelationTypeKey, RefDocRelationInfo refDocRelationInfo) throws DataValidationErrorException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 273  3
         checkForMissingParameter(refObjectTypeKey, "refObjectTypeKey");
 274  3
         checkForMissingParameter(refObjectId, "refObjectId");
 275  3
         checkForMissingParameter(refDocRelationTypeKey, "refDocRelationTypeKey");
 276  3
         checkForMissingParameter(refDocRelationInfo, "refDocRelationInfo");
 277  
         
 278  
         List<ValidationResultInfo> validationResults;
 279  
         try {
 280  3
             validationResults = validateRefDocRelation("OBJECT", refDocRelationInfo);
 281  0
         } catch (DoesNotExistException e) {
 282  0
             throw new OperationFailedException("Validation call failed." + e.getMessage());
 283  3
         }
 284  3
         if (null != validationResults && validationResults.size() > 0) {
 285  0
             throw new DataValidationErrorException("Validation error!", validationResults);
 286  
         }
 287  
                          
 288  3
         refDocRelationInfo.setRefObjectTypeKey(refObjectTypeKey);
 289  3
         refDocRelationInfo.setRefObjectId(refObjectId);
 290  3
         refDocRelationInfo.setType(refDocRelationTypeKey);;
 291  
         
 292  3
         RefDocRelation refDocRelation = DocumentServiceAssembler.toRefDocRelation(new RefDocRelation(), refDocRelationInfo, dao);
 293  
 
 294  1
         return DocumentServiceAssembler.toRefDocRelationInfo(dao.create(refDocRelation));
 295  
     }
 296  
 
 297  
     @Override
 298  
     public RefDocRelationInfo updateRefDocRelation(String refDocRelationId, RefDocRelationInfo refDocRelationInfo) throws DataValidationErrorException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, VersionMismatchException, DoesNotExistException {
 299  2
         checkForMissingParameter(refDocRelationId, "refDocRelationId");
 300  2
         checkForMissingParameter(refDocRelationInfo, "refDocRelationInfo");
 301  
         
 302  2
         List<ValidationResultInfo> validationResults = validateRefDocRelation("OBJECT", refDocRelationInfo);
 303  2
         if (null != validationResults && validationResults.size() > 0) {
 304  0
             throw new DataValidationErrorException("Validation error!", validationResults);
 305  
         }
 306  
                 
 307  2
         refDocRelationInfo.setId(refDocRelationId);
 308  
         
 309  2
         RefDocRelation refDocRelation = dao.fetch(RefDocRelation.class, refDocRelationId);
 310  
         
 311  2
         if (!String.valueOf(refDocRelation.getVersionNumber()).equals(refDocRelationInfo.getMetaInfo().getVersionInd())){
 312  1
             throw new VersionMismatchException("RefDocRelation to be updated is not the current version");
 313  
         }
 314  
         
 315  1
         refDocRelation = DocumentServiceAssembler.toRefDocRelation(refDocRelation, refDocRelationInfo, dao);
 316  
                 
 317  1
         return DocumentServiceAssembler.toRefDocRelationInfo(dao.update(refDocRelation));
 318  
     }
 319  
 
 320  
     @Override
 321  
     public StatusInfo deleteRefDocRelation(String refDocRelationId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 322  1
         checkForMissingParameter(refDocRelationId, "refDocRelationId");
 323  1
         dao.delete(RefDocRelation.class, refDocRelationId);
 324  1
         return new StatusInfo();
 325  
     }
 326  
 
 327  
    
 328  
     /**
 329  
      * Check for missing parameter and throw localized exception if missing
 330  
      *
 331  
      * @param param
 332  
      * @param parameter name
 333  
      * @throws MissingParameterException
 334  
      */
 335  
     private void checkForMissingParameter(Object param, String paramName)
 336  
             throws MissingParameterException {
 337  84
         if (param == null) {
 338  0
             throw new MissingParameterException(paramName + " can not be null");
 339  
         }
 340  84
     }
 341  
 
 342  
     /**
 343  
      * @param param
 344  
      * @param paramName
 345  
      * @throws MissingParameterException
 346  
      */
 347  
     private void checkForEmptyList(Object param, String paramName)
 348  
             throws MissingParameterException {
 349  1
         if (param != null && param instanceof List<?> && ((List<?>)param).size() == 0) {
 350  0
             throw new MissingParameterException(paramName + " can not be an empty list");
 351  
         }
 352  1
     }
 353  
     
 354  
     @Override
 355  
     public ObjectStructureDefinition getObjectStructure(String objectTypeKey) {
 356  10
         return dictionaryServiceDelegate.getObjectStructure(objectTypeKey);
 357  
     }
 358  
 
 359  
     @Override
 360  
     public List<String> getObjectTypes() {
 361  0
         return dictionaryServiceDelegate.getObjectTypes();
 362  
     }
 363  
     
 364  
     public DictionaryService getDictionaryServiceDelegate() {
 365  0
         return dictionaryServiceDelegate;
 366  
     }
 367  
 
 368  
     public void setDictionaryServiceDelegate(DictionaryService dictionaryServiceDelegate) {
 369  1
         this.dictionaryServiceDelegate = dictionaryServiceDelegate;
 370  1
     }
 371  
 
 372  
         public DocumentDao getDao() {
 373  0
                 return dao;
 374  
         }
 375  
 
 376  
         public void setDao(DocumentDao dao) {
 377  1
                 this.dao = dao;
 378  1
         }
 379  
 
 380  
         public SearchManager getSearchManager() {
 381  0
                 return searchManager;
 382  
         }
 383  
 
 384  
         public void setSearchManager(SearchManager searchManager) {
 385  0
                 this.searchManager = searchManager;
 386  0
         }
 387  
 
 388  
     public ValidatorFactory getValidatorFactory() {
 389  0
         return validatorFactory;
 390  
     }
 391  
 
 392  
     public void setValidatorFactory(ValidatorFactory validatorFactory) {
 393  1
         this.validatorFactory = validatorFactory;
 394  1
     }        
 395  
 }