1 | |
package org.kuali.student.lum.program.client.variation.edit; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.List; |
5 | |
|
6 | |
import org.kuali.student.common.assembly.data.Data; |
7 | |
import org.kuali.student.common.ui.client.application.Application; |
8 | |
import org.kuali.student.common.ui.client.application.ViewContext; |
9 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
10 | |
import org.kuali.student.common.ui.client.mvc.DataModel; |
11 | |
import org.kuali.student.common.ui.client.mvc.HasCrossConstraints; |
12 | |
import org.kuali.student.common.ui.client.mvc.ModelRequestCallback; |
13 | |
import org.kuali.student.common.ui.client.mvc.history.HistoryManager; |
14 | |
import org.kuali.student.common.ui.client.widgets.KSButton; |
15 | |
import org.kuali.student.common.ui.client.widgets.KSButtonAbstract; |
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.widgets.AppLocations; |
20 | |
import org.kuali.student.lum.program.client.ProgramConstants; |
21 | |
import org.kuali.student.lum.program.client.ProgramRegistry; |
22 | |
import org.kuali.student.lum.program.client.ProgramSections; |
23 | |
import org.kuali.student.lum.program.client.events.ChangeViewEvent; |
24 | |
import org.kuali.student.lum.program.client.events.ModelLoadedEvent; |
25 | |
import org.kuali.student.lum.program.client.events.SpecializationCreatedEvent; |
26 | |
import org.kuali.student.lum.program.client.events.SpecializationSaveEvent; |
27 | |
import org.kuali.student.lum.program.client.events.SpecializationUpdateEvent; |
28 | |
import org.kuali.student.lum.program.client.events.StoreSpecRequirementIDsEvent; |
29 | |
import org.kuali.student.lum.program.client.major.MajorController; |
30 | |
import org.kuali.student.lum.program.client.major.edit.MajorEditController; |
31 | |
import org.kuali.student.lum.program.client.major.proposal.MajorProposalController; |
32 | |
import org.kuali.student.lum.program.client.properties.ProgramProperties; |
33 | |
import org.kuali.student.lum.program.client.variation.VariationController; |
34 | |
import org.kuali.student.lum.program.client.widgets.ProgramSideBar; |
35 | |
|
36 | |
import com.google.gwt.core.client.GWT; |
37 | |
import com.google.gwt.event.dom.client.ClickEvent; |
38 | |
import com.google.gwt.event.dom.client.ClickHandler; |
39 | |
import com.google.gwt.event.shared.HandlerManager; |
40 | |
import com.google.gwt.user.client.Window; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | public class VariationEditController extends VariationController { |
46 | |
|
47 | 0 | private final KSButton saveButton = new KSButton(ProgramProperties.get().common_save()); |
48 | 0 | private final KSButton cancelButton = new KSButton(ProgramProperties.get().common_cancel(), KSButtonAbstract.ButtonStyle.ANCHOR_LARGE_CENTERED); |
49 | |
|
50 | |
private String currentId; |
51 | |
|
52 | |
public VariationEditController(DataModel programModel, ViewContext viewContext, HandlerManager eventBus, MajorController majorController) { |
53 | 0 | super(programModel, viewContext, eventBus, majorController); |
54 | 0 | configurer = GWT.create(VariationEditConfigurer.class); |
55 | 0 | sideBar.setState(ProgramSideBar.State.EDIT); |
56 | 0 | if (getStringProperty(ProgramConstants.ID) != null) { |
57 | 0 | setDefaultView(ProgramSections.SUMMARY); |
58 | |
} |
59 | 0 | initHandlers(); |
60 | 0 | } |
61 | |
|
62 | |
private void initHandlers() { |
63 | 0 | saveButton.addClickHandler(new ClickHandler() { |
64 | |
|
65 | |
@Override |
66 | |
public void onClick(ClickEvent event) { |
67 | 0 | doSave(); |
68 | 0 | } |
69 | |
}); |
70 | 0 | cancelButton.addClickHandler(new ClickHandler() { |
71 | |
|
72 | |
@Override |
73 | |
public void onClick(ClickEvent event) { |
74 | 0 | doCancel(); |
75 | 0 | } |
76 | |
}); |
77 | 0 | ModelLoadedEvent.Handler modelLoadedHandler = new ModelLoadedEvent.Handler() { |
78 | |
@Override |
79 | |
public void onEvent(ModelLoadedEvent event) { |
80 | 0 | DataModel dataModel = event.getModel(); |
81 | 0 | Data variationMap = dataModel.get(ProgramConstants.VARIATIONS); |
82 | 0 | if (variationMap != null) { |
83 | 0 | int row = 0; |
84 | 0 | for (Data.Property property : variationMap) { |
85 | 0 | final Data variationData = property.getValue(); |
86 | 0 | if (variationData.get(ProgramConstants.ID).equals(currentId)) { |
87 | 0 | programModel.setRoot(variationData); |
88 | 0 | ProgramRegistry.setData(variationData); |
89 | 0 | ProgramRegistry.setRow(row); |
90 | 0 | setContentTitle(getProgramName()); |
91 | 0 | row++; |
92 | 0 | return; |
93 | |
} |
94 | 0 | } |
95 | |
} |
96 | 0 | } |
97 | |
}; |
98 | 0 | ProgramRegistry.addHandler(ModelLoadedEvent.TYPE, modelLoadedHandler); |
99 | 0 | eventBus.addHandler(ModelLoadedEvent.TYPE, modelLoadedHandler); |
100 | |
|
101 | 0 | ChangeViewEvent.Handler changeViewHandler = new ChangeViewEvent.Handler() { |
102 | |
@Override |
103 | |
public void onEvent(ChangeViewEvent event) { |
104 | 0 | Enum<?> viewToken = event.getViewToken(); |
105 | 0 | if (!viewToken.name().equals(ProgramSections.SPECIALIZATIONS_EDIT.name())) { |
106 | 0 | showView(viewToken); |
107 | |
} |
108 | 0 | } |
109 | |
}; |
110 | 0 | ProgramRegistry.addHandler(ChangeViewEvent.TYPE, changeViewHandler); |
111 | 0 | eventBus.addHandler(ChangeViewEvent.TYPE, changeViewHandler); |
112 | 0 | eventBus.addHandler(SpecializationCreatedEvent.TYPE, new SpecializationCreatedEvent.Handler() { |
113 | |
|
114 | |
@Override |
115 | |
public void onEvent(SpecializationCreatedEvent event) { |
116 | 0 | programModel.getRoot().set(ProgramConstants.ID, event.getSpecializationId()); |
117 | 0 | showWarnings(); |
118 | 0 | } |
119 | |
}); |
120 | |
|
121 | 0 | eventBus.addHandler(SpecializationUpdateEvent.TYPE, new SpecializationUpdateEvent.Handler() { |
122 | |
@Override |
123 | |
public void onEvent(SpecializationUpdateEvent event) { |
124 | |
|
125 | |
|
126 | |
|
127 | 0 | String currSpecializationId = programModel.getRoot().get(ProgramConstants.ID); |
128 | 0 | for (Data.Property prop : event.getSpecializations()) { |
129 | 0 | String specId = (String) ((Data) prop.getValue()).get(ProgramConstants.ID); |
130 | |
|
131 | 0 | if (null != specId && specId.equals(currSpecializationId) && prop.getValueType().equals(Data.class)) { |
132 | |
|
133 | 0 | programModel.setRoot((Data) prop.getValue()); |
134 | 0 | showView(getCurrentViewEnum()); |
135 | |
} |
136 | 0 | } |
137 | |
|
138 | |
|
139 | 0 | showWarnings(); |
140 | 0 | } |
141 | |
}); |
142 | |
|
143 | 0 | StoreSpecRequirementIDsEvent.Handler requirementsHandler = new StoreSpecRequirementIDsEvent.Handler() { |
144 | |
@Override |
145 | |
public void onEvent(StoreSpecRequirementIDsEvent event) { |
146 | 0 | final String programId = event.getProgramId(); |
147 | 0 | final List<String> ids = event.getProgramRequirementIds(); |
148 | |
|
149 | 0 | requestModel(new ModelRequestCallback<DataModel>() { |
150 | |
@Override |
151 | |
public void onModelReady(final DataModel model) { |
152 | 0 | Data programRequirements = null; |
153 | |
|
154 | |
|
155 | |
|
156 | 0 | Data variationData = model.getRoot(); |
157 | 0 | if (variationData.get(ProgramConstants.ID).equals(programId)) { |
158 | 0 | variationData.set(ProgramConstants.PROGRAM_REQUIREMENTS, new Data()); |
159 | 0 | programRequirements = variationData.get(ProgramConstants.PROGRAM_REQUIREMENTS); |
160 | |
|
161 | |
} |
162 | |
|
163 | |
|
164 | 0 | if (programRequirements == null) { |
165 | 0 | Window.alert("Cannot find program requirements in data model."); |
166 | 0 | GWT.log("Cannot find program requirements in data model", null); |
167 | 0 | return; |
168 | |
} |
169 | |
|
170 | 0 | for (String id : ids) { |
171 | 0 | programRequirements.add(id); |
172 | |
} |
173 | 0 | doSave(); |
174 | 0 | } |
175 | |
|
176 | |
@Override |
177 | |
public void onRequestFail(Throwable cause) { |
178 | 0 | GWT.log("Unable to retrieve model for validation and save", cause); |
179 | 0 | } |
180 | |
|
181 | |
}); |
182 | 0 | } |
183 | |
}; |
184 | 0 | ProgramRegistry.addHandler(StoreSpecRequirementIDsEvent.TYPE, requirementsHandler); |
185 | 0 | eventBus.addHandler(StoreSpecRequirementIDsEvent.TYPE, requirementsHandler); |
186 | 0 | } |
187 | |
|
188 | |
@Override |
189 | |
protected void fireUpdateEvent(Callback<Boolean> okToChange) { |
190 | 0 | doSave(okToChange); |
191 | 0 | } |
192 | |
|
193 | |
private void doSave(final Callback<Boolean> okToChange) { |
194 | 0 | requestModel(new ModelRequestCallback<DataModel>() { |
195 | |
@Override |
196 | |
public void onModelReady(final DataModel model) { |
197 | 0 | VariationEditController.this.updateModelFromCurrentView(); |
198 | 0 | model.setParentPath(ProgramConstants.VARIATIONS + "/" + ProgramRegistry.getRow()); |
199 | 0 | model.validate(new Callback<List<ValidationResultInfo>>() { |
200 | |
@Override |
201 | |
public void exec(List<ValidationResultInfo> results) { |
202 | 0 | boolean isSectionValid = isValid(results, true); |
203 | 0 | if (isSectionValid) { |
204 | 0 | saveData(model); |
205 | 0 | okToChange.exec(true); |
206 | |
} else { |
207 | 0 | okToChange.exec(false); |
208 | 0 | KSNotifier.add(new KSNotification("Unable to save, please check fields for errors.", false, true, 5000)); |
209 | |
} |
210 | 0 | } |
211 | |
}); |
212 | 0 | } |
213 | |
|
214 | |
@Override |
215 | |
public void onRequestFail(Throwable cause) { |
216 | 0 | GWT.log("Unable to retrieve model for validation and save", cause); |
217 | 0 | } |
218 | |
|
219 | |
}); |
220 | 0 | } |
221 | |
|
222 | |
@Override |
223 | |
protected void configureView() { |
224 | 0 | super.configureView(); |
225 | 0 | if (!initialized) { |
226 | 0 | List<Enum<?>> excludedViews = new ArrayList<Enum<?>>(); |
227 | 0 | excludedViews.add(ProgramSections.PROGRAM_REQUIREMENTS_EDIT); |
228 | 0 | excludedViews.add(ProgramSections.SUPPORTING_DOCUMENTS_EDIT); |
229 | 0 | excludedViews.add(ProgramSections.SUMMARY); |
230 | 0 | addCommonButton(ProgramProperties.get().program_menu_sections(), saveButton, excludedViews); |
231 | 0 | addCommonButton(ProgramProperties.get().program_menu_sections(), cancelButton, excludedViews); |
232 | 0 | initialized = true; |
233 | |
} |
234 | |
|
235 | 0 | } |
236 | |
|
237 | |
@Override |
238 | |
protected void resetModel() { |
239 | 0 | currentId = getStringProperty(ProgramConstants.ID); |
240 | 0 | programModel.resetRoot(); |
241 | 0 | } |
242 | |
|
243 | |
private void doCancel() { |
244 | 0 | navigateToParent(ProgramSections.SUMMARY); |
245 | 0 | } |
246 | |
|
247 | |
@Override |
248 | |
protected void doSave() { |
249 | 0 | doSave(NO_OP_CALLBACK); |
250 | 0 | } |
251 | |
|
252 | |
private void saveData(DataModel model) { |
253 | 0 | currentId = model.get(ProgramConstants.ID); |
254 | 0 | eventBus.fireEvent(new SpecializationSaveEvent(model.getRoot())); |
255 | 0 | setContentTitle(getProgramName()); |
256 | 0 | setName(getProgramName()); |
257 | 0 | resetFieldInteractionFlag(); |
258 | 0 | } |
259 | |
|
260 | |
@Override |
261 | |
protected void navigateToParent() { |
262 | 0 | navigateToParent(ProgramSections.SPECIALIZATIONS_EDIT); |
263 | 0 | } |
264 | |
|
265 | |
private void navigateToParent(ProgramSections parentSection) { |
266 | 0 | String appLoc = ""; |
267 | |
|
268 | 0 | if(!(majorController instanceof MajorProposalController)) |
269 | 0 | appLoc = AppLocations.Locations.EDIT_PROGRAM_SPEC.getLocation(); |
270 | |
else |
271 | 0 | appLoc = AppLocations.Locations.PROGRAM_PROPOSAL.getLocation(); |
272 | 0 | String path = HistoryManager.appendContext(appLoc, getViewContext()) + "/" + parentSection; |
273 | 0 | HistoryManager.navigate(path); |
274 | 0 | } |
275 | |
|
276 | |
|
277 | |
@Override |
278 | |
public void beforeShow(final Callback<Boolean> onReadyCallback) { |
279 | |
|
280 | 0 | Application.getApplicationContext().clearCrossConstraintsWithStartingPath(null,ProgramConstants.VARIATIONS); |
281 | |
|
282 | |
|
283 | 0 | String newParentPath = ProgramConstants.VARIATIONS+"/"+org.kuali.student.lum.program.client.ProgramRegistry.getRow()+"/"; |
284 | 0 | Application.getApplicationContext().setParentPath(newParentPath); |
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | 0 | Callback<Boolean> finalizeVariationView = new Callback<Boolean>(){ |
290 | |
public void exec(Boolean result) { |
291 | |
|
292 | 0 | for(HasCrossConstraints crossConstraint:Application.getApplicationContext().getCrossConstraints(null)){ |
293 | 0 | crossConstraint.reprocessWithUpdatedConstraints(); |
294 | |
} |
295 | |
|
296 | 0 | onReadyCallback.exec(result); |
297 | 0 | } |
298 | |
}; |
299 | 0 | super.beforeShow(finalizeVariationView); |
300 | 0 | } |
301 | |
|
302 | |
|
303 | |
|
304 | |
@Override |
305 | |
public <V extends Enum<?>> void showView(V viewType, |
306 | |
final Callback<Boolean> onReadyCallback) { |
307 | 0 | Callback<Boolean> updateCrossConstraintsCallback = new Callback<Boolean>(){ |
308 | |
public void exec(Boolean result) { |
309 | 0 | onReadyCallback.exec(result); |
310 | 0 | showWarnings(); |
311 | 0 | } |
312 | |
}; |
313 | 0 | super.showView(viewType, updateCrossConstraintsCallback); |
314 | 0 | } |
315 | |
|
316 | |
} |