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.r1.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.r1.common.dao.CrudDao;
25  import org.kuali.student.r2.common.dto.MetaInfo;
26  import org.kuali.student.r2.common.dto.RichTextInfo;
27  import org.kuali.student.r1.common.dto.TypeInfo;
28  import org.kuali.student.r1.common.entity.Attribute;
29  import org.kuali.student.r1.common.entity.AttributeOwner;
30  import org.kuali.student.r1.common.entity.Meta;
31  import org.kuali.student.r1.common.entity.MetaEntity;
32  import org.kuali.student.r1.common.entity.RichText;
33  import org.kuali.student.r1.common.entity.Type;
34  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
35  import org.springframework.beans.BeanUtils;
36  
37  @Deprecated
38  public class BaseAssembler {
39      
40      final static Logger logger = Logger.getLogger(BaseAssembler.class);
41  
42  	public static Map<String, String> toAttributeMap(
43  			List<? extends Attribute<?>> attributes) {
44  
45  		Map<String, String> attributeInfos = new HashMap<String, String>();
46  
47  		for (Attribute<?> attribute : attributes) {
48  			attributeInfos.put(attribute.getName(), attribute.getValue());
49  		}
50  
51  		return attributeInfos;
52  	}
53  
54  	public static <A extends Attribute<O>, O extends AttributeOwner<A>> List<A> toGenericAttributes(
55  			Class<A> attributeClass, Map<String, String> attributeMap, O owner,
56  			CrudDao dao) throws InvalidParameterException {
57  		List<A> attributes = new ArrayList<A>();
58  
59  		if(owner.getAttributes()==null){
60  			owner.setAttributes(new ArrayList<A>());
61  		}
62  		
63  		Map<String, A> currentAttributes = new HashMap<String,A>();
64  		
65  		// Find all the old attributes(if the owner is not null)
66  		for (A attribute : owner.getAttributes()) {
67  			currentAttributes.put(attribute.getName(), attribute);
68  			
69  		}
70  		
71  		//Clear out the attributes
72  		owner.getAttributes().clear();
73  	
74  		//Update anything that exists, or create a new attribute if it doesn't
75  		for (Map.Entry<String, String> attributeEntry : attributeMap.entrySet()) {
76  			
77  			A attribute;
78  			if(currentAttributes.containsKey(attributeEntry.getKey())){
79  				attribute = currentAttributes.remove(attributeEntry.getKey());
80  			}else{
81  				try{
82  					attribute = attributeClass.newInstance();
83  				}catch(Exception e){
84  					throw new RuntimeException("Error copying attributes.",e);
85  				}
86  				attribute.setName(attributeEntry.getKey());
87  				attribute.setOwner(owner);
88  			}
89  			attribute.setValue(attributeEntry.getValue());
90  			attributes.add(attribute);
91  		}
92  		
93  		//Delete leftovers here if behavior is desired
94  
95  		return attributes;
96  	}
97  
98  	/**
99  	 * @param <T>
100 	 *            TypeInfo class
101 	 * @param <S>
102 	 *            Type Class
103 	 * @param typeInfoClass
104 	 *            the class of the resulting typeInfo object
105 	 * @param typeEntity
106 	 *            the typeEntity to copy from
107 	 * @return a new TypeInfo
108 	 */
109 	public static <T extends TypeInfo, S extends Type<?>> T toGenericTypeInfo(
110 			Class<T> typeInfoClass, S typeEntity) {
111 		if (typeEntity == null) {
112 			return null;
113 		}
114 
115 		T typeInfo;
116 		try {
117 			// Create a new TypeInfo based on the <T> class and copy the
118 			// properties
119 			typeInfo = typeInfoClass.newInstance();
120 			BeanUtils.copyProperties(typeEntity, typeInfo,
121 					new String[] { "attributes" });
122 
123 			// Copy the attributes
124 			typeInfo.setAttributes(toAttributeMap(typeEntity.getAttributes()));
125 
126 			//Copy the description
127 			typeInfo.setDescr(typeEntity.getDescr());
128 			
129 			return typeInfo;
130 
131 		} catch (Exception e) {
132 			logger.error("Exception occured: ", e);
133 		}
134 		return null;
135 
136 	}
137 
138 	public static <T extends TypeInfo, S extends Type<?>> List<T> toGenericTypeInfoList(
139 			Class<T> typeInfoClass, List<S> typeEntities) {
140 		List<T> typeInfoList = new ArrayList<T>();
141 		if(typeEntities!=null){
142 			for (S typeEntity : typeEntities) {
143 				typeInfoList.add(toGenericTypeInfo(typeInfoClass, typeEntity));
144 			}
145 		}
146 		return typeInfoList;
147 	}
148 	
149 	public static List<String> toGenericTypeKeyList( List<? extends Type<?>> typeEntities){
150 		List<String> typeKeys = new ArrayList<String>();
151 		if(typeEntities!=null){
152 			for(Type<?> typeEntity:typeEntities){
153 				typeKeys.add(typeEntity.getId());
154 			}
155 		}
156 		return typeKeys;
157 	}
158 
159 	protected static MetaInfo toMetaInfo(MetaEntity metaEntity) {
160 		if(metaEntity == null){
161 			return null;
162 		}
163 		return toMetaInfo(metaEntity.getMeta(), metaEntity.getVersionNumber());
164 	}
165 	
166 	protected static MetaInfo toMetaInfo(Meta meta, Long versionInd) {
167 
168 		MetaInfo metaInfo = new MetaInfo();
169 		// If there was a meta passed in then copy the values
170 		if (meta != null) {
171 			BeanUtils.copyProperties(meta, metaInfo);
172 		}
173 		if(versionInd==null){
174 			metaInfo.setVersionInd(null);
175 		}else{
176 			metaInfo.setVersionInd(versionInd.toString());
177 		}
178 
179 		return metaInfo;
180 	}
181 
182 	public static <T extends RichText> T toRichText(Class<T> richTextClass, RichTextInfo richTextInfo) {
183 		if(richTextInfo == null){
184 			return null;
185 		}
186 		
187 		T richText = null;
188 		
189 		try {
190     		richText = richTextClass.newInstance();
191     		BeanUtils.copyProperties(richTextInfo, richText);
192     	} catch (Exception e) {
193             throw new RuntimeException(e);
194         }
195     	
196 		return richText;
197 	}
198 
199 	public static RichTextInfo toRichTextInfo(RichText entity) {
200         if(entity==null){
201             return null;
202         }
203         
204         RichTextInfo dto = new RichTextInfo();
205         BeanUtils.copyProperties(entity, dto, new String[] { "id" });
206         
207         return dto;
208     }
209 
210 }