Clover Coverage Report - KS Common 1.2-SNAPSHOT (Aggregated)
Coverage timestamp: Thu Mar 3 2011 04:36:44 EST
../../../../../../img/srcFileCovDistChart9.png 18% of files have more coverage
77   225   24   7.7
22   150   0.31   10
10     2.4  
1    
 
  BaseAssembler       Line # 39 77 0% 24 17 84.4% 0.8440367
 
  (306)
 
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.common.service.impl;
17   
18    import java.util.ArrayList;
19    import java.util.HashMap;
20    import java.util.List;
21    import java.util.Map;
22   
23    import org.apache.log4j.Logger;
24    import org.kuali.student.common.dao.CrudDao;
25    import org.kuali.student.common.dto.MetaInfo;
26    import org.kuali.student.common.dto.RichTextInfo;
27    import org.kuali.student.common.dto.TypeInfo;
28    import org.kuali.student.common.entity.Attribute;
29    import org.kuali.student.common.entity.AttributeOwner;
30    import org.kuali.student.common.entity.Meta;
31    import org.kuali.student.common.entity.MetaEntity;
32    import org.kuali.student.common.entity.RichText;
33    import org.kuali.student.common.entity.Type;
34    import org.kuali.student.common.entity.Version;
35    import org.kuali.student.common.exceptions.InvalidParameterException;
36    import org.kuali.student.common.versionmanagement.dto.VersionInfo;
37    import org.springframework.beans.BeanUtils;
38   
 
