Coverage Report - org.kuali.student.core.comment.service.impl.CommentServiceAssembler
 
Classes in this File Line Coverage Branch Coverage Complexity
CommentServiceAssembler
0%
0/96
0%
0/22
2.286
 
 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.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import org.kuali.student.common.dto.ReferenceTypeInfo;
 22  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 23  
 import org.kuali.student.common.exceptions.InvalidParameterException;
 24  
 import org.kuali.student.common.service.impl.BaseAssembler;
 25  
 import org.kuali.student.core.comment.dao.CommentDao;
 26  
 import org.kuali.student.core.comment.dto.CommentInfo;
 27  
 import org.kuali.student.core.comment.dto.CommentTypeInfo;
 28  
 import org.kuali.student.core.comment.dto.TagInfo;
 29  
 import org.kuali.student.core.comment.dto.TagTypeInfo;
 30  
 import org.kuali.student.core.comment.entity.Comment;
 31  
 import org.kuali.student.core.comment.entity.CommentAttribute;
 32  
 import org.kuali.student.core.comment.entity.CommentRichText;
 33  
 import org.kuali.student.core.comment.entity.CommentType;
 34  
 import org.kuali.student.core.comment.entity.Reference;
 35  
 import org.kuali.student.core.comment.entity.ReferenceType;
 36  
 import org.kuali.student.core.comment.entity.Tag;
 37  
 import org.kuali.student.core.comment.entity.TagAttribute;
 38  
 import org.kuali.student.core.comment.entity.TagType;
 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 CommentServiceAssembler extends BaseAssembler {
 48  
 
 49  
     public static CommentInfo toCommentInfo(Comment entity) {
 50  0
         CommentInfo dto = new CommentInfo();
 51  
 
 52  0
         BeanUtils.copyProperties(entity, dto,
 53  
                 new String[] { "commentText", "attributes", "type", "metaInfo" });
 54  
 
 55  0
         dto.setCommentText(toRichTextInfo(entity.getCommentText()));
 56  0
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 57  0
         dto.setMetaInfo(toMetaInfo(entity.getMeta(), entity.getVersionNumber()));
 58  0
         dto.setType(entity.getType().getId());
 59  
 
 60  0
         return dto;
 61  
     }
 62  
 
 63  
     public static List<CommentInfo> toCommentInfos(List<Comment> entities) {
 64  0
         List<CommentInfo> dtos = new ArrayList<CommentInfo>(entities.size());
 65  0
         for (Comment entity : entities) {
 66  0
             dtos.add(toCommentInfo(entity));
 67  
         }
 68  0
         return dtos;
 69  
     }
 70  
 
 71  
     public static TagInfo toTagInfo(Tag entity) {
 72  0
         TagInfo dto = new TagInfo();
 73  0
         BeanUtils.copyProperties(entity, dto, new String[]{"attributes","type","reference"});
 74  0
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 75  0
         dto.setType(entity.getType().getId());
 76  0
         dto.setReferenceId(entity.getReferennce().getReferenceId());
 77  0
         dto.setReferenceTypeKey(entity.getReferennce().getReferenceType().getId());
 78  0
         return dto;
 79  
 
 80  
     }
 81  
 
 82  
     public static List<TagInfo> toTagInfos(List<Tag> entries){
 83  0
         List<TagInfo> dtos = new ArrayList<TagInfo>(entries.size());
 84  0
         for(Tag entry: entries){
 85  0
             dtos.add(toTagInfo(entry));
 86  
         }
 87  0
         return dtos;
 88  
     }
 89  
 
 90  
     public static TagTypeInfo toTagTypeInfo(TagType entity){
 91  0
         TagTypeInfo dto = new TagTypeInfo();
 92  0
         BeanUtils.copyProperties(entity, dto,new String[]{"attributes"});
 93  0
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 94  0
         return dto;
 95  
     }
 96  
 
 97  
     public static List<TagTypeInfo> toTagTypeInfos(List<TagType> entries){
 98  0
         List<TagTypeInfo> dtos = new ArrayList<TagTypeInfo>(entries.size());
 99  0
         for(TagType entity: entries){
 100  0
             dtos.add(toTagTypeInfo(entity));
 101  
         }
 102  
 
 103  0
         return dtos;
 104  
     }
 105  
 
 106  
     public static List<CommentTypeInfo> toCommentTypeInfos(List<CommentType> entities) {
 107  0
         List<CommentTypeInfo> dtos = new ArrayList<CommentTypeInfo>(entities.size());
 108  0
         for (CommentType entity : entities) {
 109  0
             dtos.add(toCommentTypeInfo(entity));
 110  
         }
 111  0
         return dtos;
 112  
     }
 113  
 
 114  
     private static CommentTypeInfo toCommentTypeInfo(CommentType entity) {
 115  0
             CommentTypeInfo dto = new CommentTypeInfo();
 116  0
         BeanUtils.copyProperties(entity, dto,new String[]{"attributes"});
 117  0
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 118  0
         return dto;
 119  
     }
 120  
 
 121  
     public static Comment toComment(boolean isUpdate,CommentInfo dto,CommentDao dao) throws InvalidParameterException, DoesNotExistException{
 122  
 
 123  0
         Comment entity = new Comment();
 124  0
         BeanUtils.copyProperties(dto,entity,new String[]{"reference","commentText", "attributes", "type", "metaInfo"});
 125  0
         entity.setAttributes(toGenericAttributes(CommentAttribute.class,dto.getAttributes(),entity,dao));
 126  0
         Reference reference = dao.getReference(dto.getReferenceId(), dto.getReferenceTypeKey());
 127  0
         if (reference == null) {
 128  0
             throw new InvalidParameterException(
 129  
                     "Reference does not exist for id: " + dto.getReferenceId());
 130  
         }
 131  0
         entity.setReference(reference);
 132  0
         CommentType type = dao.fetch(CommentType.class,dto.getType());
 133  0
         if (type == null) {
 134  0
             throw new InvalidParameterException(
 135  
                     "Tag Type does not exist for id: " + dto.getType());
 136  
         }
 137  0
         entity.setType(type);
 138  0
         entity.setCommentText(toRichText(CommentRichText.class, dto.getCommentText()));
 139  0
         entity.setAttributes(toGenericAttributes(CommentAttribute.class, dto.getAttributes(), entity, dao));
 140  0
                 dto.setMetaInfo(toMetaInfo(entity.getMeta(), entity.getVersionNumber()));
 141  0
         return entity;
 142  
     }
 143  
     public static Tag toTag(boolean isUpdate,TagInfo dto, CommentDao dao) throws InvalidParameterException, DoesNotExistException{
 144  
 
 145  0
         Tag entity = new Tag();
 146  0
         BeanUtils.copyProperties(dto,entity,new String[]{"reference","type","attributes"});
 147  0
         entity.setAttributes(toGenericAttributes(TagAttribute.class,dto.getAttributes(),entity,dao));
 148  
 
 149  0
         Reference reference = dao.getReference(dto.getReferenceId(), dto.getReferenceTypeKey());
 150  0
         if (reference == null) {
 151  0
             throw new InvalidParameterException(
 152  
                     "Reference does not exist for id: " + dto.getReferenceId());
 153  
         }
 154  0
         entity.setReference(reference);
 155  0
         TagType type = dao.fetch(TagType.class,dto.getType());
 156  0
         if (type == null) {
 157  0
             throw new InvalidParameterException(
 158  
                     "Tag Type does not exist for id: " + dto.getType());
 159  
         }
 160  0
         entity.setType(type);
 161  0
         return entity;
 162  
     }
 163  
 
 164  
     public static Comment toComment(String referenceId,
 165  
                         String referenceTypeKey, CommentInfo dto,
 166  
                         CommentDao commentDao) throws InvalidParameterException, DoesNotExistException {
 167  0
             Comment entity = new Comment();
 168  
 
 169  0
             return toComment(entity, referenceId, referenceTypeKey, dto, commentDao);
 170  
     }
 171  
 
 172  
     public static Comment toComment(Comment entity, String referenceId,
 173  
                         String referenceTypeKey, CommentInfo dto, CommentDao commentDao)
 174  
                         throws InvalidParameterException, DoesNotExistException {
 175  0
                 BeanUtils.copyProperties(dto, entity, new String[] { "id, reference",
 176  
                                 "commentText", "attributes", "type", "metaInfo" });
 177  0
                 entity.setCommentText(toRichText(CommentRichText.class, dto.getCommentText()));
 178  
 
 179  0
                 entity.setAttributes(toGenericAttributes(CommentAttribute.class, dto
 180  
                                 .getAttributes(), entity, commentDao));
 181  0
                 CommentType type = commentDao.fetch(CommentType.class, dto.getType());
 182  0
                 if (type == null) {
 183  0
                         throw new InvalidParameterException(
 184  
                                         "Tag Type does not exist for id: " + dto.getType());
 185  
                 }
 186  0
                 entity.setType(type);
 187  0
                 dto.setMetaInfo(toMetaInfo(entity.getMeta(), entity.getVersionNumber()));
 188  
 
 189  0
                 Reference reference = commentDao.getReference(referenceId,
 190  
                                 referenceTypeKey);
 191  0
                 if (reference == null) {
 192  0
             reference = new Reference();
 193  0
             reference.setReferenceId(referenceId);
 194  
                         try {
 195  0
                                 ReferenceType referenceType = commentDao.fetch(ReferenceType.class, referenceTypeKey);
 196  0
                     reference.setReferenceType(referenceType);
 197  0
                     commentDao.create(reference);
 198  0
                         } catch (DoesNotExistException e) {
 199  0
                                 throw new InvalidParameterException(e.getMessage());
 200  0
                         }
 201  
 
 202  
                 }
 203  0
                 entity.setReference(reference);
 204  
 
 205  0
                 return entity;
 206  
     }
 207  
 
 208  
         public static List<ReferenceTypeInfo> toReferenceTypeInfos(
 209  
                         List<ReferenceType> entities) {
 210  0
                 List<ReferenceTypeInfo> dtos = new ArrayList<ReferenceTypeInfo>(entities.size());
 211  0
                 for (ReferenceType entity : entities) {
 212  0
                         dtos.add(toReferenceType(entity));
 213  
                 }
 214  0
                 return dtos;
 215  
 
 216  
         }
 217  
 
 218  
         private static ReferenceTypeInfo toReferenceType(ReferenceType entity) {
 219  0
                 ReferenceTypeInfo dto = new ReferenceTypeInfo();
 220  
 
 221  0
         BeanUtils.copyProperties(entity, dto,new String[]{"attributes"});
 222  0
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 223  0
         return dto;
 224  
         }
 225  
         
 226  
 }