Coverage Report - org.kuali.student.lum.lrc.service.impl.LrcServiceAssembler
 
Classes in this File Line Coverage Branch Coverage Complexity
LrcServiceAssembler
79%
43/54
81%
13/16
2.143
 
 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.lum.lrc.service.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.HashMap;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 import java.util.Map.Entry;
 23  
 
 24  
 import org.kuali.student.common.exceptions.DataValidationErrorException;
 25  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 26  
 import org.kuali.student.common.exceptions.InvalidParameterException;
 27  
 import org.kuali.student.common.service.impl.BaseAssembler;
 28  
 import org.kuali.student.lum.lrc.dao.LrcDao;
 29  
 import org.kuali.student.lum.lrc.dto.ResultComponentInfo;
 30  
 import org.kuali.student.lum.lrc.dto.ResultComponentTypeInfo;
 31  
 import org.kuali.student.lum.lrc.dto.ScaleInfo;
 32  
 import org.kuali.student.lum.lrc.entity.LrcRichText;
 33  
 import org.kuali.student.lum.lrc.entity.ResultComponent;
 34  
 import org.kuali.student.lum.lrc.entity.ResultComponentAttribute;
 35  
 import org.kuali.student.lum.lrc.entity.ResultComponentType;
 36  
 import org.kuali.student.lum.lrc.entity.ResultValue;
 37  
 import org.kuali.student.lum.lrc.entity.Scale;
 38  
 import org.springframework.beans.BeanUtils;
 39  
 
 40  0
 public class LrcServiceAssembler extends BaseAssembler {
 41  
    
 42  
 
 43  
     public static ResultComponentInfo toResultComponentInfo(ResultComponent entity) {
 44  111
         ResultComponentInfo dto = new ResultComponentInfo();
 45  
 
 46  111
         BeanUtils.copyProperties(entity, dto,
 47  
                 new String[] { "resultValues", "desc", "attributes", "type" });
 48  111
         List<String> resultValues = new ArrayList<String>(entity.getResultValues().size());
 49  111
         for (ResultValue rv : entity.getResultValues()) {
 50  272
                 resultValues.add(rv.getValue());
 51  
         }
 52  111
         dto.setDesc(toRichTextInfo(entity.getDescr()));
 53  111
         dto.setResultValues(resultValues);
 54  111
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 55  111
                 dto.setMetaInfo(toMetaInfo(entity.getMeta(), entity.getVersionNumber()));
 56  111
         dto.setType(entity.getType().getId());
 57  111
         return dto;
 58  
     }
 59  
 
 60  
     public static List<ResultComponentInfo> toReListComonentInfos(List<ResultComponent> entities) {
 61  0
         List<ResultComponentInfo> dtos = new ArrayList<ResultComponentInfo>(entities.size());
 62  0
         for (ResultComponent entity : entities) {
 63  0
             dtos.add(toResultComponentInfo(entity));
 64  
         }
 65  0
         return dtos;
 66  
     }
 67  
 
 68  
     public static ResultComponentTypeInfo toResultComponentTypeInfo(ResultComponentType entity) {
 69  71
         ResultComponentTypeInfo dto = new ResultComponentTypeInfo();
 70  
 
 71  71
         BeanUtils.copyProperties(entity, dto,
 72  
                 new String[] {"attributes" });
 73  71
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 74  
 
 75  71
         return dto;
 76  
     }
 77  
     public static List<ResultComponentTypeInfo> toResultComponentTypeInfos(List<ResultComponentType> entites) {
 78  10
         List<ResultComponentTypeInfo> dtos = new ArrayList<ResultComponentTypeInfo>(entites.size());
 79  10
         for (ResultComponentType entity : entites) {
 80  70
             dtos.add(toResultComponentTypeInfo(entity));
 81  
         }
 82  10
         return dtos;
 83  
     }
 84  
 
 85  
 
 86  
     public static ScaleInfo toScaleInfo(Scale entity) {
 87  0
        ScaleInfo dto = new ScaleInfo();
 88  0
        BeanUtils.copyProperties(entity, dto,
 89  
                new String[] { "desc", "attributes" });
 90  0
        dto.setDesc(toRichTextInfo(entity.getDesc()));
 91  0
        dto.setAttributes(toAttributeMap(entity.getAttributes()));
 92  0
        return dto;
 93  
     }
 94  
 
 95  
     public static ResultComponent toResultComponent(String resultComponentTypeKey, ResultComponentInfo dto, LrcDao lrcDao) throws DoesNotExistException, InvalidParameterException, DataValidationErrorException {
 96  5
         ResultComponent entity = new ResultComponent();
 97  5
         toResultComponent(entity, dto, lrcDao);
 98  5
         return entity;
 99  
     }
 100  
 
 101  
     public static void toResultComponent(ResultComponent entity, ResultComponentInfo dto, LrcDao lrcDao) throws DoesNotExistException, InvalidParameterException, DataValidationErrorException {
 102  7
         BeanUtils.copyProperties(dto, entity,
 103  
                 new String[] { "desc", "resultValues", "attributes", "metaInfo", "type" });
 104  7
         ResultComponentType type = lrcDao.fetch(ResultComponentType.class, dto.getType());
 105  7
         entity.setType(type);
 106  
         
 107  7
         entity.setDescr(toRichText(LrcRichText.class, dto.getDesc()));
 108  
 
 109  
         //Create new Result Values, and delete unwanted ones, keep the overlap 
 110  7
         List<ResultValue> resultValues = new ArrayList<ResultValue>(dto.getResultValues().size());
 111  7
         Map<String,ResultValue> currentResultValues = new HashMap<String,ResultValue>();
 112  7
         if(entity.getResultValues()!=null){
 113  2
                 for(ResultValue resultValue:entity.getResultValues()){
 114  3
                         currentResultValues.put(resultValue.getValue(),resultValue);
 115  
                 }
 116  
         }
 117  
         
 118  7
         for(String value:dto.getResultValues()){
 119  21
                 if(!currentResultValues.containsKey(value)){
 120  18
                         ResultValue newResultValue = new ResultValue();
 121  18
                         newResultValue.setValue(value);
 122  18
                         newResultValue.setResultComponent(entity);
 123  18
                         resultValues.add(newResultValue);
 124  18
                 }else{
 125  3
                         resultValues.add(currentResultValues.remove(value));
 126  
                 }
 127  
         }
 128  
         //Delete leftovers
 129  7
         for(Entry<String,ResultValue> entry:currentResultValues.entrySet()){
 130  0
                 lrcDao.delete(entry.getValue());
 131  
         }
 132  
         
 133  7
         entity.setResultValues(resultValues);
 134  7
         entity.setAttributes(toGenericAttributes(ResultComponentAttribute.class, dto.getAttributes(), entity, lrcDao));
 135  7
     }
 136  
 }