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.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.core.CoreController; |
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 CoreEditController extends CoreController { |
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 CoreEditController(DataModel programModel, ViewContext viewContext, HandlerManager eventBus) { |
59 | 0 | super(programModel, viewContext, eventBus); |
60 | 0 | configurer = GWT.create(CoreEditConfigurer.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_CORE_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 | |
@Override |
173 | |
protected void loadModel(ModelRequestCallback<DataModel> callback) { |
174 | 0 | ViewContext viewContext = getViewContext(); |
175 | 0 | if (viewContext.getIdType() == IdType.COPY_OF_OBJECT_ID) { |
176 | 0 | createNewVersionAndLoadModel(callback, viewContext); |
177 | |
} else { |
178 | 0 | super.loadModel(callback); |
179 | |
} |
180 | 0 | } |
181 | |
|
182 | |
protected void createNewVersionAndLoadModel(final ModelRequestCallback<DataModel> callback, final ViewContext viewContext) { |
183 | 0 | Data data = new Data(); |
184 | 0 | Data versionData = new Data(); |
185 | 0 | versionData.set(new Data.StringKey("versionIndId"), getViewContext().getId()); |
186 | 0 | versionData.set(new Data.StringKey("versionComment"), "Core Program Version"); |
187 | 0 | data.set(new Data.StringKey("versionInfo"), versionData); |
188 | |
|
189 | 0 | programRemoteService.saveData(data, new AbstractCallback<DataSaveResult>(ProgramProperties.get().common_retrievingData()) { |
190 | |
@Override |
191 | |
public void onSuccess(DataSaveResult result) { |
192 | 0 | super.onSuccess(result); |
193 | 0 | programModel.setRoot(result.getValue()); |
194 | 0 | viewContext.setIdType(IdType.OBJECT_ID); |
195 | 0 | viewContext.setId(getStringProperty(ProgramConstants.ID)); |
196 | 0 | setHeaderTitle(); |
197 | 0 | setStatus(); |
198 | 0 | callback.onModelReady(programModel); |
199 | 0 | eventBus.fireEvent(new ModelLoadedEvent(programModel)); |
200 | 0 | } |
201 | |
|
202 | |
@Override |
203 | |
public void onFailure(Throwable caught) { |
204 | 0 | super.onFailure(caught); |
205 | 0 | callback.onRequestFail(caught); |
206 | 0 | } |
207 | |
}); |
208 | |
|
209 | 0 | } |
210 | |
|
211 | |
private void doSave(final Callback<Boolean> okCallback) { |
212 | 0 | requestModel(new ModelRequestCallback<DataModel>() { |
213 | |
@Override |
214 | |
public void onModelReady(DataModel model) { |
215 | 0 | CoreEditController.this.updateModelFromCurrentView(); |
216 | 0 | model.validate(new Callback<List<ValidationResultInfo>>() { |
217 | |
@Override |
218 | |
public void exec(List<ValidationResultInfo> result) { |
219 | 0 | boolean isSectionValid = isValid(result, true); |
220 | 0 | if (isSectionValid) { |
221 | 0 | saveData(okCallback); |
222 | |
} else { |
223 | 0 | okCallback.exec(false); |
224 | 0 | KSNotifier.add(new KSNotification("Unable to save, please check fields for errors.", false, true, 5000)); |
225 | |
} |
226 | 0 | } |
227 | |
}); |
228 | |
|
229 | 0 | } |
230 | |
|
231 | |
@Override |
232 | |
public void onRequestFail(Throwable cause) { |
233 | 0 | GWT.log("Unable to retrieve model for validation and save", cause); |
234 | 0 | } |
235 | |
}); |
236 | 0 | } |
237 | |
|
238 | |
private void saveData(final Callback<Boolean> okCallback) { |
239 | 0 | programRemoteService.saveData(programModel.getRoot(), new AbstractCallback<DataSaveResult>(ProgramProperties.get().common_savingData()) { |
240 | |
@Override |
241 | |
public void onSuccess(DataSaveResult result) { |
242 | 0 | super.onSuccess(result); |
243 | 0 | if (result.getValidationResults() != null && !result.getValidationResults().isEmpty()) { |
244 | 0 | if (previousState != null) { |
245 | 0 | ProgramUtils.setStatus(programModel, previousState.getValue()); |
246 | |
} |
247 | 0 | isValid(result.getValidationResults(), false, true); |
248 | 0 | StringBuilder msg = new StringBuilder(); |
249 | 0 | for (ValidationResultInfo vri : result.getValidationResults()) { |
250 | 0 | msg.append(vri.getMessage()); |
251 | |
} |
252 | 0 | okCallback.exec(false); |
253 | 0 | } else { |
254 | 0 | previousState = null; |
255 | 0 | programModel.setRoot(result.getValue()); |
256 | 0 | setHeaderTitle(); |
257 | 0 | setStatus(); |
258 | 0 | if (ProgramSections.getViewForUpdate().contains(getCurrentViewEnum().name())) { |
259 | 0 | processBeforeShow = false; |
260 | 0 | showView(getCurrentViewEnum()); |
261 | |
} |
262 | 0 | resetFieldInteractionFlag(); |
263 | 0 | throwAfterSaveEvent(); |
264 | 0 | HistoryManager.logHistoryChange(); |
265 | 0 | KSNotifier.show(ProgramProperties.get().common_successfulSave()); |
266 | 0 | okCallback.exec(true); |
267 | |
} |
268 | 0 | } |
269 | |
}); |
270 | 0 | } |
271 | |
|
272 | |
private void throwAfterSaveEvent() { |
273 | 0 | eventBus.fireEvent(new AfterSaveEvent(programModel, this)); |
274 | 0 | } |
275 | |
|
276 | |
@Override |
277 | |
public void onModelLoadedEvent() { |
278 | 0 | Enum<?> changeSection = ProgramRegistry.getSection(); |
279 | 0 | if (changeSection != null) { |
280 | 0 | showView(changeSection); |
281 | 0 | ProgramRegistry.setSection(null); |
282 | |
} else { |
283 | 0 | String id = (String) programModel.get(ProgramConstants.ID); |
284 | 0 | if (id == null) { |
285 | 0 | showView(ProgramSections.PROGRAM_DETAILS_EDIT); |
286 | |
} else { |
287 | 0 | showView(ProgramSections.SUMMARY); |
288 | |
} |
289 | |
} |
290 | 0 | } |
291 | |
} |