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