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 String courseState, final Callback<List<StatementTreeViewInfo>> callback) { |
177 | |
|
178 | 0 | if (courseId == null) { |
179 | 0 | final ConfirmationDialog dialog = new ConfirmationDialog("Submit Course Title", "Before saving rules please submit course proposal title"); |
180 | 0 | dialog.getConfirmButton().addClickHandler(new ClickHandler(){ |
181 | |
@Override |
182 | |
public void onClick(ClickEvent event) { |
183 | 0 | dialog.hide(); |
184 | 0 | } |
185 | |
}); |
186 | 0 | dialog.show(); |
187 | 0 | return; |
188 | |
} |
189 | |
|
190 | 0 | final List<String> referencedProgReqIds = new ArrayList<String>(); |
191 | |
|
192 | 0 | courseRemoteService.storeCourseStatements(courseId.toString(), courseState, courseReqState, courseReqInfos, new KSAsyncCallback<Map<Integer, StatementTreeViewInfo>>() { |
193 | |
@Override |
194 | |
public void handleFailure(Throwable caught) { |
195 | 0 | Window.alert(caught.getMessage()); |
196 | 0 | GWT.log("storeProgramRequirements failed", caught); |
197 | 0 | } |
198 | |
@Override |
199 | |
public void onSuccess(Map<Integer, StatementTreeViewInfo> storedRules) { |
200 | |
|
201 | 0 | for (Integer internalProgReqID : storedRules.keySet()) { |
202 | 0 | StatementTreeViewInfo storedRule = storedRules.get(internalProgReqID); |
203 | 0 | switch (courseReqState.get(internalProgReqID)) { |
204 | |
case STORED: |
205 | |
|
206 | 0 | referencedProgReqIds.add(internalProgReqID.toString()); |
207 | 0 | break; |
208 | |
case ADDED: |
209 | 0 | referencedProgReqIds.add(storedRule.getId()); |
210 | 0 | courseReqInfos.put(internalProgReqID, storedRule); |
211 | 0 | origCourseReqInfos.put(internalProgReqID, RulesUtil.clone(storedRule)); |
212 | 0 | origCourseReqState.put(internalProgReqID, requirementState.STORED); |
213 | 0 | courseReqState.put(internalProgReqID, requirementState.STORED); |
214 | 0 | break; |
215 | |
case EDITED: |
216 | 0 | referencedProgReqIds.add(storedRule.getId()); |
217 | 0 | courseReqInfos.put(internalProgReqID, storedRule); |
218 | 0 | origCourseReqInfos.put(internalProgReqID, RulesUtil.clone(storedRule)); |
219 | 0 | origCourseReqState.put(internalProgReqID, requirementState.STORED); |
220 | 0 | courseReqState.put(internalProgReqID, requirementState.STORED); |
221 | 0 | break; |
222 | |
case DELETED: |
223 | 0 | courseReqInfos.remove(internalProgReqID); |
224 | 0 | origCourseReqInfos.remove(internalProgReqID); |
225 | 0 | origCourseReqState.remove(internalProgReqID); |
226 | 0 | courseReqState.remove(internalProgReqID); |
227 | 0 | break; |
228 | |
default: |
229 | |
break; |
230 | |
} |
231 | 0 | } |
232 | |
|
233 | 0 | KSNotifier.add(new KSNotification("Save Successful", false, 4000)); |
234 | |
|
235 | 0 | callback.exec(new ArrayList(storedRules.values())); |
236 | 0 | } |
237 | |
}); |
238 | 0 | } |
239 | |
|
240 | |
public static void stripStatementIds(StatementTreeViewInfo tree) { |
241 | 0 | List<StatementTreeViewInfo> statements = tree.getStatements(); |
242 | 0 | List<ReqComponentInfo> reqComponentInfos = tree.getReqComponents(); |
243 | |
|
244 | 0 | if ((tree.getId() != null) && (tree.getId().indexOf(CourseRequirementsSummaryView.NEW_STMT_TREE_ID)) >= 0) { |
245 | 0 | tree.setId(null); |
246 | |
} |
247 | |
|
248 | 0 | if ((statements != null) && (statements.size() > 0)) { |
249 | |
|
250 | 0 | for (StatementTreeViewInfo statement : statements) { |
251 | 0 | stripStatementIds(statement); |
252 | |
} |
253 | 0 | } else if ((reqComponentInfos != null) && (reqComponentInfos.size() > 0)) { |
254 | |
|
255 | 0 | for (ReqComponentInfo reqComponent : reqComponentInfos) { |
256 | 0 | if ((reqComponent.getId() != null) && (reqComponent.getId().indexOf(CourseRequirementsSummaryView.NEW_REQ_COMP_ID) >= 0)) { |
257 | 0 | reqComponent.setId(null); |
258 | |
} |
259 | |
|
260 | 0 | for (ReqCompFieldInfo field : reqComponent.getReqCompFields()) { |
261 | 0 | field.setId(null); |
262 | |
} |
263 | |
} |
264 | |
} |
265 | 0 | } |
266 | |
|
267 | |
public List<StatementTreeViewInfo> getCourseReqInfo(String stmtTypeId) { |
268 | 0 | List<StatementTreeViewInfo> rules = new ArrayList<StatementTreeViewInfo>(); |
269 | 0 | for(StatementTreeViewInfo rule : courseReqInfos.values()) { |
270 | 0 | if (rule.getType().equals(stmtTypeId)) { |
271 | 0 | rules.add(rule); |
272 | |
} |
273 | |
} |
274 | 0 | return rules; |
275 | |
} |
276 | |
|
277 | |
public Integer getInternalCourseReqID(StatementTreeViewInfo rule) { |
278 | 0 | for(Integer key : courseReqInfos.keySet()) { |
279 | 0 | if (courseReqInfos.get(key) == rule) { |
280 | 0 | return key; |
281 | |
} |
282 | |
} |
283 | |
|
284 | 0 | Window.alert("Problem retrieving key for course requisite: " + rule.getId()); |
285 | 0 | GWT.log("Problem retrieving key for course requisite: " + rule.getId(), null); |
286 | |
|
287 | 0 | return null; |
288 | |
} |
289 | |
|
290 | |
public StatementTypeInfo getStmtTypeInfo(String stmtTypeId) { |
291 | 0 | for (StatementTypeInfo stmtInfo : stmtTypes) { |
292 | 0 | if (stmtInfo.getId().equals(stmtTypeId)) { |
293 | 0 | return stmtInfo; |
294 | |
} |
295 | |
} |
296 | |
|
297 | |
try { |
298 | |
|
299 | 0 | Window.alert("Did not find StatementTypeInfo based on typeA: " + stmtTypeId + new Throwable().getStackTrace().toString()); |
300 | 0 | GWT.log("Did not find StatementTypeInfo based on type: " + stmtTypeId + new Throwable().getStackTrace()); |
301 | |
|
302 | |
|
303 | 0 | throw new Exception(); |
304 | |
|
305 | 0 | } catch (Exception e) { |
306 | 0 | Window.alert("Exception" + e.getStackTrace().toString()); |
307 | |
} |
308 | 0 | return null; |
309 | |
} |
310 | |
|
311 | |
public void deleteRule(Integer internalProgReqID) { |
312 | 0 | if (courseReqState.get(internalProgReqID) == requirementState.ADDED) { |
313 | |
|
314 | 0 | courseReqState.remove(internalProgReqID); |
315 | 0 | courseReqInfos.remove(internalProgReqID); |
316 | |
} else { |
317 | 0 | markRuleAsDeleted(internalProgReqID); |
318 | |
} |
319 | 0 | } |
320 | |
|
321 | |
public void addRule(StatementTreeViewInfo rule) { |
322 | 0 | courseReqInfos.put(courseReqIDs, rule); |
323 | 0 | courseReqState.put(courseReqIDs++, requirementState.ADDED); |
324 | 0 | } |
325 | |
|
326 | |
public void updateRule(Integer internalProgReqID, StatementTreeViewInfo rule) { |
327 | 0 | courseReqInfos.put(internalProgReqID, rule); |
328 | 0 | markRuleAsEdited(internalProgReqID); |
329 | 0 | } |
330 | |
|
331 | |
public void markRuleAsDeleted(Integer internalCourseReqID) { |
332 | 0 | if ((courseReqState.get(internalCourseReqID) == requirementState.STORED) || |
333 | |
(courseReqState.get(internalCourseReqID) == requirementState.EDITED)) { |
334 | 0 | courseReqState.put(internalCourseReqID, requirementState.DELETED); |
335 | |
} |
336 | 0 | } |
337 | |
|
338 | |
public void markRuleAsEdited(Integer internalCourseReqID) { |
339 | 0 | if (courseReqState.get(internalCourseReqID) == requirementState.STORED) { |
340 | 0 | courseReqState.put(internalCourseReqID, requirementState.EDITED); |
341 | |
} |
342 | 0 | } |
343 | |
|
344 | |
public String getStmtTypeName(String stmtTypeId) { |
345 | 0 | String name = getStmtTypeInfo(stmtTypeId).getName(); |
346 | 0 | return (name == null ? "" : name); |
347 | |
} |
348 | |
|
349 | |
public boolean isRuleExists(String stmtTypeId) { |
350 | 0 | boolean showNoRuleText = true; |
351 | 0 | for(StatementTreeViewInfo ruleInfo : courseReqInfos.values()) { |
352 | 0 | if ((ruleInfo.getType().equals(stmtTypeId)) && (courseReqState.get(getInternalCourseReqID(ruleInfo)) != requirementState.DELETED)) { |
353 | 0 | showNoRuleText = false; |
354 | |
} |
355 | |
} |
356 | 0 | return showNoRuleText; |
357 | |
} |
358 | |
|
359 | |
public void removeEmptyRules() { |
360 | 0 | for(StatementTreeViewInfo rule : courseReqInfos.values()) { |
361 | 0 | if (isEmpty(rule)) { |
362 | 0 | Integer ruleId = getInternalCourseReqID(rule); |
363 | 0 | courseReqInfos.remove(ruleId); |
364 | 0 | courseReqState.remove(ruleId); |
365 | 0 | } |
366 | |
} |
367 | 0 | } |
368 | |
|
369 | |
public boolean isDirty() { |
370 | |
|
371 | 0 | if (courseReqState.size() != origCourseReqState.size()) { |
372 | 0 | return true; |
373 | |
} |
374 | |
|
375 | 0 | for(Integer key : courseReqState.keySet()) { |
376 | 0 | if (!courseReqState.get(key).equals(origCourseReqState.get(key))) { |
377 | 0 | return true; |
378 | |
} |
379 | |
} |
380 | |
|
381 | 0 | return false; |
382 | |
} |
383 | |
|
384 | |
public void revertRuleChanges() { |
385 | 0 | courseReqInfos = new HashMap<Integer, StatementTreeViewInfo>(); |
386 | 0 | courseReqState = new HashMap<Integer, requirementState>(); |
387 | 0 | for(Integer key : origCourseReqInfos.keySet()) { |
388 | 0 | courseReqInfos.put(key, RulesUtil.clone(origCourseReqInfos.get(key))); |
389 | 0 | courseReqState.put(key, origCourseReqState.get(key)); |
390 | |
} |
391 | 0 | } |
392 | |
|
393 | |
public StatementTreeViewInfo getRule(Integer internalCourseReqID) { |
394 | 0 | return courseReqInfos.get(internalCourseReqID); |
395 | |
} |
396 | |
|
397 | |
public static void getStatementTypes(final Callback<List<StatementTypeInfo>> onReadyCallback) { |
398 | |
|
399 | 0 | StatementRpcServiceAsync statementRpcServiceAsync = GWT.create(StatementRpcService.class); |
400 | |
|
401 | |
|
402 | 0 | statementRpcServiceAsync.getStatementTypesForStatementTypeForCourse("kuali.statement.type.course", new KSAsyncCallback<List<StatementTypeInfo>>() { |
403 | |
@Override |
404 | |
public void handleFailure(Throwable caught) { |
405 | 0 | Window.alert(caught.getMessage()); |
406 | 0 | GWT.log("getStatementTypes failed", caught); |
407 | 0 | onReadyCallback.exec(new ArrayList<StatementTypeInfo>()); |
408 | 0 | } |
409 | |
|
410 | |
@Override |
411 | |
public void onSuccess(List<StatementTypeInfo> stmtInfoTypes) { |
412 | |
|
413 | 0 | List<StatementTypeInfo> stmtTypes = new ArrayList<StatementTypeInfo>(); |
414 | 0 | for (StatementTypeInfo stmtInfoType : stmtInfoTypes) { |
415 | 0 | stmtTypes.add(stmtInfoType); |
416 | |
} |
417 | 0 | onReadyCallback.exec(stmtTypes); |
418 | 0 | } |
419 | |
}); |
420 | 0 | } |
421 | |
|
422 | |
public static boolean isEmpty(StatementTreeViewInfo rule) { |
423 | 0 | return (((rule.getStatements() == null) || rule.getStatements().isEmpty()) && ((rule.getReqComponents() == null) || rule.getReqComponents().isEmpty())); |
424 | |
} |
425 | |
|
426 | |
public boolean isInitialized() { |
427 | 0 | return isInitialized; |
428 | |
} |
429 | |
|
430 | |
public void setInitialized(boolean initialized) { |
431 | 0 | isInitialized = initialized; |
432 | 0 | } |
433 | |
|
434 | |
public List<StatementTypeInfo> getStmtTypes() { |
435 | 0 | return stmtTypes; |
436 | |
} |
437 | |
} |