View Javadoc

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.assembly.data.Data;
7   import org.kuali.student.common.ui.client.application.Application;
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.HasCrossConstraints;
12  import org.kuali.student.common.ui.client.mvc.ModelRequestCallback;
13  import org.kuali.student.common.ui.client.mvc.history.HistoryManager;
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.KSNotification;
17  import org.kuali.student.common.ui.client.widgets.notification.KSNotifier;
18  import org.kuali.student.common.validation.dto.ValidationResultInfo;
19  import org.kuali.student.lum.common.client.widgets.AppLocations;
20  import org.kuali.student.lum.program.client.ProgramConstants;
21  import org.kuali.student.lum.program.client.ProgramRegistry;
22  import org.kuali.student.lum.program.client.ProgramSections;
23  import org.kuali.student.lum.program.client.events.ChangeViewEvent;
24  import org.kuali.student.lum.program.client.events.ModelLoadedEvent;
25  import org.kuali.student.lum.program.client.events.SpecializationCreatedEvent;
26  import org.kuali.student.lum.program.client.events.SpecializationSaveEvent;
27  import org.kuali.student.lum.program.client.events.SpecializationUpdateEvent;
28  import org.kuali.student.lum.program.client.events.StoreSpecRequirementIDsEvent;
29  import org.kuali.student.lum.program.client.major.edit.MajorEditController;
30  import org.kuali.student.lum.program.client.properties.ProgramProperties;
31  import org.kuali.student.lum.program.client.variation.VariationController;
32  import org.kuali.student.lum.program.client.widgets.ProgramSideBar;
33  
34  import com.google.gwt.core.client.GWT;
35  import com.google.gwt.event.dom.client.ClickEvent;
36  import com.google.gwt.event.dom.client.ClickHandler;
37  import com.google.gwt.event.shared.HandlerManager;
38  import com.google.gwt.user.client.Window;
39  
40  /**
41   * @author Igor
42   */
43  public class VariationEditController extends VariationController {
44  
45      private final KSButton saveButton = new KSButton(ProgramProperties.get().common_save());
46      private final KSButton cancelButton = new KSButton(ProgramProperties.get().common_cancel(), KSButtonAbstract.ButtonStyle.ANCHOR_LARGE_CENTERED);
47  
48      private String currentId;
49  
50      public VariationEditController(DataModel programModel, ViewContext viewContext, HandlerManager eventBus, MajorEditController majorController) {
51          super(programModel, viewContext, eventBus, majorController);
52          configurer = GWT.create(VariationEditConfigurer.class);
53          sideBar.setState(ProgramSideBar.State.EDIT);
54          if (getStringProperty(ProgramConstants.ID) != null) {
55              setDefaultView(ProgramSections.SUMMARY);
56          }
57          initHandlers();
58      }
59  
60      private void initHandlers() {
61          saveButton.addClickHandler(new ClickHandler() {
62  
63              @Override
64              public void onClick(ClickEvent event) {
65                  doSave();
66              }
67          });
68          cancelButton.addClickHandler(new ClickHandler() {
69  
70              @Override
71              public void onClick(ClickEvent event) {
72                  doCancel();
73              }
74          });
75          ModelLoadedEvent.Handler modelLoadedHandler = new ModelLoadedEvent.Handler() {
76              @Override
77              public void onEvent(ModelLoadedEvent event) {
78                  DataModel dataModel = event.getModel();
79                  Data variationMap = dataModel.get(ProgramConstants.VARIATIONS);
80                  if (variationMap != null) {
81                      int row = 0;
82                      for (Data.Property property : variationMap) {
83                          final Data variationData = property.getValue();
84                          if (variationData.get(ProgramConstants.ID).equals(currentId)) {
85                              programModel.setRoot(variationData);
86                              ProgramRegistry.setData(variationData);
87                              ProgramRegistry.setRow(row);
88                              setContentTitle(getProgramName());
89                              row++;
90                              return;
91                          }
92                      }
93                  }
94              }
95          };
96          ProgramRegistry.addHandler(ModelLoadedEvent.TYPE, modelLoadedHandler);
97          eventBus.addHandler(ModelLoadedEvent.TYPE, modelLoadedHandler);
98  
99          ChangeViewEvent.Handler changeViewHandler = new ChangeViewEvent.Handler() {
100             @Override
101             public void onEvent(ChangeViewEvent event) {
102                 Enum<?> viewToken = event.getViewToken();
103                 if (!viewToken.name().equals(ProgramSections.SPECIALIZATIONS_EDIT.name())) {
104                     showView(viewToken);
105                 }
106             }
107         };
108         ProgramRegistry.addHandler(ChangeViewEvent.TYPE, changeViewHandler);
109         eventBus.addHandler(ChangeViewEvent.TYPE, changeViewHandler);
110         eventBus.addHandler(SpecializationCreatedEvent.TYPE, new SpecializationCreatedEvent.Handler() {
111 
112             @Override
113             public void onEvent(SpecializationCreatedEvent event) {
114                 programModel.getRoot().set(ProgramConstants.ID, event.getSpecializationId());
115             }
116         });
117 
118         eventBus.addHandler(SpecializationUpdateEvent.TYPE, new SpecializationUpdateEvent.Handler() {
119             @Override
120             public void onEvent(SpecializationUpdateEvent event) {
121                 // update our model to the updated specialization
122 
123                 // gotta find it first
124                 String currSpecializationId = programModel.getRoot().get(ProgramConstants.ID);
125                 for (Data.Property prop : event.getSpecializations()) {
126                     String specId = (String) ((Data) prop.getValue()).get(ProgramConstants.ID);
127 
128                     if (null != specId && specId.equals(currSpecializationId) && prop.getValueType().equals(Data.class)) {
129                         // found it
130                         programModel.setRoot((Data) prop.getValue());
131                         showView(getCurrentViewEnum());
132                     }
133                 }
134             }
135         });
136 
137         StoreSpecRequirementIDsEvent.Handler requirementsHandler = new StoreSpecRequirementIDsEvent.Handler() {
138             @Override
139             public void onEvent(StoreSpecRequirementIDsEvent event) {
140                 final String programId = event.getProgramId();
141                 final List<String> ids = event.getProgramRequirementIds();
142 
143                 requestModel(new ModelRequestCallback<DataModel>() {
144                     @Override
145                     public void onModelReady(final DataModel model) {
146                         Data programRequirements = null;
147 
148                         // find the specialization that we need to update
149                         //for (Data.Property property : model.getRoot()) {
150                         Data variationData = model.getRoot();
151                         if (variationData.get(ProgramConstants.ID).equals(programId)) {
152                             variationData.set(ProgramConstants.PROGRAM_REQUIREMENTS, new Data());
153                             programRequirements = variationData.get(ProgramConstants.PROGRAM_REQUIREMENTS);
154                             // break;
155                         }
156                         // }
157 
158                         if (programRequirements == null) {
159                             Window.alert("Cannot find program requirements in data model.");
160                             GWT.log("Cannot find program requirements in data model", null);
161                             return;
162                         }
163 
164                         for (String id : ids) {
165                             programRequirements.add(id);
166                         }
167                         doSave();
168                     }
169 
170                     @Override
171                     public void onRequestFail(Throwable cause) {
172                         GWT.log("Unable to retrieve model for validation and save", cause);
173                     }
174 
175                 });
176             }
177         };
178         ProgramRegistry.addHandler(StoreSpecRequirementIDsEvent.TYPE, requirementsHandler);
179         eventBus.addHandler(StoreSpecRequirementIDsEvent.TYPE, requirementsHandler);
180     }
181 
182     @Override
183     protected void fireUpdateEvent(Callback<Boolean> okToChange) {
184         doSave(okToChange);
185     }
186 
187     private void doSave(final Callback<Boolean> okToChange) {
188         requestModel(new ModelRequestCallback<DataModel>() {
189             @Override
190             public void onModelReady(final DataModel model) {
191                 VariationEditController.this.updateModelFromCurrentView();
192                 model.setParentPath(ProgramConstants.VARIATIONS + "/" + ProgramRegistry.getRow());
193                 model.validate(new Callback<List<ValidationResultInfo>>() {
194                     @Override
195                     public void exec(List<ValidationResultInfo> results) {
196                         boolean isSectionValid = isValid(results, true);
197                         if (isSectionValid) {
198                             saveData(model);
199                             okToChange.exec(true);
200                         } else {
201                             okToChange.exec(false);
202                             KSNotifier.add(new KSNotification("Unable to save, please check fields for errors.", false, true, 5000));
203                         }
204                     }
205                 });
206             }
207 
208             @Override
209             public void onRequestFail(Throwable cause) {
210                 GWT.log("Unable to retrieve model for validation and save", cause);
211             }
212 
213         });
214     }
215 
216     @Override
217     protected void configureView() {
218         super.configureView();
219         if (!initialized) {
220             List<Enum<?>> excludedViews = new ArrayList<Enum<?>>();
221             excludedViews.add(ProgramSections.PROGRAM_REQUIREMENTS_EDIT);
222             excludedViews.add(ProgramSections.SUPPORTING_DOCUMENTS_EDIT);
223             excludedViews.add(ProgramSections.SUMMARY);
224             addCommonButton(ProgramProperties.get().program_menu_sections(), saveButton, excludedViews);
225             addCommonButton(ProgramProperties.get().program_menu_sections(), cancelButton, excludedViews);
226             initialized = true;
227         }
228 
229     }
230 
231     @Override
232     protected void resetModel() {
233         currentId = getStringProperty(ProgramConstants.ID);
234         programModel.resetRoot();
235     }
236 
237     private void doCancel() {
238        navigateToParent(ProgramSections.SUMMARY);
239     }
240 
241     @Override
242     protected void doSave() {
243         doSave(NO_OP_CALLBACK);
244     }
245 
246     private void saveData(DataModel model) {
247         currentId = model.get(ProgramConstants.ID);
248         eventBus.fireEvent(new SpecializationSaveEvent(model.getRoot()));
249         setContentTitle(getProgramName());
250         setName(getProgramName());
251         resetFieldInteractionFlag();
252     }
253 
254     @Override
255     protected void navigateToParent() {
256         navigateToParent(ProgramSections.SPECIALIZATIONS_EDIT);
257     }
258 
259     private void navigateToParent(ProgramSections parentSection) {
260 
261         String path = HistoryManager.appendContext(AppLocations.Locations.EDIT_PROGRAM_SPEC.getLocation(), getViewContext()) + "/" + parentSection;
262         HistoryManager.navigate(path);
263     }
264 
265     
266 	@Override
267 	public void beforeShow(Callback<Boolean> onReadyCallback) {
268     	//clear all cross constraints that start with variations
269     	Application.getApplicationContext().clearCrossConstraintsWithStartingPath(null,ProgramConstants.VARIATIONS);
270     	
271     	//Set the context parent path so the proper mapping is retained 
272     	String newParentPath = ProgramConstants.VARIATIONS+"/"+org.kuali.student.lum.program.client.ProgramRegistry.getRow()+"/";
273     	Application.getApplicationContext().setParentPath(newParentPath);
274 		
275     	super.beforeShow(onReadyCallback);
276         
277         //Update any cross fields
278         for(HasCrossConstraints crossConstraint:Application.getApplicationContext().getCrossConstraints(null)){
279         	crossConstraint.reprocessWithUpdatedConstraints();
280         }
281         
282 	}
283 //	@Override
284 //	public void beforeShow(Callback<Boolean> onReadyCallback) {
285 //		super.beforeShow(onReadyCallback);
286 //        if(majorController!=null){
287 //        	majorController.requestModel(new ModelRequestCallback<DataModel>(){
288 //				public void onModelReady(DataModel programModel) {
289 //	            	//Update default values from other fields if they are currently null 
290 //	            	for(Map.Entry<FieldDescriptor,String> entry:Application.getApplicationContext().getDefaultValueMapping(null).entrySet()){
291 //	            		FieldDescriptor fd = entry.getKey();
292 //	            		if(fd != null){
293 //	            			final String pathToSet = fd.getFieldKey();
294 //	            			if(programModel.get(pathToSet)==null){
295 //	            				final Data defaultValue = ((Data)programModel.get(entry.getValue())).copy();
296 //	            				VariationEditController.this.requestModel(new ModelRequestCallback<DataModel>(){
297 //									public void onModelReady(DataModel vairationModel) {
298 //										vairationModel.set(QueryPath.parse(pathToSet), defaultValue);
299 //			            			}
300 //									public void onRequestFail(Throwable cause) {
301 //									}
302 //	            				});
303 //	            			}
304 //	            		}
305 //	            	}
306 //				}
307 //				public void onRequestFail(Throwable cause) {
308 //				}
309 //	        });
310 //        }
311 //	}
312 
313 }