Coverage Report - org.kuali.student.enrollment.class2.acal.service.assembler.AtpAtpRelationAssembler
 
Classes in this File Line Coverage Branch Coverage Complexity
AtpAtpRelationAssembler
0%
0/63
0%
0/34
3.889
 
 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.DTOAssembler;
 11  
 import org.kuali.student.r2.common.dto.ContextInfo;
 12  
 import org.kuali.student.r2.common.dto.StatusInfo;
 13  
 import org.kuali.student.r2.common.exceptions.AlreadyExistsException;
 14  
 import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
 15  
 import org.kuali.student.r2.common.exceptions.DoesNotExistException;
 16  
 import org.kuali.student.r2.common.exceptions.InvalidParameterException;
 17  
 import org.kuali.student.r2.common.exceptions.MissingParameterException;
 18  
 import org.kuali.student.r2.common.exceptions.OperationFailedException;
 19  
 import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
 20  
 import org.kuali.student.r2.common.exceptions.VersionMismatchException;
 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 AtpAtpRelationAssembler implements DTOAssembler<Object, Object> {
 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  
         public List<String> assemble(String atpKey, String relatedAtpType, ContextInfo context){
 38  0
         List<String> ccKeys = new ArrayList<String>();
 39  
         List<AtpAtpRelationInfo> atpRels;
 40  
         try {
 41  0
             atpRels = atpService.getAtpAtpRelationsByAtp(atpKey, context);
 42  
             
 43  0
             if(atpRels != null && !atpRels.isEmpty()){                  
 44  0
                 for(AtpAtpRelationInfo atpRelInfo : atpRels){
 45  0
                         if(atpRelInfo.getAtpKey().equals(atpKey)){
 46  0
                             if(atpRelInfo.getTypeKey().equals(AtpServiceConstants.ATP_ATP_RELATION_ASSOCIATED_TYPE_KEY)){
 47  0
                                     AtpInfo thisAtp = atpService.getAtp(atpRelInfo.getRelatedAtpKey(), context);
 48  0
                                 if(thisAtp != null && thisAtp.getTypeKey().equals(relatedAtpType))
 49  0
                                 ccKeys.add(atpRelInfo.getRelatedAtpKey());
 50  0
                             }
 51  
                         }
 52  
                 }
 53  
             }
 54  0
         } catch (Exception e) {
 55  0
             return null;
 56  0
         }
 57  
         
 58  0
         return ccKeys;
 59  
     }
 60  
         
 61  
     public StatusInfo disassemble(String atpKey, List<String> relatedAtpKeys, String relatedAtpType, ContextInfo context) 
 62  
                     throws AlreadyExistsException, DataValidationErrorException, InvalidParameterException, MissingParameterException, 
 63  
                     OperationFailedException, PermissionDeniedException, VersionMismatchException{
 64  0
             StatusInfo status = new StatusInfo();
 65  0
         status.setSuccess(Boolean.TRUE);        
 66  
         
 67  0
         disassembleAtpAtpRelations(atpKey, relatedAtpKeys, relatedAtpType, context);
 68  
               
 69  0
         return status;
 70  
     }
 71  
     
 72  
     private void disassembleAtpAtpRelations(String atpKey, List<String> relatedAtpKeys, String relatedAtpType, ContextInfo context) throws AlreadyExistsException,
 73  
     DataValidationErrorException, InvalidParameterException, MissingParameterException,
 74  
     OperationFailedException, PermissionDeniedException, VersionMismatchException {
 75  
         
 76  
         try {
 77  0
             List<AtpAtpRelationInfo > atpRels = atpService.getAtpAtpRelationsByAtp(atpKey, context);
 78  0
             Map<String, String> currentRelIds = new HashMap<String, String>();
 79  
             
 80  0
             if(atpRels != null && !atpRels.isEmpty()){
 81  0
                 for(AtpAtpRelationInfo atpRelInfo : atpRels){
 82  0
                         if(atpRelInfo.getAtpKey().equals(atpKey)){
 83  0
                             if(AtpServiceConstants.ATP_ATP_RELATION_ASSOCIATED_TYPE_KEY.equals(atpRelInfo.getTypeKey())){
 84  0
                                 AtpInfo thisAtp = atpService.getAtp(atpRelInfo.getRelatedAtpKey(), context);
 85  0
                                 if(thisAtp != null && thisAtp.getTypeKey().equals(relatedAtpType))
 86  0
                                     currentRelIds.put(atpRelInfo.getRelatedAtpKey(), atpRelInfo.getId());
 87  
         
 88  0
                                 }
 89  
                     }
 90  
                 }
 91  
             }
 92  
             
 93  0
             for (String relatedKey : relatedAtpKeys){
 94  0
                 if (!currentRelIds.containsKey(relatedKey))
 95  0
                     createAtpAtpRelations(atpKey, relatedKey, AtpServiceConstants.ATP_ATP_RELATION_ASSOCIATED_TYPE_KEY, context);
 96  
                 else
 97  0
                     updateAtpAtpRelations(currentRelIds.get(relatedKey), context);
 98  
             }
 99  
 
 100  0
         } catch (DoesNotExistException e) {
 101  
             //if not exist, create relations
 102  0
             for (String relatedKey : relatedAtpKeys){
 103  0
                 createAtpAtpRelations(atpKey, relatedKey, AtpServiceConstants.ATP_ATP_RELATION_ASSOCIATED_TYPE_KEY, context);
 104  
             }
 105  0
         }
 106  0
     }
 107  
     
 108  
     public void createAtpAtpRelations(String atpKey, String relatedAtpKey, String relationType, ContextInfo context) throws AlreadyExistsException,
 109  
     DataValidationErrorException, InvalidParameterException, MissingParameterException,
 110  
     OperationFailedException, PermissionDeniedException {
 111  0
             AtpAtpRelationInfo atpRel = new AtpAtpRelationInfo();
 112  0
         atpRel.setId(UUIDHelper.genStringUUID());
 113  0
         atpRel.setAtpKey(atpKey);
 114  0
         atpRel.setRelatedAtpKey(relatedAtpKey);
 115  0
         atpRel.setTypeKey(relationType);
 116  0
         atpRel.setStateKey(AtpServiceConstants.ATP_ATP_RELATION_ACTIVE_STATE_KEY);
 117  0
         atpRel.setEffectiveDate(new Date());
 118  0
         atpService.createAtpAtpRelation(atpRel, context);        
 119  0
     }
 120  
     
 121  
     public void updateAtpAtpRelations(String atpAtpRelationId, ContextInfo context) throws AlreadyExistsException,
 122  
     DataValidationErrorException, InvalidParameterException, MissingParameterException,
 123  
     OperationFailedException, PermissionDeniedException, DoesNotExistException, VersionMismatchException{
 124  
         AtpAtpRelationInfo atpRel;
 125  
         try {
 126  0
                 atpRel = new AtpAtpRelationInfo(atpService.getAtpAtpRelation(atpAtpRelationId, context));
 127  
             //TODO:what to update? should state same as atpRel.atp?
 128  
            // atpRel.setStateKey(state);
 129  
             try {
 130  0
                 atpService.updateAtpAtpRelation(atpAtpRelationId, atpRel, context);
 131  0
             } catch (DoesNotExistException e) {
 132  0
                     throw new DoesNotExistException(atpAtpRelationId);
 133  0
             } catch (VersionMismatchException e) {
 134  0
                     throw new VersionMismatchException(atpAtpRelationId);
 135  0
             }   
 136  0
         } catch (DoesNotExistException e1) {
 137  0
                 throw new DoesNotExistException(atpAtpRelationId);
 138  0
         }
 139  0
     }
 140  
 
 141  
         @Override
 142  
         public Object assemble(Object baseDTO, ContextInfo context) {
 143  0
                 return null;
 144  
         }
 145  
 
 146  
         @Override
 147  
         public Object disassemble(Object businessDTO, ContextInfo context) {
 148  0
                 return null;
 149  
         }
 150  
 
 151  
 }