1 | |
package org.kuali.student.lum.program.client.variation.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.widgets.KSButton; |
12 | |
import org.kuali.student.common.ui.client.widgets.KSButtonAbstract; |
13 | |
import org.kuali.student.common.ui.client.widgets.notification.KSNotification; |
14 | |
import org.kuali.student.common.ui.client.widgets.notification.KSNotifier; |
15 | |
import org.kuali.student.core.assembly.data.Data; |
16 | |
import org.kuali.student.core.validation.dto.ValidationResultInfo; |
17 | |
import org.kuali.student.lum.common.client.widgets.AppLocations; |
18 | |
import org.kuali.student.lum.program.client.ProgramConstants; |
19 | |
import org.kuali.student.lum.program.client.ProgramRegistry; |
20 | |
import org.kuali.student.lum.program.client.ProgramSections; |
21 | |
import org.kuali.student.lum.program.client.events.ChangeViewEvent; |
22 | |
import org.kuali.student.lum.program.client.events.ModelLoadedEvent; |
23 | |
import org.kuali.student.lum.program.client.events.SpecializationCreatedEvent; |
24 | |
import org.kuali.student.lum.program.client.events.SpecializationSaveEvent; |
25 | |
import org.kuali.student.lum.program.client.events.SpecializationUpdateEvent; |
26 | |
import org.kuali.student.lum.program.client.events.StoreSpecRequirementIDsEvent; |
27 | |
import org.kuali.student.lum.program.client.major.edit.MajorEditController; |
28 | |
import org.kuali.student.lum.program.client.properties.ProgramProperties; |
29 | |
import org.kuali.student.lum.program.client.variation.VariationController; |
30 | |
import org.kuali.student.lum.program.client.widgets.ProgramSideBar; |
31 | |
|
32 | |
import com.google.gwt.core.client.GWT; |
33 | |
import com.google.gwt.event.dom.client.ClickEvent; |
34 | |
import com.google.gwt.event.dom.client.ClickHandler; |
35 | |
import com.google.gwt.event.shared.HandlerManager; |
36 | |
import com.google.gwt.user.client.Window; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 0 | public class VariationEditController extends VariationController { |
42 | |
|
43 | 0 | private final KSButton saveButton = new KSButton(ProgramProperties.get().common_save()); |
44 | 0 | private final KSButton cancelButton = new KSButton(ProgramProperties.get().common_cancel(), KSButtonAbstract.ButtonStyle.ANCHOR_LARGE_CENTERED); |
45 | |
|
46 | |
private String currentId; |
47 | |
|
48 | |
public VariationEditController(DataModel programModel, ViewContext viewContext, HandlerManager eventBus, MajorEditController majorController) { |
49 | 0 | super(programModel, viewContext, eventBus, majorController); |
50 | 0 | configurer = GWT.create(VariationEditConfigurer.class); |
51 | 0 | sideBar.setState(ProgramSideBar.State.EDIT); |
52 | 0 | if (getStringProperty(ProgramConstants.ID) != null) { |
53 | 0 | setDefaultView(ProgramSections.SUMMARY); |
54 | |
} |
55 | 0 | initHandlers(); |
56 | 0 | } |
57 | |
|
58 | |
private void initHandlers() { |
59 | 0 | saveButton.addClickHandler(new ClickHandler() { |
60 | |
|
61 | |
@Override |
62 | |
public void onClick(ClickEvent event) { |
63 | 0 | doSave(); |
64 | 0 | } |
65 | |
}); |
66 | 0 | cancelButton.addClickHandler(new ClickHandler() { |
67 | |
|
68 | |
@Override |
69 | |
public void onClick(ClickEvent event) { |
70 | 0 | doCancel(); |
71 | 0 | } |
72 | |
}); |
73 | 0 | ModelLoadedEvent.Handler modelLoadedHandler = new ModelLoadedEvent.Handler() { |
74 | |
@Override |
75 | |
public void onEvent(ModelLoadedEvent event) { |
76 | 0 | DataModel dataModel = event.getModel(); |
77 | 0 | Data variationMap = dataModel.get(ProgramConstants.VARIATIONS); |
78 | 0 | if (variationMap != null) { |
79 | 0 | int row = 0; |
80 | 0 | for (Data.Property property : variationMap) { |
81 | 0 | final Data variationData = property.getValue(); |
82 | 0 | if (variationData.get(ProgramConstants.ID).equals(currentId)) { |
83 | 0 | programModel.setRoot(variationData); |
84 | 0 | ProgramRegistry.setData(variationData); |
85 | 0 | ProgramRegistry.setRow(row); |
86 | 0 | setContentTitle(getProgramName()); |
87 | 0 | row++; |
88 | 0 | return; |
89 | |
} |
90 | 0 | } |
91 | |
} |
92 | 0 | } |
93 | |
}; |
94 | 0 | ProgramRegistry.addHandler(ModelLoadedEvent.TYPE, modelLoadedHandler); |
95 | 0 | eventBus.addHandler(ModelLoadedEvent.TYPE, modelLoadedHandler); |
96 | |
|
97 | 0 | ChangeViewEvent.Handler changeViewHandler = new ChangeViewEvent.Handler() { |
98 | |
@Override |
99 | |
public void onEvent(ChangeViewEvent event) { |
100 | 0 | Enum<?> viewToken = event.getViewToken(); |
101 | 0 | if (!viewToken.name().equals(ProgramSections.SPECIALIZATIONS_EDIT.name())) { |
102 | 0 | showView(viewToken); |
103 | |
} |
104 | 0 | } |
105 | |
}; |
106 | 0 | ProgramRegistry.addHandler(ChangeViewEvent.TYPE, changeViewHandler); |
107 | 0 | eventBus.addHandler(ChangeViewEvent.TYPE, changeViewHandler); |
108 | 0 | eventBus.addHandler(SpecializationCreatedEvent.TYPE, new SpecializationCreatedEvent.Handler() { |
109 | |
|
110 | |
@Override |
111 | |
public void onEvent(SpecializationCreatedEvent event) { |
112 | 0 | programModel.getRoot().set(ProgramConstants.ID, event.getSpecializationId()); |
113 | 0 | } |
114 | |
}); |
115 | |
|
116 | 0 | eventBus.addHandler(SpecializationUpdateEvent.TYPE, new SpecializationUpdateEvent.Handler() { |
117 | |
@Override |
118 | |
public void onEvent(SpecializationUpdateEvent event) { |
119 | |
|
120 | |
|
121 | |
|
122 | 0 | String currSpecializationId = programModel.getRoot().get(ProgramConstants.ID); |
123 | 0 | for (Data.Property prop : event.getSpecializations()) { |
124 | 0 | String specId = (String) ((Data) prop.getValue()).get(ProgramConstants.ID); |
125 | |
|
126 | 0 | if (null != specId && specId.equals(currSpecializationId) && prop.getValueType().equals(Data.class)) { |
127 | |
|
128 | 0 | programModel.setRoot((Data) prop.getValue()); |
129 | 0 | showView(getCurrentViewEnum()); |
130 | |
} |
131 | 0 | } |
132 | 0 | } |
133 | |
}); |
134 | |
|
135 | 0 | StoreSpecRequirementIDsEvent.Handler requirementsHandler = new StoreSpecRequirementIDsEvent.Handler() { |
136 | |
@Override |
137 | |
public void onEvent(StoreSpecRequirementIDsEvent event) { |
138 | 0 | final String programId = event.getProgramId(); |
139 | 0 | final List<String> ids = event.getProgramRequirementIds(); |
140 | |
|
141 | 0 | requestModel(new ModelRequestCallback<DataModel>() { |
142 | |
@Override |
143 | |
public void onModelReady(final DataModel model) { |
144 | 0 | Data programRequirements = null; |
145 | |
|
146 | |
|
147 | |
|
148 | 0 | Data variationData = model.getRoot(); |
149 | 0 | if (variationData.get(ProgramConstants.ID).equals(programId)) { |
150 | 0 | variationData.set(ProgramConstants.PROGRAM_REQUIREMENTS, new Data()); |
151 | 0 | programRequirements = variationData.get(ProgramConstants.PROGRAM_REQUIREMENTS); |
152 | |
|
153 | |
} |
154 | |
|
155 | |
|
156 | 0 | if (programRequirements == null) { |
157 | 0 | Window.alert("Cannot find program requirements in data model."); |
158 | 0 | GWT.log("Cannot find program requirements in data model", null); |
159 | 0 | return; |
160 | |
} |
161 | |
|
162 | 0 | for (String id : ids) { |
163 | 0 | programRequirements.add(id); |
164 | |
} |
165 | 0 | doSave(); |
166 | 0 | } |
167 | |
|
168 | |
@Override |
169 | |
public void onRequestFail(Throwable cause) { |
170 | 0 | GWT.log("Unable to retrieve model for validation and save", cause); |
171 | 0 | } |
172 | |
|
173 | |
}); |
174 | 0 | } |
175 | |
}; |
176 | 0 | ProgramRegistry.addHandler(StoreSpecRequirementIDsEvent.TYPE, requirementsHandler); |
177 | 0 | eventBus.addHandler(StoreSpecRequirementIDsEvent.TYPE, requirementsHandler); |
178 | 0 | } |
179 | |
|
180 | |
@Override |
181 | |
protected void fireUpdateEvent(Callback<Boolean> okToChange) { |
182 | 0 | doSave(okToChange); |
183 | 0 | } |
184 | |
|
185 | |
private void doSave(final Callback<Boolean> okToChange) { |
186 | 0 | requestModel(new ModelRequestCallback<DataModel>() { |
187 | |
@Override |
188 | |
public void onModelReady(final DataModel model) { |
189 | 0 | VariationEditController.this.updateModelFromCurrentView(); |
190 | 0 | model.setParentPath(ProgramConstants.VARIATIONS + "/" + ProgramRegistry.getRow()); |
191 | 0 | model.validate(new Callback<List<ValidationResultInfo>>() { |
192 | |
@Override |
193 | |
public void exec(List<ValidationResultInfo> results) { |
194 | 0 | boolean isSectionValid = isValid(results, true); |
195 | 0 | if (isSectionValid) { |
196 | 0 | saveData(model); |
197 | 0 | okToChange.exec(true); |
198 | |
} else { |
199 | 0 | okToChange.exec(false); |
200 | 0 | KSNotifier.add(new KSNotification("Unable to save, please check fields for errors.", false, true, 5000)); |
201 | |
} |
202 | 0 | } |
203 | |
}); |
204 | 0 | } |
205 | |
|
206 | |
@Override |
207 | |
public void onRequestFail(Throwable cause) { |
208 | 0 | GWT.log("Unable to retrieve model for validation and save", cause); |
209 | 0 | } |
210 | |
|
211 | |
}); |
212 | 0 | } |
213 | |
|
214 | |
@Override |
215 | |
protected void configureView() { |
216 | 0 | super.configureView(); |
217 | 0 | if (!initialized) { |
218 | 0 | List<Enum<?>> excludedViews = new ArrayList<Enum<?>>(); |
219 | 0 | excludedViews.add(ProgramSections.PROGRAM_REQUIREMENTS_EDIT); |
220 | 0 | excludedViews.add(ProgramSections.SUPPORTING_DOCUMENTS_EDIT); |
221 | 0 | excludedViews.add(ProgramSections.SUMMARY); |
222 | 0 | addCommonButton(ProgramProperties.get().program_menu_sections(), saveButton, excludedViews); |
223 | 0 | addCommonButton(ProgramProperties.get().program_menu_sections(), cancelButton, excludedViews); |
224 | 0 | initialized = true; |
225 | |
} |
226 | 0 | } |
227 | |
|
228 | |
@Override |
229 | |
protected void resetModel() { |
230 | 0 | currentId = getStringProperty(ProgramConstants.ID); |
231 | 0 | programModel.resetRoot(); |
232 | 0 | } |
233 | |
|
234 | |
private void doCancel() { |
235 | 0 | navigateToParent(ProgramSections.SUMMARY); |
236 | 0 | } |
237 | |
|
238 | |
@Override |
239 | |
protected void doSave() { |
240 | 0 | doSave(NO_OP_CALLBACK); |
241 | 0 | } |
242 | |
|
243 | |
private void saveData(DataModel model) { |
244 | 0 | currentId = model.get(ProgramConstants.ID); |
245 | 0 | eventBus.fireEvent(new SpecializationSaveEvent(model.getRoot())); |
246 | 0 | setContentTitle(getProgramName()); |
247 | 0 | setName(getProgramName()); |
248 | 0 | resetFieldInteractionFlag(); |
249 | 0 | } |
250 | |
|
251 | |
@Override |
252 | |
protected void navigateToParent() { |
253 | 0 | navigateToParent(ProgramSections.SPECIALIZATIONS_EDIT); |
254 | 0 | } |
255 | |
|
256 | |
private void navigateToParent(ProgramSections parentSection) { |
257 | 0 | String path = HistoryManager.appendContext(AppLocations.Locations.EDIT_PROGRAM_SPEC.getLocation(), getViewContext()) + "/" + parentSection; |
258 | 0 | HistoryManager.navigate(path); |
259 | 0 | } |
260 | |
|
261 | |
} |