1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.kuali.student.lum.program.client.requirements; |
16 | |
|
17 | |
import java.util.*; |
18 | |
|
19 | |
import org.kuali.student.common.assembly.data.Data; |
20 | |
import org.kuali.student.common.ui.client.application.KSAsyncCallback; |
21 | |
import org.kuali.student.common.ui.client.mvc.*; |
22 | |
import org.kuali.student.core.statement.dto.StatementTreeViewInfo; |
23 | |
import org.kuali.student.core.statement.dto.StatementTypeInfo; |
24 | |
import org.kuali.student.core.statement.ui.client.widgets.rules.RulesUtil; |
25 | |
import org.kuali.student.lum.program.client.ProgramConstants; |
26 | |
import org.kuali.student.lum.program.client.events.StoreRequirementIDsEvent; |
27 | |
import org.kuali.student.lum.program.client.events.StoreSpecRequirementIDsEvent; |
28 | |
import org.kuali.student.lum.program.client.rpc.MajorDisciplineRpcService; |
29 | |
import org.kuali.student.lum.program.client.rpc.MajorDisciplineRpcServiceAsync; |
30 | |
import org.kuali.student.lum.program.client.rpc.StatementRpcService; |
31 | |
import org.kuali.student.lum.program.client.rpc.StatementRpcServiceAsync; |
32 | |
import org.kuali.student.lum.program.dto.ProgramRequirementInfo; |
33 | |
|
34 | |
import com.google.gwt.core.client.GWT; |
35 | |
import com.google.gwt.event.shared.HandlerManager; |
36 | |
import com.google.gwt.user.client.Window; |
37 | |
|
38 | 0 | public class ProgramRequirementsDataModel { |
39 | |
|
40 | 0 | private final MajorDisciplineRpcServiceAsync programRemoteService = GWT.create(MajorDisciplineRpcService.class); |
41 | 0 | private StatementRpcServiceAsync statementRpcServiceAsync = GWT.create(StatementRpcService.class); |
42 | 0 | private Model model = null; |
43 | |
private HandlerManager eventBus; |
44 | |
|
45 | |
|
46 | 0 | public enum requirementState {STORED, ADDED, EDITED, DELETED;} |
47 | 0 | private Map<Integer, ProgramRequirementInfo> progReqInfos = new LinkedHashMap<Integer, ProgramRequirementInfo>(); |
48 | 0 | private Map<Integer, ProgramRequirementInfo> origProgReqInfos = new LinkedHashMap<Integer, ProgramRequirementInfo>(); |
49 | 0 | private Map<Integer, requirementState> progReqState = new HashMap<Integer, requirementState>(); |
50 | 0 | private Map<Integer, requirementState> origProgReqState = new HashMap<Integer, requirementState>(); |
51 | 0 | private List<StatementTypeInfo> stmtTypes = new ArrayList<StatementTypeInfo>(); |
52 | 0 | private boolean isInitialized = false; |
53 | 0 | private static Integer progReqIDs = 111111; |
54 | |
|
55 | 0 | public ProgramRequirementsDataModel(HandlerManager eventBus) { |
56 | 0 | this.eventBus = eventBus; |
57 | 0 | } |
58 | |
|
59 | |
|
60 | |
public void setupRules(Controller parentController, String modelId, final Callback<Boolean> onReadyCallback) { |
61 | 0 | parentController.requestModel(modelId, new ModelRequestCallback() { |
62 | |
|
63 | |
@Override |
64 | |
public void onRequestFail(Throwable cause) { |
65 | 0 | GWT.log("Unable to retrieve program model for program summary view", cause); |
66 | 0 | onReadyCallback.exec(false); |
67 | 0 | } |
68 | |
|
69 | |
@Override |
70 | |
public void onModelReady(Model modelIn) { |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | 0 | resetRules(); |
76 | |
|
77 | 0 | model = modelIn; |
78 | 0 | onReadyCallback.exec(true); |
79 | 0 | } |
80 | |
}); |
81 | 0 | } |
82 | |
|
83 | |
private void resetRules() { |
84 | 0 | progReqInfos = new LinkedHashMap<Integer, ProgramRequirementInfo>(); |
85 | 0 | origProgReqInfos = new LinkedHashMap<Integer, ProgramRequirementInfo>(); |
86 | 0 | progReqState = new HashMap<Integer, requirementState>(); |
87 | 0 | origProgReqState = new HashMap<Integer, requirementState>(); |
88 | 0 | stmtTypes = new ArrayList<StatementTypeInfo>(); |
89 | 0 | model = null; |
90 | 0 | isInitialized = false; |
91 | 0 | } |
92 | |
|
93 | |
|
94 | |
public void retrieveProgramRequirements(Controller parentController, String modelId, final Callback<Boolean> onReadyCallback) { |
95 | |
|
96 | 0 | setupRules(parentController, modelId, new Callback<Boolean>() { |
97 | |
@Override |
98 | |
public void exec(Boolean result) { |
99 | 0 | Data program = ((DataModel)model).getRoot().get(ProgramConstants.PROGRAM_REQUIREMENTS); |
100 | |
|
101 | 0 | Iterator<Data.Property> realPropertyIterator = program.realPropertyIterator(); |
102 | 0 | ArrayList<String> programRequirementIds = new ArrayList<String>(); |
103 | 0 | while(realPropertyIterator.hasNext()) { |
104 | 0 | programRequirementIds.add((String)realPropertyIterator.next().getValue()); |
105 | |
} |
106 | 0 | retrieveStatementTypes(programRequirementIds, onReadyCallback); |
107 | 0 | } |
108 | |
}); |
109 | 0 | } |
110 | |
|
111 | |
private void retrieveStatementTypes(final List<String> programRequirementIds, final Callback<Boolean> onReadyCallback) { |
112 | |
|
113 | |
|
114 | 0 | statementRpcServiceAsync.getStatementTypesForStatementType("kuali.statement.type.program", new KSAsyncCallback<List<StatementTypeInfo>>() { |
115 | |
@Override |
116 | |
public void handleFailure(Throwable caught) { |
117 | 0 | Window.alert(caught.getMessage()); |
118 | 0 | GWT.log("getStatementTypes failed", caught); |
119 | 0 | onReadyCallback.exec(false); |
120 | 0 | } |
121 | |
|
122 | |
@Override |
123 | |
public void onSuccess(List<StatementTypeInfo> stmtInfoTypes) { |
124 | |
|
125 | 0 | for (StatementTypeInfo stmtInfoType : stmtInfoTypes) { |
126 | 0 | stmtTypes.add(stmtInfoType); |
127 | |
} |
128 | |
|
129 | |
|
130 | 0 | retrieveRules(programRequirementIds, onReadyCallback); |
131 | 0 | } |
132 | |
}); |
133 | 0 | } |
134 | |
|
135 | |
private void retrieveRules(List<String> programRequirementIds, final Callback<Boolean> onReadyCallback) { |
136 | |
|
137 | |
|
138 | 0 | if ((programRequirementIds == null) || programRequirementIds.isEmpty()) { |
139 | 0 | isInitialized = true; |
140 | 0 | onReadyCallback.exec(true); |
141 | 0 | return; |
142 | |
} |
143 | |
|
144 | 0 | programRemoteService.getProgramRequirements(programRequirementIds, new KSAsyncCallback<List<ProgramRequirementInfo>>() { |
145 | |
@Override |
146 | |
public void handleFailure(Throwable caught) { |
147 | 0 | Window.alert(caught.getMessage()); |
148 | 0 | GWT.log("getProgramRequirement failed", caught); |
149 | 0 | onReadyCallback.exec(false); |
150 | 0 | } |
151 | |
|
152 | |
@Override |
153 | |
public void onSuccess(List<ProgramRequirementInfo> programReqInfos) { |
154 | |
|
155 | 0 | origProgReqInfos.clear(); |
156 | 0 | origProgReqState.clear(); |
157 | 0 | progReqInfos.clear(); |
158 | 0 | progReqState.clear(); |
159 | 0 | for (ProgramRequirementInfo programReqInfo : programReqInfos) { |
160 | |
|
161 | 0 | if (getStmtTypeInfo(programReqInfo.getStatement().getType()) == null) { |
162 | 0 | Window.alert("Did not find corresponding statement type for program requirement of type: " + programReqInfo.getStatement().getType()); |
163 | 0 | GWT.log("Did not find corresponding statement type for program requirement of type: " + programReqInfo.getStatement().getType(), null); |
164 | |
} |
165 | |
|
166 | 0 | setRuleState(programReqInfo); |
167 | 0 | origProgReqInfos.put(progReqIDs, cloneProgReq(programReqInfo)); |
168 | 0 | origProgReqState.put(progReqIDs, requirementState.STORED); |
169 | 0 | progReqInfos.put(progReqIDs, programReqInfo); |
170 | 0 | progReqState.put(progReqIDs++, requirementState.STORED); |
171 | |
} |
172 | |
|
173 | 0 | isInitialized = true; |
174 | 0 | onReadyCallback.exec(true); |
175 | 0 | } |
176 | |
}); |
177 | 0 | } |
178 | |
|
179 | |
public ProgramRequirementInfo updateRules(StatementTreeViewInfo newSubRule, Integer internalProgReqID, boolean isNewRule) { |
180 | |
|
181 | 0 | ProgramRequirementInfo affectedRule = progReqInfos.get(internalProgReqID); |
182 | |
|
183 | 0 | if (affectedRule == null) { |
184 | 0 | Window.alert("Cannot find program requirement with a statement that has id: '" + newSubRule.getId() + "'"); |
185 | 0 | GWT.log("Cannot find program requirement with a statement that has id: '" + newSubRule.getId() + "'", null); |
186 | 0 | return null; |
187 | |
} |
188 | |
|
189 | 0 | if (progReqState.get(internalProgReqID) == ProgramRequirementsDataModel.requirementState.STORED) { |
190 | 0 | progReqState.put(internalProgReqID, ProgramRequirementsDataModel.requirementState.EDITED); |
191 | |
} |
192 | |
|
193 | |
|
194 | 0 | StatementTreeViewInfo affectedTopTree = affectedRule.getStatement(); |
195 | 0 | if ((affectedTopTree.getReqComponents() != null) && !affectedTopTree.getReqComponents().isEmpty()) { |
196 | 0 | StatementTreeViewInfo stmtTree = new StatementTreeViewInfo(); |
197 | 0 | stmtTree.setId(ProgramRequirementsSummaryView.generateStatementTreeId()); |
198 | 0 | stmtTree.setType( affectedRule.getStatement().getType()); |
199 | 0 | stmtTree.setReqComponents(affectedTopTree.getReqComponents()); |
200 | 0 | List<StatementTreeViewInfo> stmtList = new ArrayList<StatementTreeViewInfo>(); |
201 | 0 | stmtList.add(stmtTree); |
202 | 0 | affectedTopTree.setStatements(stmtList); |
203 | |
} |
204 | |
|
205 | |
|
206 | 0 | List<StatementTreeViewInfo> affectedSubRules = affectedTopTree.getStatements(); |
207 | 0 | if (isNewRule) { |
208 | 0 | affectedSubRules.add(newSubRule); |
209 | |
} else { |
210 | |
|
211 | 0 | if (affectedSubRules == null || affectedSubRules.isEmpty()) { |
212 | 0 | affectedRule.setStatement(newSubRule); |
213 | |
} else { |
214 | 0 | for (StatementTreeViewInfo subRule : affectedSubRules) { |
215 | 0 | if (subRule.getId().equals(newSubRule.getId())) { |
216 | 0 | int treeIx = affectedSubRules.indexOf(subRule); |
217 | |
|
218 | 0 | if (!isEmptyRule(newSubRule)) { |
219 | 0 | affectedSubRules.add(treeIx, newSubRule); |
220 | |
} |
221 | 0 | affectedSubRules.remove(subRule); |
222 | 0 | break; |
223 | |
} |
224 | |
} |
225 | |
} |
226 | |
} |
227 | |
|
228 | 0 | return affectedRule; |
229 | |
} |
230 | |
|
231 | |
public void updateProgramEntities(final Callback<List<ProgramRequirementInfo>> callback) { |
232 | |
|
233 | 0 | final List<String> referencedProgReqIds = new ArrayList<String>(); |
234 | |
|
235 | 0 | programRemoteService.storeProgramRequirements(progReqState, progReqInfos, new KSAsyncCallback<Map<Integer, ProgramRequirementInfo>>() { |
236 | |
@Override |
237 | |
public void handleFailure(Throwable caught) { |
238 | 0 | Window.alert(caught.getMessage()); |
239 | 0 | GWT.log("storeProgramRequirements failed", caught); |
240 | 0 | } |
241 | |
@Override |
242 | |
public void onSuccess(Map<Integer, ProgramRequirementInfo> storedRules) { |
243 | |
|
244 | 0 | for (Integer internalProgReqID : storedRules.keySet()) { |
245 | 0 | ProgramRequirementInfo storedRule = storedRules.get(internalProgReqID); |
246 | 0 | switch (progReqState.get(internalProgReqID)) { |
247 | |
case STORED: |
248 | |
|
249 | 0 | referencedProgReqIds.add(progReqInfos.get(internalProgReqID).getId()); |
250 | 0 | break; |
251 | |
case ADDED: |
252 | 0 | referencedProgReqIds.add(storedRule.getId()); |
253 | 0 | progReqInfos.put(internalProgReqID, storedRule); |
254 | 0 | origProgReqInfos.put(internalProgReqID, cloneProgReq(storedRule)); |
255 | 0 | origProgReqState.put(internalProgReqID, ProgramRequirementsDataModel.requirementState.STORED); |
256 | 0 | progReqState.put(internalProgReqID, ProgramRequirementsDataModel.requirementState.STORED); |
257 | 0 | break; |
258 | |
case EDITED: |
259 | 0 | referencedProgReqIds.add(storedRule.getId()); |
260 | 0 | progReqInfos.put(internalProgReqID, storedRule); |
261 | 0 | origProgReqInfos.put(internalProgReqID, cloneProgReq(storedRule)); |
262 | 0 | origProgReqState.put(internalProgReqID, ProgramRequirementsDataModel.requirementState.STORED); |
263 | 0 | progReqState.put(internalProgReqID, ProgramRequirementsDataModel.requirementState.STORED); |
264 | 0 | break; |
265 | |
case DELETED: |
266 | 0 | progReqInfos.remove(internalProgReqID); |
267 | 0 | origProgReqInfos.remove(internalProgReqID); |
268 | 0 | origProgReqState.remove(internalProgReqID); |
269 | 0 | progReqState.remove(internalProgReqID); |
270 | 0 | break; |
271 | |
default: |
272 | |
break; |
273 | |
} |
274 | 0 | } |
275 | |
|
276 | 0 | saveRequirementIds(referencedProgReqIds, storedRules, callback); |
277 | 0 | } |
278 | |
}); |
279 | 0 | } |
280 | |
|
281 | |
private void saveRequirementIds(final List<String> referencedProgReqIds, final Map<Integer, ProgramRequirementInfo> storedRules, final Callback<List<ProgramRequirementInfo>> callback) { |
282 | 0 | String programId = ((DataModel)model).getRoot().get("id"); |
283 | 0 | String programType = ((DataModel)model).getRoot().get("type"); |
284 | |
|
285 | |
|
286 | 0 | if (programType == null) { |
287 | 0 | programType = ((DataModel)model).getRoot().get("credentialProgramType"); |
288 | |
} |
289 | |
|
290 | |
|
291 | 0 | if (programType.equals(ProgramConstants.VARIATION_TYPE_KEY)) { |
292 | 0 | eventBus.fireEvent(new StoreSpecRequirementIDsEvent(programId, programType, referencedProgReqIds)); |
293 | |
} else { |
294 | 0 | eventBus.fireEvent(new StoreRequirementIDsEvent(programId, programType, referencedProgReqIds)); |
295 | |
} |
296 | |
|
297 | 0 | callback.exec(new ArrayList(storedRules.values())); |
298 | 0 | } |
299 | |
|
300 | |
|
301 | |
public List<ProgramRequirementInfo> getProgReqInfo(String stmtTypeId) { |
302 | 0 | List<ProgramRequirementInfo> rules = new ArrayList<ProgramRequirementInfo>(); |
303 | 0 | for(ProgramRequirementInfo progReqInfo : progReqInfos.values()) { |
304 | 0 | if (progReqInfo.getStatement().getType().equals(stmtTypeId)) { |
305 | 0 | rules.add(progReqInfo); |
306 | |
} |
307 | |
} |
308 | 0 | return rules; |
309 | |
} |
310 | |
|
311 | |
public Integer getInternalProgReqID(ProgramRequirementInfo progReqInfo) { |
312 | 0 | for(Integer key : progReqInfos.keySet()) { |
313 | 0 | if (progReqInfos.get(key) == progReqInfo) { |
314 | 0 | return key; |
315 | |
} |
316 | |
} |
317 | |
|
318 | 0 | Window.alert("Problem retrieving key for program requirement: " + progReqInfo.getId()); |
319 | 0 | GWT.log("Problem retrieving key for program requirement: " + progReqInfo.getId(), null); |
320 | |
|
321 | 0 | return null; |
322 | |
} |
323 | |
|
324 | |
public StatementTypeInfo getStmtTypeInfo(String stmtTypeId) { |
325 | 0 | for (StatementTypeInfo stmtInfo : stmtTypes) { |
326 | 0 | if (stmtInfo.getId().equals(stmtTypeId)) { |
327 | 0 | return stmtInfo; |
328 | |
} |
329 | |
} |
330 | |
|
331 | 0 | Window.alert("Did not find StatementTypeInfo based on type: " + stmtTypeId); |
332 | 0 | GWT.log("Did not find StatementTypeInfo based on type: " + stmtTypeId); |
333 | |
|
334 | 0 | return null; |
335 | |
} |
336 | |
|
337 | |
public void deleteRule(Integer internalProgReqID) { |
338 | 0 | if (progReqState.get(internalProgReqID) == ProgramRequirementsDataModel.requirementState.ADDED) { |
339 | |
|
340 | 0 | progReqState.remove(internalProgReqID); |
341 | 0 | progReqInfos.remove(internalProgReqID); |
342 | |
} else { |
343 | 0 | markRuleAsDeleted(internalProgReqID); |
344 | |
} |
345 | 0 | } |
346 | |
|
347 | |
public void addRule(ProgramRequirementInfo programReqInfo) { |
348 | 0 | setRuleState(programReqInfo); |
349 | 0 | progReqInfos.put(progReqIDs, programReqInfo); |
350 | 0 | progReqState.put(progReqIDs++, requirementState.ADDED); |
351 | 0 | } |
352 | |
|
353 | |
public void updateRule(Integer internalProgReqID, ProgramRequirementInfo programReqInfo) { |
354 | 0 | setRuleState(programReqInfo); |
355 | 0 | progReqInfos.put(internalProgReqID, programReqInfo); |
356 | 0 | markRuleAsEdited(internalProgReqID); |
357 | 0 | } |
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
protected void setRuleState(ProgramRequirementInfo programReqInfo) { |
365 | 0 | if (model != null) { |
366 | 0 | String programState = ((DataModel) model).get(ProgramConstants.STATE); |
367 | 0 | programReqInfo.setState(programState); |
368 | |
} |
369 | 0 | } |
370 | |
|
371 | |
public void markRuleAsDeleted(Integer internalProgReqID) { |
372 | 0 | if ((progReqState.get(internalProgReqID) == ProgramRequirementsDataModel.requirementState.STORED) || |
373 | |
(progReqState.get(internalProgReqID) == ProgramRequirementsDataModel.requirementState.EDITED)) { |
374 | 0 | progReqState.put(internalProgReqID, ProgramRequirementsDataModel.requirementState.DELETED); |
375 | |
} |
376 | 0 | } |
377 | |
|
378 | |
public void markRuleAsEdited(Integer internalProgReqID) { |
379 | 0 | if (progReqState.get(internalProgReqID) == ProgramRequirementsDataModel.requirementState.STORED) { |
380 | 0 | progReqState.put(internalProgReqID, ProgramRequirementsDataModel.requirementState.EDITED); |
381 | |
} |
382 | 0 | } |
383 | |
|
384 | |
public String getStmtTypeName(String stmtTypeId) { |
385 | 0 | String name = getStmtTypeInfo(stmtTypeId).getName(); |
386 | 0 | return (name == null ? "" : name); |
387 | |
} |
388 | |
|
389 | |
public boolean isRuleExists(String stmtTypeId) { |
390 | 0 | boolean showNoRuleText = true; |
391 | 0 | for(ProgramRequirementInfo ruleInfo : progReqInfos.values()) { |
392 | 0 | if ((ruleInfo.getStatement().getType().equals(stmtTypeId)) && (progReqState.get(getInternalProgReqID(ruleInfo)) != ProgramRequirementsDataModel.requirementState.DELETED)) { |
393 | 0 | showNoRuleText = false; |
394 | |
} |
395 | |
} |
396 | 0 | return showNoRuleText; |
397 | |
} |
398 | |
|
399 | |
public boolean isDirty() { |
400 | |
|
401 | 0 | if (progReqState.size() != origProgReqState.size()) { |
402 | 0 | return true; |
403 | |
} |
404 | |
|
405 | 0 | for(Integer key : progReqState.keySet()) { |
406 | 0 | if (!progReqState.get(key).equals(origProgReqState.get(key))) { |
407 | 0 | return true; |
408 | |
} |
409 | |
} |
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
|
417 | 0 | return false; |
418 | |
} |
419 | |
|
420 | |
public void revertRuleChanges() { |
421 | 0 | progReqInfos = new HashMap<Integer, ProgramRequirementInfo>(); |
422 | 0 | progReqState = new HashMap<Integer, requirementState>(); |
423 | 0 | for(Integer key : origProgReqInfos.keySet()) { |
424 | 0 | progReqInfos.put(key, cloneProgReq(origProgReqInfos.get(key))); |
425 | 0 | progReqState.put(key, origProgReqState.get(key)); |
426 | |
} |
427 | 0 | } |
428 | |
|
429 | |
public ProgramRequirementInfo getProgReqByInternalId(Integer internalProgReqID) { |
430 | 0 | return progReqInfos.get(internalProgReqID); |
431 | |
} |
432 | |
|
433 | |
public boolean isEmptyRule(StatementTreeViewInfo tree) { |
434 | 0 | return (tree.getStatements() == null || tree.getStatements().isEmpty() && (tree.getReqComponents() == null || tree.getReqComponents().isEmpty())); |
435 | |
} |
436 | |
|
437 | |
public boolean isInitialized() { |
438 | 0 | return isInitialized; |
439 | |
} |
440 | |
|
441 | |
public void setInitialized(boolean initialized) { |
442 | 0 | isInitialized = initialized; |
443 | 0 | } |
444 | |
|
445 | |
public List<StatementTypeInfo> getStmtTypes() { |
446 | 0 | return stmtTypes; |
447 | |
} |
448 | |
|
449 | |
private ProgramRequirementInfo cloneProgReq(ProgramRequirementInfo inProgReqInfo) { |
450 | 0 | ProgramRequirementInfo clonedProgReqInfo = null; |
451 | 0 | if (inProgReqInfo != null) { |
452 | 0 | clonedProgReqInfo = new ProgramRequirementInfo(); |
453 | 0 | clonedProgReqInfo.setId(inProgReqInfo.getId()); |
454 | 0 | clonedProgReqInfo.setShortTitle(inProgReqInfo.getShortTitle()); |
455 | 0 | clonedProgReqInfo.setLongTitle(inProgReqInfo.getLongTitle()); |
456 | 0 | clonedProgReqInfo.setDescr(inProgReqInfo.getDescr()); |
457 | 0 | clonedProgReqInfo.setMinCredits(inProgReqInfo.getMinCredits()); |
458 | 0 | clonedProgReqInfo.setMaxCredits(inProgReqInfo.getMaxCredits()); |
459 | 0 | clonedProgReqInfo.setState(inProgReqInfo.getState()); |
460 | 0 | clonedProgReqInfo.setType(inProgReqInfo.getType()); |
461 | 0 | clonedProgReqInfo.setStatement(RulesUtil.clone(inProgReqInfo.getStatement())); |
462 | |
|
463 | |
|
464 | |
|
465 | |
} |
466 | 0 | return clonedProgReqInfo; |
467 | |
} |
468 | |
} |