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 | |
|
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 | |
|
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 | |
|
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 | |
public static void setStatus(DataModel dataModel, String status) { |
74 | 0 | QueryPath statePath = new QueryPath(); |
75 | 0 | statePath.add(new Data.StringKey(ProgramConstants.STATE)); |
76 | 0 | dataModel.set(statePath, status); |
77 | 0 | setStatus((Data) dataModel.get(ProgramConstants.VARIATIONS), status); |
78 | 0 | } |
79 | |
|
80 | |
public static void setPreviousStatus(DataModel dataModel, String status) { |
81 | 0 | QueryPath statePath = QueryPath.parse(ProgramConstants.PREV_STATE); |
82 | 0 | dataModel.set(statePath, status); |
83 | 0 | } |
84 | |
|
85 | |
private static void setStatus(Data inputData, String status) { |
86 | 0 | if (inputData != null) { |
87 | 0 | for (Data.Property property : inputData) { |
88 | 0 | Data data = property.getValue(); |
89 | 0 | data.set(new Data.StringKey(ProgramConstants.STATE), status); |
90 | 0 | } |
91 | |
} |
92 | 0 | } |
93 | |
|
94 | |
public static void retrofitValidationResults(List<ValidationResultInfo> validationResults) { |
95 | 0 | for (ValidationResultInfo validationResult : validationResults) { |
96 | 0 | String key = validationResult.getElement(); |
97 | 0 | if (ProgramConstants.RICH_TEXT_KEYS.contains(key)) { |
98 | 0 | key = key + "/plain"; |
99 | 0 | validationResult.setElement(key); |
100 | |
} |
101 | 0 | } |
102 | 0 | } |
103 | |
|
104 | |
public static void handleValidationErrorsForSpecializations(List<ValidationResultInfo> validationResults, DataModel programModel) { |
105 | 0 | Set<Integer> failedSpecializations = new TreeSet<Integer>(); |
106 | 0 | for (ValidationResultInfo validationResult : validationResults) { |
107 | 0 | String element = validationResult.getElement(); |
108 | 0 | if (element.contains(ProgramConstants.VARIATIONS)) { |
109 | 0 | FieldDescriptor fd = Application.getApplicationContext().getPathToFieldMapping(null, element); |
110 | 0 | if(fd!=null){ |
111 | 0 | fd.getFieldElement().processValidationResult(validationResult); |
112 | |
}else{ |
113 | 0 | int specializationIndex = Integer.parseInt(element.split("/")[1]); |
114 | 0 | failedSpecializations.add(specializationIndex); |
115 | |
} |
116 | |
} |
117 | 0 | } |
118 | 0 | if (!failedSpecializations.isEmpty()) { |
119 | 0 | final Data variationMap = programModel.get(ProgramConstants.VARIATIONS); |
120 | 0 | StringBuilder validationMessage = new StringBuilder(); |
121 | 0 | for (Integer failedSpecialization : failedSpecializations) { |
122 | 0 | Data data = variationMap.get(failedSpecialization); |
123 | 0 | validationMessage.append(data.get(ProgramConstants.LONG_TITLE)).append(", "); |
124 | 0 | } |
125 | 0 | String resultMessage = validationMessage.toString(); |
126 | |
|
127 | 0 | resultMessage = resultMessage.substring(0, resultMessage.length() - 2); |
128 | |
|
129 | 0 | if (failedSpecializations.size() == 1) { |
130 | 0 | KSNotifier.add(new KSNotification(ProgramProperties.get().major_variationFailed(resultMessage), false, true, 5000)); |
131 | |
} else { |
132 | 0 | KSNotifier.add(new KSNotification(ProgramProperties.get().major_variationsFailed(resultMessage), false, true, 5000)); |
133 | |
} |
134 | |
} |
135 | 0 | } |
136 | |
|
137 | |
|
138 | |
public static void syncMetadata(AbstractProgramConfigurer configurer, ModelDefinition modelDefinition) { |
139 | 0 | ConfigurationManager configurationManager = configurer.getProgramSectionConfigManager(); |
140 | 0 | for (Configuration conf : configurationManager.getConfigurations()) { |
141 | 0 | if (conf instanceof AbstractSectionConfiguration) { |
142 | 0 | AbstractSectionConfiguration configuration = (AbstractSectionConfiguration) conf; |
143 | 0 | View view = configuration.getView(false); |
144 | 0 | if (view != null && view instanceof SectionView) { |
145 | 0 | SectionView sectionView = (SectionView) view; |
146 | 0 | sectionView.updateMetadata(modelDefinition); |
147 | |
} |
148 | 0 | } |
149 | |
} |
150 | 0 | } |
151 | |
|
152 | |
public static void unregisterUnusedHandlers(HandlerManager eventBus) { |
153 | 0 | HashMap<GwtEvent.Type, EventHandler> eventsMap = ProgramRegistry.getSpecializationHandlers(); |
154 | 0 | if (eventsMap != null) { |
155 | 0 | for (Map.Entry<GwtEvent.Type, EventHandler> typeEventHandlerEntry : eventsMap.entrySet()) { |
156 | 0 | eventBus.removeHandler(typeEventHandlerEntry.getKey(), typeEventHandlerEntry.getValue()); |
157 | |
} |
158 | |
} |
159 | 0 | } |
160 | |
|
161 | |
public static String getProgramId(DataModel programModel) { |
162 | 0 | return programModel.get(ProgramConstants.ID); |
163 | |
} |
164 | |
|
165 | |
public static String getProgramState(DataModel programModel) { |
166 | 0 | return programModel.get( ProgramConstants.STATE); |
167 | |
} |
168 | |
} |