Coverage Report - org.kuali.student.lum.program.client.ProgramUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ProgramUtils
0%
0/72
0%
0/48
3.667
 
 1  
 package org.kuali.student.lum.program.client;
 2  
 
 3  
 import java.util.HashMap;
 4  
 import java.util.List;
 5  
 import java.util.Map;
 6  
 import java.util.Set;
 7  
 import java.util.TreeSet;
 8  
 
 9  
 import org.kuali.student.common.assembly.data.Data;
 10  
 import org.kuali.student.common.assembly.data.ModelDefinition;
 11  
 import org.kuali.student.common.assembly.data.QueryPath;
 12  
 import org.kuali.student.common.ui.client.application.Application;
 13  
 import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor;
 14  
 import org.kuali.student.common.ui.client.configurable.mvc.views.SectionView;
 15  
 import org.kuali.student.common.ui.client.mvc.DataModel;
 16  
 import org.kuali.student.common.ui.client.mvc.View;
 17  
 import org.kuali.student.common.ui.client.widgets.notification.KSNotification;
 18  
 import org.kuali.student.common.ui.client.widgets.notification.KSNotifier;
 19  
 import org.kuali.student.common.validation.dto.ValidationResultInfo;
 20  
 import org.kuali.student.lum.common.client.configuration.AbstractSectionConfiguration;
 21  
 import org.kuali.student.lum.common.client.configuration.Configuration;
 22  
 import org.kuali.student.lum.common.client.configuration.ConfigurationManager;
 23  
 import org.kuali.student.lum.program.client.properties.ProgramProperties;
 24  
 
 25  
 import com.google.gwt.event.shared.EventHandler;
 26  
 import com.google.gwt.event.shared.GwtEvent;
 27  
 import com.google.gwt.event.shared.HandlerManager;
 28  
 import com.google.gwt.i18n.client.DateTimeFormat;
 29  
 
 30  
 /**
 31  
  * @author Igor
 32  
  */
 33  
 public class ProgramUtils {
 34  
 
 35  0
     public static DateTimeFormat df = DateTimeFormat.getFormat("dd-MMM-yyyy");
 36  
 
 37  0
     private ProgramUtils() {
 38  0
     }
 39  
 
 40  
     public static void addCredentialProgramDataToVariation(Data variationData, DataModel model) {
 41  0
         Data credentialProgram = new Data();
 42  
         
 43  
         // this should mimic the formats in VariationInformationEditConfiguration::createReadOnlySection
 44  0
         credentialProgram.set(ProgramConstants.INSTITUTION, model.<Data>get(ProgramConstants.CREDENTIAL_PROGRAM + "/" + ProgramConstants.INSTITUTION));
 45  0
         credentialProgram.set(ProgramConstants.PROGRAM_LEVEL, model.<String>get(ProgramConstants.CREDENTIAL_PROGRAM + "/" + ProgramConstants.PROGRAM_LEVEL));        
 46  0
         credentialProgram.set(ProgramConstants.RUNTIME_DATA, model.<Data>get(ProgramConstants.CREDENTIAL_PROGRAM + "/" + ProgramConstants.RUNTIME_DATA));
 47  
 
 48  0
         variationData.set(ProgramConstants.CREDENTIAL_PROGRAM, credentialProgram);
 49  0
     }
 50  
 
 51  
     public static Data createNewSpecializationBasedOnMajor(DataModel programModel) {
 52  0
         Data newSpecializationData = new Data();
 53  0
         newSpecializationData.set(ProgramConstants.STATE, programModel.<String>get(ProgramConstants.STATE));
 54  0
         newSpecializationData.set(ProgramConstants.TYPE, ProgramConstants.VARIATION_TYPE_KEY);
 55  0
         newSpecializationData.set(ProgramConstants.PROGRAM_REQUIREMENTS, new Data());
 56  
         
 57  
         //Manually copy default values here... we don't have access to the metadata and we only want to default the first time
 58  0
         newSpecializationData.set(ProgramConstants.FINANCIAL_CONTROL_DIVISION, programModel.<Data>get(ProgramConstants.FINANCIAL_CONTROL_DIVISION)==null?null:programModel.<Data>get(ProgramConstants.FINANCIAL_CONTROL_DIVISION).copy());
 59  0
         newSpecializationData.set(ProgramConstants.FINANCIAL_CONTROL_UNIT, programModel.<Data>get(ProgramConstants.FINANCIAL_CONTROL_UNIT)==null?null:programModel.<Data>get(ProgramConstants.FINANCIAL_CONTROL_UNIT).copy());
 60  0
         newSpecializationData.set(ProgramConstants.FINANCIAL_RESOURCES_DIVISION, programModel.<Data>get(ProgramConstants.FINANCIAL_RESOURCES_DIVISION)==null?null:programModel.<Data>get(ProgramConstants.FINANCIAL_RESOURCES_DIVISION).copy());
 61  0
         newSpecializationData.set(ProgramConstants.FINANCIAL_RESOURCES_UNIT, programModel.<Data>get(ProgramConstants.FINANCIAL_RESOURCES_UNIT)==null?null:programModel.<Data>get(ProgramConstants.FINANCIAL_RESOURCES_UNIT).copy());
 62  0
         newSpecializationData.set(ProgramConstants.CURRICULUM_OVERSIGHT_DIVISION, programModel.<Data>get(ProgramConstants.CURRICULUM_OVERSIGHT_DIVISION)==null?null:programModel.<Data>get(ProgramConstants.CURRICULUM_OVERSIGHT_DIVISION).copy());
 63  0
         newSpecializationData.set(ProgramConstants.CURRICULUM_OVERSIGHT_UNIT, programModel.<Data>get(ProgramConstants.CURRICULUM_OVERSIGHT_UNIT)==null?null:programModel.<Data>get(ProgramConstants.CURRICULUM_OVERSIGHT_UNIT).copy());
 64  0
         newSpecializationData.set(ProgramConstants.DEPLOYMENT_DIVISION, programModel.<Data>get(ProgramConstants.DEPLOYMENT_DIVISION)==null?null:programModel.<Data>get(ProgramConstants.DEPLOYMENT_DIVISION).copy());
 65  0
         newSpecializationData.set(ProgramConstants.DEPLOYMENT_UNIT, programModel.<Data>get(ProgramConstants.DEPLOYMENT_UNIT)==null?null:programModel.<Data>get(ProgramConstants.DEPLOYMENT_UNIT).copy());
 66  0
         newSpecializationData.set(ProgramConstants.STUDENT_OVERSIGHT_DIVISION, programModel.<Data>get(ProgramConstants.STUDENT_OVERSIGHT_DIVISION)==null?null:programModel.<Data>get(ProgramConstants.STUDENT_OVERSIGHT_DIVISION).copy());
 67  0
         newSpecializationData.set(ProgramConstants.STUDENT_OVERSIGHT_UNIT, programModel.<Data>get(ProgramConstants.STUDENT_OVERSIGHT_UNIT)==null?null:programModel.<Data>get(ProgramConstants.STUDENT_OVERSIGHT_UNIT).copy());
 68  
 
 69  0
         addCredentialProgramDataToVariation(newSpecializationData, programModel);
 70  0
         return newSpecializationData;
 71  
     }
 72  
 
 73  
 
 74  
 
 75  
     public static void retrofitValidationResults(List<ValidationResultInfo> validationResults) {
 76  0
         for (ValidationResultInfo validationResult : validationResults) {
 77  0
             String key = validationResult.getElement();
 78  0
             if (ProgramConstants.RICH_TEXT_KEYS.contains(key)) {
 79  0
                 key = key + "/plain";
 80  0
                 validationResult.setElement(key);
 81  
             }
 82  0
         }
 83  0
     }
 84  
 
 85  
     public static void handleValidationErrorsForSpecializations(List<ValidationResultInfo> validationResults, DataModel programModel) {
 86  0
         Set<Integer> failedSpecializations = new TreeSet<Integer>();
 87  0
         for (ValidationResultInfo validationResult : validationResults) {
 88  0
             String element = validationResult.getElement();
 89  0
             if (element.contains(ProgramConstants.VARIATIONS)) {
 90  0
                     FieldDescriptor fd = Application.getApplicationContext().getPathToFieldMapping(null, element);
 91  0
                     if(fd!=null){
 92  0
                             fd.getFieldElement().processValidationResult(validationResult);
 93  
                     }else{
 94  0
                             int specializationIndex = Integer.parseInt(element.split("/")[1]);
 95  0
                             failedSpecializations.add(specializationIndex);
 96  
                     }
 97  
             }
 98  0
         }
 99  0
         if (!failedSpecializations.isEmpty()) {
 100  0
             final Data variationMap = programModel.get(ProgramConstants.VARIATIONS);
 101  0
             StringBuilder validationMessage = new StringBuilder();
 102  0
             for (Integer failedSpecialization : failedSpecializations) {
 103  0
                 Data data = variationMap.get(failedSpecialization);
 104  0
                 validationMessage.append(data.get(ProgramConstants.LONG_TITLE)).append(", ");
 105  0
             }
 106  0
             String resultMessage = validationMessage.toString();
 107  
             //Cutoff ', ' from the result
 108  0
             resultMessage = resultMessage.substring(0, resultMessage.length() - 2);
 109  
             
 110  0
             if (failedSpecializations.size() == 1) {
 111  0
                     KSNotifier.add(new KSNotification(ProgramProperties.get().major_variationFailed(resultMessage), false, true, 5000));
 112  
             } else {
 113  0
                     KSNotifier.add(new KSNotification(ProgramProperties.get().major_variationsFailed(resultMessage), false, true, 5000));
 114  
             }
 115  
         }
 116  0
     }
 117  
 
 118  
 
 119  
     public static void syncMetadata(AbstractProgramConfigurer configurer, ModelDefinition modelDefinition) {
 120  0
         ConfigurationManager configurationManager = configurer.getProgramSectionConfigManager();
 121  0
         for (Configuration conf : configurationManager.getConfigurations()) {
 122  0
             if (conf instanceof AbstractSectionConfiguration) {
 123  0
                 AbstractSectionConfiguration configuration = (AbstractSectionConfiguration) conf;
 124  0
                 View view = configuration.getView(false);
 125  0
                 if (view != null && view instanceof SectionView) {
 126  0
                     SectionView sectionView = (SectionView) view;
 127  0
                     sectionView.updateMetadata(modelDefinition);
 128  
                 }
 129  0
             }
 130  
         }
 131  0
     }
 132  
 
 133  
     public static void unregisterUnusedHandlers(HandlerManager eventBus) {
 134  0
         HashMap<GwtEvent.Type, EventHandler> eventsMap = ProgramRegistry.getSpecializationHandlers();
 135  0
         if (eventsMap != null) {
 136  0
             for (Map.Entry<GwtEvent.Type, EventHandler> typeEventHandlerEntry : eventsMap.entrySet()) {
 137  0
                 eventBus.removeHandler(typeEventHandlerEntry.getKey(), typeEventHandlerEntry.getValue());
 138  
             }
 139  
         }
 140  0
     }
 141  
 
 142  
     public static String getProgramId(DataModel programModel) {
 143  0
         return programModel.get(ProgramConstants.ID);
 144  
     }
 145  
 
 146  
     public static String getProgramState(DataModel programModel) {
 147  0
         return programModel.get(                ProgramConstants.STATE);
 148  
     }
 149  
 }