39    public class BaseAssembler {
40   
41    final static Logger logger = Logger.getLogger(BaseAssembler.class);
42   
 
43  10315 toggle public static Map<String, String> toAttributeMap(
44    List<? extends Attribute<?>> attributes) {
45   
46  10315 Map<String, String> attributeInfos = new HashMap<String, String>();
47   
48  10315 for (Attribute<?> attribute : attributes) {
49  4726 attributeInfos.put(attribute.getName(), attribute.getValue());
50    }
51   
52  10315 return attributeInfos;
53    }
54   
 
55  3819 toggle public static <A extends Attribute<O>, O extends AttributeOwner<A>> List<A> toGenericAttributes(
56    Class<A> attributeClass, Map<String, String> attributeMap, O owner,
57    CrudDao dao) throws InvalidParameterException {
58  3819 List<A> attributes = new ArrayList<A>();
59   
60  3819 if(owner.getAttributes()==null){
61  3365 owner.setAttributes(new ArrayList<A>());
62    }
63   
64  3819 Map<String, A> currentAttributes = new HashMap<String,A>();
65   
66    // Find all the old attributes(if the owner is not null)
67  3819 for (A attribute : owner.getAttributes()) {
68  307 currentAttributes.put(attribute.getName(), attribute);
69   
70    }
71   
72    //Clear out the attributes
73  3819 owner.getAttributes().clear();
74   
75    //Update anything that exists, or create a new attribute if it doesn't
76  3819 for (Map.Entry<String, String> attributeEntry : attributeMap.entrySet()) {
77   
78  2525 A attribute;
79  2525 if(currentAttributes.containsKey(attributeEntry.getKey())){
80  279 attribute = currentAttributes.remove(attributeEntry.getKey());
81    }else{
82  2246 try{
83  2246 attribute = attributeClass.newInstance();
84    }catch(Exception e){
85  0 throw new RuntimeException("Error copying attributes.",e);
86    }
87  2246 attribute.setName(attributeEntry.getKey());
88  2246 attribute.setOwner(owner);
89    }
90  2525 attribute.setValue(attributeEntry.getValue());
91  2525 attributes.add(attribute);
92    }
93   
94    //Delete leftovers here if behavior is desired
95   
96  3819 return attributes;
97    }
98   
99    /**
100    * @param <T>
101    * TypeInfo class
102    * @param <S>
103    * Type Class
104    * @param typeInfoClass
105    * the class of the resulting typeInfo object
106    * @param typeEntity
107    * the typeEntity to copy from
108    * @return a new TypeInfo
109    */
 
110  375 toggle public static <T extends TypeInfo, S extends Type<?>> T toGenericTypeInfo(
111    Class<T> typeInfoClass, S typeEntity) {
112  375 if (typeEntity == null) {
113  0 return null;
114    }
115   
116  375 T typeInfo;
117  375 try {
118    // Create a new TypeInfo based on the <T> class and copy the
119    // properties
120  375 typeInfo = typeInfoClass.newInstance();
121  375 BeanUtils.copyProperties(typeEntity, typeInfo,
122    new String[] { "attributes" });
123   
124    // Copy the attributes
125  375 typeInfo.setAttributes(toAttributeMap(typeEntity.getAttributes()));
126   
127    //Copy the description
128  375 typeInfo.setDescr(typeEntity.getDescr());
129   
130  375 return typeInfo;
131   
132    } catch (Exception e) {
133  0 logger.error("Exception occured: ", e);
134    }
135  0 return null;
136   
137    }
138   
 
139  3 toggle public static <T extends TypeInfo, S extends Type<?>> List<T> toGenericTypeInfoList(
140    Class<T> typeInfoClass, List<S> typeEntities) {
141  3 List<T> typeInfoList = new ArrayList<T>();
142  3 if(typeEntities!=null){
143  3 for (S typeEntity : typeEntities) {
144  4 typeInfoList.add(toGenericTypeInfo(typeInfoClass, typeEntity));
145    }
146    }
147  3 return typeInfoList;
148    }
149   
 
150  0 toggle public static List<String> toGenericTypeKeyList( List<? extends Type<?>> typeEntities){
151  0 List<String> typeKeys = new ArrayList<String>();
152  0 if(typeEntities!=null){
153  0 for(Type<?> typeEntity:typeEntities){
154  0 typeKeys.add(typeEntity.getId());
155    }
156    }
157  0 return typeKeys;
158    }
159   
 
160  362 toggle protected static MetaInfo toMetaInfo(MetaEntity metaEntity) {
161  362 if(metaEntity == null){
162  0 return null;
163    }
164  362 return toMetaInfo(metaEntity.getMeta(), metaEntity.getVersionNumber());
165    }
166   
 
167  6654 toggle protected static MetaInfo toMetaInfo(Meta meta, Long versionInd) {
168   
169  6654 MetaInfo metaInfo = new MetaInfo();
170    // If there was a meta passed in then copy the values
171  6654 if (meta != null) {
172  5601 BeanUtils.copyProperties(meta, metaInfo);
173    }
174  6654 if(versionInd==null){
175  58 metaInfo.setVersionInd(null);
176    }else{
177  6596 metaInfo.setVersionInd(versionInd.toString());
178    }
179   
180  6654 return metaInfo;
181    }
182   
 
183  1382 toggle public static <T extends RichText> T toRichText(Class<T> richTextClass, RichTextInfo richTextInfo) {
184  1382 if(richTextInfo == null){
185  186 return null;
186    }
187   
188  1196 T richText = null;
189   
190  1196 try {
191  1196 richText = richTextClass.newInstance();
192  1196 BeanUtils.copyProperties(richTextInfo, richText);
193    } catch (Exception e) {
194  0 throw new RuntimeException(e);
195    }
196   
197  1196 return richText;
198    }
199   
 
200  4313 toggle public static RichTextInfo toRichTextInfo(RichText entity) {
201  4313 if(entity==null){
202  789 return null;
203    }
204   
205  3524 RichTextInfo dto = new RichTextInfo();
206  3524 BeanUtils.copyProperties(entity, dto, new String[] { "id" });
207   
208  3524 return dto;
209    }
210   
 
211  553 toggle public static VersionInfo toVersionInfo(Version version) {
212  553 if(version==null){
213  65 return null;
214    }
215  488 VersionInfo versionInfo = new VersionInfo();
216  488 versionInfo.setCurrentVersionStart(version.getCurrentVersionStart());
217  488 versionInfo.setCurrentVersionEnd(version.getCurrentVersionEnd());
218  488 versionInfo.setSequenceNumber(version.getSequenceNumber());
219  488 versionInfo.setVersionComment(version.getVersionComment());
220  488 versionInfo.setVersionIndId(version.getVersionIndId());
221  488 versionInfo.setVersionedFromId(version.getVersionedFromId());
222   
223  488 return versionInfo;
224    }
225    }