1 | |
package org.kuali.student.lum.lu.ui.course.client.requirements; |
2 | |
|
3 | |
import java.util.*; |
4 | |
|
5 | |
import org.kuali.student.common.ui.client.application.Application; |
6 | |
import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle; |
7 | |
import org.kuali.student.common.ui.client.configurable.mvc.views.SectionView; |
8 | |
import org.kuali.student.common.ui.client.configurable.mvc.views.VerticalSectionView; |
9 | |
import org.kuali.student.common.ui.client.mvc.*; |
10 | |
import org.kuali.student.common.ui.client.widgets.KSButton; |
11 | |
import org.kuali.student.common.ui.client.widgets.KSButtonAbstract; |
12 | |
import org.kuali.student.common.ui.client.widgets.buttongroups.ButtonEnumerations; |
13 | |
import org.kuali.student.common.ui.client.widgets.dialog.ConfirmationDialog; |
14 | |
import org.kuali.student.common.ui.client.widgets.field.layout.button.ActionCancelGroup; |
15 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton; |
16 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.LabelPanel; |
17 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.SpanPanel; |
18 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton.AbbrButtonType; |
19 | |
import org.kuali.student.common.ui.client.widgets.rules.RulesUtil; |
20 | |
import org.kuali.student.common.ui.client.widgets.rules.SubrulePreviewWidget; |
21 | |
import org.kuali.student.core.dto.RichTextInfo; |
22 | |
import org.kuali.student.core.statement.dto.ReqCompFieldInfo; |
23 | |
import org.kuali.student.core.statement.dto.ReqComponentInfo; |
24 | |
import org.kuali.student.core.statement.dto.StatementTreeViewInfo; |
25 | |
import org.kuali.student.core.statement.dto.StatementTypeInfo; |
26 | |
import org.kuali.student.lum.common.client.widgets.AppLocations; |
27 | |
import org.kuali.student.lum.common.client.widgets.CluSetDetailsWidget; |
28 | |
import org.kuali.student.lum.common.client.widgets.CluSetRetriever; |
29 | |
import org.kuali.student.lum.common.client.widgets.CluSetRetrieverImpl; |
30 | |
import org.kuali.student.lum.lu.ui.course.client.configuration.AbstractCourseConfigurer; |
31 | |
import org.kuali.student.lum.lu.ui.course.client.configuration.CourseConfigurer; |
32 | |
import org.kuali.student.lum.lu.ui.course.client.controllers.CourseProposalController; |
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.user.client.Window; |
38 | |
import com.google.gwt.user.client.ui.FlowPanel; |
39 | |
import com.google.gwt.user.client.ui.HorizontalPanel; |
40 | |
import com.google.gwt.user.client.ui.VerticalPanel; |
41 | |
import com.google.gwt.user.client.ui.Widget; |
42 | |
|
43 | 0 | public class CourseRequirementsSummaryView extends VerticalSectionView { |
44 | |
|
45 | 0 | private static CluSetRetriever cluSetRetriever = new CluSetRetrieverImpl(); |
46 | |
|
47 | |
|
48 | 0 | private FlowPanel layout = new FlowPanel(); |
49 | 0 | private ActionCancelGroup actionCancelButtons = new ActionCancelGroup(ButtonEnumerations.SaveContinueCancelEnum.SAVE_CONTINUE, ButtonEnumerations.SaveContinueCancelEnum.CANCEL); |
50 | 0 | private Map<String, Widget> addButtonsList = new HashMap<String, Widget>(); |
51 | |
|
52 | |
|
53 | |
private CourseRequirementsViewController parentController; |
54 | |
private CourseRequirementsDataModel rules; |
55 | |
private boolean isReadOnly; |
56 | 0 | private static int tempProgReqInfoID = 9999; |
57 | |
public static final String NEW_STMT_TREE_ID = "NEWSTMTTREE"; |
58 | |
public static final String NEW_REQ_COMP_ID = "NEWREQCOMP"; |
59 | |
|
60 | 0 | private Map<String, SpanPanel> perCourseRequisiteTypePanel = new LinkedHashMap<String, SpanPanel>(); |
61 | |
|
62 | |
public CourseRequirementsSummaryView(final CourseRequirementsViewController parentController, Enum<?> viewEnum, String name, |
63 | |
String modelId, CourseRequirementsDataModel rulesData, boolean isReadOnly) { |
64 | 0 | super(viewEnum, name, modelId); |
65 | 0 | this.parentController = parentController; |
66 | 0 | rules = rulesData; |
67 | 0 | rules.setInitialized(false); |
68 | 0 | this.isReadOnly = isReadOnly; |
69 | |
|
70 | 0 | if (!isReadOnly) { |
71 | 0 | setupSaveCancelButtons(); |
72 | |
} |
73 | 0 | } |
74 | |
|
75 | |
@Override |
76 | |
public boolean isDirty() { |
77 | 0 | return rules.isDirty(); |
78 | |
} |
79 | |
|
80 | |
@Override |
81 | |
public void beforeShow(final Callback<Boolean> onReadyCallback) { |
82 | |
|
83 | |
|
84 | 0 | if (!rules.isInitialized()) { |
85 | 0 | rules.retrieveCourseRequirements(AbstractCourseConfigurer.CLU_PROPOSAL_MODEL, new Callback<Boolean>() { |
86 | |
@Override |
87 | |
public void exec(Boolean result) { |
88 | 0 | if (result) { |
89 | 0 | displayRules(); |
90 | |
} |
91 | 0 | onReadyCallback.exec(result); |
92 | 0 | } |
93 | |
}); |
94 | 0 | return; |
95 | |
} |
96 | |
|
97 | |
|
98 | 0 | if (isReadOnly) { |
99 | 0 | displayRules(); |
100 | 0 | onReadyCallback.exec(true); |
101 | 0 | return; |
102 | |
} |
103 | |
|
104 | |
|
105 | 0 | parentController.getView(CourseRequirementsViewController.CourseRequirementsViews.MANAGE, new Callback<View>(){ |
106 | |
@Override |
107 | |
public void exec(View result) { |
108 | 0 | CourseRequirementsManageView manageView = (CourseRequirementsManageView) result; |
109 | |
|
110 | |
|
111 | 0 | if (!manageView.isDirty() || !manageView.isUserClickedSaveButton()) { |
112 | 0 | rules.removeEmptyRules(); |
113 | 0 | onReadyCallback.exec(true); |
114 | 0 | return; |
115 | |
} |
116 | |
|
117 | |
|
118 | 0 | ((SectionView)parentController.getCurrentView()).setIsDirty(false); |
119 | 0 | manageView.setUserClickedSaveButton(false); |
120 | |
|
121 | |
|
122 | 0 | StatementTreeViewInfo affectedRule = rules.updateRules(manageView.getRuleTree(), manageView.getInternalCourseReqID(), manageView.isNewRule()); |
123 | 0 | updateRequirementWidgets(affectedRule); |
124 | |
|
125 | 0 | onReadyCallback.exec(true); |
126 | 0 | } |
127 | |
}); |
128 | 0 | } |
129 | |
|
130 | |
public void revertRuleChanges() { |
131 | 0 | rules.revertRuleChanges(); |
132 | 0 | displayRules(); |
133 | 0 | } |
134 | |
|
135 | |
private void updateRequirementWidgets(StatementTreeViewInfo rule) { |
136 | 0 | if (rule != null) { |
137 | 0 | StatementTypeInfo affectedStatementTypeInfo = rules.getStmtTypeInfo(rule.getType()); |
138 | 0 | SpanPanel reqPanel = perCourseRequisiteTypePanel.get(affectedStatementTypeInfo.getId()); |
139 | |
|
140 | |
|
141 | 0 | addButtonsList.get(rule.getType()).setVisible(false); |
142 | |
|
143 | |
|
144 | 0 | if (reqPanel.getWidgetCount() == 0) { |
145 | 0 | SubrulePreviewWidget newRulePreviewWidget = addCourseRequisite(reqPanel, rule); |
146 | 0 | reqPanel.add(newRulePreviewWidget); |
147 | 0 | return; |
148 | |
} |
149 | |
|
150 | 0 | for (int i = 0; i < reqPanel.getWidgetCount(); i++) { |
151 | 0 | SubrulePreviewWidget subrulePreviewWidget = (SubrulePreviewWidget)reqPanel.getWidget(i); |
152 | 0 | SubrulePreviewWidget newRulePreviewWidget = addCourseRequisite(reqPanel, rule); |
153 | 0 | reqPanel.insert(newRulePreviewWidget, i); |
154 | 0 | reqPanel.remove(subrulePreviewWidget); |
155 | 0 | break; |
156 | |
} |
157 | |
} |
158 | 0 | } |
159 | |
|
160 | |
public void displayRules() { |
161 | 0 | remove(layout); |
162 | 0 | layout.clear(); |
163 | |
|
164 | |
|
165 | 0 | if (!isReadOnly) { |
166 | 0 | SectionTitle pageTitle = SectionTitle.generateH2Title("Course Requisites"); |
167 | 0 | pageTitle.addStyleName("ks-layout-header"); |
168 | 0 | layout.add(pageTitle); |
169 | |
} |
170 | |
|
171 | |
|
172 | 0 | boolean firstSubHeader = true; |
173 | 0 | for (StatementTypeInfo stmtTypeInfo : rules.getStmtTypes()) { |
174 | |
|
175 | |
|
176 | 0 | if (isTopStatement(stmtTypeInfo)) { |
177 | 0 | SectionTitle title = SectionTitle.generateH3Title(stmtTypeInfo.getName()); |
178 | 0 | title.addStyleName("KS-Course-Requisites-Top-Stmt-Header"); |
179 | 0 | layout.add(title); |
180 | 0 | firstSubHeader = true; |
181 | 0 | continue; |
182 | |
} |
183 | |
|
184 | |
|
185 | 0 | SpanPanel requirementsPanel = new SpanPanel(); |
186 | 0 | perCourseRequisiteTypePanel.put(stmtTypeInfo.getId(), requirementsPanel); |
187 | 0 | displayRequirementSectionForGivenType(requirementsPanel, stmtTypeInfo, firstSubHeader); |
188 | 0 | firstSubHeader = false; |
189 | |
|
190 | |
|
191 | 0 | for (StatementTreeViewInfo ruleInfo : rules.getCourseReqInfo(stmtTypeInfo.getId())) { |
192 | 0 | SubrulePreviewWidget subrulePreviewWidget = addCourseRequisite(requirementsPanel, ruleInfo); |
193 | 0 | requirementsPanel.add(subrulePreviewWidget); |
194 | 0 | } |
195 | 0 | } |
196 | |
|
197 | |
|
198 | 0 | if (!isReadOnly) { |
199 | 0 | layout.add(actionCancelButtons); |
200 | |
} |
201 | |
|
202 | 0 | addWidget(layout); |
203 | 0 | } |
204 | |
|
205 | |
private void displayRequirementSectionForGivenType(final SpanPanel requirementsPanel, final StatementTypeInfo stmtTypeInfo, boolean firstSubHeader) { |
206 | |
|
207 | |
|
208 | 0 | SectionTitle title = SectionTitle.generateH3Title(stmtTypeInfo.getName()); |
209 | 0 | title.setStyleName((firstSubHeader ? "KS-Course-Requisites-Preview-Rule-Type-First-Header" : "KS-Course-Requisites-Preview-Rule-Type-Header")); |
210 | 0 | layout.add(title); |
211 | |
|
212 | 0 | LabelPanel labelExamples = new LabelPanel(stmtTypeInfo.getDescr()); |
213 | 0 | labelExamples.getElement().setAttribute("style", "font-weight: normal; width: 80%;"); |
214 | |
|
215 | 0 | String examplesHtml = Application.getApplicationContext().getMessage(stmtTypeInfo.getId()); |
216 | |
|
217 | 0 | if (examplesHtml != null && !examplesHtml.equals("")) { |
218 | 0 | AbbrButton examples = new AbbrButton(AbbrButtonType.EXAMPLES); |
219 | 0 | examples.setVisible(true); |
220 | 0 | examples.setHoverHTML(examplesHtml); |
221 | 0 | examples.getHoverPopup().addStyleName("ks-example-popup"); |
222 | 0 | labelExamples.add(examples); |
223 | |
} |
224 | |
|
225 | 0 | HorizontalPanel spacer0 = new HorizontalPanel(); |
226 | 0 | spacer0.addStyleName("KS-Course-Requisites-Button-Spacer"); |
227 | 0 | labelExamples.add(spacer0); |
228 | |
|
229 | 0 | layout.add(labelExamples); |
230 | |
|
231 | |
|
232 | 0 | final String stmtId = stmtTypeInfo.getId(); |
233 | 0 | if (!isReadOnly) { |
234 | 0 | KSButton addCourseReqButton = new KSButton("Add " + stmtTypeInfo.getName(), KSButtonAbstract.ButtonStyle.FORM_SMALL); |
235 | 0 | addCourseReqButton.addClickHandler(new ClickHandler(){ |
236 | |
public void onClick(ClickEvent event) { |
237 | |
|
238 | 0 | storeRules(false, new Callback<Boolean>() { |
239 | |
@Override |
240 | |
public void exec(Boolean courseExists) { |
241 | 0 | if (courseExists) { |
242 | 0 | final StatementTreeViewInfo newRule = new StatementTreeViewInfo(); |
243 | 0 | newRule.setId(generateStatementTreeId()); |
244 | 0 | newRule.setType(stmtId); |
245 | 0 | RichTextInfo text = new RichTextInfo(); |
246 | 0 | text.setPlain(""); |
247 | 0 | newRule.setDesc(text); |
248 | 0 | parentController.getView(CourseRequirementsViewController.CourseRequirementsViews.MANAGE, new Callback<View>(){ |
249 | |
@Override |
250 | |
public void exec(View result) { |
251 | 0 | rules.addRule(newRule); |
252 | 0 | ((CourseRequirementsManageView) result).setRuleTree(newRule, true, rules.getInternalCourseReqID(newRule)); |
253 | 0 | parentController.showView(CourseRequirementsViewController.CourseRequirementsViews.MANAGE); |
254 | 0 | } |
255 | |
}); |
256 | |
} |
257 | 0 | } |
258 | |
}); |
259 | 0 | }; |
260 | |
}); |
261 | 0 | addCourseReqButton.setVisible(rules.getCourseReqInfo(stmtId).isEmpty()); |
262 | 0 | addButtonsList.put(stmtId, addCourseReqButton); |
263 | 0 | layout.add(addCourseReqButton); |
264 | 0 | VerticalPanel spacer = new VerticalPanel(); |
265 | 0 | spacer.addStyleName("KS-Course-Requisites-Button-Spacer"); |
266 | 0 | layout.add(spacer); |
267 | |
} |
268 | |
|
269 | 0 | layout.add(requirementsPanel); |
270 | 0 | } |
271 | |
|
272 | |
private SubrulePreviewWidget addCourseRequisite(final SpanPanel requirementsPanel,final StatementTreeViewInfo rule) { |
273 | |
|
274 | 0 | Integer internalProgReqID = rules.getInternalCourseReqID(rule); |
275 | 0 | String stmtTypeId = rule.getType(); |
276 | |
|
277 | 0 | final SubrulePreviewWidget rulePreviewWidget = new SubrulePreviewWidget(rule, isReadOnly, getCluSetWidgetList(rule)); |
278 | |
|
279 | 0 | addRulePreviewWidgetHandlers(requirementsPanel, rulePreviewWidget, stmtTypeId, internalProgReqID); |
280 | 0 | return rulePreviewWidget; |
281 | |
} |
282 | |
|
283 | |
private void addRulePreviewWidgetHandlers(final SpanPanel requirementsPanel, final SubrulePreviewWidget subRuleWidget, final String stmtTypeId, final Integer internalProgReqID) { |
284 | |
|
285 | 0 | subRuleWidget.addEditButtonClickHandler(new ClickHandler() { |
286 | |
public void onClick(ClickEvent event) { |
287 | 0 | parentController.getView(CourseRequirementsViewController.CourseRequirementsViews.MANAGE, new Callback<View>(){ |
288 | |
@Override |
289 | |
public void exec(View result) { |
290 | 0 | ((CourseRequirementsManageView) result).setRuleTree(rules.getRule(internalProgReqID), false, internalProgReqID); |
291 | 0 | parentController.showView(CourseRequirementsViewController.CourseRequirementsViews.MANAGE); |
292 | 0 | } |
293 | |
}); |
294 | 0 | } |
295 | |
}); |
296 | |
|
297 | 0 | subRuleWidget.addDeleteButtonClickHandler(new ClickHandler() { |
298 | |
public void onClick(ClickEvent event) { |
299 | |
|
300 | 0 | rules.markRuleAsEdited(internalProgReqID); |
301 | |
|
302 | 0 | rules.deleteRule(internalProgReqID); |
303 | 0 | requirementsPanel.remove(subRuleWidget); |
304 | 0 | addButtonsList.get(stmtTypeId).setVisible(true); |
305 | 0 | } |
306 | |
}); |
307 | 0 | } |
308 | |
|
309 | |
static public boolean isTopStatement(StatementTypeInfo stmtInfo) { |
310 | 0 | return ((stmtInfo.getAllowedStatementTypes() != null) && !stmtInfo.getAllowedStatementTypes().isEmpty()); |
311 | |
} |
312 | |
|
313 | |
static public Map<String, Widget> getCluSetWidgetList(StatementTreeViewInfo rule) { |
314 | 0 | Map<String, Widget> widgetList = new HashMap<String, Widget>(); |
315 | 0 | Set<String> cluSetIds = new HashSet<String>(); |
316 | 0 | findCluSetIds(rule, cluSetIds); |
317 | 0 | for (String clusetId : cluSetIds) { |
318 | 0 | widgetList.put(clusetId, new CluSetDetailsWidget(clusetId, cluSetRetriever)); |
319 | |
} |
320 | |
|
321 | 0 | return widgetList; |
322 | |
} |
323 | |
|
324 | |
static private void findCluSetIds(StatementTreeViewInfo rule, Set<String> list) { |
325 | |
|
326 | 0 | List<StatementTreeViewInfo> statements = rule.getStatements(); |
327 | 0 | List<ReqComponentInfo> reqComponentInfos = rule.getReqComponents(); |
328 | |
|
329 | 0 | if ((statements != null) && (statements.size() > 0)) { |
330 | |
|
331 | 0 | for (StatementTreeViewInfo statement : statements) { |
332 | 0 | findCluSetIds(statement, list); |
333 | |
} |
334 | 0 | } else if ((reqComponentInfos != null) && (reqComponentInfos.size() > 0)) { |
335 | |
|
336 | 0 | for (ReqComponentInfo reqComponent : reqComponentInfos) { |
337 | 0 | List<ReqCompFieldInfo> fieldInfos = reqComponent.getReqCompFields(); |
338 | 0 | for (ReqCompFieldInfo fieldInfo : fieldInfos) { |
339 | 0 | if (RulesUtil.isCluSetWidget(fieldInfo.getType())) { |
340 | 0 | list.add(fieldInfo.getValue()); |
341 | |
} |
342 | |
} |
343 | 0 | } |
344 | |
} |
345 | 0 | } |
346 | |
|
347 | |
private void setupSaveCancelButtons() { |
348 | 0 | actionCancelButtons.addStyleName("KS-Course-Requisites-Save-Button"); |
349 | 0 | actionCancelButtons.addCallback(new Callback<ButtonEnumerations.ButtonEnum>(){ |
350 | |
@Override |
351 | |
public void exec(ButtonEnumerations.ButtonEnum result) { |
352 | 0 | if (result == ButtonEnumerations.SaveContinueCancelEnum.SAVE_CONTINUE) { |
353 | 0 | storeRules(true, new Callback<Boolean>() { |
354 | |
@Override |
355 | |
public void exec(Boolean result) { |
356 | 0 | if (result) { |
357 | 0 | CourseProposalController courseController = ((CourseProposalController) parentController.getParentController()); |
358 | 0 | courseController.showNextViewOnMenu(); |
359 | 0 | courseController.getReqDataModel().retrieveStatementTypes(courseController.getCourseId(), Controller.NO_OP_CALLBACK); |
360 | |
} |
361 | 0 | } |
362 | |
}); |
363 | |
} else { |
364 | 0 | if(! ((CourseProposalController)parentController.getController()).isNew()){ |
365 | 0 | (parentController.getController()).showView(CourseConfigurer.CourseSections.SUMMARY); |
366 | |
} |
367 | |
else{ |
368 | 0 | Application.navigate(AppLocations.Locations.CURRICULUM_MANAGEMENT.getLocation()); |
369 | |
} |
370 | |
} |
371 | 0 | } |
372 | |
}); |
373 | 0 | } |
374 | |
|
375 | |
public void storeRules(final boolean storeRules, final Callback<Boolean> callback) { |
376 | 0 | parentController.requestModel(CourseRequirementsViewController.CLU_PROPOSAL_MODEL, new ModelRequestCallback() { |
377 | |
@Override |
378 | |
public void onRequestFail(Throwable cause) { |
379 | 0 | Window.alert(cause.getMessage()); |
380 | 0 | GWT.log("Unable to retrieve model for course requirements view", cause); |
381 | 0 | callback.exec(false); |
382 | 0 | } |
383 | |
@Override |
384 | |
public void onModelReady(Model model) { |
385 | 0 | String courseId = ((DataModel)model).getRoot().get("id"); |
386 | 0 | if (courseId == null) { |
387 | 0 | final ConfirmationDialog dialog = new ConfirmationDialog("Submit Course Title", "Before saving rules please submit course proposal title"); |
388 | 0 | dialog.getConfirmButton().addClickHandler(new ClickHandler(){ |
389 | |
@Override |
390 | |
public void onClick(ClickEvent event) { |
391 | 0 | dialog.hide(); |
392 | 0 | } |
393 | |
}); |
394 | 0 | dialog.show(); |
395 | 0 | callback.exec(false); |
396 | 0 | } else { |
397 | 0 | if (storeRules) { |
398 | 0 | rules.updateCourseRequisites(courseId, new Callback<List<StatementTreeViewInfo>>() { |
399 | |
@Override |
400 | |
public void exec(List<StatementTreeViewInfo> rules) { |
401 | 0 | for (StatementTreeViewInfo rule : rules) { |
402 | 0 | updateRequirementWidgets(rule); |
403 | |
} |
404 | 0 | callback.exec(true); |
405 | 0 | } |
406 | |
}); |
407 | |
} else { |
408 | 0 | callback.exec(true); |
409 | |
} |
410 | |
} |
411 | 0 | } |
412 | |
}); |
413 | 0 | } |
414 | |
|
415 | |
static public String generateStatementTreeId() { |
416 | 0 | return (NEW_STMT_TREE_ID + Integer.toString(tempProgReqInfoID++)); |
417 | |
} |
418 | |
} |