| 1 | |
package org.kuali.student.enrollment.classII.acal.service.assembler; |
| 2 | |
|
| 3 | |
import java.util.ArrayList; |
| 4 | |
import java.util.HashMap; |
| 5 | |
import java.util.List; |
| 6 | |
import java.util.Map; |
| 7 | |
|
| 8 | |
import org.kuali.student.common.util.UUIDHelper; |
| 9 | |
import org.kuali.student.enrollment.acal.dto.AcademicCalendarInfo; |
| 10 | |
import org.kuali.student.r2.common.dto.AttributeInfo; |
| 11 | |
import org.kuali.student.r2.common.dto.ContextInfo; |
| 12 | |
import org.kuali.student.r2.common.exceptions.AlreadyExistsException; |
| 13 | |
import org.kuali.student.r2.common.exceptions.DataValidationErrorException; |
| 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.MissingParameterException; |
| 17 | |
import org.kuali.student.r2.common.exceptions.OperationFailedException; |
| 18 | |
import org.kuali.student.r2.common.exceptions.PermissionDeniedException; |
| 19 | |
import org.kuali.student.r2.common.exceptions.VersionMismatchException; |
| 20 | |
import org.kuali.student.r2.common.infc.AtpAssembler; |
| 21 | |
import org.kuali.student.r2.common.util.constants.AtpServiceConstants; |
| 22 | |
import org.kuali.student.r2.core.atp.dto.AtpAtpRelationInfo; |
| 23 | |
import org.kuali.student.r2.core.atp.dto.AtpInfo; |
| 24 | |
import org.kuali.student.r2.core.atp.service.AtpService; |
| 25 | |
|
| 26 | 0 | public class AcademicCalendarAssembler implements AtpAssembler<AcademicCalendarInfo, AtpInfo> { |
| 27 | |
private AtpService atpService; |
| 28 | |
|
| 29 | |
public AtpService getAtpService() { |
| 30 | 0 | return atpService; |
| 31 | |
} |
| 32 | |
|
| 33 | |
public void setAtpService(AtpService atpService) { |
| 34 | 0 | this.atpService = atpService; |
| 35 | 0 | } |
| 36 | |
|
| 37 | |
@Override |
| 38 | |
public AcademicCalendarInfo assemble(AtpInfo atp, ContextInfo context) { |
| 39 | 0 | if(atp != null){ |
| 40 | 0 | AcademicCalendarInfo acal = AcademicCalendarInfo.newInstance(); |
| 41 | 0 | acal.setKey(atp.getKey()); |
| 42 | 0 | acal.setName(atp.getName()); |
| 43 | 0 | acal.setDescr(atp.getDescr()); |
| 44 | 0 | acal.setStartDate(atp.getStartDate()); |
| 45 | 0 | acal.setEndDate(atp.getEndDate()); |
| 46 | 0 | acal.setTypeKey(atp.getTypeKey()); |
| 47 | 0 | acal.setStateKey(atp.getStateKey()); |
| 48 | 0 | acal.setMetaInfo(atp.getMetaInfo()); |
| 49 | 0 | acal.setAttributes(atp.getAttributes()); |
| 50 | |
|
| 51 | 0 | List<AttributeInfo> attributes = atp.getAttributes(); |
| 52 | 0 | if(attributes != null && !attributes.isEmpty()){ |
| 53 | 0 | for (AttributeInfo attribute : attributes){ |
| 54 | 0 | if(attribute.getKey().equals("CredentialProgramType")){ |
| 55 | 0 | acal.setCredentialProgramTypeKey(attribute.getValue()); |
| 56 | 0 | break; |
| 57 | |
} |
| 58 | |
} |
| 59 | |
} |
| 60 | |
|
| 61 | |
|
| 62 | 0 | assembleAtpAtpRelations(atp.getKey(), acal, context); |
| 63 | 0 | return acal; |
| 64 | |
} |
| 65 | |
else |
| 66 | 0 | return null; |
| 67 | |
} |
| 68 | |
|
| 69 | |
private List<String> assembleAtpAtpRelations(String atpKey, AcademicCalendarInfo acal, ContextInfo context){ |
| 70 | 0 | List<String> ccKeys = new ArrayList<String>(); |
| 71 | |
List<AtpAtpRelationInfo> atpRels; |
| 72 | |
try { |
| 73 | 0 | atpRels = atpService.getAtpAtpRelationsByAtp(atpKey, context); |
| 74 | |
|
| 75 | 0 | if(atpRels != null && !atpRels.isEmpty()){ |
| 76 | 0 | for(AtpAtpRelationInfo atpRel : atpRels){ |
| 77 | 0 | if(atpRel.getTypeKey().equals(AtpServiceConstants.ATP_ATP_RELATION_INCLUDES_TYPE_KEY)){ |
| 78 | 0 | ccKeys.add(atpRel.getRelatedAtpKey()); |
| 79 | |
} |
| 80 | |
} |
| 81 | |
} |
| 82 | 0 | } catch (Exception e) { |
| 83 | 0 | return null; |
| 84 | 0 | } |
| 85 | |
|
| 86 | 0 | return ccKeys; |
| 87 | |
} |
| 88 | |
|
| 89 | |
@Override |
| 90 | |
public AtpInfo disassemble(AcademicCalendarInfo acal, ContextInfo context) { |
| 91 | 0 | AtpInfo atp = AtpInfo.newInstance(); |
| 92 | 0 | atp.setKey(acal.getKey()); |
| 93 | 0 | atp.setName(acal.getName()); |
| 94 | 0 | atp.setDescr(acal.getDescr()); |
| 95 | 0 | atp.setStartDate(acal.getStartDate()); |
| 96 | 0 | atp.setEndDate(acal.getEndDate()); |
| 97 | 0 | atp.setTypeKey(AtpServiceConstants.ATP_ACADEMIC_CALENDAR_TYPE_KEY); |
| 98 | 0 | atp.setStateKey(acal.getStateKey()); |
| 99 | 0 | atp.setMetaInfo(acal.getMetaInfo()); |
| 100 | |
|
| 101 | 0 | List<AttributeInfo> attributes = (null != acal.getAttributes()? acal.getAttributes(): new ArrayList<AttributeInfo>()); |
| 102 | |
|
| 103 | 0 | if(acal.getCredentialProgramTypeKey() != null){ |
| 104 | 0 | AttributeInfo cpt = AttributeInfo.newInstance(); |
| 105 | 0 | cpt.setKey("CredentialProgramType"); |
| 106 | 0 | cpt.setValue(acal.getCredentialProgramTypeKey()); |
| 107 | 0 | attributes.add(cpt); |
| 108 | |
} |
| 109 | 0 | atp.setAttributes(attributes); |
| 110 | |
|
| 111 | 0 | if(acal.getCampusCalendarKeys() != null && !acal.getCampusCalendarKeys().isEmpty()){ |
| 112 | |
try{ |
| 113 | |
|
| 114 | 0 | disassembleAtpAtpRelations(acal.getKey(), acal.getCampusCalendarKeys(), acal.getStateKey(), context); |
| 115 | 0 | }catch (Exception e){ |
| 116 | 0 | return null; |
| 117 | 0 | } |
| 118 | |
} |
| 119 | |
|
| 120 | 0 | return atp; |
| 121 | |
} |
| 122 | |
|
| 123 | |
private void disassembleAtpAtpRelations(String atpKey, List<String> relatedAtpKeys, String state, ContextInfo context) throws AlreadyExistsException, |
| 124 | |
DataValidationErrorException, InvalidParameterException, MissingParameterException, |
| 125 | |
OperationFailedException, PermissionDeniedException { |
| 126 | |
|
| 127 | |
try { |
| 128 | 0 | List<AtpAtpRelationInfo > atpRels = atpService.getAtpAtpRelationsByAtp(atpKey, context); |
| 129 | 0 | Map<String, String> currentRelIds = new HashMap<String, String>(); |
| 130 | |
|
| 131 | 0 | if(atpRels != null && !atpRels.isEmpty()){ |
| 132 | 0 | for(AtpAtpRelationInfo atpRelInfo : atpRels){ |
| 133 | 0 | if(AtpServiceConstants.ATP_ATP_RELATION_INCLUDES_TYPE_KEY.equals(atpRelInfo.getTypeKey())){ |
| 134 | 0 | currentRelIds.put(atpRelInfo.getRelatedAtpKey(), atpRelInfo.getId()); |
| 135 | |
} |
| 136 | |
} |
| 137 | |
} |
| 138 | |
|
| 139 | 0 | for (String relatedKey : relatedAtpKeys){ |
| 140 | 0 | if (!currentRelIds.containsKey(relatedKey)) |
| 141 | 0 | createAtpAtpRelations(atpKey, relatedKey, state, context); |
| 142 | |
else |
| 143 | 0 | updateAtpAtpRelations(currentRelIds.get(relatedKey), state, context); |
| 144 | |
} |
| 145 | |
|
| 146 | 0 | } catch (DoesNotExistException e) { |
| 147 | |
|
| 148 | 0 | for (String relatedKey : relatedAtpKeys){ |
| 149 | 0 | createAtpAtpRelations(atpKey, relatedKey, state, context); |
| 150 | |
} |
| 151 | 0 | } |
| 152 | 0 | } |
| 153 | |
|
| 154 | |
private void createAtpAtpRelations(String atpKey, String relatedAtpKey, String state, ContextInfo context) throws AlreadyExistsException, |
| 155 | |
DataValidationErrorException, InvalidParameterException, MissingParameterException, |
| 156 | |
OperationFailedException, PermissionDeniedException { |
| 157 | 0 | AtpAtpRelationInfo atpRel = AtpAtpRelationInfo.newInstance(); |
| 158 | 0 | atpRel.setId(UUIDHelper.genStringUUID()); |
| 159 | 0 | atpRel.setAtpKey(atpKey); |
| 160 | 0 | atpRel.setRelatedAtpKey(relatedAtpKey); |
| 161 | 0 | atpRel.setTypeKey(AtpServiceConstants.ATP_ATP_RELATION_INCLUDES_TYPE_KEY); |
| 162 | 0 | atpRel.setStateKey(state); |
| 163 | 0 | atpService.createAtpAtpRelation(atpRel, context); |
| 164 | 0 | } |
| 165 | |
|
| 166 | |
private void updateAtpAtpRelations(String atpAtpRelationId, String state, ContextInfo context) throws AlreadyExistsException, |
| 167 | |
DataValidationErrorException, InvalidParameterException, MissingParameterException, |
| 168 | |
OperationFailedException, PermissionDeniedException { |
| 169 | |
AtpAtpRelationInfo atpRel; |
| 170 | |
try { |
| 171 | 0 | atpRel = AtpAtpRelationInfo.getInstance(atpService.getAtpAtpRelation(atpAtpRelationId, context)); |
| 172 | 0 | atpRel.setStateKey(state); |
| 173 | |
try { |
| 174 | 0 | atpService.updateAtpAtpRelation(atpAtpRelationId, atpRel, context); |
| 175 | 0 | } catch (DoesNotExistException e) { |
| 176 | 0 | e.printStackTrace(); |
| 177 | 0 | } catch (VersionMismatchException e) { |
| 178 | 0 | e.printStackTrace(); |
| 179 | 0 | } |
| 180 | 0 | } catch (DoesNotExistException e1) { |
| 181 | 0 | e1.printStackTrace(); |
| 182 | 0 | } |
| 183 | 0 | } |
| 184 | |
} |