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