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