1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.kuali.student.lum.lu.ui.course.client.requirements; |
16 | |
|
17 | |
import java.util.*; |
18 | |
|
19 | |
import org.kuali.student.common.ui.client.application.KSAsyncCallback; |
20 | |
import org.kuali.student.common.ui.client.mvc.*; |
21 | |
import org.kuali.student.common.ui.client.widgets.dialog.ConfirmationDialog; |
22 | |
import org.kuali.student.common.ui.client.widgets.notification.KSNotification; |
23 | |
import org.kuali.student.common.ui.client.widgets.notification.KSNotifier; |
24 | |
import org.kuali.student.common.ui.client.widgets.rules.RulesUtil; |
25 | |
import org.kuali.student.core.statement.dto.ReqCompFieldInfo; |
26 | |
import org.kuali.student.core.statement.dto.ReqComponentInfo; |
27 | |
import org.kuali.student.core.statement.dto.StatementTreeViewInfo; |
28 | |
import org.kuali.student.core.statement.dto.StatementTypeInfo; |
29 | |
import org.kuali.student.lum.lu.ui.course.client.service.CourseRpcService; |
30 | |
import org.kuali.student.lum.lu.ui.course.client.service.CourseRpcServiceAsync; |
31 | |
import org.kuali.student.lum.program.client.rpc.StatementRpcService; |
32 | |
import org.kuali.student.lum.program.client.rpc.StatementRpcServiceAsync; |
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 | |
|
39 | 0 | public class CourseRequirementsDataModel { |
40 | |
|
41 | 0 | private final CourseRpcServiceAsync courseRemoteService = GWT.create(CourseRpcService.class); |
42 | 0 | private final StatementRpcServiceAsync statementRpcServiceAsync = GWT.create(StatementRpcService.class); |
43 | |
private Controller parentController; |
44 | |
|
45 | |
|
46 | 0 | public enum requirementState {STORED, ADDED, EDITED, DELETED;} |
47 | 0 | private Map<Integer, StatementTreeViewInfo> courseReqInfos = new LinkedHashMap<Integer, StatementTreeViewInfo>(); |
48 | 0 | private Map<Integer, StatementTreeViewInfo> origCourseReqInfos = new LinkedHashMap<Integer, StatementTreeViewInfo>(); |
49 | 0 | private Map<Integer, requirementState> courseReqState = new HashMap<Integer, requirementState>(); |
50 | 0 | private Map<Integer, requirementState> origCourseReqState = new HashMap<Integer, requirementState>(); |
51 | 0 | private List<StatementTypeInfo> stmtTypes = new ArrayList<StatementTypeInfo>(); |
52 | 0 | private boolean isInitialized = false; |
53 | 0 | private static Integer courseReqIDs = 111111; |
54 | |
|
55 | 0 | public CourseRequirementsDataModel(Controller parentController) { |
56 | 0 | this.parentController = parentController; |
57 | 0 | } |
58 | |
|
59 | |
|
60 | |
public void retrieveCourseRequirements(String modelId, final Callback<Boolean> onReadyCallback) { |
61 | |
|
62 | 0 | courseReqInfos = new LinkedHashMap<Integer, StatementTreeViewInfo>(); |
63 | 0 | origCourseReqInfos = new LinkedHashMap<Integer, StatementTreeViewInfo>(); |
64 | 0 | courseReqState = new HashMap<Integer, requirementState>(); |
65 | 0 | origCourseReqState = new HashMap<Integer, requirementState>(); |
66 | 0 | stmtTypes = new ArrayList<StatementTypeInfo>(); |
67 | 0 | isInitialized = false; |
68 | |
|
69 | 0 | parentController.requestModel(modelId, new ModelRequestCallback() { |
70 | |
|
71 | |
@Override |
72 | |
public void onRequestFail(Throwable cause) { |
73 | 0 | Window.alert(cause.getMessage()); |
74 | 0 | GWT.log("Unable to retrieve model for course requirements view", cause); |
75 | 0 | onReadyCallback.exec(false); |
76 | 0 | } |
77 | |
|
78 | |
@Override |
79 | |
public void onModelReady(Model model) { |
80 | 0 | String courseId = ((DataModel)model).getRoot().get("id"); |
81 | 0 | retrieveStatementTypes(courseId, onReadyCallback); |
82 | 0 | } |
83 | |
}); |
84 | 0 | } |
85 | |
|
86 | |
public void retrieveStatementTypes(final String courseId, final Callback<Boolean> onReadyCallback) { |
87 | |
|
88 | |
|
89 | 0 | statementRpcServiceAsync.getStatementTypesForStatementTypeForCourse("kuali.statement.type.course", new KSAsyncCallback<List<StatementTypeInfo>>() { |
90 | |
@Override |
91 | |
public void handleFailure(Throwable caught) { |
92 | 0 | Window.alert(caught.getMessage()); |
93 | 0 | GWT.log("getStatementTypes failed", caught); |
94 | 0 | onReadyCallback.exec(false); |
95 | 0 | } |
96 | |
|
97 | |
@Override |
98 | |
public void onSuccess(List<StatementTypeInfo> stmtInfoTypes) { |
99 | |
|
100 | 0 | stmtTypes.clear(); |
101 | 0 | for (StatementTypeInfo stmtInfoType : stmtInfoTypes) { |
102 | 0 | stmtTypes.add(stmtInfoType); |
103 | |
} |
104 | |
|
105 | |
|
106 | 0 | retrieveRules(courseId, onReadyCallback); |
107 | 0 | } |
108 | |
}); |
109 | 0 | } |
110 | |
|
111 | |
private void retrieveRules(String courseId, final Callback<Boolean> onReadyCallback) { |
112 | |
|
113 | |
|
114 | 0 | if ((courseId == null) || courseId.isEmpty()) { |
115 | 0 | isInitialized = true; |
116 | 0 | onReadyCallback.exec(true); |
117 | 0 | return; |
118 | |
} |
119 | |
|
120 | 0 | courseRemoteService.getCourseStatements(courseId, CourseRequirementsManageView.RULEEDIT_TEMLATE, |
121 | 0 | CourseRequirementsManageView.TEMLATE_LANGUAGE, new KSAsyncCallback<List<StatementTreeViewInfo>>() { |
122 | |
@Override |
123 | |
public void handleFailure(Throwable caught) { |
124 | 0 | Window.alert(caught.getMessage()); |
125 | 0 | GWT.log("getCourseStatements failed", caught); |
126 | 0 | onReadyCallback.exec(false); |
127 | 0 | } |
128 | |
|
129 | |
@Override |
130 | |
public void onSuccess(List<StatementTreeViewInfo> foundRules) { |
131 | |
|
132 | 0 | origCourseReqInfos.clear(); |
133 | 0 | origCourseReqState.clear(); |
134 | 0 | courseReqInfos.clear(); |
135 | 0 | courseReqState.clear(); |
136 | 0 | if (foundRules != null) { |
137 | 0 | for (StatementTreeViewInfo foundRule : foundRules) { |
138 | |
|
139 | 0 | if (getStmtTypeInfo(foundRule.getType()) == null) { |
140 | |
|
141 | 0 | GWT.log("Did not find corresponding statement type for course requirement of type: " + foundRule.getType(), null); |
142 | |
} |
143 | |
|
144 | 0 | origCourseReqInfos.put(courseReqIDs, RulesUtil.clone(foundRule)); |
145 | 0 | origCourseReqState.put(courseReqIDs, requirementState.STORED); |
146 | 0 | courseReqInfos.put(courseReqIDs, foundRule); |
147 | 0 | courseReqState.put(courseReqIDs++, requirementState.STORED); |
148 | |
} |
149 | |
} |
150 | |
|
151 | 0 | isInitialized = true; |
152 | 0 | onReadyCallback.exec(true); |
153 | 0 | } |
154 | |
}); |
155 | 0 | } |
156 | |
|
157 | |
public StatementTreeViewInfo updateRules(StatementTreeViewInfo newSubRule, Integer internalCourseReqID, boolean isNewRule) { |
158 | |
|
159 | 0 | StatementTreeViewInfo affectedRule = courseReqInfos.get(internalCourseReqID); |
160 | |
|
161 | 0 | if (affectedRule == null) { |
162 | 0 | Window.alert("Cannot find course requisite with a statement that has id: '" + newSubRule.getId() + "'"); |
163 | 0 | GWT.log("Cannot find course requisite with a statement that has id: '" + newSubRule.getId() + "'", null); |
164 | 0 | return null; |
165 | |
} |
166 | |
|
167 | 0 | if (courseReqState.get(internalCourseReqID) == CourseRequirementsDataModel.requirementState.STORED) { |
168 | 0 | courseReqState.put(internalCourseReqID, CourseRequirementsDataModel.requirementState.EDITED); |
169 | |
} |
170 | |
|
171 | 0 | courseReqInfos.put(internalCourseReqID, newSubRule); |
172 | |
|
173 | 0 | return newSubRule; |
174 | |
} |
175 | |
|
176 | |
public void updateCourseRequisites(final String courseId, final Callback<List<StatementTreeViewInfo>> callback) { |
177 | |
|
178 | |
|
179 | 0 | if (courseId == null) { |
180 | 0 | final ConfirmationDialog dialog = new ConfirmationDialog("Submit Course Title", "Before saving rules please submit course proposal title"); |
181 | 0 | dialog.getConfirmButton().addClickHandler(new ClickHandler(){ |
182 | |
@Override |
183 | |
public void onClick(ClickEvent event) { |
184 | 0 | dialog.hide(); |
185 | 0 | } |
186 | |
}); |
187 | 0 | dialog.show(); |
188 | 0 | return; |
189 | |
} |
190 | |
|
191 | 0 | final List<String> referencedProgReqIds = new ArrayList<String>(); |
192 | |
|
193 | 0 | courseRemoteService.storeCourseStatements(courseId.toString(), courseReqState, courseReqInfos, new KSAsyncCallback<Map<Integer, StatementTreeViewInfo>>() { |
194 | |
@Override |
195 | |
public void handleFailure(Throwable caught) { |
196 | 0 | Window.alert(caught.getMessage()); |
197 | 0 | GWT.log("storeProgramRequirements failed", caught); |
198 | 0 | } |
199 | |
@Override |
200 | |
public void onSuccess(Map<Integer, StatementTreeViewInfo> storedRules) { |
201 | |
|
202 | 0 | for (Integer internalProgReqID : storedRules.keySet()) { |
203 | 0 | StatementTreeViewInfo storedRule = storedRules.get(internalProgReqID); |
204 | 0 | switch (courseReqState.get(internalProgReqID)) { |
205 | |
case STORED: |
206 | |
|
207 | 0 | referencedProgReqIds.add(internalProgReqID.toString()); |
208 | 0 | break; |
209 | |
case ADDED: |
210 | 0 | referencedProgReqIds.add(storedRule.getId()); |
211 | 0 | courseReqInfos.put(internalProgReqID, storedRule); |
212 | 0 | origCourseReqInfos.put(internalProgReqID, RulesUtil.clone(storedRule)); |
213 | 0 | origCourseReqState.put(internalProgReqID, requirementState.STORED); |
214 | 0 | courseReqState.put(internalProgReqID, requirementState.STORED); |
215 | 0 | break; |
216 | |
case EDITED: |
217 | 0 | referencedProgReqIds.add(storedRule.getId()); |
218 | 0 | courseReqInfos.put(internalProgReqID, storedRule); |
219 | 0 | origCourseReqInfos.put(internalProgReqID, RulesUtil.clone(storedRule)); |
220 | 0 | origCourseReqState.put(internalProgReqID, requirementState.STORED); |
221 | 0 | courseReqState.put(internalProgReqID, requirementState.STORED); |
222 | 0 | break; |
223 | |
case DELETED: |
224 | 0 | courseReqInfos.remove(internalProgReqID); |
225 | 0 | origCourseReqInfos.remove(internalProgReqID); |
226 | 0 | origCourseReqState.remove(internalProgReqID); |
227 | 0 | courseReqState.remove(internalProgReqID); |
228 | 0 | break; |
229 | |
default: |
230 | |
break; |
231 | |
} |
232 | 0 | } |
233 | |
|
234 | 0 | KSNotifier.add(new KSNotification("Save Successful", false, 4000)); |
235 | |
|
236 | 0 | callback.exec(new ArrayList(storedRules.values())); |
237 | 0 | } |
238 | |
}); |
239 | 0 | } |
240 | |
|
241 | |
public static void stripStatementIds(StatementTreeViewInfo tree) { |
242 | 0 | List<StatementTreeViewInfo> statements = tree.getStatements(); |
243 | 0 | List<ReqComponentInfo> reqComponentInfos = tree.getReqComponents(); |
244 | |
|
245 | 0 | if ((tree.getId() != null) && (tree.getId().indexOf(CourseRequirementsSummaryView.NEW_STMT_TREE_ID)) >= 0) { |
246 | 0 | tree.setId(null); |
247 | |
} |
248 | 0 | tree.setState("Active"); |
249 | |
|
250 | 0 | if ((statements != null) && (statements.size() > 0)) { |
251 | |
|
252 | 0 | for (StatementTreeViewInfo statement : statements) { |
253 | 0 | stripStatementIds(statement); |
254 | |
} |
255 | 0 | } else if ((reqComponentInfos != null) && (reqComponentInfos.size() > 0)) { |
256 | |
|
257 | 0 | for (ReqComponentInfo reqComponent : reqComponentInfos) { |
258 | 0 | if ((reqComponent.getId() != null) && (reqComponent.getId().indexOf(CourseRequirementsSummaryView.NEW_REQ_COMP_ID) >= 0)) { |
259 | 0 | reqComponent.setId(null); |
260 | |
} |
261 | |
|
262 | 0 | for (ReqCompFieldInfo field : reqComponent.getReqCompFields()) { |
263 | 0 | field.setId(null); |
264 | |
} |
265 | |
|
266 | 0 | reqComponent.setState("Active"); |
267 | |
} |
268 | |
} |
269 | 0 | } |
270 | |
|
271 | |
public List<StatementTreeViewInfo> getCourseReqInfo(String stmtTypeId) { |
272 | 0 | List<StatementTreeViewInfo> rules = new ArrayList<StatementTreeViewInfo>(); |
273 | 0 | for(StatementTreeViewInfo rule : courseReqInfos.values()) { |
274 | 0 | if (rule.getType().equals(stmtTypeId)) { |
275 | 0 | rules.add(rule); |
276 | |
} |
277 | |
} |
278 | 0 | return rules; |
279 | |
} |
280 | |
|
281 | |
public Integer getInternalCourseReqID(StatementTreeViewInfo rule) { |
282 | 0 | for(Integer key : courseReqInfos.keySet()) { |
283 | 0 | if (courseReqInfos.get(key) == rule) { |
284 | 0 | return key; |
285 | |
} |
286 | |
} |
287 | |
|
288 | 0 | Window.alert("Problem retrieving key for course requisite: " + rule.getId()); |
289 | 0 | GWT.log("Problem retrieving key for course requisite: " + rule.getId(), null); |
290 | |
|
291 | 0 | return null; |
292 | |
} |
293 | |
|
294 | |
public StatementTypeInfo getStmtTypeInfo(String stmtTypeId) { |
295 | 0 | for (StatementTypeInfo stmtInfo : stmtTypes) { |
296 | 0 | if (stmtInfo.getId().equals(stmtTypeId)) { |
297 | 0 | return stmtInfo; |
298 | |
} |
299 | |
} |
300 | |
|
301 | |
try { |
302 | |
|
303 | 0 | Window.alert("Did not find StatementTypeInfo based on typeA: " + stmtTypeId + new Throwable().getStackTrace().toString()); |
304 | 0 | GWT.log("Did not find StatementTypeInfo based on type: " + stmtTypeId + new Throwable().getStackTrace()); |
305 | |
|
306 | |
|
307 | 0 | throw new Exception(); |
308 | |
|
309 | 0 | } catch (Exception e) { |
310 | 0 | Window.alert("Exception" + e.getStackTrace().toString()); |
311 | |
} |
312 | 0 | return null; |
313 | |
} |
314 | |
|
315 | |
public void deleteRule(Integer internalProgReqID) { |
316 | 0 | if (courseReqState.get(internalProgReqID) == requirementState.ADDED) { |
317 | |
|
318 | 0 | courseReqState.remove(internalProgReqID); |
319 | 0 | courseReqInfos.remove(internalProgReqID); |
320 | |
} else { |
321 | 0 | markRuleAsDeleted(internalProgReqID); |
322 | |
} |
323 | 0 | } |
324 | |
|
325 | |
public void addRule(StatementTreeViewInfo rule) { |
326 | 0 | courseReqInfos.put(courseReqIDs, rule); |
327 | 0 | courseReqState.put(courseReqIDs++, requirementState.ADDED); |
328 | 0 | } |
329 | |
|
330 | |
public void updateRule(Integer internalProgReqID, StatementTreeViewInfo rule) { |
331 | 0 | courseReqInfos.put(internalProgReqID, rule); |
332 | 0 | markRuleAsEdited(internalProgReqID); |
333 | 0 | } |
334 | |
|
335 | |
public void markRuleAsDeleted(Integer internalCourseReqID) { |
336 | 0 | if ((courseReqState.get(internalCourseReqID) == requirementState.STORED) || |
337 | |
(courseReqState.get(internalCourseReqID) == requirementState.EDITED)) { |
338 | 0 | courseReqState.put(internalCourseReqID, requirementState.DELETED); |
339 | |
} |
340 | 0 | } |
341 | |
|
342 | |
public void markRuleAsEdited(Integer internalCourseReqID) { |
343 | 0 | if (courseReqState.get(internalCourseReqID) == requirementState.STORED) { |
344 | 0 | courseReqState.put(internalCourseReqID, requirementState.EDITED); |
345 | |
} |
346 | 0 | } |
347 | |
|
348 | |
public String getStmtTypeName(String stmtTypeId) { |
349 | 0 | String name = getStmtTypeInfo(stmtTypeId).getName(); |
350 | 0 | return (name == null ? "" : name); |
351 | |
} |
352 | |
|
353 | |
public boolean isRuleExists(String stmtTypeId) { |
354 | 0 | boolean showNoRuleText = true; |
355 | 0 | for(StatementTreeViewInfo ruleInfo : courseReqInfos.values()) { |
356 | 0 | if ((ruleInfo.getType().equals(stmtTypeId)) && (courseReqState.get(getInternalCourseReqID(ruleInfo)) != requirementState.DELETED)) { |
357 | 0 | showNoRuleText = false; |
358 | |
} |
359 | |
} |
360 | 0 | return showNoRuleText; |
361 | |
} |
362 | |
|
363 | |
public void removeEmptyRules() { |
364 | 0 | for(StatementTreeViewInfo rule : courseReqInfos.values()) { |
365 | 0 | if (isEmpty(rule)) { |
366 | 0 | Integer ruleId = getInternalCourseReqID(rule); |
367 | 0 | courseReqInfos.remove(ruleId); |
368 | 0 | courseReqState.remove(ruleId); |
369 | 0 | } |
370 | |
} |
371 | 0 | } |
372 | |
|
373 | |
public boolean isDirty() { |
374 | |
|
375 | 0 | if (courseReqState.size() != origCourseReqState.size()) { |
376 | 0 | return true; |
377 | |
} |
378 | |
|
379 | 0 | for(Integer key : courseReqState.keySet()) { |
380 | 0 | if (!courseReqState.get(key).equals(origCourseReqState.get(key))) { |
381 | 0 | return true; |
382 | |
} |
383 | |
} |
384 | |
|
385 | 0 | return false; |
386 | |
} |
387 | |
|
388 | |
public void revertRuleChanges() { |
389 | 0 | courseReqInfos = new HashMap<Integer, StatementTreeViewInfo>(); |
390 | 0 | courseReqState = new HashMap<Integer, requirementState>(); |
391 | 0 | for(Integer key : origCourseReqInfos.keySet()) { |
392 | 0 | courseReqInfos.put(key, RulesUtil.clone(origCourseReqInfos.get(key))); |
393 | 0 | courseReqState.put(key, origCourseReqState.get(key)); |
394 | |
} |
395 | 0 | } |
396 | |
|
397 | |
public StatementTreeViewInfo getRule(Integer internalCourseReqID) { |
398 | 0 | return courseReqInfos.get(internalCourseReqID); |
399 | |
} |
400 | |
|
401 | |
public static void getStatementTypes(final Callback<List<StatementTypeInfo>> onReadyCallback) { |
402 | |
|
403 | 0 | StatementRpcServiceAsync statementRpcServiceAsync = GWT.create(StatementRpcService.class); |
404 | |
|
405 | |
|
406 | 0 | statementRpcServiceAsync.getStatementTypesForStatementTypeForCourse("kuali.statement.type.course", new KSAsyncCallback<List<StatementTypeInfo>>() { |
407 | |
@Override |
408 | |
public void handleFailure(Throwable caught) { |
409 | 0 | Window.alert(caught.getMessage()); |
410 | 0 | GWT.log("getStatementTypes failed", caught); |
411 | 0 | onReadyCallback.exec(new ArrayList<StatementTypeInfo>()); |
412 | 0 | } |
413 | |
|
414 | |
@Override |
415 | |
public void onSuccess(List<StatementTypeInfo> stmtInfoTypes) { |
416 | |
|
417 | 0 | List<StatementTypeInfo> stmtTypes = new ArrayList<StatementTypeInfo>(); |
418 | 0 | for (StatementTypeInfo stmtInfoType : stmtInfoTypes) { |
419 | 0 | stmtTypes.add(stmtInfoType); |
420 | |
} |
421 | 0 | onReadyCallback.exec(stmtTypes); |
422 | 0 | } |
423 | |
}); |
424 | 0 | } |
425 | |
|
426 | |
public static boolean isEmpty(StatementTreeViewInfo rule) { |
427 | 0 | return (((rule.getStatements() == null) || rule.getStatements().isEmpty()) && ((rule.getReqComponents() == null) || rule.getReqComponents().isEmpty())); |
428 | |
} |
429 | |
|
430 | |
public boolean isInitialized() { |
431 | 0 | return isInitialized; |
432 | |
} |
433 | |
|
434 | |
public void setInitialized(boolean initialized) { |
435 | 0 | isInitialized = initialized; |
436 | 0 | } |
437 | |
|
438 | |
public List<StatementTypeInfo> getStmtTypes() { |
439 | 0 | return stmtTypes; |
440 | |
} |
441 | |
} |