Coverage Report - org.kuali.student.lum.lrc.service.impl.LrcServiceAssembler
 
Classes in this File Line Coverage Branch Coverage Complexity
LrcServiceAssembler
0%
0/54
0%
0/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  0
         ResultComponentInfo dto = new ResultComponentInfo();
 45  
 
 46  0
         BeanUtils.copyProperties(entity, dto,
 47  
                 new String[] { "resultValues", "desc", "attributes", "type" });
 48  0
         List<String> resultValues = new ArrayList<String>(entity.getResultValues().size());
 49  0
         for (ResultValue rv : entity.getResultValues()) {
 50  0
                 resultValues.add(rv.getValue());
 51  
         }
 52  0
         dto.setDesc(toRichTextInfo(entity.getDescr()));
 53  0
         dto.setResultValues(resultValues);
 54  0
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 55  0
                 dto.setMetaInfo(toMetaInfo(entity.getMeta(), entity.getVersionNumber()));
 56  0
         dto.setType(entity.getType().getId());
 57  0
         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  0
         ResultComponentTypeInfo dto = new ResultComponentTypeInfo();
 70  
 
 71  0
         BeanUtils.copyProperties(entity, dto,
 72  
                 new String[] {"attributes" });
 73  0
         dto.setAttributes(toAttributeMap(entity.getAttributes()));
 74  
 
 75  0
         return dto;
 76  
     }
 77  
     public static List<ResultComponentTypeInfo> toResultComponentTypeInfos(List<ResultComponentType> entites) {
 78  0
         List<ResultComponentTypeInfo> dtos = new ArrayList<ResultComponentTypeInfo>(entites.size());
 79  0
         for (ResultComponentType entity : entites) {
 80  0
             dtos.add(toResultComponentTypeInfo(entity));
 81  
         }
 82  0
         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  0
         ResultComponent entity = new ResultComponent();
 97  0
         toResultComponent(entity, dto, lrcDao);
 98  0
         return entity;
 99  
     }
 100  
 
 101  
     public static void toResultComponent(ResultComponent entity, ResultComponentInfo dto, LrcDao lrcDao) throws DoesNotExistException, InvalidParameterException, DataValidationErrorException {
 102  0
         BeanUtils.copyProperties(dto, entity,
 103  
                 new String[] { "desc", "resultValues", "attributes", "metaInfo", "type" });
 104  0
         ResultComponentType type = lrcDao.fetch(ResultComponentType.class, dto.getType());
 105  0
         entity.setType(type);
 106  
         
 107  0
         entity.setDescr(toRichText(LrcRichText.class, dto.getDesc()));
 108  
 
 109  
         //Create new Result Values, and delete unwanted ones, keep the overlap 
 110  0
         List<ResultValue> resultValues = new ArrayList<ResultValue>(dto.getResultValues().size());
 111  0
         Map<String,ResultValue> currentResultValues = new HashMap<String,ResultValue>();
 112  0
         if(entity.getResultValues()!=null){
 113  0
                 for(ResultValue resultValue:entity.getResultValues()){
 114  0
                         currentResultValues.put(resultValue.getValue(),resultValue);
 115  
                 }
 116  
         }
 117  
         
 118  0
         for(String value:dto.getResultValues()){
 119  0
                 if(!currentResultValues.containsKey(value)){
 120  0
                         ResultValue newResultValue = new ResultValue();
 121  0
                         newResultValue.setValue(value);
 122  0
                         newResultValue.setResultComponent(entity);
 123  0
                         resultValues.add(newResultValue);
 124  0
                 }else{
 125  0
                         resultValues.add(currentResultValues.remove(value));
 126  
                 }
 127  
         }
 128  
         //Delete leftovers
 129  0
         for(Entry<String,ResultValue> entry:currentResultValues.entrySet()){
 130  0
                 lrcDao.delete(entry.getValue());
 131  
         }
 132  
         
 133  0
         entity.setResultValues(resultValues);
 134  0
         entity.setAttributes(toGenericAttributes(ResultComponentAttribute.class, dto.getAttributes(), entity, lrcDao));
 135  0
     }
 136  
 }