View Javadoc

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.r1.common.assembly.data.Data;
10  import org.kuali.student.r1.common.assembly.data.ModelDefinition;
11  import org.kuali.student.r2.common.dto.ValidationResultInfo;
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.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      public static DateTimeFormat df = DateTimeFormat.getFormat("dd-MMM-yyyy");
34  
35      private ProgramUtils() {
36      }
37  
38      public static void addCredentialProgramDataToVariation(Data variationData, DataModel model) {
39          Data credentialProgram = new Data();
40          
41          // this should mimic the formats in VariationInformationEditConfiguration::createReadOnlySection
42          credentialProgram.set(ProgramConstants.INSTITUTION, model.<Data>get(ProgramConstants.CREDENTIAL_PROGRAM + "/" + ProgramConstants.INSTITUTION));
43          credentialProgram.set(ProgramConstants.PROGRAM_LEVEL, model.<String>get(ProgramConstants.CREDENTIAL_PROGRAM + "/" + ProgramConstants.PROGRAM_LEVEL));        
44          credentialProgram.set(ProgramConstants.RUNTIME_DATA, model.<Data>get(ProgramConstants.CREDENTIAL_PROGRAM + "/" + ProgramConstants.RUNTIME_DATA));
45  
46          variationData.set(ProgramConstants.CREDENTIAL_PROGRAM, credentialProgram);
47      }
48  
49      public static Data createNewSpecializationBasedOnMajor(DataModel programModel) {
50          Data newSpecializationData = new Data();
51          newSpecializationData.set(ProgramConstants.STATE, programModel.<String>get(ProgramConstants.STATE));
52          newSpecializationData.set(ProgramConstants.TYPE, ProgramConstants.VARIATION_TYPE_KEY);
53          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          newSpecializationData.set(ProgramConstants.FINANCIAL_CONTROL_DIVISION, programModel.<Data>get(ProgramConstants.FINANCIAL_CONTROL_DIVISION)==null?null:programModel.<Data>get(ProgramConstants.FINANCIAL_CONTROL_DIVISION).copy());
57          newSpecializationData.set(ProgramConstants.FINANCIAL_CONTROL_UNIT, programModel.<Data>get(ProgramConstants.FINANCIAL_CONTROL_UNIT)==null?null:programModel.<Data>get(ProgramConstants.FINANCIAL_CONTROL_UNIT).copy());
58          newSpecializationData.set(ProgramConstants.FINANCIAL_RESOURCES_DIVISION, programModel.<Data>get(ProgramConstants.FINANCIAL_RESOURCES_DIVISION)==null?null:programModel.<Data>get(ProgramConstants.FINANCIAL_RESOURCES_DIVISION).copy());
59          newSpecializationData.set(ProgramConstants.FINANCIAL_RESOURCES_UNIT, programModel.<Data>get(ProgramConstants.FINANCIAL_RESOURCES_UNIT)==null?null:programModel.<Data>get(ProgramConstants.FINANCIAL_RESOURCES_UNIT).copy());
60          newSpecializationData.set(ProgramConstants.CURRICULUM_OVERSIGHT_DIVISION, programModel.<Data>get(ProgramConstants.CURRICULUM_OVERSIGHT_DIVISION)==null?null:programModel.<Data>get(ProgramConstants.CURRICULUM_OVERSIGHT_DIVISION).copy());
61          newSpecializationData.set(ProgramConstants.CURRICULUM_OVERSIGHT_UNIT, programModel.<Data>get(ProgramConstants.CURRICULUM_OVERSIGHT_UNIT)==null?null:programModel.<Data>get(ProgramConstants.CURRICULUM_OVERSIGHT_UNIT).copy());
62          newSpecializationData.set(ProgramConstants.DEPLOYMENT_DIVISION, programModel.<Data>get(ProgramConstants.DEPLOYMENT_DIVISION)==null?null:programModel.<Data>get(ProgramConstants.DEPLOYMENT_DIVISION).copy());
63          newSpecializationData.set(ProgramConstants.DEPLOYMENT_UNIT, programModel.<Data>get(ProgramConstants.DEPLOYMENT_UNIT)==null?null:programModel.<Data>get(ProgramConstants.DEPLOYMENT_UNIT).copy());
64          newSpecializationData.set(ProgramConstants.STUDENT_OVERSIGHT_DIVISION, programModel.<Data>get(ProgramConstants.STUDENT_OVERSIGHT_DIVISION)==null?null:programModel.<Data>get(ProgramConstants.STUDENT_OVERSIGHT_DIVISION).copy());
65          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          addCredentialProgramDataToVariation(newSpecializationData, programModel);
68          return newSpecializationData;
69      }
70  
71  
72  
73      public static void retrofitValidationResults(List<ValidationResultInfo> validationResults) {
74          for (ValidationResultInfo validationResult : validationResults) {
75              String key = validationResult.getElement();
76              if (ProgramConstants.RICH_TEXT_KEYS.contains(key)) {
77                  key = key + "/plain";
78                  validationResult.setElement(key);
79              }
80          }
81      }
82  
83      public static void handleValidationErrorsForSpecializations(List<ValidationResultInfo> validationResults, DataModel programModel) {
84          Set<Integer> failedSpecializations = new TreeSet<Integer>();
85          for (ValidationResultInfo validationResult : validationResults) {
86              String element = validationResult.getElement();
87              if (element.contains(ProgramConstants.VARIATIONS)) {
88              	String fdPath = element;
89              	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              		fdPath = element.substring(0,element.lastIndexOf("/"));
92              	}
93              	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              	if(fd!=null){
97              		fd.getFieldElement().processValidationResult(validationResult);
98              	}else{
99              		int specializationIndex = Integer.parseInt(element.split("/")[1]);
100             		failedSpecializations.add(specializationIndex);
101             	}
102             }
103         }
104         if (!failedSpecializations.isEmpty()) {
105             final Data variationMap = programModel.get(ProgramConstants.VARIATIONS);
106             StringBuilder validationMessage = new StringBuilder();
107             for (Integer failedSpecialization : failedSpecializations) {
108                 Data data = variationMap.get(failedSpecialization);
109                 validationMessage.append(data.get(ProgramConstants.LONG_TITLE)).append(", ");
110             }
111             String resultMessage = validationMessage.toString();
112             //Cutoff ', ' from the result
113             resultMessage = resultMessage.substring(0, resultMessage.length() - 2);
114             
115             if (failedSpecializations.size() == 1) {
116             	KSNotifier.add(new KSNotification(getLabel(ProgramMsgConstants.MAJOR_VARIATIONFAILED, resultMessage), false, true, 5000));
117             } else {
118             	KSNotifier.add(new KSNotification(getLabel(ProgramMsgConstants.MAJOR_VARIATIONSFAILED, resultMessage), false, true, 5000));
119             }
120         }
121     }
122 
123 
124     public static void syncMetadata(AbstractProgramConfigurer configurer, ModelDefinition modelDefinition) {
125         ConfigurationManager configurationManager = configurer.getProgramSectionConfigManager();
126         for (Configuration conf : configurationManager.getConfigurations()) {
127             if (conf instanceof AbstractSectionConfiguration) {
128                 AbstractSectionConfiguration configuration = (AbstractSectionConfiguration) conf;
129                 View view = configuration.getView(false);
130                 if (view != null && view instanceof SectionView) {
131                     SectionView sectionView = (SectionView) view;
132                     sectionView.updateMetadata(modelDefinition);
133                 }
134             }
135         }
136     }
137 
138     public static void unregisterUnusedHandlers(HandlerManager eventBus) {
139 		HashMap<GwtEvent.Type, EventHandler> eventsMap = ProgramRegistry.getSpecializationHandlers();
140 		if (eventsMap != null) {
141 			for (Map.Entry<GwtEvent.Type, EventHandler> typeEventHandlerEntry : eventsMap.entrySet()) {
142 				try {
143 					eventBus.removeHandler(typeEventHandlerEntry.getKey(),typeEventHandlerEntry.getValue());
144 				} catch(Exception e) {
145 					//FIXME: Unregistering of handlers should be better handled
146 				}
147 				finally{return;}
148 			}
149 		}
150     }
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         return programModel.get(ProgramConstants.PROPOSAL_ID);
161     }
162 
163     public static String getProgramId(DataModel programModel) {
164         return programModel.get(ProgramConstants.ID);
165     }
166 
167     public static String getProgramState(DataModel programModel) {
168         return programModel.get(                ProgramConstants.STATE);
169     }
170     
171     public static String getLabel(String messageKey, String parameter) {
172         Map<String, Object> parameters = new HashMap<String, Object>();
173         parameters.put("0", parameter);
174         return Application.getApplicationContext().getUILabel(ProgramMsgConstants.PROGRAM_MSG_GROUP, messageKey, parameters);
175     }
176 }