Coverage Report - org.kuali.student.lum.program.client.ProgramUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ProgramUtils
0%
0/81
0%
0/50
3.364
 
 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.ui.client.application.Application;
 12  
 import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor;
 13  
 import org.kuali.student.common.ui.client.configurable.mvc.views.SectionView;
 14  
 import org.kuali.student.common.ui.client.mvc.DataModel;
 15  
 import org.kuali.student.common.ui.client.mvc.View;
 16  
 import org.kuali.student.common.ui.client.widgets.notification.KSNotification;
 17  
 import org.kuali.student.common.ui.client.widgets.notification.KSNotifier;
 18  
 import org.kuali.student.common.validation.dto.ValidationResultInfo;
 19  
 import org.kuali.student.lum.common.client.configuration.AbstractSectionConfiguration;
 20  
 import org.kuali.student.lum.common.client.configuration.Configuration;
 21  
 import org.kuali.student.lum.common.client.configuration.ConfigurationManager;
 22  
 
 23  
 import com.google.gwt.event.shared.EventHandler;
 24  
 import com.google.gwt.event.shared.GwtEvent;
 25  
 import com.google.gwt.event.shared.HandlerManager;
 26  
 import com.google.gwt.i18n.client.DateTimeFormat;
 27  
 
 28  
 /**
 29  
  * @author Igor
 30  
  */
 31  
 public class ProgramUtils {
 32  
 
 33  0
     public static DateTimeFormat df = DateTimeFormat.getFormat("dd-MMM-yyyy");
 34  
 
 35  0
     private ProgramUtils() {
 36  0
     }
 37  
 
 38  
     public static void addCredentialProgramDataToVariation(Data variationData, DataModel model) {
 39  0
         Data credentialProgram = new Data();
 40  
         
 41  
         // this should mimic the formats in VariationInformationEditConfiguration::createReadOnlySection
 42  0
         credentialProgram.set(ProgramConstants.INSTITUTION, model.<Data>get(ProgramConstants.CREDENTIAL_PROGRAM + "/" + ProgramConstants.INSTITUTION));
 43  0
         credentialProgram.set(ProgramConstants.PROGRAM_LEVEL, model.<String>get(ProgramConstants.CREDENTIAL_PROGRAM + "/" + ProgramConstants.PROGRAM_LEVEL));        
 44  0
         credentialProgram.set(ProgramConstants.RUNTIME_DATA, model.<Data>get(ProgramConstants.CREDENTIAL_PROGRAM + "/" + ProgramConstants.RUNTIME_DATA));
 45  
 
 46  0
         variationData.set(ProgramConstants.CREDENTIAL_PROGRAM, credentialProgram);
 47  0
     }
 48  
 
 49  
     public static Data createNewSpecializationBasedOnMajor(DataModel programModel) {
 50  0
         Data newSpecializationData = new Data();
 51  0
         newSpecializationData.set(ProgramConstants.STATE, programModel.<String>get(ProgramConstants.STATE));
 52  0
         newSpecializationData.set(ProgramConstants.TYPE, ProgramConstants.VARIATION_TYPE_KEY);
 53  0
         newSpecializationData.set(ProgramConstants.PROGRAM_REQUIREMENTS, new Data());
 54  
         
 55  
         //Manually copy default values here... we don't have access to the metadata and we only want to default the first time
 56  0
         newSpecializationData.set(ProgramConstants.FINANCIAL_CONTROL_DIVISION, programModel.<Data>get(ProgramConstants.FINANCIAL_CONTROL_DIVISION)==null?null:programModel.<Data>get(ProgramConstants.FINANCIAL_CONTROL_DIVISION).copy());
 57  0
         newSpecializationData.set(ProgramConstants.FINANCIAL_CONTROL_UNIT, programModel.<Data>get(ProgramConstants.FINANCIAL_CONTROL_UNIT)==null?null:programModel.<Data>get(ProgramConstants.FINANCIAL_CONTROL_UNIT).copy());
 58  0
         newSpecializationData.set(ProgramConstants.FINANCIAL_RESOURCES_DIVISION, programModel.<Data>get(ProgramConstants.FINANCIAL_RESOURCES_DIVISION)==null?null:programModel.<Data>get(ProgramConstants.FINANCIAL_RESOURCES_DIVISION).copy());
 59  0
         newSpecializationData.set(ProgramConstants.FINANCIAL_RESOURCES_UNIT, programModel.<Data>get(ProgramConstants.FINANCIAL_RESOURCES_UNIT)==null?null:programModel.<Data>get(ProgramConstants.FINANCIAL_RESOURCES_UNIT).copy());
 60  0
         newSpecializationData.set(ProgramConstants.CURRICULUM_OVERSIGHT_DIVISION, programModel.<Data>get(ProgramConstants.CURRICULUM_OVERSIGHT_DIVISION)==null?null:programModel.<Data>get(ProgramConstants.CURRICULUM_OVERSIGHT_DIVISION).copy());
 61  0
         newSpecializationData.set(ProgramConstants.CURRICULUM_OVERSIGHT_UNIT, programModel.<Data>get(ProgramConstants.CURRICULUM_OVERSIGHT_UNIT)==null?null:programModel.<Data>get(ProgramConstants.CURRICULUM_OVERSIGHT_UNIT).copy());
 62  0
         newSpecializationData.set(ProgramConstants.DEPLOYMENT_DIVISION, programModel.<Data>get(ProgramConstants.DEPLOYMENT_DIVISION)==null?null:programModel.<Data>get(ProgramConstants.DEPLOYMENT_DIVISION).copy());
 63  0
         newSpecializationData.set(ProgramConstants.DEPLOYMENT_UNIT, programModel.<Data>get(ProgramConstants.DEPLOYMENT_UNIT)==null?null:programModel.<Data>get(ProgramConstants.DEPLOYMENT_UNIT).copy());
 64  0
         newSpecializationData.set(ProgramConstants.STUDENT_OVERSIGHT_DIVISION, programModel.<Data>get(ProgramConstants.STUDENT_OVERSIGHT_DIVISION)==null?null:programModel.<Data>get(ProgramConstants.STUDENT_OVERSIGHT_DIVISION).copy());
 65  0
         newSpecializationData.set(ProgramConstants.STUDENT_OVERSIGHT_UNIT, programModel.<Data>get(ProgramConstants.STUDENT_OVERSIGHT_UNIT)==null?null:programModel.<Data>get(ProgramConstants.STUDENT_OVERSIGHT_UNIT).copy());
 66  
 
 67  0
         addCredentialProgramDataToVariation(newSpecializationData, programModel);
 68  0
         return newSpecializationData;
 69  
     }
 70  
 
 71  
 
 72  
 
 73  
     public static void retrofitValidationResults(List<ValidationResultInfo> validationResults) {
 74  0
         for (ValidationResultInfo validationResult : validationResults) {
 75  0
             String key = validationResult.getElement();
 76  0
             if (ProgramConstants.RICH_TEXT_KEYS.contains(key)) {
 77  0
                 key = key + "/plain";
 78  0
                 validationResult.setElement(key);
 79  
             }
 80  0
         }
 81  0
     }
 82  
 
 83  
     public static void handleValidationErrorsForSpecializations(List<ValidationResultInfo> validationResults, DataModel programModel) {
 84  0
         Set<Integer> failedSpecializations = new TreeSet<Integer>();
 85  0
         for (ValidationResultInfo validationResult : validationResults) {
 86  0
             String element = validationResult.getElement();
 87  0
             if (element.contains(ProgramConstants.VARIATIONS)) {
 88  0
                     String fdPath = element;
 89  0
                     if (element.matches(".*/[0-9]+")){
 90  
                             //If path ends in number then strip it off, it is for an individual item in a list element
 91  0
                             fdPath = element.substring(0,element.lastIndexOf("/"));
 92  
                     }
 93  0
                     FieldDescriptor fd = Application.getApplicationContext().getPathToFieldMapping(null, fdPath);
 94  
                                         
 95  
                     //If field descriptor found, display error on the field, otherwise display a generic error message.
 96  0
                     if(fd!=null){
 97  0
                             fd.getFieldElement().processValidationResult(validationResult);
 98  
                     }else{
 99  0
                             int specializationIndex = Integer.parseInt(element.split("/")[1]);
 100  0
                             failedSpecializations.add(specializationIndex);
 101  
                     }
 102  
             }
 103  0
         }
 104  0
         if (!failedSpecializations.isEmpty()) {
 105  0
             final Data variationMap = programModel.get(ProgramConstants.VARIATIONS);
 106  0
             StringBuilder validationMessage = new StringBuilder();
 107  0
             for (Integer failedSpecialization : failedSpecializations) {
 108  0
                 Data data = variationMap.get(failedSpecialization);
 109  0
                 validationMessage.append(data.get(ProgramConstants.LONG_TITLE)).append(", ");
 110  0
             }
 111  0
             String resultMessage = validationMessage.toString();
 112  
             //Cutoff ', ' from the result
 113  0
             resultMessage = resultMessage.substring(0, resultMessage.length() - 2);
 114  
             
 115  0
             if (failedSpecializations.size() == 1) {
 116  0
                     KSNotifier.add(new KSNotification(getLabel(ProgramMsgConstants.MAJOR_VARIATIONFAILED, resultMessage), false, true, 5000));
 117  
             } else {
 118  0
                     KSNotifier.add(new KSNotification(getLabel(ProgramMsgConstants.MAJOR_VARIATIONSFAILED, resultMessage), false, true, 5000));
 119  
             }
 120  
         }
 121  0
     }
 122  
 
 123  
 
 124  
     public static void syncMetadata(AbstractProgramConfigurer configurer, ModelDefinition modelDefinition) {
 125  0
         ConfigurationManager configurationManager = configurer.getProgramSectionConfigManager();
 126  0
         for (Configuration conf : configurationManager.getConfigurations()) {
 127  0
             if (conf instanceof AbstractSectionConfiguration) {
 128  0
                 AbstractSectionConfiguration configuration = (AbstractSectionConfiguration) conf;
 129  0
                 View view = configuration.getView(false);
 130  0
                 if (view != null && view instanceof SectionView) {
 131  0
                     SectionView sectionView = (SectionView) view;
 132  0
                     sectionView.updateMetadata(modelDefinition);
 133  
                 }
 134  0
             }
 135  
         }
 136  0
     }
 137  
 
 138  
     public static void unregisterUnusedHandlers(HandlerManager eventBus) {
 139  0
                 HashMap<GwtEvent.Type, EventHandler> eventsMap = ProgramRegistry.getSpecializationHandlers();
 140  0
                 if (eventsMap != null) {
 141  0
                         for (Map.Entry<GwtEvent.Type, EventHandler> typeEventHandlerEntry : eventsMap.entrySet()) {
 142  
                                 try {
 143  0
                                         eventBus.removeHandler(typeEventHandlerEntry.getKey(),typeEventHandlerEntry.getValue());
 144  0
                                 } catch(Exception e) {
 145  
                                         //FIXME: Unregistering of handlers should be better handled
 146  
                                 }
 147  0
                                 finally{return;}
 148  
                         }
 149  
                 }
 150  0
     }
 151  
     
 152  
     /**
 153  
      * 
 154  
      * This method will grab the proposal ID from the data model.
 155  
      * 
 156  
      * @param programModel XML data model
 157  
      * @return
 158  
      */
 159  
     public static String getProposalId(DataModel programModel) {
 160  0
         return programModel.get(ProgramConstants.PROPOSAL_ID);
 161  
     }
 162  
 
 163  
     public static String getProgramId(DataModel programModel) {
 164  0
         return programModel.get(ProgramConstants.ID);
 165  
     }
 166  
 
 167  
     public static String getProgramState(DataModel programModel) {
 168  0
         return programModel.get(                ProgramConstants.STATE);
 169  
     }
 170  
     
 171  
     public static String getLabel(String messageKey, String parameter) {
 172  0
         Map<String, Object> parameters = new HashMap<String, Object>();
 173  0
         parameters.put("0", parameter);
 174  0
         return Application.getApplicationContext().getUILabel(ProgramMsgConstants.PROGRAM_MSG_GROUP, messageKey, parameters);
 175  
     }
 176  
 }