1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.lum.lo.service.impl; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.List; |
20 | |
|
21 | |
import org.kuali.student.core.dao.CrudDao; |
22 | |
import org.kuali.student.core.exceptions.DoesNotExistException; |
23 | |
import org.kuali.student.core.exceptions.InvalidParameterException; |
24 | |
import org.kuali.student.core.exceptions.VersionMismatchException; |
25 | |
import org.kuali.student.core.service.impl.BaseAssembler; |
26 | |
import org.kuali.student.lum.lo.dao.LoDao; |
27 | |
import org.kuali.student.lum.lo.dto.LoCategoryInfo; |
28 | |
import org.kuali.student.lum.lo.dto.LoCategoryTypeInfo; |
29 | |
import org.kuali.student.lum.lo.dto.LoInfo; |
30 | |
import org.kuali.student.lum.lo.dto.LoLoRelationInfo; |
31 | |
import org.kuali.student.lum.lo.dto.LoLoRelationTypeInfo; |
32 | |
import org.kuali.student.lum.lo.dto.LoRepositoryInfo; |
33 | |
import org.kuali.student.lum.lo.dto.LoTypeInfo; |
34 | |
import org.kuali.student.lum.lo.entity.Lo; |
35 | |
import org.kuali.student.lum.lo.entity.LoAttribute; |
36 | |
import org.kuali.student.lum.lo.entity.LoCategory; |
37 | |
import org.kuali.student.lum.lo.entity.LoCategoryAttribute; |
38 | |
import org.kuali.student.lum.lo.entity.LoCategoryType; |
39 | |
import org.kuali.student.lum.lo.entity.LoCategoryTypeAttribute; |
40 | |
import org.kuali.student.lum.lo.entity.LoLoRelation; |
41 | |
import org.kuali.student.lum.lo.entity.LoLoRelationAttribute; |
42 | |
import org.kuali.student.lum.lo.entity.LoLoRelationType; |
43 | |
import org.kuali.student.lum.lo.entity.LoRepository; |
44 | |
import org.kuali.student.lum.lo.entity.LoRichText; |
45 | |
import org.kuali.student.lum.lo.entity.LoType; |
46 | |
import org.springframework.beans.BeanUtils; |
47 | |
|
48 | 0 | public class LearningObjectiveServiceAssembler extends BaseAssembler { |
49 | |
|
50 | |
public static Lo toLo(boolean isUpdate, LoInfo dto, CrudDao dao) throws InvalidParameterException, DoesNotExistException, VersionMismatchException { |
51 | 638 | return toLo(isUpdate, new Lo(), dto, dao); |
52 | |
} |
53 | |
|
54 | |
public static Lo toLo(boolean isUpdate, Lo entity, LoInfo dto, CrudDao dao) throws InvalidParameterException, DoesNotExistException, VersionMismatchException { |
55 | 660 | if(null == dto) { |
56 | 0 | return null; |
57 | |
} |
58 | |
Lo lo; |
59 | |
|
60 | 660 | if (isUpdate) { |
61 | 22 | lo = dao.fetch(Lo.class, dto.getId()); |
62 | 22 | if (lo == null) { |
63 | 0 | throw new DoesNotExistException((new StringBuilder()).append("Lo does not exist for id: ").append(dto.getId()).toString()); |
64 | |
} |
65 | 22 | if ( ! String.valueOf(lo.getVersionNumber()).equals(dto.getMetaInfo().getVersionInd()) ) { |
66 | 0 | throw new VersionMismatchException("Lo to be updated is not the current version"); |
67 | |
} |
68 | |
} else { |
69 | 638 | lo = new Lo(); |
70 | |
} |
71 | |
|
72 | 660 | BeanUtils.copyProperties(dto, lo, new String[] { "desc", "loRepository", "loType", "attributes", "metaInfo" }); |
73 | |
|
74 | 660 | lo.setAttributes(toGenericAttributes(LoAttribute.class, dto.getAttributes(), lo, dao)); |
75 | 660 | lo.setDescr(toRichText(LoRichText.class, dto.getDesc())); |
76 | |
|
77 | 660 | LoRepository repository = dao.fetch(LoRepository.class, dto.getLoRepositoryKey()); |
78 | 660 | if(null == repository) { |
79 | 0 | throw new InvalidParameterException((new StringBuilder()).append("LoRepository does not exist for id: ").append(dto.getLoRepositoryKey()).toString()); |
80 | |
} |
81 | 660 | lo.setLoRepository(repository); |
82 | |
|
83 | 660 | LoType type = dao.fetch(LoType.class, dto.getType()); |
84 | 660 | if(null == type) { |
85 | 0 | throw new InvalidParameterException((new StringBuilder()).append("LoType does not exist for id: ").append(dto.getType()).toString()); |
86 | |
} |
87 | 660 | lo.setLoType(type); |
88 | |
|
89 | 660 | return lo; |
90 | |
} |
91 | |
|
92 | |
public static LoInfo toLoInfo(Lo entity) { |
93 | 1045 | LoInfo dto = new LoInfo(); |
94 | |
|
95 | 1045 | BeanUtils.copyProperties(entity, dto, |
96 | |
new String[] { "desc", "attributes", "type" }); |
97 | 1045 | dto.setDesc(toRichTextInfo(entity.getDescr())); |
98 | 1045 | dto.setMetaInfo(toMetaInfo(entity.getMeta(), entity.getVersionNumber())); |
99 | 1045 | dto.setAttributes(toAttributeMap(entity.getAttributes())); |
100 | 1045 | dto.setType(entity.getLoType().getId()); |
101 | 1045 | dto.setLoRepositoryKey(entity.getLoRepository() == null? null: entity.getLoRepository().getId()); |
102 | 1045 | return dto; |
103 | |
} |
104 | |
|
105 | |
public static LoCategory toLoCategory(LoCategoryInfo dto, LoDao dao) throws InvalidParameterException, DoesNotExistException { |
106 | 5 | return toLoCategory(new LoCategory(), dto, dao); |
107 | |
} |
108 | |
|
109 | |
public static LoCategory toLoCategory(LoCategory entity, LoCategoryInfo dto, LoDao dao) throws InvalidParameterException, DoesNotExistException { |
110 | 5 | if(entity == null) |
111 | 0 | entity = new LoCategory(); |
112 | 5 | BeanUtils.copyProperties(dto, entity, |
113 | |
new String[] { "desc", "attributes", "metaInfo", "loRepository", "type", "id"}); |
114 | 5 | entity.setDesc(toRichText(LoRichText.class, dto.getDesc())); |
115 | 5 | entity.setAttributes(toGenericAttributes(LoCategoryAttribute.class, dto.getAttributes(), entity, dao)); |
116 | 5 | entity.setLoRepository(dao.fetch(LoRepository.class, dto.getLoRepository())); |
117 | 5 | entity.setLoCategoryType(dao.fetch(LoCategoryType.class, dto.getType())); |
118 | 5 | return entity; |
119 | |
} |
120 | |
|
121 | |
public static LoCategoryInfo toLoCategoryInfo(LoCategory entity) { |
122 | 772 | LoCategoryInfo dto = new LoCategoryInfo(); |
123 | |
|
124 | 772 | BeanUtils.copyProperties(entity, dto, |
125 | |
new String[] { "desc", "attributes", "loRepository", "loCategoryType" }); |
126 | 772 | dto.setDesc(toRichTextInfo(entity.getDescr())); |
127 | 772 | dto.setMetaInfo(toMetaInfo(entity.getMeta(), entity.getVersionNumber())); |
128 | 772 | dto.setAttributes(toAttributeMap(entity.getAttributes())); |
129 | 772 | dto.setLoRepository(entity.getLoRepository().getId()); |
130 | 772 | dto.setType(entity.getLoCategoryType().getId()); |
131 | |
|
132 | 772 | return dto; |
133 | |
} |
134 | |
|
135 | |
public static LoRepositoryInfo toLoRepositoryInfo(LoRepository entity) { |
136 | 4 | LoRepositoryInfo dto = new LoRepositoryInfo(); |
137 | |
|
138 | 4 | BeanUtils.copyProperties(entity, dto, |
139 | |
new String[] { "desc", "attributes", "rootLo" }); |
140 | 4 | dto.setDesc(toRichTextInfo(entity.getDescr())); |
141 | 4 | dto.setMetaInfo(toMetaInfo(entity.getMeta(), entity.getVersionNumber())); |
142 | 4 | dto.setAttributes(toAttributeMap(entity.getAttributes())); |
143 | 4 | dto.setRootLoId(entity.getRootLo() == null? null :entity.getRootLo().getId()); |
144 | 4 | return dto; |
145 | |
} |
146 | |
|
147 | |
public static LoTypeInfo toLoTypeInfo(LoType entity) { |
148 | 4 | LoTypeInfo dto = new LoTypeInfo(); |
149 | |
|
150 | 4 | BeanUtils.copyProperties(entity, dto, |
151 | |
new String[] { "attributes" }); |
152 | 4 | dto.setAttributes(toAttributeMap(entity.getAttributes())); |
153 | 4 | return dto; |
154 | |
} |
155 | |
|
156 | |
|
157 | |
public static List<LoInfo> toLoInfos(List<Lo> los) { |
158 | 380 | List<LoInfo> list = new ArrayList<LoInfo>(); |
159 | 380 | for (Lo lo : los) { |
160 | 302 | list.add(toLoInfo(lo)); |
161 | |
} |
162 | 380 | return list; |
163 | |
} |
164 | |
|
165 | |
public static List<LoCategoryInfo> toLoCategoryInfos(List<LoCategory> categories) { |
166 | 435 | if (null == categories) { |
167 | 0 | return new ArrayList<LoCategoryInfo>(0); |
168 | |
} |
169 | 435 | List<LoCategoryInfo> list = new ArrayList<LoCategoryInfo>(categories.size()); |
170 | 435 | for (LoCategory loCategory : categories) { |
171 | 763 | list.add(toLoCategoryInfo(loCategory)); |
172 | |
} |
173 | 435 | return list; |
174 | |
} |
175 | |
|
176 | |
public static List<LoRepositoryInfo> toLoRepositoryInfos(List<LoRepository> repositories) { |
177 | 1 | List<LoRepositoryInfo> list = new ArrayList<LoRepositoryInfo>(); |
178 | 1 | for (LoRepository loRepository : repositories) { |
179 | 3 | list.add(toLoRepositoryInfo(loRepository)); |
180 | |
} |
181 | 1 | return list; |
182 | |
} |
183 | |
|
184 | |
public static List<LoTypeInfo> toLoTypeInfos(List<LoType> find) { |
185 | 1 | List<LoTypeInfo> list = new ArrayList<LoTypeInfo>(); |
186 | 1 | for (LoType loType : find) { |
187 | 2 | list.add(toLoTypeInfo(loType)); |
188 | |
} |
189 | 1 | return list; |
190 | |
} |
191 | |
|
192 | |
public static List<LoLoRelationTypeInfo> toLoLoRelationTypeInfos(List<LoLoRelationType> find) { |
193 | 1 | List<LoLoRelationTypeInfo> list = new ArrayList<LoLoRelationTypeInfo>(); |
194 | 1 | for (LoLoRelationType loType : find) { |
195 | 2 | list.add(toLoLoRelationTypeInfo(loType)); |
196 | |
} |
197 | 1 | return list; |
198 | |
} |
199 | |
|
200 | |
public static LoLoRelationTypeInfo toLoLoRelationTypeInfo(LoLoRelationType loLoRelType) { |
201 | 3 | LoLoRelationTypeInfo dto = new LoLoRelationTypeInfo(); |
202 | 3 | BeanUtils.copyProperties(loLoRelType, dto, |
203 | |
new String[] { "attributes" }); |
204 | 3 | dto.setAttributes(toAttributeMap(loLoRelType.getAttributes())); |
205 | 3 | return dto; |
206 | |
} |
207 | |
|
208 | |
public static LoLoRelation toLoLoRelation(boolean isUpdate, LoLoRelationInfo dto, CrudDao dao) throws DoesNotExistException, VersionMismatchException, InvalidParameterException { |
209 | 542 | return toLoLoRelation(isUpdate, null, dto, dao); |
210 | |
} |
211 | |
|
212 | |
public static LoLoRelation toLoLoRelation(boolean isUpdate, LoLoRelation entity, LoLoRelationInfo dto, CrudDao dao) throws DoesNotExistException, VersionMismatchException, InvalidParameterException { |
213 | 542 | if(null == dto) { |
214 | 0 | return null; |
215 | |
} |
216 | |
LoLoRelation llRelation; |
217 | |
|
218 | 542 | if (isUpdate) { |
219 | 0 | llRelation = dao.fetch(LoLoRelation.class, dto.getId()); |
220 | 0 | if (llRelation == null) { |
221 | 0 | throw new DoesNotExistException((new StringBuilder()).append("LoLoRelation does not exist for id: ").append(dto.getId()).toString()); |
222 | |
} |
223 | 0 | if ( ! String.valueOf(llRelation.getVersionNumber()).equals(dto.getMetaInfo().getVersionInd()) ) { |
224 | 0 | throw new VersionMismatchException("LoLoRelation to be updated is not the current version"); |
225 | |
} |
226 | |
} else { |
227 | 542 | llRelation = new LoLoRelation(); |
228 | |
} |
229 | |
|
230 | 542 | BeanUtils.copyProperties(dto, llRelation, new String[] { "lo", "relatedLo", "attributes", "metaInfo" }); |
231 | |
|
232 | 542 | llRelation.setAttributes(toGenericAttributes(LoLoRelationAttribute.class, dto.getAttributes(), llRelation, dao)); |
233 | |
|
234 | 542 | Lo lo = null; |
235 | 542 | Lo relatedLo = null; |
236 | 542 | LoLoRelationType relationType = null; |
237 | |
try { |
238 | 542 | lo = dao.fetch(Lo.class, dto.getLoId()); |
239 | 542 | relatedLo = dao.fetch(Lo.class, dto.getRelatedLoId()); |
240 | 542 | relationType = dao.fetch(LoLoRelationType.class, dto.getType()); |
241 | 0 | } catch (DoesNotExistException dnee) { |
242 | 0 | throw new DoesNotExistException((null == lo ? "Lo" : (null == relatedLo ? "Related Lo" : "Lo-Lo relation type")) + |
243 | |
" does not exist when creating LoLoRelation", dnee); |
244 | 542 | } |
245 | |
|
246 | 542 | llRelation.setLo(lo); |
247 | 542 | llRelation.setRelatedLo(relatedLo); |
248 | 542 | llRelation.setLoLoRelationType(relationType); |
249 | |
|
250 | 542 | return llRelation; |
251 | |
} |
252 | |
|
253 | |
public static LoLoRelationInfo toLoLoRelationInfo(LoLoRelation entity) { |
254 | 611 | LoLoRelationInfo dto = new LoLoRelationInfo(); |
255 | |
|
256 | 611 | BeanUtils.copyProperties(entity, dto, |
257 | |
new String[] { "lo", "relatedLo", "type", "attributes" }); |
258 | 611 | dto.setLoId(entity.getLo().getId()); |
259 | 611 | dto.setRelatedLoId(entity.getRelatedLo().getId()); |
260 | 611 | dto.setType(entity.getLoLoRelationType().getId()); |
261 | 611 | dto.setMetaInfo(toMetaInfo(entity.getMeta(), entity.getVersionNumber())); |
262 | 611 | dto.setAttributes(toAttributeMap(entity.getAttributes())); |
263 | 611 | return dto; |
264 | |
} |
265 | |
|
266 | |
public static LoCategoryType toLoCategoryType(LoCategoryType entity, LoCategoryTypeInfo dto, LoDao dao) throws InvalidParameterException { |
267 | 0 | if(entity == null) |
268 | 0 | entity = new LoCategoryType(); |
269 | 0 | BeanUtils.copyProperties(dto, entity, |
270 | |
new String[] { "attributes", "metaInfo" }); |
271 | 0 | entity.setAttributes(toGenericAttributes(LoCategoryTypeAttribute.class, dto.getAttributes(), entity, dao)); |
272 | 0 | entity.setDescr(dto.getDesc()); |
273 | 0 | return entity; |
274 | |
} |
275 | |
|
276 | |
|
277 | |
public static LoCategoryTypeInfo toLoCategoryTypeInfo( LoCategoryType loCatType) { |
278 | 3 | LoCategoryTypeInfo dto = new LoCategoryTypeInfo(); |
279 | 3 | BeanUtils.copyProperties(loCatType, dto, new String[] { "attributes" }); |
280 | 3 | dto.setAttributes(toAttributeMap(loCatType.getAttributes())); |
281 | 3 | dto.setDesc(loCatType.getDescr()); |
282 | 3 | return dto; |
283 | |
} |
284 | |
|
285 | |
public static List<LoCategoryTypeInfo> toLoCategoryTypeInfos(List<LoCategoryType> categoryTypes) { |
286 | 1 | ArrayList<LoCategoryTypeInfo> list = new ArrayList<LoCategoryTypeInfo>(); |
287 | 1 | for (LoCategoryType catType : categoryTypes) { |
288 | 3 | list.add(toLoCategoryTypeInfo(catType)); |
289 | |
} |
290 | 1 | return list; |
291 | |
} |
292 | |
|
293 | |
public static List<LoLoRelationInfo> toLoLoRelationInfos( List<LoLoRelation> llRelations) { |
294 | 52 | List<LoLoRelationInfo> llRelInfos = new ArrayList<LoLoRelationInfo>(); |
295 | 52 | for (LoLoRelation llRelation : llRelations) { |
296 | 66 | llRelInfos.add(toLoLoRelationInfo(llRelation)); |
297 | |
} |
298 | 52 | return llRelInfos; |
299 | |
} |
300 | |
} |