View Javadoc

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