1 | |
package org.kuali.student.lum.program.client.core.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.assembly.data.QueryPath; |
8 | |
import org.kuali.student.common.ui.client.application.Application; |
9 | |
import org.kuali.student.common.ui.client.application.ViewContext; |
10 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
11 | |
import org.kuali.student.common.ui.client.mvc.DataModel; |
12 | |
import org.kuali.student.common.ui.client.mvc.HasCrossConstraints; |
13 | |
import org.kuali.student.common.ui.client.mvc.ModelRequestCallback; |
14 | |
import org.kuali.student.common.ui.client.mvc.history.HistoryManager; |
15 | |
import org.kuali.student.common.ui.client.service.DataSaveResult; |
16 | |
import org.kuali.student.common.ui.client.validator.ValidatorClientUtils; |
17 | |
import org.kuali.student.common.ui.client.widgets.KSButton; |
18 | |
import org.kuali.student.common.ui.client.widgets.KSButtonAbstract; |
19 | |
import org.kuali.student.common.ui.client.widgets.notification.KSNotification; |
20 | |
import org.kuali.student.common.ui.client.widgets.notification.KSNotifier; |
21 | |
import org.kuali.student.common.ui.shared.IdAttributes.IdType; |
22 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo; |
23 | |
import org.kuali.student.lum.common.client.widgets.AppLocations; |
24 | |
import org.kuali.student.lum.program.client.ProgramConstants; |
25 | |
import org.kuali.student.lum.program.client.ProgramMsgConstants; |
26 | |
import org.kuali.student.lum.program.client.ProgramRegistry; |
27 | |
import org.kuali.student.lum.program.client.ProgramSections; |
28 | |
import org.kuali.student.lum.program.client.ProgramUtils; |
29 | |
import org.kuali.student.lum.program.client.core.CoreController; |
30 | |
import org.kuali.student.lum.program.client.events.AfterSaveEvent; |
31 | |
import org.kuali.student.lum.program.client.events.ChangeViewEvent; |
32 | |
import org.kuali.student.lum.program.client.events.MetadataLoadedEvent; |
33 | |
import org.kuali.student.lum.program.client.events.ModelLoadedEvent; |
34 | |
import org.kuali.student.lum.program.client.events.StateChangeEvent; |
35 | |
import org.kuali.student.lum.program.client.events.StoreRequirementIDsEvent; |
36 | |
import org.kuali.student.lum.program.client.events.UpdateEvent; |
37 | |
import org.kuali.student.lum.program.client.rpc.AbstractCallback; |
38 | |
|
39 | |
import com.google.gwt.core.client.GWT; |
40 | |
import com.google.gwt.event.dom.client.ClickEvent; |
41 | |
import com.google.gwt.event.dom.client.ClickHandler; |
42 | |
import com.google.gwt.event.shared.HandlerManager; |
43 | |
import com.google.gwt.user.client.Window; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | public class CoreEditController extends CoreController { |
49 | |
|
50 | 0 | private final KSButton saveButton = new KSButton(getLabel(ProgramMsgConstants.COMMON_SAVE)); |
51 | 0 | private final KSButton cancelButton = new KSButton(getLabel(ProgramMsgConstants.COMMON_CANCEL), KSButtonAbstract.ButtonStyle.ANCHOR_LARGE_CENTERED); |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public CoreEditController(DataModel programModel, ViewContext viewContext, HandlerManager eventBus) { |
60 | 0 | super(programModel, viewContext, eventBus); |
61 | 0 | configurer = GWT.create(CoreEditConfigurer.class); |
62 | 0 | bind(); |
63 | 0 | } |
64 | |
|
65 | |
@Override |
66 | |
protected void configureView() { |
67 | 0 | super.configureView(); |
68 | 0 | if (!initialized) { |
69 | 0 | eventBus.fireEvent(new MetadataLoadedEvent(programModel.getDefinition(), this)); |
70 | 0 | List<Enum<?>> excludedViews = new ArrayList<Enum<?>>(); |
71 | 0 | excludedViews.add(ProgramSections.PROGRAM_REQUIREMENTS_EDIT); |
72 | 0 | excludedViews.add(ProgramSections.SUPPORTING_DOCUMENTS_EDIT); |
73 | 0 | excludedViews.add(ProgramSections.SUMMARY); |
74 | 0 | addCommonButton(getLabel(ProgramMsgConstants.PROGRAM_MENU_SECTIONS), saveButton, excludedViews); |
75 | 0 | addCommonButton(getLabel(ProgramMsgConstants.PROGRAM_MENU_SECTIONS), cancelButton, excludedViews); |
76 | 0 | initialized = true; |
77 | |
} |
78 | 0 | } |
79 | |
|
80 | |
private void bind() { |
81 | 0 | saveButton.addClickHandler(new ClickHandler() { |
82 | |
|
83 | |
@Override |
84 | |
public void onClick(ClickEvent event) { |
85 | 0 | doSave(); |
86 | 0 | } |
87 | |
}); |
88 | 0 | cancelButton.addClickHandler(new ClickHandler() { |
89 | |
|
90 | |
@Override |
91 | |
public void onClick(ClickEvent event) { |
92 | 0 | doCancel(); |
93 | 0 | } |
94 | |
}); |
95 | 0 | eventBus.addHandler(StoreRequirementIDsEvent.TYPE, new StoreRequirementIDsEvent.Handler() { |
96 | |
@Override |
97 | |
public void onEvent(StoreRequirementIDsEvent event) { |
98 | 0 | List<String> ids = event.getProgramRequirementIds(); |
99 | |
|
100 | 0 | programModel.set(QueryPath.parse(ProgramConstants.PROGRAM_REQUIREMENTS), new Data()); |
101 | 0 | Data programRequirements = getDataProperty(ProgramConstants.PROGRAM_REQUIREMENTS); |
102 | |
|
103 | 0 | if (programRequirements == null) { |
104 | 0 | Window.alert("Cannot find program requirements in data model."); |
105 | 0 | GWT.log("Cannot find program requirements in data model", null); |
106 | 0 | return; |
107 | |
} |
108 | |
|
109 | 0 | for (String id : ids) { |
110 | 0 | programRequirements.add(id); |
111 | |
} |
112 | 0 | doSave(); |
113 | 0 | } |
114 | |
}); |
115 | 0 | eventBus.addHandler(ChangeViewEvent.TYPE, new ChangeViewEvent.Handler() { |
116 | |
@Override |
117 | |
public void onEvent(ChangeViewEvent event) { |
118 | 0 | showView(event.getViewToken()); |
119 | 0 | } |
120 | |
}); |
121 | 0 | eventBus.addHandler(UpdateEvent.TYPE, new UpdateEvent.Handler() { |
122 | |
@Override |
123 | |
public void onEvent(UpdateEvent event) { |
124 | 0 | doSave(event.getOkCallback()); |
125 | 0 | } |
126 | |
}); |
127 | 0 | eventBus.addHandler(StateChangeEvent.TYPE, new StateChangeEvent.Handler() { |
128 | |
@Override |
129 | |
public void onEvent(final StateChangeEvent event) { |
130 | 0 | programModel.validateNextState(new Callback<List<ValidationResultInfo>>() { |
131 | |
@Override |
132 | |
public void exec(List<ValidationResultInfo> result) { |
133 | 0 | boolean isSectionValid = isValid(result, true); |
134 | 0 | if (isSectionValid) { |
135 | 0 | Callback<Boolean> callback = new Callback<Boolean>() { |
136 | |
@Override |
137 | |
public void exec(Boolean result) { |
138 | 0 | if (result) { |
139 | 0 | reloadMetadata = true; |
140 | 0 | loadMetadata(new Callback<Boolean>() { |
141 | |
@Override |
142 | |
public void exec(Boolean result) { |
143 | 0 | if (result) { |
144 | 0 | ProgramUtils.syncMetadata(configurer, programModel.getDefinition()); |
145 | 0 | HistoryManager.navigate(AppLocations.Locations.VIEW_CORE_PROGRAM.getLocation(), context); |
146 | |
} |
147 | 0 | } |
148 | |
}); |
149 | |
} |
150 | 0 | } |
151 | |
}; |
152 | 0 | updateState(event.getProgramStatus().getValue(), callback); |
153 | 0 | } else { |
154 | 0 | KSNotifier.add(new KSNotification("Unable to save, please check fields for errors.", false, true, 5000)); |
155 | |
} |
156 | 0 | } |
157 | |
}); |
158 | 0 | } |
159 | |
}); |
160 | 0 | } |
161 | |
|
162 | |
private void doCancel() { |
163 | 0 | showView(ProgramSections.SUMMARY); |
164 | 0 | } |
165 | |
|
166 | |
@Override |
167 | |
protected void doSave() { |
168 | 0 | doSave(NO_OP_CALLBACK); |
169 | 0 | } |
170 | |
|
171 | |
@Override |
172 | |
protected void loadModel(ModelRequestCallback<DataModel> callback) { |
173 | 0 | ViewContext viewContext = getViewContext(); |
174 | 0 | if (viewContext.getIdType() == IdType.COPY_OF_OBJECT_ID) { |
175 | 0 | createNewVersionAndLoadModel(callback, viewContext); |
176 | |
} else { |
177 | 0 | super.loadModel(callback); |
178 | |
} |
179 | 0 | } |
180 | |
|
181 | |
protected void createNewVersionAndLoadModel(final ModelRequestCallback<DataModel> callback, final ViewContext viewContext) { |
182 | 0 | Data data = new Data(); |
183 | 0 | Data versionData = new Data(); |
184 | 0 | versionData.set(new Data.StringKey("versionIndId"), getViewContext().getId()); |
185 | 0 | versionData.set(new Data.StringKey("versionComment"), "Core Program Version"); |
186 | 0 | data.set(new Data.StringKey("versionInfo"), versionData); |
187 | |
|
188 | 0 | programRemoteService.saveData(data, new AbstractCallback<DataSaveResult>(getLabel(ProgramMsgConstants.COMMON_RETRIEVINGDATA)) { |
189 | |
@Override |
190 | |
public void onSuccess(DataSaveResult result) { |
191 | 0 | super.onSuccess(result); |
192 | 0 | viewContext.setIdType(IdType.OBJECT_ID); |
193 | 0 | viewContext.setId(getStringProperty(ProgramConstants.ID)); |
194 | 0 | refreshModelAndView(result); |
195 | 0 | callback.onModelReady(programModel); |
196 | 0 | eventBus.fireEvent(new ModelLoadedEvent(programModel)); |
197 | 0 | } |
198 | |
|
199 | |
@Override |
200 | |
public void onFailure(Throwable caught) { |
201 | 0 | super.onFailure(caught); |
202 | 0 | callback.onRequestFail(caught); |
203 | 0 | } |
204 | |
}); |
205 | |
|
206 | 0 | } |
207 | |
|
208 | |
private void doSave(final Callback<Boolean> okCallback) { |
209 | 0 | requestModel(new ModelRequestCallback<DataModel>() { |
210 | |
@Override |
211 | |
public void onModelReady(DataModel model) { |
212 | 0 | CoreEditController.this.updateModelFromCurrentView(); |
213 | 0 | model.validate(new Callback<List<ValidationResultInfo>>() { |
214 | |
@Override |
215 | |
public void exec(List<ValidationResultInfo> result) { |
216 | 0 | boolean isSectionValid = isValid(result, true); |
217 | 0 | if (isSectionValid) { |
218 | 0 | saveData(okCallback); |
219 | |
} else { |
220 | 0 | okCallback.exec(false); |
221 | 0 | KSNotifier.add(new KSNotification("Unable to save, please check fields for errors.", false, true, 5000)); |
222 | |
} |
223 | 0 | } |
224 | |
}); |
225 | |
|
226 | 0 | } |
227 | |
|
228 | |
@Override |
229 | |
public void onRequestFail(Throwable cause) { |
230 | 0 | GWT.log("Unable to retrieve model for validation and save", cause); |
231 | 0 | } |
232 | |
}); |
233 | 0 | } |
234 | |
|
235 | |
private void saveData(final Callback<Boolean> okCallback) { |
236 | 0 | programRemoteService.saveData(programModel.getRoot(), new AbstractCallback<DataSaveResult>(getLabel(ProgramMsgConstants.COMMON_SAVINGDATA)) { |
237 | |
@Override |
238 | |
public void onSuccess(DataSaveResult result) { |
239 | 0 | super.onSuccess(result); |
240 | |
|
241 | 0 | clearAllWarnings(); |
242 | 0 | Application.getApplicationContext().clearValidationWarnings(); |
243 | |
|
244 | 0 | List<ValidationResultInfo> validationResults = result.getValidationResults(); |
245 | 0 | Application.getApplicationContext().addValidationWarnings(validationResults); |
246 | |
|
247 | 0 | if (ValidatorClientUtils.hasErrors(validationResults)) { |
248 | 0 | isValid(result.getValidationResults(), false, true); |
249 | 0 | StringBuilder msg = new StringBuilder(); |
250 | 0 | for (ValidationResultInfo vri : result.getValidationResults()) { |
251 | 0 | if(!msg.toString().contains(vri.getMessage())) |
252 | 0 | msg.append(vri.getMessage() + " "); |
253 | |
} |
254 | 0 | if (msg.length() > 0) |
255 | 0 | KSNotifier.add(new KSNotification(msg.toString(), false, true, 5000)); |
256 | 0 | okCallback.exec(false); |
257 | 0 | } else { |
258 | 0 | refreshModelAndView(result); |
259 | 0 | if (ProgramSections.getViewForUpdate().contains(getCurrentViewEnum().name())) { |
260 | 0 | processBeforeShow = false; |
261 | 0 | showView(getCurrentViewEnum()); |
262 | |
} |
263 | 0 | resetFieldInteractionFlag(); |
264 | 0 | throwAfterSaveEvent(); |
265 | 0 | HistoryManager.logHistoryChange(); |
266 | |
|
267 | 0 | if (ValidatorClientUtils.hasWarnings(validationResults)){ |
268 | 0 | isValid(result.getValidationResults(), false, true); |
269 | 0 | KSNotifier.show("Saved with Warnings"); |
270 | |
} else { |
271 | 0 | KSNotifier.show(getLabel(ProgramMsgConstants.COMMON_SUCCESSFULSAVE)); |
272 | |
} |
273 | |
|
274 | 0 | okCallback.exec(true); |
275 | |
} |
276 | 0 | } |
277 | |
}); |
278 | 0 | } |
279 | |
|
280 | |
private void throwAfterSaveEvent() { |
281 | 0 | eventBus.fireEvent(new AfterSaveEvent(programModel, this)); |
282 | 0 | } |
283 | |
|
284 | |
@Override |
285 | |
public void onModelLoadedEvent() { |
286 | 0 | Enum<?> changeSection = ProgramRegistry.getSection(); |
287 | 0 | if (changeSection != null) { |
288 | 0 | showView(changeSection); |
289 | 0 | ProgramRegistry.setSection(null); |
290 | |
} else { |
291 | 0 | String id = (String) programModel.get(ProgramConstants.ID); |
292 | 0 | if (id == null) { |
293 | 0 | showView(ProgramSections.PROGRAM_DETAILS_EDIT); |
294 | |
} else { |
295 | 0 | showView(ProgramSections.SUMMARY); |
296 | |
} |
297 | |
} |
298 | 0 | } |
299 | |
@Override |
300 | |
public void beforeShow(final Callback<Boolean> onReadyCallback) { |
301 | 0 | if(!initialized){ |
302 | 0 | Application.getApplicationContext().clearCrossConstraintMap(null); |
303 | 0 | Application.getApplicationContext().clearPathToFieldMapping(null); |
304 | |
} |
305 | |
|
306 | 0 | Application.getApplicationContext().setParentPath(""); |
307 | 0 | super.beforeShow(onReadyCallback); |
308 | 0 | } |
309 | |
|
310 | |
|
311 | |
|
312 | |
@Override |
313 | |
public <V extends Enum<?>> void showView(V viewType, |
314 | |
final Callback<Boolean> onReadyCallback) { |
315 | 0 | Callback<Boolean> updateCrossConstraintsCallback = new Callback<Boolean>(){ |
316 | |
public void exec(Boolean result) { |
317 | 0 | onReadyCallback.exec(result); |
318 | 0 | for(HasCrossConstraints crossConstraint:Application.getApplicationContext().getCrossConstraints(null)){ |
319 | 0 | crossConstraint.reprocessWithUpdatedConstraints(); |
320 | |
} |
321 | 0 | showWarnings(); |
322 | 0 | } |
323 | |
}; |
324 | 0 | super.showView(viewType, updateCrossConstraintsCallback); |
325 | 0 | } |
326 | |
} |