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