Coverage Report - org.kuali.student.core.document.service.impl.DocumentServiceAssembler
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentServiceAssembler
94%
74/78
80%
16/20
3
 
 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.List;
 20  
 
 21  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 22  
 import org.kuali.student.common.exceptions.InvalidParameterException;
 23  
 import org.kuali.student.common.exceptions.OperationFailedException;
 24  
 import org.kuali.student.common.service.impl.BaseAssembler;
 25  
 import org.kuali.student.core.document.dao.DocumentDao;
 26  
 import org.kuali.student.core.document.dto.DocumentBinaryInfo;
 27  
 import org.kuali.student.core.document.dto.DocumentCategoryInfo;
 28  
 import org.kuali.student.core.document.dto.DocumentInfo;
 29  
 import org.kuali.student.core.document.dto.RefDocRelationInfo;
 30  
 import org.kuali.student.core.document.entity.Document;
 31  
 import org.kuali.student.core.document.entity.DocumentAttribute;
 32  
 import org.kuali.student.core.document.entity.DocumentCategory;
 33  
 import org.kuali.student.core.document.entity.DocumentRichText;
 34  
 import org.kuali.student.core.document.entity.RefDocRelation;
 35  
 import org.kuali.student.core.document.entity.RefDocRelationAttribute;
 36  
 import org.kuali.student.core.document.entity.RefDocRelationType;
 37  
 import org.kuali.student.core.document.entity.RefObjectSubType;
 38  
 import org.kuali.student.core.document.entity.RefObjectType;
 39  
 import org.springframework.beans.BeanUtils;
 40  
 
 41  
 /**
 42  
  * This is a description of what this class does - lindholm don't forget to fill this in.
 43  
  *
 44  
  * @author Kuali Rice Team (kuali-rice@googlegroups.com)
 45  
  *
 46  
  */
 47  0
 public class DocumentServiceAssembler extends BaseAssembler {
 48  
 
 49  
     public static DocumentInfo toDocumentInfo(Document entity) {
 50  10
         DocumentInfo dto = new DocumentInfo();
 51  
 
 52  10
         BeanUtils.copyProperties(entity, dto,
 53  
                 new String[] { "desc", "attributes", "metaInfo","type", "categoryList", "document" });
 54  10
         dto.setDesc(toRichTextInfo(entity.getDescr()));
 55  10
         dto.setMetaInfo(toMetaInfo(entity));
 56  10
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 57  10
         dto.setType(entity.getType().getId());
 58  10
         dto.setDocumentBinaryInfo(new DocumentBinaryInfo());
 59  10
         dto.getDocumentBinaryInfo().setBinary(entity.getDocument());
 60  10
         return dto;
 61  
     }
 62  
     
 63  
     public static List<DocumentInfo> toDocumentInfos(List<Document> entities) {
 64  1
         List<DocumentInfo> dtos = new ArrayList<DocumentInfo>();
 65  1
         if(entities!=null){
 66  1
                 for (Document entity : entities) {
 67  1
                         dtos.add(toDocumentInfo(entity));
 68  
                 }
 69  
                }
 70  1
         return dtos;
 71  
     }
 72  
 
 73  
     public static Document toDocument(Document entity, DocumentInfo dto, DocumentDao dao) throws InvalidParameterException {
 74  4
         BeanUtils.copyProperties(dto, entity,
 75  
                 new String[] { "desc", "attributes", "metaInfo", "type", "id", "documentBinaryInfo" });
 76  4
         entity.setDescr(toRichText(DocumentRichText.class, dto.getDesc()));
 77  4
         entity.setDocument(dto.getDocumentBinaryInfo().getBinary());
 78  4
         entity.setAttributes(toGenericAttributes(DocumentAttribute.class, dto.getAttributes(), entity, dao));
 79  4
         return entity;
 80  
     }
 81  
 
 82  
     public static DocumentCategoryInfo toDocumentCategoryInfo(DocumentCategory entity) {
 83  15
         DocumentCategoryInfo dto = new DocumentCategoryInfo();
 84  
 
 85  15
         BeanUtils.copyProperties(entity, dto,
 86  
                 new String[] { "desc", "attributes", "type", "documents" });
 87  15
         dto.setDesc(toRichTextInfo(entity.getDescr()));
 88  15
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 89  15
         return dto;
 90  
     }
 91  
 
 92  
     public static List<DocumentCategoryInfo> toDocumentCategoryInfos(List<DocumentCategory> entities) {
 93  7
         List<DocumentCategoryInfo> dtos = new ArrayList<DocumentCategoryInfo>();
 94  7
         if(entities!=null){
 95  7
                 for (DocumentCategory entity : entities) {
 96  14
                         dtos.add(toDocumentCategoryInfo(entity));
 97  
                 }
 98  
         }
 99  7
         return dtos;
 100  
     }
 101  
 
 102  
         public static RefDocRelationInfo toRefDocRelationInfo(RefDocRelation entity) {
 103  5
                 RefDocRelationInfo dto = new RefDocRelationInfo();
 104  
                 
 105  5
                 dto.setAttributes(toAttributeMap(entity.getAttributes()));
 106  5
                 dto.setDesc(toRichTextInfo(entity.getDescr()));
 107  5
                 dto.setDocumentId(entity.getDocument().getId());
 108  5
                 dto.setEffectiveDate(entity.getEffectiveDate());
 109  5
                 dto.setExpirationDate(entity.getExpirationDate());
 110  5
                 dto.setId(entity.getId());
 111  5
                 dto.setMetaInfo(toMetaInfo(entity));
 112  5
                 dto.setRefObjectId(entity.getRefObjectId());
 113  5
                 dto.setRefObjectTypeKey(entity.getRefObjectType().getId());
 114  5
                 dto.setState(entity.getState());
 115  5
                 dto.setTitle(entity.getTitle());
 116  5
                 dto.setType(entity.getRefDocRelationType().getId());
 117  
                 
 118  5
                 return dto;
 119  
         }
 120  
 
 121  
         public static List<RefDocRelationInfo> toRefDocRelationInfos(
 122  
                         List<RefDocRelation> entities) {
 123  2
                 List<RefDocRelationInfo> dtos = new ArrayList<RefDocRelationInfo>();
 124  2
                 if(entities!=null){
 125  2
                         for(RefDocRelation entity:entities){
 126  2
                                 dtos.add(toRefDocRelationInfo(entity));
 127  
                         }
 128  
                 }
 129  2
                 return dtos;
 130  
         }
 131  
 
 132  
         public static RefDocRelation toRefDocRelation(RefDocRelation entity,
 133  
                         RefDocRelationInfo dto, DocumentDao dao) throws InvalidParameterException, OperationFailedException {
 134  
         
 135  
                 //Substructures that need to be looked up from persistence
 136  4
                 RefObjectType refObjectType = null; 
 137  
         RefDocRelationType refDocRelationType;
 138  
         Document document;
 139  
         
 140  
         //Get the relation type
 141  
         try{
 142  4
                 refDocRelationType = dao.fetch(RefDocRelationType.class, dto.getType());
 143  1
         }catch(DoesNotExistException e){
 144  1
                 throw new InvalidParameterException("RefDocRelationType does not exist for key:" + dto.getType());
 145  3
         } 
 146  
         
 147  
         //Check that the relation type has subtypes
 148  3
         if(refDocRelationType.getRefObjectSubTypes()==null){
 149  0
                 throw new OperationFailedException("No RefObjectSubTypes are defined for this relation Type");
 150  
         }
 151  
         
 152  
         //Check that that the object type is valid for this relation type and get the object type in the process
 153  3
         for(RefObjectSubType subType:refDocRelationType.getRefObjectSubTypes()){
 154  5
                 if(dto.getRefObjectTypeKey().equals(subType.getRefObjectType().getId())){
 155  2
                         refObjectType = subType.getRefObjectType();
 156  2
                         break;
 157  
                 }
 158  
         }
 159  3
         if(refObjectType == null){
 160  1
                 throw new InvalidParameterException("RefObjectType: " + dto.getRefObjectTypeKey()+
 161  
                                 " does not exist or is invalid for relation type: " + dto.getType());
 162  
         }
 163  
         
 164  
         //Get the document
 165  
         try{
 166  2
                 document = dao.fetch(Document.class, dto.getDocumentId());
 167  0
         }catch(DoesNotExistException e){
 168  0
                 throw new InvalidParameterException("Document does not exist for key:" + dto.getDocumentId());
 169  2
         } 
 170  
         
 171  
         //Copy the fields
 172  2
         entity.setAttributes(toGenericAttributes(RefDocRelationAttribute.class, dto.getAttributes(), entity, dao));
 173  2
         entity.setDescr(toRichText(DocumentRichText.class, dto.getDesc()));
 174  2
         entity.setDocument(document);
 175  2
         entity.setEffectiveDate(dto.getEffectiveDate());
 176  2
         entity.setExpirationDate(dto.getExpirationDate());
 177  2
         entity.setId(dto.getId());
 178  2
         entity.setRefDocRelationType(refDocRelationType);
 179  2
         entity.setRefObjectId(dto.getRefObjectId());
 180  2
         entity.setRefObjectType(refObjectType);
 181  2
         entity.setState(dto.getState());
 182  2
         entity.setTitle(dto.getTitle());
 183  
         
 184  2
         return entity;
 185  
         }
 186  
 
 187  
 }