| 1 | |
package org.kuali.student.enrollment.class2.acal.service.assembler; |
| 2 | |
|
| 3 | |
import java.util.ArrayList; |
| 4 | |
import java.util.Date; |
| 5 | |
import java.util.HashMap; |
| 6 | |
import java.util.List; |
| 7 | |
import java.util.Map; |
| 8 | |
|
| 9 | |
import org.kuali.student.common.util.UUIDHelper; |
| 10 | |
import org.kuali.student.r2.common.assembler.AssemblyException; |
| 11 | |
import org.kuali.student.r2.common.assembler.DTOAssembler; |
| 12 | |
import org.kuali.student.r2.common.dto.ContextInfo; |
| 13 | |
import org.kuali.student.r2.common.dto.StatusInfo; |
| 14 | |
import org.kuali.student.r2.common.exceptions.AlreadyExistsException; |
| 15 | |
import org.kuali.student.r2.common.exceptions.DataValidationErrorException; |
| 16 | |
import org.kuali.student.r2.common.exceptions.DoesNotExistException; |
| 17 | |
import org.kuali.student.r2.common.exceptions.InvalidParameterException; |
| 18 | |
import org.kuali.student.r2.common.exceptions.MissingParameterException; |
| 19 | |
import org.kuali.student.r2.common.exceptions.OperationFailedException; |
| 20 | |
import org.kuali.student.r2.common.exceptions.PermissionDeniedException; |
| 21 | |
import org.kuali.student.r2.common.exceptions.VersionMismatchException; |
| 22 | |
import org.kuali.student.r2.common.util.constants.AtpServiceConstants; |
| 23 | |
import org.kuali.student.r2.core.atp.dto.AtpAtpRelationInfo; |
| 24 | |
import org.kuali.student.r2.core.atp.dto.AtpInfo; |
| 25 | |
import org.kuali.student.r2.core.atp.service.AtpService; |
| 26 | |
|
| 27 | 0 | public class AtpAtpRelationAssembler implements DTOAssembler<Object, Object> { |
| 28 | |
private AtpService atpService; |
| 29 | |
|
| 30 | |
public AtpService getAtpService() { |
| 31 | 0 | return atpService; |
| 32 | |
} |
| 33 | |
|
| 34 | |
public void setAtpService(AtpService atpService) { |
| 35 | 0 | this.atpService = atpService; |
| 36 | 0 | } |
| 37 | |
|
| 38 | |
public List<String> assemble(String atpKey, String relatedAtpType, ContextInfo context) throws AssemblyException { |
| 39 | 0 | List<String> ccKeys = new ArrayList<String>(); |
| 40 | |
List<AtpAtpRelationInfo> atpRels; |
| 41 | |
try { |
| 42 | 0 | atpRels = atpService.getAtpAtpRelationsByAtp(atpKey, context); |
| 43 | |
|
| 44 | 0 | if(atpRels != null && !atpRels.isEmpty()){ |
| 45 | 0 | for(AtpAtpRelationInfo atpRelInfo : atpRels){ |
| 46 | 0 | if(atpRelInfo.getAtpKey().equals(atpKey)){ |
| 47 | 0 | if(atpRelInfo.getTypeKey().equals(AtpServiceConstants.ATP_ATP_RELATION_ASSOCIATED_TYPE_KEY)){ |
| 48 | 0 | AtpInfo thisAtp = atpService.getAtp(atpRelInfo.getRelatedAtpKey(), context); |
| 49 | 0 | if(thisAtp != null && thisAtp.getTypeKey().equals(relatedAtpType)) |
| 50 | 0 | ccKeys.add(atpRelInfo.getRelatedAtpKey()); |
| 51 | 0 | } |
| 52 | |
} |
| 53 | |
} |
| 54 | |
} |
| 55 | 0 | } catch (Exception e) { |
| 56 | 0 | throw new AssemblyException(e.getMessage()); |
| 57 | 0 | } |
| 58 | |
|
| 59 | 0 | return ccKeys; |
| 60 | |
} |
| 61 | |
|
| 62 | |
public StatusInfo disassemble(String atpKey, List<String> relatedAtpKeys, String relatedAtpType, ContextInfo context) |
| 63 | |
throws AlreadyExistsException, DataValidationErrorException, InvalidParameterException, MissingParameterException, |
| 64 | |
OperationFailedException, PermissionDeniedException, VersionMismatchException{ |
| 65 | 0 | StatusInfo status = new StatusInfo(); |
| 66 | 0 | status.setSuccess(Boolean.TRUE); |
| 67 | |
|
| 68 | 0 | disassembleAtpAtpRelations(atpKey, relatedAtpKeys, relatedAtpType, context); |
| 69 | |
|
| 70 | 0 | return status; |
| 71 | |
} |
| 72 | |
|
| 73 | |
private void disassembleAtpAtpRelations(String atpKey, List<String> relatedAtpKeys, String relatedAtpType, ContextInfo context) throws AlreadyExistsException, |
| 74 | |
DataValidationErrorException, InvalidParameterException, MissingParameterException, |
| 75 | |
OperationFailedException, PermissionDeniedException, VersionMismatchException { |
| 76 | |
|
| 77 | |
try { |
| 78 | 0 | List<AtpAtpRelationInfo > atpRels = atpService.getAtpAtpRelationsByAtp(atpKey, context); |
| 79 | 0 | Map<String, String> currentRelIds = new HashMap<String, String>(); |
| 80 | |
|
| 81 | 0 | if(atpRels != null && !atpRels.isEmpty()){ |
| 82 | 0 | for(AtpAtpRelationInfo atpRelInfo : atpRels){ |
| 83 | 0 | if(atpRelInfo.getAtpKey().equals(atpKey)){ |
| 84 | 0 | if(AtpServiceConstants.ATP_ATP_RELATION_ASSOCIATED_TYPE_KEY.equals(atpRelInfo.getTypeKey())){ |
| 85 | 0 | AtpInfo thisAtp = atpService.getAtp(atpRelInfo.getRelatedAtpKey(), context); |
| 86 | 0 | if(thisAtp != null && thisAtp.getTypeKey().equals(relatedAtpType)) |
| 87 | 0 | currentRelIds.put(atpRelInfo.getRelatedAtpKey(), atpRelInfo.getId()); |
| 88 | |
|
| 89 | 0 | } |
| 90 | |
} |
| 91 | |
} |
| 92 | |
} |
| 93 | |
|
| 94 | 0 | for (String relatedKey : relatedAtpKeys){ |
| 95 | 0 | if (!currentRelIds.containsKey(relatedKey)) |
| 96 | 0 | createAtpAtpRelations(atpKey, relatedKey, AtpServiceConstants.ATP_ATP_RELATION_ASSOCIATED_TYPE_KEY, context); |
| 97 | |
else |
| 98 | 0 | updateAtpAtpRelations(currentRelIds.get(relatedKey), context); |
| 99 | |
} |
| 100 | |
|
| 101 | 0 | } catch (DoesNotExistException e) { |
| 102 | |
|
| 103 | 0 | for (String relatedKey : relatedAtpKeys){ |
| 104 | 0 | createAtpAtpRelations(atpKey, relatedKey, AtpServiceConstants.ATP_ATP_RELATION_ASSOCIATED_TYPE_KEY, context); |
| 105 | |
} |
| 106 | 0 | } |
| 107 | 0 | } |
| 108 | |
|
| 109 | |
public void createAtpAtpRelations(String atpKey, String relatedAtpKey, String relationType, ContextInfo context) throws AlreadyExistsException, |
| 110 | |
DataValidationErrorException, InvalidParameterException, MissingParameterException, |
| 111 | |
OperationFailedException, PermissionDeniedException { |
| 112 | 0 | AtpAtpRelationInfo atpRel = new AtpAtpRelationInfo(); |
| 113 | 0 | atpRel.setId(UUIDHelper.genStringUUID()); |
| 114 | 0 | atpRel.setAtpKey(atpKey); |
| 115 | 0 | atpRel.setRelatedAtpKey(relatedAtpKey); |
| 116 | 0 | atpRel.setTypeKey(relationType); |
| 117 | 0 | atpRel.setStateKey(AtpServiceConstants.ATP_ATP_RELATION_ACTIVE_STATE_KEY); |
| 118 | 0 | atpRel.setEffectiveDate(new Date()); |
| 119 | 0 | atpService.createAtpAtpRelation(atpRel, context); |
| 120 | 0 | } |
| 121 | |
|
| 122 | |
public void updateAtpAtpRelations(String atpAtpRelationId, ContextInfo context) throws AlreadyExistsException, |
| 123 | |
DataValidationErrorException, InvalidParameterException, MissingParameterException, |
| 124 | |
OperationFailedException, PermissionDeniedException, DoesNotExistException, VersionMismatchException{ |
| 125 | |
AtpAtpRelationInfo atpRel; |
| 126 | |
try { |
| 127 | 0 | atpRel = new AtpAtpRelationInfo(atpService.getAtpAtpRelation(atpAtpRelationId, context)); |
| 128 | |
|
| 129 | |
|
| 130 | |
try { |
| 131 | 0 | atpService.updateAtpAtpRelation(atpAtpRelationId, atpRel, context); |
| 132 | 0 | } catch (DoesNotExistException e) { |
| 133 | 0 | throw new DoesNotExistException(atpAtpRelationId); |
| 134 | 0 | } catch (VersionMismatchException e) { |
| 135 | 0 | throw new VersionMismatchException(atpAtpRelationId); |
| 136 | 0 | } |
| 137 | 0 | } catch (DoesNotExistException e1) { |
| 138 | 0 | throw new DoesNotExistException(atpAtpRelationId); |
| 139 | 0 | } |
| 140 | 0 | } |
| 141 | |
|
| 142 | |
@Override |
| 143 | |
public Object assemble(Object baseDTO, ContextInfo context) throws AssemblyException{ |
| 144 | 0 | return null; |
| 145 | |
} |
| 146 | |
|
| 147 | |
@Override |
| 148 | |
public Object disassemble(Object businessDTO, ContextInfo context) throws AssemblyException{ |
| 149 | 0 | return null; |
| 150 | |
} |
| 151 | |
|
| 152 | |
} |