Clover Coverage Report - KS Common 1.3.0-SNAPSHOT (Aggregated)
Coverage timestamp: Thu Apr 28 2011 06:00:36 EDT
../../../../../../img/srcFileCovDistChart0.png 0% 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 109 0% 0.0
 
No Tests
 
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  0 toggle public static Map<String, String> toAttributeMap(
44    List<? extends Attribute<?>> attributes) {
45   
46  0 Map<String, String> attributeInfos = new HashMap<String, String>();
47   
48  0 for (Attribute<?> attribute : attributes) {
49  0 attributeInfos.put(attribute.getName(), attribute.getValue());
50    }
51   
52  0 return attributeInfos;
53    }
54   
 
55  0 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  0 List<A> attributes = new ArrayList<A>();
59   
60  0 if(owner.getAttributes()==null){
61  0 owner.setAttributes(new ArrayList<A>());
62    }
63   
64  0 Map<String, A> currentAttributes = new HashMap<String,A>();
65   
66    // Find all the old attributes(if the owner is not null)
67  0 for (A attribute : owner.getAttributes()) {
68  0 currentAttributes.put(attribute.getName(), attribute);
69   
70    }
71   
72    //Clear out the attributes
73  0 owner.getAttributes().clear();
74   
75    //Update anything that exists, or create a new attribute if it doesn't
76  0 for (Map.Entry<String, String> attributeEntry : attributeMap.entrySet()) {
77   
78  0 A attribute;
79  0 if(currentAttributes.containsKey(attributeEntry.getKey())){
80  0 attribute = currentAttributes.remove(attributeEntry.getKey());
81    }else{
82  0 try{
83  0 attribute = attributeClass.newInstance();
84    }catch(Exception e){
85  0 throw new RuntimeException("Error copying attributes.",e);
86    }
87  0 attribute.setName(attributeEntry.getKey());
88  0 attribute.setOwner(owner);
89    }
90  0 attribute.setValue(attributeEntry.getValue());
91  0 attributes.add(attribute);
92    }
93   
94    //Delete leftovers here if behavior is desired
95   
96  0 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  0 toggle public static <T extends TypeInfo, S extends Type<?>> T toGenericTypeInfo(
111    Class<T> typeInfoClass, S typeEntity) {
112  0 if (typeEntity == null) {
113  0 return null;
114    }
115   
116  0 T typeInfo;
117  0 try {
118    // Create a new TypeInfo based on the <T> class and copy the
119    // properties
120  0 typeInfo = typeInfoClass.newInstance();
121  0 BeanUtils.copyProperties(typeEntity, typeInfo,
122    new String[] { "attributes" });
123   
124    // Copy the attributes
125  0 typeInfo.setAttributes(toAttributeMap(typeEntity.getAttributes()));
126   
127    //Copy the description
128  0 typeInfo.setDescr(typeEntity.getDescr());
129   
130  0 return typeInfo;
131   
132    } catch (Exception e) {
133  0 logger.error("Exception occured: ", e);
134    }
135  0 return null;
136   
137    }
138   
 
139  0 toggle public static <T extends TypeInfo, S extends Type<?>> List<T> toGenericTypeInfoList(
140    Class<T> typeInfoClass, List<S> typeEntities) {
141  0 List<T> typeInfoList = new ArrayList<T>();
142  0 if(typeEntities!=null){
143  0 for (S typeEntity : typeEntities) {
144  0 typeInfoList.add(toGenericTypeInfo(typeInfoClass, typeEntity));
145    }
146    }
147  0 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  0 toggle protected static MetaInfo toMetaInfo(MetaEntity metaEntity) {
161  0 if(metaEntity == null){
162  0 return null;
163    }
164  0 return toMetaInfo(metaEntity.getMeta(), metaEntity.getVersionNumber());
165    }
166   
 
167  0 toggle protected static MetaInfo toMetaInfo(Meta meta, Long versionInd) {
168   
169  0 MetaInfo metaInfo = new MetaInfo();
170    // If there was a meta passed in then copy the values
171  0 if (meta != null) {
172  0 BeanUtils.copyProperties(meta, metaInfo);
173    }
174  0 if(versionInd==null){
175  0 metaInfo.setVersionInd(null);
176    }else{
177  0 metaInfo.setVersionInd(versionInd.toString());
178    }
179   
180  0 return metaInfo;
181    }
182   
 
183  0 toggle public static <T extends RichText> T toRichText(Class<T> richTextClass, RichTextInfo richTextInfo) {
184  0 if(richTextInfo == null){
185  0 return null;
186    }
187   
188  0 T richText = null;
189   
190  0 try {
191  0 richText = richTextClass.newInstance();
192  0 BeanUtils.copyProperties(richTextInfo, richText);
193    } catch (Exception e) {
194  0 throw new RuntimeException(e);
195    }
196   
197  0 return richText;
198    }
199   
 
200  0 toggle public static RichTextInfo toRichTextInfo(RichText entity) {
201  0 if(entity==null){
202  0 return null;
203    }
204   
205  0 RichTextInfo dto = new RichTextInfo();
206  0 BeanUtils.copyProperties(entity, dto, new String[] { "id" });
207   
208  0 return dto;
209    }
210   
 
211  0 toggle public static VersionInfo toVersionInfo(Version version) {
212  0 if(version==null){
213  0 return null;
214    }
215  0 VersionInfo versionInfo = new VersionInfo();
216  0 versionInfo.setCurrentVersionStart(version.getCurrentVersionStart());
217  0 versionInfo.setCurrentVersionEnd(version.getCurrentVersionEnd());
218  0 versionInfo.setSequenceNumber(version.getSequenceNumber());
219  0 versionInfo.setVersionComment(version.getVersionComment());
220  0 versionInfo.setVersionIndId(version.getVersionIndId());
221  0 versionInfo.setVersionedFromId(version.getVersionedFromId());
222   
223  0 return versionInfo;
224    }
225    }