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