View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   package org.kuali.student.r2.lum.lu.service.impl;
6   
7   import org.kuali.student.r1.common.entity.Amount;
8   import org.kuali.student.r1.common.entity.CurrencyAmount;
9   import org.kuali.student.r1.common.entity.TimeAmount;
10  import org.kuali.student.r2.common.dto.AmountInfo;
11  import org.kuali.student.r2.common.dto.CurrencyAmountInfo;
12  import org.kuali.student.r2.common.dto.RichTextInfo;
13  import org.kuali.student.r2.common.dto.TimeAmountInfo;
14  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
15  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
16  import org.kuali.student.r2.common.exceptions.VersionMismatchException;
17  import org.kuali.student.r2.core.search.dto.SearchParamInfo;
18  import org.kuali.student.r2.core.search.infc.SearchParam;
19  import org.kuali.student.r2.core.class1.type.dto.TypeInfo;
20  import org.kuali.student.r2.core.service.assembly.BaseAssembler;
21  import org.kuali.student.r2.lum.clu.dto.*;
22  import org.kuali.student.r2.lum.lu.dao.LuDao;
23  import org.kuali.student.r2.lum.lu.entity.*;
24  import org.springframework.beans.BeanUtils;
25  
26  import java.util.ArrayList;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  import java.util.Map.Entry;
31  
32  public class CluServiceAssembler extends BaseAssembler {
33  
34      public static List<CluCluRelationInfo> toCluCluRelationInfos(
35              List<CluCluRelation> entities) {
36          List<CluCluRelationInfo> dtos = new ArrayList<CluCluRelationInfo>(
37                  entities.size());
38          if (entities != null) {
39              for (CluCluRelation entity : entities) {
40                  dtos.add(toCluCluRelationInfo(entity));
41              }
42          }
43          return dtos;
44  
45      }
46  
47      public static CluCluRelationInfo toCluCluRelationInfo(CluCluRelation entity) {
48          if (entity == null) {
49              return null;
50          }
51          CluCluRelationInfo dto = new CluCluRelationInfo();
52          BeanUtils.copyProperties(entity, dto,
53                  new String[]{"cluId", "relatedCluId", "cluRelationRequired",
54                          "attributes", "meta"});
55  
56          dto.setIsCluRelationRequired(entity.isCluRelationRequired());
57          dto.setCluId(entity.getClu().getId());
58          dto.setRelatedCluId(entity.getRelatedClu().getId());
59          dto.setTypeKey(entity.getLuLuRelationType().getId());
60          dto.setAttributes(toAttributeList(entity.getAttributes()));
61          dto.setMeta(toMetaInfo(entity.getMeta(), entity.getVersionNumber()));
62  
63          return dto;
64  
65      }
66  
67      public static List<CluLoRelationInfo> toCluLoRelationInfos(
68              List<CluLoRelation> entities) {
69  
70          List<CluLoRelationInfo> dtos = new ArrayList<CluLoRelationInfo>(
71                  entities.size());
72          if (entities != null) {
73              for (CluLoRelation entity : entities) {
74                  dtos.add(toCluLoRelationInfo(entity));
75              }
76          }
77          return dtos;
78      }
79  
80      public static CluLoRelationInfo toCluLoRelationInfo(CluLoRelation entity) {
81          if (entity == null) {
82              return null;
83          }
84          CluLoRelationInfo dto = new CluLoRelationInfo();
85          BeanUtils.copyProperties(entity, dto, new String[]{"cluId",
86                  "attributes", "meta", "type"});
87  
88          dto.setCluId(entity.getClu().getId());
89          dto.setAttributes(toAttributeList(entity.getAttributes()));
90          dto.setMeta(toMetaInfo(entity.getMeta(), entity.getVersionNumber()));
91          dto.setTypeKey(entity.getType().getId());
92  
93          return dto;
94      }
95  
96      public static List<CluInfo> toCluInfos(List<Clu> entities) {
97  
98          // return an empty list (Effective Java 2ndEd, item #43)
99          if (entities == null) {
100             return new ArrayList<CluInfo>(0);
101         }
102 
103         List<CluInfo> dtos = new ArrayList<CluInfo>(entities.size());
104         for (Clu entity : entities) {
105             dtos.add(toCluInfo(entity));
106         }
107         return dtos;
108 
109     }
110 
111     public static CluInfo toCluInfo(Clu entity) {
112         if (entity == null) {
113             return null;
114         }
115         CluInfo dto = new CluInfo();
116 
117         // copy all simple fields - exclude complex data types
118         BeanUtils.copyProperties(entity, dto, new String[]{
119                 "officialIdentifier", "alternateIdentifiers", "descr",
120                 "participatingOrgs", "primaryInstructor", "instructors",
121                 "stdDuration", "luCodes", "credit", "offeredAtpTypes", "fee",
122                 "accounting", "intensity",
123                 "campusLocationList", "accreditationList",
124                 "adminOrgs", "attributes", "meta", "version"});
125         dto.setOfficialIdentifier(toCluIdentifierInfo(entity.getOfficialIdentifier()));
126         dto.setAlternateIdentifiers(toCluIdentifierInfos(entity.getAlternateIdentifiers()));
127         dto.setDescr(toRichTextInfo(entity.getDescr()));
128 
129         // accreditingOrg Deprecated in v 1.0-rc2 Replaced by Primary and
130         // Alternate admin orgs
131         dto.setAccreditations(toAccreditationInfos(entity.getAccreditations()));
132 
133         dto.setAdminOrgs(toCluAdminOrgInfos(entity.getAdminOrgs()));
134 
135         dto.setPrimaryInstructor(toCluInstructorInfo(entity.getPrimaryInstructor()));
136         dto.setInstructors(toCluInstructorInfos(entity.getInstructors()));
137         dto.setStdDuration(toTimeAmountInfo(entity.getStdDuration()));
138         dto.setLuCodes(toLuCodeInfos(entity.getLuCodes()));
139 
140         if (entity.getOfferedAtpTypes() != null) {
141             List<String> offeredAtpTypes = new ArrayList<String>(entity.getOfferedAtpTypes().size());
142             for (CluAtpTypeKey key : entity.getOfferedAtpTypes()) {
143                 offeredAtpTypes.add(key.getAtpTypeKey());
144             }
145             dto.setOfferedAtpTypes(offeredAtpTypes);
146         }
147 
148         dto.setFeeInfo(toCluFeeInfo(entity.getFee()));
149         dto.setAccountingInfo(toCluAccountingInfo(entity.getAccounting()));
150 
151         dto.setAttributes(toAttributeList(entity.getAttributes()));
152         dto.setMeta(toMetaInfo(entity.getMeta(), entity.getVersionNumber()));
153 
154         dto.setTypeKey(entity.getLuType().getId());
155 
156         if (entity.getCampusLocations() != null) {
157             List<String> campusLocations = new ArrayList<String>(entity.getCampusLocations().size());
158             for (CluCampusLocation cluCamp : entity.getCampusLocations()) {
159                 campusLocations.add(cluCamp.getCampusLocation());
160             }
161             dto.setCampusLocations(campusLocations);
162         }
163 
164         dto.setIntensity(toAmountInfo(entity.getIntensity()));
165 
166         dto.setVersion(toVersionInfo(entity.getVersion()));
167         dto.setIsEnrollable(entity.isEnrollable());
168         dto.setCanCreateLui(entity.isCanCreateLui());
169         dto.setIsHasEarlyDropDeadline(entity.isHasEarlyDropDeadline());
170         dto.setIsHazardousForDisabledStudents(entity.isHazardousForDisabledStudents());
171 
172         return dto;
173 
174     }
175 
176     public static List<CluSetInfo> toCluSetInfos(List<CluSet> entities) {
177         List<CluSetInfo> dtos = new ArrayList<CluSetInfo>(entities.size());
178         if (entities != null) {
179             for (CluSet entity : entities) {
180                 dtos.add(toCluSetInfo(entity));
181             }
182         }
183         return dtos;
184     }
185 
186     public static CluSet toCluSetEntity(CluSetInfo cluSetInfo, LuDao luDao) throws InvalidParameterException, DoesNotExistException {
187         CluSet cluSet = new CluSet();
188 
189         BeanUtils.copyProperties(cluSetInfo, cluSet, new String[]{"id",
190                 "descr", "attributes", "meta", "membershipQuery"});
191         cluSet.setAttributes(toGenericAttributes(
192                 CluSetAttribute.class, cluSetInfo.getAttributes(), cluSet, luDao));
193         cluSet.setType(cluSetInfo.getTypeKey());
194         cluSet.setDescr(toRichText(LuRichText.class, cluSetInfo.getDescr()));
195 
196         for (String cluId : cluSetInfo.getCluIds()) {
197             CluSetJoinVersionIndClu join = new CluSetJoinVersionIndClu();
198             join.setCluSet(cluSet);
199             join.setCluVersionIndId(cluId);
200             cluSet.getCluVerIndIds().add(join);
201         }
202         for (String cluSetId : cluSetInfo.getCluSetIds()) {
203             CluSet c;
204             try {
205                 c = luDao.fetch(CluSet.class, cluSetId);
206             } catch (org.kuali.student.r2.common.exceptions.DoesNotExistException ex) {
207                 throw new DoesNotExistException(cluSetId);
208             }
209             if (cluSet.getCluSets() == null) {
210                 cluSet.setCluSets(new ArrayList<CluSet>());
211             }
212             cluSet.getCluSets().add(c);
213         }
214         cluSet.setMembershipQuery(toMembershipQueryEntity(cluSetInfo.getMembershipQuery()));
215 
216         return cluSet;
217     }
218 
219     public static CluSetInfo toCluSetInfo(CluSet entity) {
220         if (entity == null) {
221             return null;
222         }
223         CluSetInfo dto = new CluSetInfo();
224 
225         BeanUtils.copyProperties(entity, dto, new String[]{"descr",
226                 "cluCriteria", "cluSets", "clus", "attributes", "meta", "membershipQuery"});
227 
228         dto.setDescr(toRichTextInfo(entity.getDescr()));
229         // TODO dto.setCluCriteria()
230         if (entity.getCluSets() != null) {
231             List<String> cluSetIds = new ArrayList<String>(entity.getCluSets().size());
232             for (CluSet id : entity.getCluSets()) {
233                 cluSetIds.add(id.getId());
234             }
235             dto.setCluSetIds(cluSetIds);
236         }
237 
238         List<String> cluIds = new ArrayList<String>(entity.getCluVerIndIds().size());
239         for (CluSetJoinVersionIndClu join : entity.getCluVerIndIds()) {
240             cluIds.add(join.getCluVersionIndId());
241         }
242         dto.setCluIds(cluIds);
243 
244         dto.setAttributes(toAttributeList(entity.getAttributes()));
245         dto.setMeta(toMetaInfo(entity.getMeta(), entity.getVersionNumber()));
246 
247         MembershipQueryInfo mqInfo = toMembershipQueryInfo(entity.getMembershipQuery());
248         dto.setMembershipQuery(mqInfo);
249         dto.setIsReusable(entity.getIsReusable());
250         dto.setIsReferenceable(entity.getIsReferenceable());
251 
252         return dto;
253     }
254 
255     public static MembershipQueryInfo toMembershipQueryInfo(MembershipQuery entity) {
256         if (entity == null) {
257             return null;
258         }
259         MembershipQueryInfo dto = new MembershipQueryInfo();
260         dto.setId(entity.getId());
261         dto.setSearchTypeKey(entity.getSearchTypeKey());
262         List<SearchParamInfo> list = new ArrayList<SearchParamInfo>();
263         for (SearchParameter param : entity.getSearchParameters()) {
264             SearchParamInfo sp = toSearchParam(param);
265             list.add(sp);
266         }
267         dto.setQueryParamValues(list);
268 
269         return dto;
270     }
271 
272     public static SearchParamInfo toSearchParam(SearchParameter entity) {
273         SearchParamInfo dto = new SearchParamInfo();
274         dto.setKey(entity.getKey());
275         List<String> values = new ArrayList<String>();
276         for (SearchParameterValue value : entity.getValues()) {
277             values.add(value.getValue());
278         }
279         dto.setValues(values);
280         return dto;
281     }
282 
283     public static MembershipQuery toMembershipQueryEntity(MembershipQueryInfo dto) {
284         if (dto == null) {
285             return null;
286         }
287         MembershipQuery entity = new MembershipQuery();
288         entity.setSearchTypeKey(dto.getSearchTypeKey());
289         List<SearchParameter> list = new ArrayList<SearchParameter>();
290         for (SearchParam param : dto.getQueryParamValues()) {
291             SearchParameter sp = toSearchParameterEntity(param);
292             list.add(sp);
293         }
294         entity.setSearchParameters(list);
295 
296         return entity;
297     }
298 
299     public static SearchParameter toSearchParameterEntity(SearchParam dto) {
300         if (dto == null) {
301             return null;
302         }
303         SearchParameter entity = new SearchParameter();
304         entity.setKey(dto.getKey());
305         List<SearchParameterValue> values = new ArrayList<SearchParameterValue>();
306         for (String s : dto.getValues()) {
307             SearchParameterValue value = new SearchParameterValue();
308             value.setValue(s);
309             values.add(value);
310         }
311         entity.setValues(values);
312         return entity;
313     }
314 
315     public static CluResultInfo toCluResultInfo(CluResult entity) {
316         if (entity == null) {
317             return null;
318         }
319 
320         CluResultInfo dto = new CluResultInfo();
321 
322         BeanUtils.copyProperties(entity, dto, new String[]{"resultOptions", "desc"});
323 
324         List<ResultOptionInfo> resultOptions = toResultOptionInfos(entity.getResultOptions());
325         dto.setResultOptions(resultOptions);
326         dto.setDescr(toRichTextInfo(entity.getDesc()));
327         dto.setCluId(entity.getClu().getId());
328         TypeInfo type = toGenericTypeInfo(entity.getCluResultType());
329         if (type != null) {
330             dto.setTypeKey(type.getKey());
331         }
332         dto.setMeta(entity.toDTO());
333 
334         return dto;
335     }
336 
337     public static List<CluResultInfo> toCluResultInfos(List<CluResult> entities) {
338         List<CluResultInfo> dtos = new ArrayList<CluResultInfo>();
339         if (entities != null) {
340             for (CluResult entity : entities) {
341                 dtos.add(toCluResultInfo(entity));
342             }
343         }
344         return dtos;
345     }
346 
347     public static List<ResultOptionInfo> toResultOptionInfos(
348             List<ResultOption> entities) {
349         List<ResultOptionInfo> dtos = new ArrayList<ResultOptionInfo>();
350         if (entities != null) {
351             for (ResultOption entity : entities) {
352                 dtos.add(toResultOptionInfo(entity));
353             }
354         }
355         return dtos;
356     }
357 
358     public static ResultOptionInfo toResultOptionInfo(ResultOption entity) {
359         if (entity == null) {
360             return null;
361         }
362 
363         ResultOptionInfo dto = new ResultOptionInfo();
364 
365         BeanUtils.copyProperties(entity, dto, new String[]{"resultUsageType", "desc"});
366         if (entity.getResultUsageType() != null) {
367             dto.setResultUsageTypeKey(entity.getResultUsageType().getId());
368         }
369         dto.setDescr(toRichTextInfo(entity.getDesc()));
370         dto.setMeta(entity.toDTO());
371 
372         return dto;
373     }
374 
375     public static List<TypeInfo> toLuLuRelationTypeInfos(
376             List<LuLuRelationType> entities) {
377         List<TypeInfo> dtos = new ArrayList<TypeInfo>(
378                 entities.size());
379         if (entities != null) {
380             for (LuLuRelationType entity : entities) {
381                 dtos.add(toLuLuRelationTypeInfo(entity));
382             }
383         }
384         return dtos;
385 
386     }
387 
388     public static TypeInfo toLuLuRelationTypeInfo(
389             LuLuRelationType entity) {
390         if (entity == null) {
391             return null;
392         }
393         TypeInfo dto = new TypeInfo();
394 
395         BeanUtils.copyProperties(entity, dto, new String[]{"attributes"});
396         dto.setAttributes(toAttributeList(entity.getAttributes()));
397 
398         return dto;
399     }
400 
401     public static List<CluIdentifierInfo> toCluIdentifierInfos(
402             List<CluIdentifier> entities) {
403         List<CluIdentifierInfo> dtos = new ArrayList<CluIdentifierInfo>(
404                 entities.size());
405         if (entities != null) {
406             for (CluIdentifier entity : entities) {
407                 dtos.add(toCluIdentifierInfo(entity));
408             }
409         }
410         return dtos;
411     }
412 
413     public static CluIdentifierInfo toCluIdentifierInfo(CluIdentifier entity) {
414         if (entity == null) {
415             return null;
416         }
417 
418         CluIdentifierInfo dto = new CluIdentifierInfo();
419 
420         BeanUtils.copyProperties(entity, dto,
421                 new String[]{"attributes"});
422         dto.setAttributes(toAttributeList(entity.getAttributes()));
423         return dto;
424     }
425 
426     public static List<CluInstructorInfo> toCluInstructorInfos(
427             List<CluInstructor> entities) {
428         List<CluInstructorInfo> dtos = new ArrayList<CluInstructorInfo>(
429                 entities.size());
430         if (entities != null) {
431             for (CluInstructor entity : entities) {
432                 dtos.add(toCluInstructorInfo(entity));
433             }
434         }
435         return dtos;
436     }
437 
438     public static CluInstructorInfo toCluInstructorInfo(CluInstructor entity) {
439         if (entity == null) {
440             return null;
441         }
442         CluInstructorInfo dto = new CluInstructorInfo();
443 
444         BeanUtils.copyProperties(entity, dto,
445                 new String[]{"attributes"});
446         dto.setAttributes(toAttributeList(entity.getAttributes()));
447 
448         return dto;
449     }
450 
451     public static AmountInfo toAmountInfo(Amount entity) {
452         if (entity == null) {
453             return null;
454         }
455         AmountInfo dto = new AmountInfo();
456 
457         BeanUtils.copyProperties(entity, dto, new String[] {"unitType"});
458         dto.setUnitTypeKey(entity.getUnitType());
459 
460         return dto;
461     }
462 
463     public static TimeAmountInfo toTimeAmountInfo(TimeAmount entity) {
464         if (entity == null) {
465             return null;
466         }
467         TimeAmountInfo dto = new TimeAmountInfo();
468 
469         BeanUtils.copyProperties(entity, dto);
470 
471         return dto;
472     }
473 
474     public static List<LuCodeInfo> toLuCodeInfos(List<LuCode> entities) {
475         List<LuCodeInfo> dtos = new ArrayList<LuCodeInfo>(entities.size());
476         if (entities != null) {
477             for (LuCode entity : entities) {
478                 dtos.add(toLuCodeInfo(entity));
479             }
480         }
481         return dtos;
482     }
483 
484     public static LuCodeInfo toLuCodeInfo(LuCode entity) {
485         if (entity == null) {
486             return null;
487         }
488         LuCodeInfo dto = new LuCodeInfo();
489 
490         BeanUtils.copyProperties(entity, dto, new String[]{"attributes",
491                 "meta", "descr"});
492         RichTextInfo desc = new RichTextInfo();
493         desc.setPlain(entity.getDescr());
494         dto.setDescr(desc);
495         dto.setAttributes(toAttributeList(entity.getAttributes()));
496         dto.setMeta(toMetaInfo(entity.getMeta(), entity.getVersionNumber()));
497 
498         return dto;
499     }
500 
501     public static CluCreditInfo toCluCreditInfos(CluCredit entity) {
502         if (entity == null) {
503             return null;
504         }
505         CluCreditInfo dto = new CluCreditInfo();
506 
507         BeanUtils.copyProperties(entity, dto, new String[]{"id",
508                 "repeatTime", "minTimeToComplete", "maxTimeToComplete",
509                 "maxAllowableInactivity", "maxTimeResultsRecognized"});
510         dto.setRepeatTime(toTimeAmountInfo(entity.getRepeatTime()));
511         dto.setMinTimeToComplete(toTimeAmountInfo(entity.getMinTimeToComplete()));
512         dto.setMaxTimeToComplete(toTimeAmountInfo(entity.getMaxTimeToComplete()));
513         dto.setMaxAllowableInactivity(toTimeAmountInfo(entity.getMaxAllowableInactivity()));
514         dto.setMaxTimeResultsRecognized(toTimeAmountInfo(entity.getMaxTimeResultsRecognized()));
515 
516         return dto;
517     }
518 
519     public static List<TypeInfo> toResultComponentTypeInfo(
520             List<String> componentIds) {
521         List<TypeInfo> dtos = new ArrayList<TypeInfo>();
522         if (componentIds != null) {
523             for (String id : componentIds) {
524                 TypeInfo comp = new TypeInfo();
525                 comp.setKey(id);
526                 dtos.add(comp);
527             }
528         }
529         return dtos;
530     }
531 
532     public static CluFeeInfo toCluFeeInfo(CluFee entity) {
533         if (entity == null) {
534             return null;
535         }
536         CluFeeInfo dto = new CluFeeInfo();
537 
538         dto.setCluFeeRecords(toCluFeeRecordInfos(entity.getCluFeeRecords()));
539         dto.setId(entity.getId());
540         dto.setAttributes(toAttributeList(entity.getAttributes()));
541         dto.setDescr(toRichTextInfo(entity.getDescr()));
542         dto.setMeta(toMetaInfo(entity.getMeta(), entity.getVersionNumber()));
543 
544         return dto;
545     }
546 
547     private static List<CluFeeRecordInfo> toCluFeeRecordInfos(
548             List<CluFeeRecord> entities) {
549         List<CluFeeRecordInfo> dtos = new ArrayList<CluFeeRecordInfo>();
550         if (entities != null) {
551             for (CluFeeRecord record : entities) {
552                 dtos.add(toCluFeeRecordInfo(record));
553             }
554         }
555         return dtos;
556     }
557 
558     private static CluFeeRecordInfo toCluFeeRecordInfo(CluFeeRecord entity) {
559         if (entity == null) {
560             return null;
561         }
562 
563         CluFeeRecordInfo dto = new CluFeeRecordInfo();
564 
565         BeanUtils.copyProperties(entity, dto,
566                 new String[]{"affiliatedOrgs", "feeAmounts", "attributes", "descr", "meta"});
567 
568         dto.setAffiliatedOrgs(toAffiliatedOrgInfos(entity.getAffiliatedOrgs()));
569         dto.setFeeAmounts(toFeeAmounts(entity.getFeeAmounts()));
570         dto.setDescr(toRichTextInfo(entity.getDescr()));
571         dto.setAttributes(toAttributeList(entity.getAttributes()));
572         dto.setMeta(toMetaInfo(entity.getMeta(), entity.getVersionNumber()));
573 
574         return dto;
575     }
576 
577     private static List<CurrencyAmountInfo> toFeeAmounts(List<CluFeeAmount> cluFees) {
578         List<CurrencyAmountInfo> feeAmounts = new ArrayList<CurrencyAmountInfo>();
579 
580         if (cluFees != null) {
581             for (CluFeeAmount cluFeeAmount : cluFees) {
582                 CurrencyAmountInfo dto = new CurrencyAmountInfo();
583                 CurrencyAmount ca = cluFeeAmount.getCurrencyAmount();
584                 if (ca != null) {
585                     dto.setCurrencyQuantity(ca.getCurrencyQuantity());
586                     dto.setCurrencyTypeKey(ca.getCurrencyTypeKey());
587                 }
588                 feeAmounts.add(dto);
589             }
590         }
591 
592         return feeAmounts;
593     }
594 
595     private static List<AffiliatedOrgInfo> toAffiliatedOrgInfos(
596             List<AffiliatedOrg> entities) {
597         List<AffiliatedOrgInfo> dtos = new ArrayList<AffiliatedOrgInfo>();
598         if (entities != null) {
599             for (AffiliatedOrg record : entities) {
600                 dtos.add(toAffiliatedOrgInfo(record));
601             }
602         }
603         return dtos;
604     }
605 
606     private static AffiliatedOrgInfo toAffiliatedOrgInfo(AffiliatedOrg entity) {
607         if (entity == null) {
608             return null;
609         }
610 
611         AffiliatedOrgInfo dto = new AffiliatedOrgInfo();
612 
613         BeanUtils.copyProperties(entity, dto);
614         return dto;
615     }
616 
617     public static CluAccountingInfo toCluAccountingInfo(CluAccounting entity) {
618         if (entity == null) {
619             return null;
620         }
621         CluAccountingInfo dto = new CluAccountingInfo();
622         dto.setId(entity.getId());
623         dto.setAffiliatedOrgs(toAffiliatedOrgInfos(entity.getAffiliatedOrgs()));
624         dto.setAttributes(toAttributeList(entity.getAttributes()));
625         return dto;
626     }
627 
628     public static Amount toAmount(AmountInfo amountInfo) {
629         if (amountInfo == null) {
630             return null;
631         }
632         Amount amount = new Amount();
633         amount.setUnitQuantity(amountInfo.getUnitQuantity());
634         amount.setUnitType(amountInfo.getUnitTypeKey());
635         return amount;
636     }
637 
638     public static TimeAmount toTimeAmount(TimeAmountInfo timeAmountInfo) {
639         if (timeAmountInfo == null) {
640             return null;
641         }
642         TimeAmount timeAmount = new TimeAmount();
643         BeanUtils.copyProperties(timeAmountInfo, timeAmount);
644         return timeAmount;
645     }
646 
647     public static CluCredit toCluCredit(CluCreditInfo cluCreditInfo) {
648         if (cluCreditInfo == null) {
649             return null;
650         }
651         CluCredit cluCredit = new CluCredit();
652 
653         cluCredit.setMaxAllowableInactivity(CluServiceAssembler.toTimeAmount(cluCreditInfo.getMaxAllowableInactivity()));
654         cluCredit.setMaxTimeResultsRecognized(CluServiceAssembler.toTimeAmount(cluCreditInfo.getMaxTimeResultsRecognized()));
655         cluCredit.setMaxTimeToComplete(CluServiceAssembler.toTimeAmount(cluCreditInfo.getMaxTimeToComplete()));
656         cluCredit.setMinTimeToComplete(CluServiceAssembler.toTimeAmount(cluCreditInfo.getMinTimeToComplete()));
657         cluCredit.setRepeatTime(CluServiceAssembler.toTimeAmount(cluCreditInfo.getRepeatTime()));
658 
659         BeanUtils.copyProperties(cluCreditInfo, cluCredit, new String[]{
660                 "repeatTime", "minTimeToComplete", "maxTimeToComplete",
661                 "maxAllowableInactivity", "maxTimeResultsRecognized"});
662 
663         return cluCredit;
664     }
665 
666     public static void copyCluCredit(CluCreditInfo cluCreditInfo,
667                                      CluCredit entity) {
668         if (entity == null) {
669             return;
670         }
671         if (entity.getMaxAllowableInactivity() == null) {
672             entity.setMaxAllowableInactivity(new TimeAmount());
673         }
674         BeanUtils.copyProperties(cluCreditInfo.getMaxAllowableInactivity(),
675                 entity.getMaxAllowableInactivity());
676 
677         if (entity.getMaxTimeResultsRecognized() == null) {
678             entity.setMaxTimeResultsRecognized(new TimeAmount());
679         }
680         BeanUtils.copyProperties(cluCreditInfo.getMaxTimeResultsRecognized(),
681                 entity.getMaxTimeResultsRecognized());
682 
683         if (entity.getMaxTimeToComplete() == null) {
684             entity.setMaxTimeToComplete(new TimeAmount());
685         }
686         BeanUtils.copyProperties(cluCreditInfo.getMaxTimeToComplete(), entity.getMaxTimeToComplete());
687 
688         if (entity.getMinTimeToComplete() == null) {
689             entity.setMinTimeToComplete(new TimeAmount());
690         }
691         BeanUtils.copyProperties(cluCreditInfo.getMinTimeToComplete(), entity.getMinTimeToComplete());
692 
693         if (entity.getRepeatTime() == null) {
694             entity.setRepeatTime(new TimeAmount());
695         }
696         BeanUtils.copyProperties(cluCreditInfo.getRepeatTime(), entity.getRepeatTime());
697 
698         BeanUtils.copyProperties(cluCreditInfo, entity, new String[]{
699                 "repeatTime", "minTimeToComplete", "maxTimeToComplete",
700                 "maxAllowableInactivity", "maxTimeResultsRecognized"});
701 
702     }
703 
704     public static List<AccreditationInfo> toAccreditationInfos(
705             List<CluAccreditation> entities) {
706         List<AccreditationInfo> dtos = new ArrayList<AccreditationInfo>(entities.size());
707 
708         if (entities != null) {
709             for (CluAccreditation entity : entities) {
710                 dtos.add(toAccreditationInfo(entity));
711             }
712         }
713         return dtos;
714     }
715 
716     public static AccreditationInfo toAccreditationInfo(CluAccreditation entity) {
717         if (entity == null) {
718             return null;
719         }
720         AccreditationInfo dto = new AccreditationInfo();
721 
722         BeanUtils.copyProperties(entity, dto, new String[]{"attributes", "meta"});
723 
724         dto.setAttributes(toAttributeList(entity.getAttributes()));
725         dto.setMeta(toMetaInfo(entity.getMeta(), entity.getVersionNumber()));
726 
727         return dto;
728 
729     }
730 
731     public static List<AdminOrgInfo> toCluAdminOrgInfos(
732             List<CluAdminOrg> entities) {
733         List<AdminOrgInfo> dtos = new ArrayList<AdminOrgInfo>(entities.size());
734         if (entities != null) {
735             for (CluAdminOrg entity : entities) {
736                 dtos.add(toAdminOrgInfo(entity));
737             }
738         }
739         return dtos;
740     }
741 
742     public static AdminOrgInfo toAdminOrgInfo(CluAdminOrg entity) {
743         if (entity == null) {
744             return null;
745         }
746         AdminOrgInfo dto = new AdminOrgInfo();
747         BeanUtils.copyProperties(entity, dto, new String[]{"attributes"});
748         dto.setAttributes(toAttributeList(entity.getAttributes()));
749 
750         return dto;
751     }
752 
753     public static CluFee toCluFee(Clu clu, boolean isUpdate, CluFeeInfo feeInfo,
754                                   LuDao dao) throws DoesNotExistException, VersionMismatchException,
755             InvalidParameterException {
756         if (feeInfo == null) {
757             return null;
758         }
759 
760         CluFee fee = null;
761 
762         if (isUpdate) {
763             fee = clu.getFee();
764             if (!String.valueOf(fee.getVersionNumber()).equals(
765                     feeInfo.getMeta().getVersionInd())) {
766                 throw new VersionMismatchException(
767                         "CluFee to be updated is not the current version");
768             }
769         }
770         if (fee == null) {
771             fee = new CluFee();
772         }
773 
774         if (feeInfo.getDescr() != null && (feeInfo.getDescr().getPlain() != null || feeInfo.getDescr().getFormatted() != null)) {
775             if (fee.getDescr() == null) {
776                 fee.setDescr(new LuRichText());
777             }
778             BeanUtils.copyProperties(feeInfo.getDescr(), fee.getDescr());
779         } else if (isUpdate && fee.getDescr() != null) {
780             dao.delete(fee.getDescr());
781             fee.setDescr(null);
782         }
783 
784         fee.setAttributes(CluServiceAssembler.toGenericAttributes(
785                 CluFeeAttribute.class, feeInfo.getAttributes(), fee, dao));
786         toCluFeeRecords(isUpdate, fee, feeInfo.getCluFeeRecords(), dao);
787 
788         return fee;
789     }
790 
791     public static void toCluFeeRecords(boolean isUpdate,
792                                        CluFee cluFee, List<CluFeeRecordInfo> cluFeeRecords, LuDao dao) throws InvalidParameterException {
793 
794         if (cluFeeRecords == null) {
795             return;
796         }
797 
798         if (!isUpdate) {
799 
800             for (CluFeeRecordInfo feeRecordInfo : cluFeeRecords) {
801                 CluFeeRecord feeRec = new CluFeeRecord();
802                 feeRec.setAffiliatedOrgs(toAffiliatedOrgs(isUpdate, feeRec.getAffiliatedOrgs(), feeRecordInfo.getAffiliatedOrgs(), dao));
803                 feeRec.setFeeType(feeRecordInfo.getFeeType());
804                 feeRec.setRateType(feeRecordInfo.getRateType());
805                 feeRec.setDescr(toRichText(LuRichText.class, feeRecordInfo.getDescr()));
806                 feeRec.setFeeAmounts(toFeeAmounts(isUpdate, feeRec.getFeeAmounts(), feeRecordInfo.getFeeAmounts(), dao));
807                 feeRec.setAttributes(CluServiceAssembler.toGenericAttributes(
808                         CluFeeRecordAttribute.class, feeRecordInfo.getAttributes(), feeRec, dao));
809                 if (cluFee.getCluFeeRecords() == null) {
810                     cluFee.setCluFeeRecords(new ArrayList<CluFeeRecord>());
811                 }
812                 cluFee.getCluFeeRecords().add(feeRec);
813             }
814         } else {
815             Map<String, CluFeeRecord> oldFeeRecMap = new HashMap<String, CluFeeRecord>();
816             if (cluFee.getCluFeeRecords() != null) {
817                 for (CluFeeRecord feeRec : cluFee.getCluFeeRecords()) {
818                     oldFeeRecMap.put(feeRec.getId(), feeRec);
819                 }
820                 cluFee.getCluFeeRecords().clear();
821             }
822 
823             // Loop through the new list, if the item exists already update and
824             // remove from the list
825             // otherwise create a new entry
826             for (CluFeeRecordInfo feeRecordInfo : cluFeeRecords) {
827                 CluFeeRecord feeRec = oldFeeRecMap.remove(feeRecordInfo.getId());
828                 if (feeRec == null) {
829                     feeRec = new CluFeeRecord();
830                 }
831 
832                 feeRec.setAffiliatedOrgs(toAffiliatedOrgs(isUpdate, feeRec.getAffiliatedOrgs(), feeRecordInfo.getAffiliatedOrgs(), dao));
833                 feeRec.setFeeType(feeRecordInfo.getFeeType());
834                 feeRec.setRateType(feeRecordInfo.getRateType());
835                 feeRec.setDescr(toRichText(LuRichText.class, feeRecordInfo.getDescr()));
836                 feeRec.setFeeAmounts(toFeeAmounts(isUpdate, feeRec.getFeeAmounts(), feeRecordInfo.getFeeAmounts(), dao));
837                 feeRec.setAttributes(CluServiceAssembler.toGenericAttributes(
838                         CluFeeRecordAttribute.class, feeRecordInfo.getAttributes(), feeRec, dao));
839                 if (cluFee.getCluFeeRecords() == null) {
840                     cluFee.setCluFeeRecords(new ArrayList<CluFeeRecord>());
841                 }
842                 cluFee.getCluFeeRecords().add(feeRec);
843             }
844 
845             // Now delete anything left over
846             for (Entry<String, CluFeeRecord> entry : oldFeeRecMap.entrySet()) {
847                 dao.delete(entry.getValue());
848             }
849         }
850     }
851 
852     public static List<AffiliatedOrg> toAffiliatedOrgs(boolean isUpdate, List<AffiliatedOrg> orgList, List<AffiliatedOrgInfo> affiliatedOrgInfoList, LuDao dao) {
853         if (null == affiliatedOrgInfoList) {
854             return orgList;
855         }
856         if (orgList == null) {
857             orgList = new ArrayList<AffiliatedOrg>();
858         }
859 
860         if (!isUpdate) {
861 
862             for (AffiliatedOrgInfo orgInfo : affiliatedOrgInfoList) {
863                 AffiliatedOrg org = new AffiliatedOrg();
864                 BeanUtils.copyProperties(orgInfo, org);
865                 orgList.add(org);
866             }
867         } else {
868             Map<String, AffiliatedOrg> oldOrgMap = new HashMap<String, AffiliatedOrg>();
869             for (AffiliatedOrg org : orgList) {
870                 oldOrgMap.put(org.getId(), org);
871             }
872             orgList.clear();
873 
874             for (AffiliatedOrgInfo orgInfo : affiliatedOrgInfoList) {
875                 AffiliatedOrg org = oldOrgMap.remove(orgInfo.getId());
876                 if (org == null) {
877                     org = new AffiliatedOrg();
878                 }
879 
880                 BeanUtils.copyProperties(orgInfo, org);
881 
882                 orgList.add(org);
883             }
884 
885             // Now delete anything left over
886             for (Entry<String, AffiliatedOrg> entry : oldOrgMap.entrySet()) {
887                 dao.delete(entry.getValue());
888             }
889         }
890 
891         return orgList;
892     }
893 
894     public static List<CluFeeAmount> toFeeAmounts(boolean isUpdate, List<CluFeeAmount> caList, List<CurrencyAmountInfo> caInfoList, LuDao dao) {
895         if (null == caInfoList) {
896             return caList;
897         }
898         if (caList == null) {
899             caList = new ArrayList<CluFeeAmount>(caInfoList.size());
900         }
901 
902         if (!isUpdate) {
903             for (CurrencyAmountInfo caInfo : caInfoList) {
904                 CurrencyAmount ca = new CurrencyAmount();
905                 ca.setCurrencyQuantity(caInfo.getCurrencyQuantity());
906                 ca.setCurrencyTypeKey(caInfo.getCurrencyTypeKey());
907                 CluFeeAmount cluFeeAmount = new CluFeeAmount();
908                 cluFeeAmount.setCurrencyAmount(ca);
909                 caList.add(cluFeeAmount);
910             }
911         } else {
912             // Delete existing fee amounts (this assumes feeAmounts are not individually updated)
913             for (CluFeeAmount cluFeeAmount : caList) {
914                 dao.delete(cluFeeAmount);
915             }
916             caList.clear();
917 
918             for (CurrencyAmountInfo caInfo : caInfoList) {
919                 CurrencyAmount ca = new CurrencyAmount();
920 
921                 ca.setCurrencyQuantity(caInfo.getCurrencyQuantity());
922                 ca.setCurrencyTypeKey(caInfo.getCurrencyTypeKey());
923                 CluFeeAmount cluFeeAmount = new CluFeeAmount();
924                 cluFeeAmount.setCurrencyAmount(ca);
925                 caList.add(cluFeeAmount);
926             }
927         }
928 
929         return caList;
930     }
931 
932     public static CluIdentifier createOfficialIdentifier(CluInfo cluInfo, LuDao dao) throws InvalidParameterException {
933         CluIdentifier officialIdentifier = new CluIdentifier();
934         BeanUtils.copyProperties(cluInfo.getOfficialIdentifier(),
935                 officialIdentifier, new String[]{"attributes"});
936         officialIdentifier.setAttributes(CluServiceAssembler.toGenericAttributes(
937                 CluIdentifierAttribute.class, cluInfo.getOfficialIdentifier().getAttributes(), officialIdentifier, dao));
938 
939         return officialIdentifier;
940     }
941 
942     public static void updateOfficialIdentifier(Clu clu, CluInfo cluInfo, LuDao dao) throws InvalidParameterException {
943         if (clu.getOfficialIdentifier() == null) {
944             clu.setOfficialIdentifier(new CluIdentifier());
945         }
946         BeanUtils.copyProperties(cluInfo.getOfficialIdentifier(), clu.getOfficialIdentifier(), new String[]{"id", "attributes"});
947 
948         clu.getOfficialIdentifier().setAttributes(CluServiceAssembler.toGenericAttributes(
949                 CluIdentifierAttribute.class, cluInfo.getOfficialIdentifier().getAttributes(), clu.getOfficialIdentifier(), dao));
950 
951     }
952 
953     public static List<CluIdentifier> createAlternateIdentifiers(CluInfo cluInfo, LuDao dao) throws InvalidParameterException {
954         List<CluIdentifier> alternateIdentifiers = new ArrayList<CluIdentifier>(0);
955         for (CluIdentifierInfo cluIdInfo : cluInfo.getAlternateIdentifiers()) {
956             CluIdentifier identifier = new CluIdentifier();
957             BeanUtils.copyProperties(cluIdInfo, identifier, new String[]{"attributes"});
958 
959             identifier.setAttributes(CluServiceAssembler.toGenericAttributes(
960                     CluIdentifierAttribute.class, cluIdInfo.getAttributes(), identifier, dao));
961 
962             alternateIdentifiers.add(identifier);
963         }
964         return alternateIdentifiers;
965     }
966 
967     public static void updateAlternateIdentifier(Map<String, CluIdentifier> oldAltIdMap, Clu clu, CluInfo cluInfo, LuDao dao) throws InvalidParameterException {
968         for (CluIdentifier altIdentifier : clu.getAlternateIdentifiers()) {
969             oldAltIdMap.put(altIdentifier.getId(), altIdentifier);
970         }
971         clu.getAlternateIdentifiers().clear();
972 
973         // Loop through the new list, if the item exists already update and
974         // remove from the list
975         // otherwise create a new entry
976         for (CluIdentifierInfo cluIdInfo : cluInfo.getAlternateIdentifiers()) {
977             CluIdentifier identifier = oldAltIdMap.remove(cluIdInfo.getId());
978             if (identifier == null) {
979                 identifier = new CluIdentifier();
980             }
981             // Do Copy
982             BeanUtils.copyProperties(cluIdInfo, identifier, new String[]{"attributes"});
983 
984             identifier.setAttributes(CluServiceAssembler.toGenericAttributes(
985                     CluIdentifierAttribute.class, cluIdInfo.getAttributes(), identifier, dao));
986 
987             clu.getAlternateIdentifiers().add(identifier);
988         }
989     }
990 
991     public static List<CluPublicationVariant> toCluPublicationVariants(
992             List<FieldInfo> variantInfos, CluPublication cluPub, LuDao luDao) {
993         List<CluPublicationVariant> variants = new ArrayList<CluPublicationVariant>();
994 
995         if (cluPub.getVariants() == null) {
996             cluPub.setVariants(new ArrayList<CluPublicationVariant>());
997         }
998 
999         // Delete all the old attributes(if the owner is not null)
1000         for (CluPublicationVariant variant : cluPub.getVariants()) {
1001             luDao.delete(variant);
1002         }
1003         cluPub.getVariants().clear();
1004 
1005         for (FieldInfo variantInfo : variantInfos) {
1006             CluPublicationVariant variant = new CluPublicationVariant();
1007             variant.setKey(variantInfo.getId());
1008             variant.setValue(variantInfo.getValue());
1009             variant.setOwner(cluPub);
1010             variants.add(variant);
1011         }
1012 
1013         return variants;
1014     }
1015 
1016     public static CluPublicationInfo toCluPublicationInfo(CluPublication cluPub) {
1017         if (cluPub == null) {
1018             return null;
1019         }
1020         CluPublicationInfo cluPubInfo = new CluPublicationInfo();
1021         cluPubInfo.setCluId(cluPub.getClu().getId());
1022         cluPubInfo.setId(cluPub.getId());
1023         cluPubInfo.setEndCycle(cluPub.getEndCycle());
1024         cluPubInfo.setStartCycle(cluPub.getStartCycle());
1025         cluPubInfo.setEffectiveDate(cluPub.getEffectiveDate());
1026         cluPubInfo.setExpirationDate(cluPub.getExpirationDate());
1027         cluPubInfo.setStateKey(cluPub.getState());
1028         cluPubInfo.setTypeKey(cluPub.getType().getId());
1029         cluPubInfo.setMeta(toMetaInfo(cluPub.getMeta(), cluPub.getVersionNumber()));
1030         cluPubInfo.setAttributes(CluServiceAssembler.toAttributeList(cluPub.getAttributes()));
1031         cluPubInfo.setVariants(CluServiceAssembler.toCluPublicationVariantInfos(cluPub.getVariants()));
1032 
1033         return cluPubInfo;
1034     }
1035 
1036     private static List<FieldInfo> toCluPublicationVariantInfos(
1037             List<CluPublicationVariant> variants) {
1038         if (variants == null) {
1039             return new ArrayList<FieldInfo>(0);
1040         }
1041         List<FieldInfo> fields = new ArrayList<FieldInfo>();
1042         for (CluPublicationVariant variant : variants) {
1043             FieldInfo field = new FieldInfo();
1044             field.setId(variant.getKey());
1045             field.setValue(variant.getValue());
1046             fields.add(field);
1047         }
1048         return fields;
1049     }
1050 
1051     public static List<CluPublicationInfo> toCluPublicationInfos(
1052             List<CluPublication> cluPublications) {
1053         if (cluPublications == null) {
1054             return new ArrayList<CluPublicationInfo>(0);
1055         }
1056         List<CluPublicationInfo> cluPublicationInfos = new ArrayList<CluPublicationInfo>(cluPublications.size());
1057         for (CluPublication cluPublication : cluPublications) {
1058             cluPublicationInfos.add(toCluPublicationInfo(cluPublication));
1059         }
1060         return cluPublicationInfos;
1061     }
1062 }