1 package org.kuali.student.enrollment.class2.acal.service.assembler; 2 3 import org.kuali.student.enrollment.acal.dto.AcademicCalendarInfo; 4 import org.kuali.student.r2.common.assembler.AssemblyException; 5 import org.kuali.student.r2.common.assembler.DTOAssembler; 6 import org.kuali.student.r2.common.dto.AttributeInfo; 7 import org.kuali.student.r2.common.dto.ContextInfo; 8 import org.kuali.student.r2.core.constants.AtpServiceConstants; 9 import org.kuali.student.r2.core.atp.dto.AtpAtpRelationInfo; 10 import org.kuali.student.r2.core.atp.dto.AtpInfo; 11 import org.kuali.student.r2.core.atp.service.AtpService; 12 13 import java.util.ArrayList; 14 import java.util.List; 15 16 public class AcademicCalendarAssembler implements DTOAssembler<AcademicCalendarInfo, AtpInfo> { 17 18 private AtpService atpService; 19 20 public AtpService getAtpService() { 21 return atpService; 22 } 23 24 public void setAtpService(AtpService atpService) { 25 this.atpService = atpService; 26 } 27 28 @Override 29 public AcademicCalendarInfo assemble(AtpInfo atp, ContextInfo context) throws AssemblyException { 30 31 AcademicCalendarInfo acal = new AcademicCalendarInfo(); 32 acal.setId(atp.getId()); 33 acal.setName(atp.getName()); 34 acal.setDescr(atp.getDescr()); 35 acal.setStartDate(atp.getStartDate()); 36 acal.setEndDate(atp.getEndDate()); 37 acal.setTypeKey(atp.getTypeKey()); 38 acal.setStateKey(atp.getStateKey()); 39 acal.setMeta(atp.getMeta()); 40 acal.getAttributes().addAll(atp.getAttributes()); 41 acal.setAdminOrgId(atp.getAdminOrgId()); 42 acal.getHolidayCalendarIds().addAll(assembleHolidayCalendarIdsFromRelations(atp.getId(), 43 AtpServiceConstants.ATP_HOLIDAY_CALENDAR_TYPE_KEY, 44 context)); 45 return acal; 46 } 47 48 private List<String> assembleHolidayCalendarIdsFromRelations(String atpId, 49 String relatedAtpType, 50 ContextInfo context) 51 throws AssemblyException { 52 List<String> holidayCalendarIds = new ArrayList<String>(); 53 List<AtpAtpRelationInfo> atpRels; 54 try { 55 atpRels = atpService.getAtpAtpRelationsByAtp(atpId, context); 56 for (AtpAtpRelationInfo atpRelInfo : atpRels) { 57 if (atpRelInfo.getAtpId().equals(atpId)) { 58 if (atpRelInfo.getTypeKey().equals(AtpServiceConstants.ATP_ATP_RELATION_ASSOCIATED_TYPE_KEY)) { 59 AtpInfo atp = atpService.getAtp(atpRelInfo.getRelatedAtpId(), context); 60 if (atp.getTypeKey().equals(relatedAtpType)) { 61 holidayCalendarIds.add(atpRelInfo.getRelatedAtpId()); 62 } 63 } 64 } 65 } 66 } catch (Exception e) { 67 throw new AssemblyException(e.getMessage()); 68 } 69 return holidayCalendarIds; 70 } 71 72 @Override 73 public AtpInfo disassemble(AcademicCalendarInfo acal, ContextInfo context) throws AssemblyException { 74 AtpInfo atp = new AtpInfo(); 75 atp.setId(acal.getId()); 76 atp.setName(acal.getName()); 77 atp.setDescr(acal.getDescr()); 78 atp.setAdminOrgId(acal.getAdminOrgId()); 79 atp.setStartDate(acal.getStartDate()); 80 atp.setEndDate(acal.getEndDate()); 81 atp.setTypeKey(AtpServiceConstants.ATP_ACADEMIC_CALENDAR_TYPE_KEY); 82 atp.setStateKey(acal.getStateKey()); 83 atp.setMeta(acal.getMeta()); 84 atp.getAttributes().addAll(acal.getAttributes()); 85 86 return atp; 87 } 88 }