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