Coverage Report - org.kuali.student.lum.program.server.transform.VersionProgramFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
VersionProgramFilter
0%
0/49
0%
0/14
2.4
 
 1  
 package org.kuali.student.lum.program.server.transform;
 2  
 
 3  
 import java.util.List;
 4  
 import java.util.Map;
 5  
 
 6  
 import org.kuali.student.common.assembly.data.Data;
 7  
 import org.kuali.student.common.assembly.data.Metadata;
 8  
 import org.kuali.student.common.assembly.data.QueryPath;
 9  
 import org.kuali.student.common.assembly.data.Data.DataType;
 10  
 import org.kuali.student.common.assembly.data.Data.StringKey;
 11  
 import org.kuali.student.common.assembly.transform.AbstractDataFilter;
 12  
 import org.kuali.student.common.assembly.transform.MetadataFilter;
 13  
 import org.kuali.student.common.ui.client.mvc.DataModelDefinition;
 14  
 import org.kuali.student.lum.program.client.ProgramConstants;
 15  
 import org.kuali.student.lum.program.dto.MajorDisciplineInfo;
 16  
 import org.kuali.student.lum.program.dto.ProgramVariationInfo;
 17  
 import org.kuali.student.lum.program.service.ProgramService;
 18  
 
 19  
 /**
 20  
  * When program is activated this filter sets the previous version to superseded and updates the end program
 21  
  * entry and and enroll terms. 
 22  
  * 
 23  
  * @author Will
 24  
  *
 25  
  */
 26  0
 public class VersionProgramFilter extends AbstractDataFilter implements MetadataFilter{
 27  
         public static final String PREVIOUS_VERSION_DATA = "VersionProgramFilter.PreviousVersionData";
 28  
         public static final String PREVIOUS_VERSION_INFO = "previousVersionInfo";
 29  
         
 30  
         private Metadata previousVersionMetadata;
 31  
         protected ProgramService programService; 
 32  
         
 33  
         /**
 34  
          * Save previousVersionInfo from incoming data to be used by outbound filter
 35  
          */
 36  
         @Override
 37  
         public void applyInboundDataFilter(Data data, Metadata metadata,
 38  
                         Map<String, Object> properties) throws Exception {
 39  0
                 Data previousVersionData = data.query(PREVIOUS_VERSION_INFO);
 40  0
                 data.remove(new StringKey(PREVIOUS_VERSION_INFO));
 41  
                                 
 42  0
                 if (previousVersionData != null){        
 43  0
                         properties.put(PREVIOUS_VERSION_DATA, previousVersionData);
 44  
                 }                        
 45  0
         }
 46  
 
 47  
         /**
 48  
          * Adds previous version information to the data if it exists and updates the previous version state
 49  
          * if state changed (this happens when program is activated)
 50  
          */
 51  
         @Override
 52  
         public void applyOutboundDataFilter(Data data, Metadata metadata,
 53  
                         Map<String, Object> properties) throws Exception {
 54  0
                 Data previousVersionData = (Data)properties.get(PREVIOUS_VERSION_DATA);
 55  0
                 String versionFromId = data.query(ProgramConstants.VERSION_FROM_ID);
 56  
                 
 57  0
                 if (versionFromId != null){
 58  0
                         MajorDisciplineInfo previousVersionMajorInfo = programService.getMajorDiscipline(versionFromId);
 59  
                         
 60  0
                         if (previousVersionData == null){
 61  
                                 //This is an initial get. Create previous version data to send back to client 
 62  0
                                 previousVersionData = new Data();
 63  0
                                 previousVersionData.set(ProgramConstants.ID, previousVersionMajorInfo.getId());
 64  0
                                 previousVersionData.set(ProgramConstants.END_PROGRAM_ENTRY_TERM, previousVersionMajorInfo.getEndProgramEntryTerm());
 65  0
                                 previousVersionData.set(ProgramConstants.END_PROGRAM_ENROLL_TERM, previousVersionMajorInfo.getEndTerm());
 66  0
                                 previousVersionData.set(ProgramConstants.STATE, previousVersionMajorInfo.getState());
 67  
                         } else {
 68  
                                 //This is a save operation. Check state field change for previous version state, indicating an "Activate" action,
 69  
                                 //which requires updating previous program with new states and end terms and setting activated program
 70  
                                 //to be the current version.
 71  
                                 
 72  0
                                 String state = previousVersionData.get(ProgramConstants.STATE);
 73  0
                                 if (state!= null && !state.equals(previousVersionMajorInfo.getState())){
 74  
                                         //Update previous program version with new state and terms
 75  0
                                         String endEntryTerm = previousVersionData.get(ProgramConstants.END_PROGRAM_ENTRY_TERM); 
 76  0
                                         String endEnrollTerm = previousVersionData.get(ProgramConstants.END_PROGRAM_ENROLL_TERM);
 77  
 
 78  0
                                         previousVersionMajorInfo.setState(state);
 79  0
                                         previousVersionMajorInfo.setEndProgramEntryTerm(endEntryTerm);
 80  0
                                         previousVersionMajorInfo.setEndTerm(endEnrollTerm);
 81  
                                         
 82  
                                         //Update states on all variations for this program
 83  0
                                 List<ProgramVariationInfo> variationList = previousVersionMajorInfo.getVariations();
 84  0
                                 for (ProgramVariationInfo variation:variationList){
 85  0
                                         variation.setState(state);                                        
 86  
                                 }
 87  
 
 88  0
                                         programService.updateMajorDiscipline(previousVersionMajorInfo);
 89  
                                 
 90  
                                 
 91  
                                         //Set "activated" program to be the current version
 92  0
                                         String activatedMajorId = data.get(ProgramConstants.ID);
 93  0
                                         programService.setCurrentMajorDisciplineVersion(activatedMajorId, null);
 94  
                                 }
 95  
 
 96  
                         }
 97  
                         
 98  0
                         data.set(PREVIOUS_VERSION_INFO, previousVersionData);
 99  
                 }                
 100  0
         }
 101  
 
 102  
         /**
 103  
          * Adds previousVersionInfo metadata to metadata returned to client
 104  
          */
 105  
         @Override
 106  
         public void applyMetadataFilter(String dtoName, Metadata metadata,
 107  
                         Map<String, Object> filterProperties) {                
 108  0
                 metadata.getProperties().put(PREVIOUS_VERSION_INFO, getPreviousVersionMetadata(metadata));
 109  0
         }
 110  
 
 111  
         /**
 112  
          * This generates a phantom "previousVersionInfo" metadata using dictionary definitions for fields already
 113  
          * defined for majorDisicplineInfo.
 114  
          * 
 115  
          * @param metadata
 116  
          * @return
 117  
          */
 118  
         protected Metadata getPreviousVersionMetadata(Metadata metadata){
 119  0
                 if (previousVersionMetadata == null){
 120  0
                         DataModelDefinition modelDef = new DataModelDefinition(metadata);
 121  0
                         Metadata programIdMeta = modelDef.getMetadata(QueryPath.parse(ProgramConstants.ID));
 122  0
                         Metadata programStateMeta = modelDef.getMetadata(QueryPath.parse(ProgramConstants.STATE));
 123  0
                         Metadata endEntryTermMeta = modelDef.getMetadata(QueryPath.parse(ProgramConstants.END_PROGRAM_ENTRY_TERM));
 124  0
                         Metadata endEnrollTermMeta = modelDef.getMetadata(QueryPath.parse(ProgramConstants.END_PROGRAM_ENROLL_TERM));
 125  
                         
 126  0
                         previousVersionMetadata = new Metadata();
 127  0
                         previousVersionMetadata.setDataType(DataType.DATA);
 128  
                                         
 129  0
                         Map<String, Metadata> properties = previousVersionMetadata.getProperties();
 130  0
                         properties.put(ProgramConstants.ID, programIdMeta);
 131  0
                         properties.put(ProgramConstants.STATE, programStateMeta);
 132  0
                         properties.put(ProgramConstants.END_PROGRAM_ENTRY_TERM, endEntryTermMeta);
 133  0
                         properties.put(ProgramConstants.END_PROGRAM_ENROLL_TERM, endEnrollTermMeta);
 134  
                 }
 135  
                 
 136  0
                 return previousVersionMetadata;
 137  
         }
 138  
         
 139  
         public void setProgramService(ProgramService programService) {
 140  0
                 this.programService = programService;
 141  0
         }        
 142  
 }