Coverage Report - org.kuali.student.core.document.service.impl.DocumentServiceAssembler
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentServiceAssembler
0%
0/78
0%
0/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  0
         DocumentInfo dto = new DocumentInfo();
 51  
 
 52  0
         BeanUtils.copyProperties(entity, dto,
 53  
                 new String[] { "desc", "attributes", "metaInfo","type", "categoryList", "document" });
 54  0
         dto.setDesc(toRichTextInfo(entity.getDescr()));
 55  0
         dto.setMetaInfo(toMetaInfo(entity));
 56  0
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 57  0
         dto.setType(entity.getType().getId());
 58  0
         dto.setDocumentBinaryInfo(new DocumentBinaryInfo());
 59  0
         dto.getDocumentBinaryInfo().setBinary(entity.getDocument());
 60  0
         return dto;
 61  
     }
 62  
     
 63  
     public static List<DocumentInfo> toDocumentInfos(List<Document> entities) {
 64  0
         List<DocumentInfo> dtos = new ArrayList<DocumentInfo>();
 65  0
         if(entities!=null){
 66  0
                 for (Document entity : entities) {
 67  0
                         dtos.add(toDocumentInfo(entity));
 68  
                 }
 69  
                }
 70  0
         return dtos;
 71  
     }
 72  
 
 73  
     public static Document toDocument(Document entity, DocumentInfo dto, DocumentDao dao) throws InvalidParameterException {
 74  0
         BeanUtils.copyProperties(dto, entity,
 75  
                 new String[] { "desc", "attributes", "metaInfo", "type", "id", "documentBinaryInfo" });
 76  0
         entity.setDescr(toRichText(DocumentRichText.class, dto.getDesc()));
 77  0
         entity.setDocument(dto.getDocumentBinaryInfo().getBinary());
 78  0
         entity.setAttributes(toGenericAttributes(DocumentAttribute.class, dto.getAttributes(), entity, dao));
 79  0
         return entity;
 80  
     }
 81  
 
 82  
     public static DocumentCategoryInfo toDocumentCategoryInfo(DocumentCategory entity) {
 83  0
         DocumentCategoryInfo dto = new DocumentCategoryInfo();
 84  
 
 85  0
         BeanUtils.copyProperties(entity, dto,
 86  
                 new String[] { "desc", "attributes", "type", "documents" });
 87  0
         dto.setDesc(toRichTextInfo(entity.getDescr()));
 88  0
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 89  0
         return dto;
 90  
     }
 91  
 
 92  
     public static List<DocumentCategoryInfo> toDocumentCategoryInfos(List<DocumentCategory> entities) {
 93  0
         List<DocumentCategoryInfo> dtos = new ArrayList<DocumentCategoryInfo>();
 94  0
         if(entities!=null){
 95  0
                 for (DocumentCategory entity : entities) {
 96  0
                         dtos.add(toDocumentCategoryInfo(entity));
 97  
                 }
 98  
         }
 99  0
         return dtos;
 100  
     }
 101  
 
 102  
         public static RefDocRelationInfo toRefDocRelationInfo(RefDocRelation entity) {
 103  0
                 RefDocRelationInfo dto = new RefDocRelationInfo();
 104  
                 
 105  0
                 dto.setAttributes(toAttributeMap(entity.getAttributes()));
 106  0
                 dto.setDesc(toRichTextInfo(entity.getDescr()));
 107  0
                 dto.setDocumentId(entity.getDocument().getId());
 108  0
                 dto.setEffectiveDate(entity.getEffectiveDate());
 109  0
                 dto.setExpirationDate(entity.getExpirationDate());
 110  0
                 dto.setId(entity.getId());
 111  0
                 dto.setMetaInfo(toMetaInfo(entity));
 112  0
                 dto.setRefObjectId(entity.getRefObjectId());
 113  0
                 dto.setRefObjectTypeKey(entity.getRefObjectType().getId());
 114  0
                 dto.setState(entity.getState());
 115  0
                 dto.setTitle(entity.getTitle());
 116  0
                 dto.setType(entity.getRefDocRelationType().getId());
 117  
                 
 118  0
                 return dto;
 119  
         }
 120  
 
 121  
         public static List<RefDocRelationInfo> toRefDocRelationInfos(
 122  
                         List<RefDocRelation> entities) {
 123  0
                 List<RefDocRelationInfo> dtos = new ArrayList<RefDocRelationInfo>();
 124  0
                 if(entities!=null){
 125  0
                         for(RefDocRelation entity:entities){
 126  0
                                 dtos.add(toRefDocRelationInfo(entity));
 127  
                         }
 128  
                 }
 129  0
                 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  0
                 RefObjectType refObjectType = null; 
 137  
         RefDocRelationType refDocRelationType;
 138  
         Document document;
 139  
         
 140  
         //Get the relation type
 141  
         try{
 142  0
                 refDocRelationType = dao.fetch(RefDocRelationType.class, dto.getType());
 143  0
         }catch(DoesNotExistException e){
 144  0
                 throw new InvalidParameterException("RefDocRelationType does not exist for key:" + dto.getType());
 145  0
         } 
 146  
         
 147  
         //Check that the relation type has subtypes
 148  0
         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  0
         for(RefObjectSubType subType:refDocRelationType.getRefObjectSubTypes()){
 154  0
                 if(dto.getRefObjectTypeKey().equals(subType.getRefObjectType().getId())){
 155  0
                         refObjectType = subType.getRefObjectType();
 156  0
                         break;
 157  
                 }
 158  
         }
 159  0
         if(refObjectType == null){
 160  0
                 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  0
                 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  0
         } 
 170  
         
 171  
         //Copy the fields
 172  0
         entity.setAttributes(toGenericAttributes(RefDocRelationAttribute.class, dto.getAttributes(), entity, dao));
 173  0
         entity.setDescr(toRichText(DocumentRichText.class, dto.getDesc()));
 174  0
         entity.setDocument(document);
 175  0
         entity.setEffectiveDate(dto.getEffectiveDate());
 176  0
         entity.setExpirationDate(dto.getExpirationDate());
 177  0
         entity.setId(dto.getId());
 178  0
         entity.setRefDocRelationType(refDocRelationType);
 179  0
         entity.setRefObjectId(dto.getRefObjectId());
 180  0
         entity.setRefObjectType(refObjectType);
 181  0
         entity.setState(dto.getState());
 182  0
         entity.setTitle(dto.getTitle());
 183  
         
 184  0
         return entity;
 185  
         }
 186  
 
 187  
 }