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