1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.kuali.student.r2.common.service.impl; |
16 | |
|
17 | |
import java.util.ArrayList; |
18 | |
import java.util.HashMap; |
19 | |
import java.util.List; |
20 | |
import java.util.Map; |
21 | |
|
22 | |
import org.apache.log4j.Logger; |
23 | |
import org.kuali.student.common.dao.CrudDao; |
24 | |
|
25 | |
import org.kuali.student.common.entity.Attribute; |
26 | |
import org.kuali.student.common.entity.AttributeOwner; |
27 | |
import org.kuali.student.common.entity.Meta; |
28 | |
import org.kuali.student.common.entity.MetaEntity; |
29 | |
import org.kuali.student.common.entity.RichText; |
30 | |
import org.kuali.student.common.entity.Type; |
31 | |
import org.kuali.student.common.entity.Version; |
32 | |
|
33 | |
import org.kuali.student.r2.common.dto.AttributeInfo; |
34 | |
import org.kuali.student.r2.common.dto.MetaInfo; |
35 | |
import org.kuali.student.r2.common.dto.RichTextInfo; |
36 | |
import org.kuali.student.r2.common.exceptions.InvalidParameterException; |
37 | |
import org.kuali.student.r2.common.exceptions.VersionMismatchException; |
38 | |
import org.kuali.student.r2.common.util.RichTextHelper; |
39 | |
import org.kuali.student.r2.core.type.dto.TypeInfo; |
40 | |
import org.kuali.student.r2.core.versionmanagement.dto.VersionInfo; |
41 | |
import org.springframework.beans.BeanUtils; |
42 | |
|
43 | 0 | public class BaseAssembler { |
44 | |
|
45 | 0 | final static Logger logger = Logger.getLogger(BaseAssembler.class); |
46 | |
|
47 | |
public static List<AttributeInfo> toAttributeMap( |
48 | |
List<? extends Attribute<?>> attributes) { |
49 | 0 | List<AttributeInfo> attributeInfos = new ArrayList<AttributeInfo>(attributes.size()); |
50 | 0 | for (Attribute<?> attribute : attributes) { |
51 | 0 | AttributeInfo info = new AttributeInfo(); |
52 | 0 | info.setId(attribute.getId()); |
53 | 0 | info.setKey(attribute.getName()); |
54 | 0 | info.setValue(attribute.getValue()); |
55 | 0 | attributeInfos.add(info); |
56 | 0 | } |
57 | 0 | return attributeInfos; |
58 | |
} |
59 | |
|
60 | |
public static <A extends Attribute<O>, O extends AttributeOwner<A>> List<A> toGenericAttributes( |
61 | |
Class<A> attributeClass, List<? extends org.kuali.student.r2.common.infc.Attribute> attributeMap, O owner, |
62 | |
CrudDao dao) throws InvalidParameterException { |
63 | 0 | List<A> updatedAttributes = new ArrayList<A>(); |
64 | |
|
65 | 0 | if (owner.getAttributes() == null) { |
66 | 0 | owner.setAttributes(new ArrayList<A>()); |
67 | |
} |
68 | |
|
69 | 0 | Map<String, A> currentAttributes = new HashMap<String, A>(); |
70 | |
|
71 | |
|
72 | 0 | for (A attribute : owner.getAttributes()) { |
73 | 0 | currentAttributes.put(attribute.getId(), attribute); |
74 | |
} |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | 0 | for (org.kuali.student.r2.common.infc.Attribute attr : attributeMap) { |
81 | 0 | A attribute = null; |
82 | 0 | if (attr.getId() != null) { |
83 | 0 | if (!currentAttributes.containsKey(attr.getId())) { |
84 | 0 | throw new InvalidParameterException(attr.getId()); |
85 | |
|
86 | |
} |
87 | 0 | attribute = currentAttributes.remove(attr.getId()); |
88 | 0 | attribute.setName(attr.getKey()); |
89 | 0 | attribute.setValue(attr.getValue()); |
90 | 0 | updatedAttributes.add(attribute); |
91 | 0 | continue; |
92 | |
} |
93 | |
try { |
94 | 0 | attribute = attributeClass.newInstance(); |
95 | 0 | } catch (Exception e) { |
96 | 0 | throw new RuntimeException("Error copying attributes.", e); |
97 | 0 | } |
98 | 0 | attribute.setName(attr.getKey()); |
99 | 0 | attribute.setValue(attr.getValue()); |
100 | 0 | attribute.setOwner(owner); |
101 | 0 | updatedAttributes.add(attribute); |
102 | 0 | } |
103 | |
|
104 | 0 | return updatedAttributes; |
105 | |
} |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
public static <S extends Type<?>> TypeInfo toGenericTypeInfo(Class clazz, |
114 | |
S typeEntity) { |
115 | 0 | if (typeEntity == null) { |
116 | 0 | return null; |
117 | |
} |
118 | |
|
119 | 0 | TypeInfo info = new TypeInfo(); |
120 | 0 | info.setKey(typeEntity.getId()); |
121 | 0 | info.setDescr(new RichTextHelper().fromPlain(typeEntity.getDescr())); |
122 | 0 | info.setEffectiveDate(typeEntity.getEffectiveDate()); |
123 | 0 | info.setExpirationDate(typeEntity.getExpirationDate()); |
124 | 0 | info.setName(typeEntity.getName()); |
125 | |
|
126 | 0 | info.setRefObjectUri(typeEntity.getClass().getName()); |
127 | |
|
128 | 0 | info.setAttributes(toAttributeMap(typeEntity.getAttributes())); |
129 | |
|
130 | 0 | info.setDescr(new RichTextHelper().fromPlain(typeEntity.getDescr())); |
131 | 0 | return info; |
132 | |
} |
133 | |
|
134 | |
public static <S extends Type<?>> List<TypeInfo> toGenericTypeInfoList( |
135 | |
Class clazz, List<S> typeEntities) { |
136 | 0 | List<TypeInfo> infos = new ArrayList<TypeInfo>(); |
137 | 0 | if (typeEntities != null) { |
138 | 0 | for (S typeEntity : typeEntities) { |
139 | 0 | infos.add(toGenericTypeInfo(clazz, typeEntity)); |
140 | |
} |
141 | |
} |
142 | 0 | return infos; |
143 | |
} |
144 | |
|
145 | |
public static List<String> toGenericTypeKeyList(List<? extends Type<?>> typeEntities) { |
146 | 0 | List<String> typeKeys = new ArrayList<String>(); |
147 | 0 | if (typeEntities != null) { |
148 | 0 | for (Type<?> typeEntity : typeEntities) { |
149 | 0 | typeKeys.add(typeEntity.getId()); |
150 | |
} |
151 | |
} |
152 | 0 | return typeKeys; |
153 | |
} |
154 | |
|
155 | |
protected static MetaInfo toMetaInfo(MetaEntity metaEntity) { |
156 | 0 | if (metaEntity == null) { |
157 | 0 | return null; |
158 | |
} |
159 | 0 | return toMetaInfo(metaEntity.getMeta(), metaEntity.getVersionNumber()); |
160 | |
} |
161 | |
|
162 | |
protected static MetaInfo toMetaInfo(Meta meta, Long versionInd) { |
163 | |
|
164 | 0 | MetaInfo metaInfo = new MetaInfo(); |
165 | |
|
166 | 0 | if (meta != null) { |
167 | 0 | BeanUtils.copyProperties(meta, metaInfo); |
168 | |
} |
169 | 0 | if (versionInd == null) { |
170 | 0 | metaInfo.setVersionInd(null); |
171 | |
} else { |
172 | 0 | metaInfo.setVersionInd(versionInd.toString()); |
173 | |
} |
174 | |
|
175 | 0 | return metaInfo; |
176 | |
} |
177 | |
|
178 | |
public static <T extends RichText> T toRichText(Class<T> richTextClass, RichTextInfo richTextInfo) { |
179 | 0 | if (richTextInfo == null) { |
180 | 0 | return null; |
181 | |
} |
182 | |
|
183 | 0 | T richText = null; |
184 | |
|
185 | |
try { |
186 | 0 | richText = richTextClass.newInstance(); |
187 | 0 | BeanUtils.copyProperties(richTextInfo, richText); |
188 | 0 | } catch (Exception e) { |
189 | 0 | throw new RuntimeException(e); |
190 | 0 | } |
191 | 0 | return richText; |
192 | |
} |
193 | |
|
194 | |
public static RichTextInfo toRichTextInfo(RichText entity) { |
195 | 0 | if (entity == null) { |
196 | 0 | return null; |
197 | |
} |
198 | |
|
199 | 0 | RichTextInfo dto = new RichTextInfo(); |
200 | 0 | dto.setFormatted(entity.getFormatted()); |
201 | 0 | dto.setPlain(entity.getPlain()); |
202 | 0 | return dto; |
203 | |
} |
204 | |
|
205 | |
public static VersionInfo toVersionInfo(Version version) { |
206 | 0 | if (version == null) { |
207 | 0 | return null; |
208 | |
} |
209 | 0 | VersionInfo versionInfo = new VersionInfo(); |
210 | 0 | versionInfo.setCurrentVersionStart(version.getCurrentVersionStart()); |
211 | 0 | versionInfo.setCurrentVersionEnd(version.getCurrentVersionEnd()); |
212 | 0 | versionInfo.setSequenceNumber(version.getSequenceNumber()); |
213 | 0 | versionInfo.setVersionComment(version.getVersionComment()); |
214 | 0 | versionInfo.setVersionIndId(version.getVersionIndId()); |
215 | 0 | versionInfo.setVersionedFromId(version.getVersionedFromId()); |
216 | 0 | return versionInfo; |
217 | |
} |
218 | |
} |