1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krms.impl.ui; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.List; |
20 | |
|
21 | |
import javax.servlet.http.HttpServletRequest; |
22 | |
import javax.servlet.http.HttpServletResponse; |
23 | |
|
24 | |
import org.apache.commons.collections.MapUtils; |
25 | |
import org.kuali.rice.krad.service.BusinessObjectService; |
26 | |
import org.kuali.rice.krad.service.KRADServiceLocator; |
27 | |
import org.kuali.rice.krad.web.controller.MaintenanceDocumentController; |
28 | |
import org.kuali.rice.krad.web.form.MaintenanceForm; |
29 | |
import org.kuali.rice.krad.web.form.UifFormBase; |
30 | |
import org.kuali.rice.krms.impl.repository.AgendaBo; |
31 | |
import org.kuali.rice.krms.impl.repository.AgendaItemBo; |
32 | |
import org.kuali.rice.krms.impl.repository.ContextBo; |
33 | |
import org.springframework.stereotype.Controller; |
34 | |
import org.springframework.validation.BindingResult; |
35 | |
import org.springframework.web.bind.annotation.ModelAttribute; |
36 | |
import org.springframework.web.bind.annotation.RequestMapping; |
37 | |
import org.springframework.web.servlet.ModelAndView; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | @Controller |
45 | |
@RequestMapping(value = "/krmsAgendaEditor") |
46 | 0 | public class AgendaEditorController extends MaintenanceDocumentController { |
47 | |
|
48 | |
@Override |
49 | |
public MaintenanceForm createInitialForm(HttpServletRequest request) { |
50 | 0 | return new MaintenanceForm(); |
51 | |
} |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
@RequestMapping(params = "methodToCall=" + "refresh") |
59 | |
@Override |
60 | |
public ModelAndView refresh(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, |
61 | |
HttpServletRequest request, HttpServletResponse response) |
62 | |
throws Exception { |
63 | |
|
64 | 0 | MapUtils.verbosePrint(System.out, "actionParameters", form.getActionParameters()); |
65 | 0 | MapUtils.verbosePrint(System.out, "requestParameters", request.getParameterMap()); |
66 | |
|
67 | 0 | String agendaId = null; |
68 | |
|
69 | 0 | MaintenanceForm maintenanceForm = (MaintenanceForm) form; |
70 | 0 | String conversionFields = maintenanceForm.getActionParameters().get("conversionFields"); |
71 | 0 | String refreshCaller = request.getParameter("refreshCaller"); |
72 | |
|
73 | |
|
74 | |
|
75 | 0 | if (refreshCaller != null && refreshCaller.contains("Agenda") && refreshCaller.contains("LookupView") && |
76 | |
conversionFields != null && |
77 | |
|
78 | |
conversionFields.contains("agenda.id")) { |
79 | 0 | AgendaEditor editorDocument = |
80 | |
((AgendaEditor) maintenanceForm.getDocument().getNewMaintainableObject().getDataObject()); |
81 | 0 | agendaId = editorDocument.getAgenda().getId(); |
82 | 0 | AgendaBo agenda = getBoService().findBySinglePrimaryKey(AgendaBo.class, agendaId); |
83 | 0 | editorDocument.setAgenda(agenda); |
84 | |
|
85 | 0 | if (agenda.getContextId() != null) { |
86 | 0 | ContextBo context = getBoService().findBySinglePrimaryKey(ContextBo.class, agenda.getContextId()); |
87 | 0 | editorDocument.setContext(context); |
88 | |
} |
89 | |
} |
90 | |
|
91 | 0 | return super.refresh(form, result, request, response); |
92 | |
} |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
private AgendaItemInstanceChildAccessor getLastChildsAlwaysAccessor(AgendaItemInstanceChildAccessor instanceAccessor) { |
100 | 0 | AgendaItemBo next = instanceAccessor.getChild(); |
101 | 0 | if (next == null) return instanceAccessor; |
102 | 0 | while (next.getAlways() != null) { next = next.getAlways(); }; |
103 | 0 | return new AgendaItemInstanceChildAccessor(AgendaItemChildAccessor.always, next); |
104 | |
} |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
private AgendaItemInstanceChildAccessor getInstanceAccessorToChild(AgendaItemBo parent, String agendaItemId) { |
111 | |
|
112 | |
|
113 | 0 | for (AgendaItemChildAccessor levelOrderChildAccessor : AgendaItemChildAccessor.children) { |
114 | |
|
115 | 0 | AgendaItemBo next = levelOrderChildAccessor.getChild(parent); |
116 | |
|
117 | |
|
118 | 0 | if (next != null && agendaItemId.equals(next.getId())) return new AgendaItemInstanceChildAccessor(levelOrderChildAccessor, parent); |
119 | |
|
120 | |
|
121 | 0 | while (next != null && next.getAlwaysId() != null) { |
122 | 0 | if (next.getAlwaysId().equals(agendaItemId)) return new AgendaItemInstanceChildAccessor(AgendaItemChildAccessor.always, next); |
123 | |
|
124 | 0 | next = next.getAlways(); |
125 | |
} |
126 | |
} |
127 | |
|
128 | 0 | return null; |
129 | |
} |
130 | |
|
131 | |
@RequestMapping(params = "methodToCall=" + "moveUp") |
132 | |
public ModelAndView moveUp(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, |
133 | |
HttpServletRequest request, HttpServletResponse response) |
134 | |
throws Exception { |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | 0 | AgendaBo agenda = getAgenda(form, request); |
156 | |
|
157 | 0 | AgendaItemBo firstItem = getFirstAgendaItem(agenda); |
158 | |
|
159 | 0 | String selectedItemId = request.getParameter("agenda_item_selected"); |
160 | 0 | AgendaItemBo node = getAgendaItemById(firstItem, selectedItemId); |
161 | 0 | AgendaItemBo parent = getParent(firstItem, selectedItemId); |
162 | 0 | AgendaItemBo parentsOlderCousin = (parent == null) ? null : getNextOldestOfSameGeneration(firstItem, parent.getId()); |
163 | |
|
164 | 0 | AgendaItemChildAccessor childAccessor = getOldestChildAccessor(node, parent); |
165 | 0 | if (childAccessor != null) { |
166 | 0 | if (childAccessor == AgendaItemChildAccessor.whenFalse) { |
167 | |
|
168 | 0 | AgendaItemInstanceChildAccessor youngestWhenTrueSiblingInsertionPoint = |
169 | |
getLastChildsAlwaysAccessor(new AgendaItemInstanceChildAccessor(AgendaItemChildAccessor.whenTrue, parent)); |
170 | 0 | youngestWhenTrueSiblingInsertionPoint.setChild(node); |
171 | 0 | AgendaItemChildAccessor.whenFalse.setChild(parent, node.getAlways()); |
172 | 0 | AgendaItemChildAccessor.always.setChild(node, null); |
173 | |
|
174 | 0 | } else if (parentsOlderCousin != null) { |
175 | |
|
176 | 0 | AgendaItemInstanceChildAccessor youngestWhenFalseSiblingInsertionPoint = |
177 | |
getLastChildsAlwaysAccessor(new AgendaItemInstanceChildAccessor(AgendaItemChildAccessor.whenFalse, parentsOlderCousin)); |
178 | 0 | youngestWhenFalseSiblingInsertionPoint.setChild(node); |
179 | 0 | AgendaItemChildAccessor.whenTrue.setChild(parent, node.getAlways()); |
180 | 0 | AgendaItemChildAccessor.always.setChild(node, null); |
181 | 0 | } |
182 | 0 | } else if (!selectedItemId.equals(firstItem.getId())) { |
183 | |
|
184 | 0 | AgendaItemBo bogusRootNode = null; |
185 | 0 | if (parent == null) { |
186 | |
|
187 | 0 | bogusRootNode = new AgendaItemBo(); |
188 | 0 | AgendaItemChildAccessor.whenTrue.setChild(bogusRootNode, firstItem); |
189 | 0 | parent = bogusRootNode; |
190 | |
} |
191 | |
|
192 | |
|
193 | 0 | AgendaItemInstanceChildAccessor accessorToSelectedNode = getInstanceAccessorToChild(parent, node.getId()); |
194 | 0 | AgendaItemBo olderSibling = accessorToSelectedNode.getInstance(); |
195 | 0 | AgendaItemInstanceChildAccessor accessorToOlderSibling = getInstanceAccessorToChild(parent, olderSibling.getId()); |
196 | |
|
197 | 0 | accessorToOlderSibling.setChild(node); |
198 | 0 | accessorToSelectedNode.setChild(node.getAlways()); |
199 | 0 | AgendaItemChildAccessor.always.setChild(node, olderSibling); |
200 | |
|
201 | 0 | if (bogusRootNode != null) { |
202 | |
|
203 | 0 | agenda.setFirstItemId(bogusRootNode.getWhenTrueId()); |
204 | |
} |
205 | |
} |
206 | |
|
207 | 0 | return super.refresh(form, result, request, response); |
208 | |
} |
209 | |
|
210 | |
@RequestMapping(params = "methodToCall=" + "moveDown") |
211 | |
public ModelAndView moveDown(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, |
212 | |
HttpServletRequest request, HttpServletResponse response) |
213 | |
throws Exception { |
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | 0 | AgendaBo agenda = getAgenda(form, request); |
236 | |
|
237 | 0 | AgendaItemBo firstItem = getFirstAgendaItem(agenda); |
238 | |
|
239 | 0 | String selectedItemId = request.getParameter("agenda_item_selected"); |
240 | 0 | AgendaItemBo node = getAgendaItemById(firstItem, selectedItemId); |
241 | 0 | AgendaItemBo parent = getParent(firstItem, selectedItemId); |
242 | 0 | AgendaItemBo parentsYoungerCousin = (parent == null) ? null : getNextYoungestOfSameGeneration(firstItem, parent.getId()); |
243 | |
|
244 | 0 | if (node.getAlways() == null) { |
245 | |
|
246 | 0 | if (parent.getWhenTrue() != null && isSiblings(parent.getWhenTrue(), node)) { |
247 | |
|
248 | |
|
249 | 0 | AgendaItemInstanceChildAccessor accessorToSelectedNode = getInstanceAccessorToChild(parent, node.getId()); |
250 | 0 | accessorToSelectedNode.setChild(null); |
251 | |
|
252 | 0 | AgendaItemBo parentsFirstChild = parent.getWhenFalse(); |
253 | 0 | AgendaItemChildAccessor.whenFalse.setChild(parent, node); |
254 | 0 | AgendaItemChildAccessor.always.setChild(node, parentsFirstChild); |
255 | 0 | } else if (parentsYoungerCousin != null) { |
256 | |
|
257 | |
|
258 | 0 | AgendaItemInstanceChildAccessor accessorToSelectedNode = getInstanceAccessorToChild(parent, node.getId()); |
259 | 0 | accessorToSelectedNode.setChild(null); |
260 | |
|
261 | 0 | AgendaItemBo parentsYoungerCousinsFirstChild = parentsYoungerCousin.getWhenTrue(); |
262 | 0 | AgendaItemChildAccessor.whenTrue.setChild(parent, node); |
263 | 0 | AgendaItemChildAccessor.always.setChild(node, parentsYoungerCousinsFirstChild); |
264 | 0 | } |
265 | |
} else { |
266 | |
|
267 | 0 | AgendaItemBo bogusRootNode = null; |
268 | 0 | if (parent == null) { |
269 | |
|
270 | 0 | bogusRootNode = new AgendaItemBo(); |
271 | 0 | AgendaItemChildAccessor.whenFalse.setChild(bogusRootNode, firstItem); |
272 | 0 | parent = bogusRootNode; |
273 | |
} |
274 | |
|
275 | |
|
276 | 0 | AgendaItemInstanceChildAccessor accessorToSelectedNode = getInstanceAccessorToChild(parent, node.getId()); |
277 | 0 | AgendaItemBo youngerSibling = node.getAlways(); |
278 | 0 | accessorToSelectedNode.setChild(youngerSibling); |
279 | 0 | AgendaItemChildAccessor.always.setChild(node, youngerSibling.getAlways()); |
280 | 0 | AgendaItemChildAccessor.always.setChild(youngerSibling, node); |
281 | |
|
282 | 0 | if (bogusRootNode != null) { |
283 | |
|
284 | 0 | agenda.setFirstItemId(bogusRootNode.getWhenFalseId()); |
285 | |
} |
286 | |
} |
287 | |
|
288 | 0 | return super.refresh(form, result, request, response); |
289 | |
} |
290 | |
|
291 | |
@RequestMapping(params = "methodToCall=" + "moveLeft") |
292 | |
public ModelAndView moveLeft(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, |
293 | |
HttpServletRequest request, HttpServletResponse response) |
294 | |
throws Exception { |
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | 0 | AgendaBo agenda = getAgenda(form, request); |
302 | |
|
303 | 0 | AgendaItemBo firstItem = getFirstAgendaItem(agenda); |
304 | |
|
305 | 0 | String selectedItemId = request.getParameter("agenda_item_selected"); |
306 | 0 | AgendaItemBo node = getAgendaItemById(firstItem, selectedItemId); |
307 | 0 | AgendaItemBo parent = getParent(firstItem, selectedItemId); |
308 | |
|
309 | 0 | if (parent != null) { |
310 | 0 | AgendaItemInstanceChildAccessor accessorToSelectedNode = getInstanceAccessorToChild(parent, node.getId()); |
311 | 0 | accessorToSelectedNode.setChild(node.getAlways()); |
312 | |
|
313 | 0 | AgendaItemChildAccessor.always.setChild(node, parent.getAlways()); |
314 | 0 | AgendaItemChildAccessor.always.setChild(parent, node); |
315 | |
} |
316 | |
|
317 | 0 | return super.refresh(form, result, request, response); |
318 | |
} |
319 | |
|
320 | |
@RequestMapping(params = "methodToCall=" + "moveRight") |
321 | |
public ModelAndView moveRight(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, |
322 | |
HttpServletRequest request, HttpServletResponse response) |
323 | |
throws Exception { |
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | 0 | AgendaBo agenda = getAgenda(form, request); |
332 | |
|
333 | 0 | AgendaItemBo firstItem = getFirstAgendaItem(agenda); |
334 | |
|
335 | 0 | String selectedItemId = request.getParameter("agenda_item_selected"); |
336 | 0 | AgendaItemBo node = getAgendaItemById(firstItem, selectedItemId); |
337 | 0 | AgendaItemBo parent = getParent(firstItem, selectedItemId); |
338 | |
|
339 | 0 | AgendaItemBo bogusRootNode = null; |
340 | 0 | if (parent == null) { |
341 | |
|
342 | 0 | bogusRootNode = new AgendaItemBo(); |
343 | 0 | AgendaItemChildAccessor.whenFalse.setChild(bogusRootNode, firstItem); |
344 | 0 | parent = bogusRootNode; |
345 | |
} |
346 | |
|
347 | 0 | AgendaItemInstanceChildAccessor accessorToSelectedNode = getInstanceAccessorToChild(parent, node.getId()); |
348 | 0 | AgendaItemBo olderSibling = (accessorToSelectedNode.getInstance() == parent) ? null : accessorToSelectedNode.getInstance(); |
349 | |
|
350 | 0 | if (olderSibling != null) { |
351 | 0 | accessorToSelectedNode.setChild(node.getAlways()); |
352 | 0 | AgendaItemInstanceChildAccessor yougestWhenFalseSiblingInsertionPoint = |
353 | |
getLastChildsAlwaysAccessor(new AgendaItemInstanceChildAccessor(AgendaItemChildAccessor.whenFalse, olderSibling)); |
354 | 0 | yougestWhenFalseSiblingInsertionPoint.setChild(node); |
355 | 0 | AgendaItemChildAccessor.always.setChild(node, null); |
356 | 0 | } else if (node.getAlways() != null) { |
357 | 0 | accessorToSelectedNode.setChild(node.getAlways()); |
358 | 0 | AgendaItemBo childsWhenTrue = node.getAlways().getWhenTrue(); |
359 | 0 | AgendaItemChildAccessor.whenTrue.setChild(node.getAlways(), node); |
360 | 0 | AgendaItemChildAccessor.always.setChild(node, childsWhenTrue); |
361 | |
} |
362 | |
|
363 | 0 | if (bogusRootNode != null) { |
364 | |
|
365 | 0 | agenda.setFirstItemId(bogusRootNode.getWhenFalseId()); |
366 | |
} |
367 | |
|
368 | 0 | return super.refresh(form, result, request, response); |
369 | |
} |
370 | |
|
371 | |
|
372 | |
|
373 | |
private boolean isSiblings(AgendaItemBo cousin1, AgendaItemBo cousin2) { |
374 | 0 | if (cousin1.equals(cousin2)) return true; |
375 | |
|
376 | |
|
377 | 0 | AgendaItemBo candidate = cousin2; |
378 | 0 | while (null != (candidate = candidate.getAlways())) { |
379 | 0 | if (candidate.equals(cousin1)) return true; |
380 | |
} |
381 | |
|
382 | 0 | candidate = cousin1; |
383 | 0 | while (null != (candidate = candidate.getAlways())) { |
384 | 0 | if (candidate.equals(cousin2)) return true; |
385 | |
} |
386 | 0 | return false; |
387 | |
} |
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | |
|
394 | |
private AgendaItemChildAccessor getOldestChildAccessor( |
395 | |
AgendaItemBo child, AgendaItemBo parent) { |
396 | 0 | AgendaItemChildAccessor levelOrderChildAccessor = null; |
397 | |
|
398 | 0 | if (parent != null) { |
399 | 0 | for (AgendaItemChildAccessor childAccessor : AgendaItemChildAccessor.children) { |
400 | 0 | if (child.equals(childAccessor.getChild(parent))) { |
401 | 0 | levelOrderChildAccessor = childAccessor; |
402 | 0 | break; |
403 | |
} |
404 | |
} |
405 | |
} |
406 | 0 | return levelOrderChildAccessor; |
407 | |
} |
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
private AgendaItemBo getFirstAgendaItem(AgendaBo agenda) { |
416 | 0 | AgendaItemBo firstItem = null; |
417 | 0 | for (AgendaItemBo agendaItem : agenda.getItems()) { |
418 | 0 | if (agenda.getFirstItemId().equals(agendaItem.getId())) { |
419 | 0 | firstItem = agendaItem; |
420 | 0 | break; |
421 | |
} |
422 | |
} |
423 | 0 | return firstItem; |
424 | |
} |
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
private AgendaItemBo getNextYoungestOfSameGeneration(AgendaItemBo root, String agendaItemId) { |
431 | 0 | AgendaItemBo result = getNextYoungestOfSameGenerationHelper(root, 0, new ArrayList<AgendaItemBo>(), agendaItemId); |
432 | 0 | if (result == NULL_AGENDA_ITEM) result = null; |
433 | 0 | return result; |
434 | |
} |
435 | |
|
436 | |
private AgendaItemBo getNextYoungestOfSameGenerationHelper(AgendaItemBo node, int level, ArrayList<AgendaItemBo> youngerCousinByLevel, String agendaItemId) { |
437 | 0 | AgendaItemBo result = null; |
438 | 0 | if (agendaItemId.equals(node.getId())) { |
439 | 0 | result = (youngerCousinByLevel.size() > level) ? youngerCousinByLevel.get(level) : NULL_AGENDA_ITEM; |
440 | |
} else { |
441 | 0 | for (int i=AgendaItemChildAccessor.linkedNodes.length; i >= 0; --i) { |
442 | 0 | AgendaItemChildAccessor childAccessor = AgendaItemChildAccessor.linkedNodes[i]; |
443 | 0 | AgendaItemBo child = childAccessor.getChild(node); |
444 | 0 | if (child != null) { |
445 | 0 | int levelNext = level; |
446 | |
|
447 | |
|
448 | 0 | if (childAccessor == AgendaItemChildAccessor.always) { |
449 | |
|
450 | 0 | while (youngerCousinByLevel.size() <= level) youngerCousinByLevel.add(null); |
451 | 0 | youngerCousinByLevel.set(level, node); |
452 | |
} else { |
453 | 0 | levelNext = level +1; |
454 | |
} |
455 | 0 | result = getNextYoungestOfSameGenerationHelper(child, levelNext, youngerCousinByLevel, agendaItemId); |
456 | 0 | if (result != null) break; |
457 | |
} |
458 | |
} |
459 | |
} |
460 | 0 | return result; |
461 | |
} |
462 | |
|
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
private AgendaItemBo getNextOldestOfSameGeneration(AgendaItemBo root, String agendaItemId) { |
468 | 0 | AgendaItemBo result = getNextOldestOfSameGenerationHelper(root, 0, new ArrayList<AgendaItemBo>(), agendaItemId); |
469 | 0 | if (result == NULL_AGENDA_ITEM) result = null; |
470 | 0 | return result; |
471 | |
} |
472 | |
|
473 | |
|
474 | 0 | private static final AgendaItemBo NULL_AGENDA_ITEM = new AgendaItemBo(); |
475 | |
|
476 | |
private AgendaItemBo getNextOldestOfSameGenerationHelper(AgendaItemBo node, int level, ArrayList<AgendaItemBo> olderCousinByLevel, String agendaItemId) { |
477 | 0 | AgendaItemBo result = null; |
478 | 0 | if (agendaItemId.equals(node.getId())) { |
479 | 0 | result = (olderCousinByLevel.size() > level) ? olderCousinByLevel.get(level) : NULL_AGENDA_ITEM; |
480 | |
} else { |
481 | 0 | for (AgendaItemChildAccessor childAccessor : AgendaItemChildAccessor.linkedNodes) { |
482 | 0 | AgendaItemBo child = childAccessor.getChild(node); |
483 | 0 | if (child != null) { |
484 | 0 | int levelNext = level; |
485 | |
|
486 | |
|
487 | 0 | if (childAccessor == AgendaItemChildAccessor.always) { |
488 | |
|
489 | 0 | while (olderCousinByLevel.size() <= level) olderCousinByLevel.add(null); |
490 | 0 | olderCousinByLevel.set(level, node); |
491 | |
} else { |
492 | 0 | levelNext = level +1; |
493 | |
} |
494 | 0 | result = getNextOldestOfSameGenerationHelper(child, levelNext, olderCousinByLevel, agendaItemId); |
495 | 0 | if (result != null) break; |
496 | |
} |
497 | |
} |
498 | |
} |
499 | 0 | return result; |
500 | |
} |
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | |
private AgendaItemBo getParent(AgendaItemBo root, String agendaItemId) { |
506 | 0 | return getParentHelper(root, null, agendaItemId); |
507 | |
} |
508 | |
|
509 | |
private AgendaItemBo getParentHelper(AgendaItemBo node, AgendaItemBo levelOrderParent, String agendaItemId) { |
510 | 0 | AgendaItemBo result = null; |
511 | 0 | if (agendaItemId.equals(node.getId())) { |
512 | 0 | result = levelOrderParent; |
513 | |
} else { |
514 | 0 | for (AgendaItemChildAccessor childAccessor : AgendaItemChildAccessor.linkedNodes) { |
515 | 0 | AgendaItemBo child = childAccessor.getChild(node); |
516 | 0 | if (child != null) { |
517 | |
|
518 | 0 | AgendaItemBo lop = (childAccessor == AgendaItemChildAccessor.always) ? levelOrderParent : node; |
519 | 0 | result = getParentHelper(child, lop, agendaItemId); |
520 | 0 | if (result != null) break; |
521 | |
} |
522 | |
} |
523 | |
} |
524 | 0 | return result; |
525 | |
} |
526 | |
|
527 | |
|
528 | |
|
529 | |
|
530 | |
private AgendaItemBo getAgendaItemById(AgendaItemBo node, String agendaItemId) { |
531 | 0 | if (node == null) throw new IllegalArgumentException("node must be non-null"); |
532 | |
|
533 | 0 | AgendaItemBo result = null; |
534 | |
|
535 | 0 | if (agendaItemId.equals(node.getId())) { |
536 | 0 | result = node; |
537 | |
} else { |
538 | 0 | for (AgendaItemChildAccessor childAccessor : AgendaItemChildAccessor.linkedNodes) { |
539 | 0 | AgendaItemBo child = childAccessor.getChild(node); |
540 | 0 | if (child != null) { |
541 | 0 | result = getAgendaItemById(child, agendaItemId); |
542 | 0 | if (result != null) break; |
543 | |
} |
544 | |
} |
545 | |
} |
546 | 0 | return result; |
547 | |
} |
548 | |
|
549 | |
|
550 | |
|
551 | |
|
552 | |
|
553 | |
|
554 | |
|
555 | |
|
556 | |
private AgendaBo getAgenda(UifFormBase form, HttpServletRequest request) { |
557 | 0 | MaintenanceForm maintenanceForm = (MaintenanceForm) form; |
558 | 0 | AgendaEditor editorDocument = ((AgendaEditor)maintenanceForm.getDocument().getDocumentDataObject()); |
559 | 0 | AgendaBo agenda = editorDocument.getAgenda(); |
560 | 0 | return agenda; |
561 | |
} |
562 | |
|
563 | |
private void treeToInOrderList(AgendaItemBo agendaItem, List<AgendaItemBo> listToBuild) { |
564 | 0 | listToBuild.add(agendaItem); |
565 | 0 | for (AgendaItemChildAccessor childAccessor : AgendaItemChildAccessor.linkedNodes) { |
566 | 0 | AgendaItemBo child = childAccessor.getChild(agendaItem); |
567 | 0 | if (child != null) treeToInOrderList(child, listToBuild); |
568 | |
} |
569 | 0 | } |
570 | |
|
571 | |
|
572 | |
@RequestMapping(params = "methodToCall=" + "delete") |
573 | |
public ModelAndView delete(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, |
574 | |
HttpServletRequest request, HttpServletResponse response) |
575 | |
throws Exception { |
576 | |
|
577 | 0 | AgendaBo agenda = getAgenda(form, request); |
578 | 0 | AgendaItemBo firstItem = getFirstAgendaItem(agenda); |
579 | |
|
580 | 0 | String agendaItemSelected = request.getParameter("agenda_item_selected"); |
581 | |
|
582 | 0 | if (firstItem != null) { |
583 | |
|
584 | 0 | if (agendaItemSelected.equals(firstItem.getAgendaId())) { |
585 | 0 | agenda.setFirstItemId(firstItem.getAlwaysId()); |
586 | |
} else { |
587 | 0 | deleteAgendaItem(firstItem, agendaItemSelected); |
588 | |
} |
589 | |
} |
590 | |
|
591 | 0 | return super.refresh(form, result, request, response); |
592 | |
} |
593 | |
|
594 | |
|
595 | |
private void deleteAgendaItem(AgendaItemBo root, String agendaItemIdToDelete) { |
596 | 0 | if (deleteAgendaItem(root, AgendaItemChildAccessor.whenTrue, agendaItemIdToDelete) || |
597 | |
deleteAgendaItem(root, AgendaItemChildAccessor.whenFalse, agendaItemIdToDelete) || |
598 | |
deleteAgendaItem(root, AgendaItemChildAccessor.always, agendaItemIdToDelete)); |
599 | 0 | } |
600 | |
|
601 | |
private boolean deleteAgendaItem(AgendaItemBo agendaItem, AgendaItemChildAccessor childAccessor, String agendaItemIdToDelete) { |
602 | 0 | if (agendaItem == null || childAccessor.getChild(agendaItem) == null) return false; |
603 | 0 | if (agendaItemIdToDelete.equals(childAccessor.getChild(agendaItem).getId())) { |
604 | |
|
605 | 0 | AgendaItemBo grandchildToKeep = childAccessor.getChild(agendaItem).getAlways(); |
606 | 0 | childAccessor.setChild(agendaItem, grandchildToKeep); |
607 | 0 | return true; |
608 | |
} else { |
609 | 0 | AgendaItemBo child = childAccessor.getChild(agendaItem); |
610 | |
|
611 | 0 | for (AgendaItemChildAccessor nextChildAccessor : AgendaItemChildAccessor.linkedNodes) { |
612 | 0 | if (deleteAgendaItem(child, nextChildAccessor, agendaItemIdToDelete)) return true; |
613 | |
} |
614 | |
} |
615 | 0 | return false; |
616 | |
} |
617 | |
|
618 | |
|
619 | |
private BusinessObjectService getBoService() { |
620 | 0 | return KRADServiceLocator.getBusinessObjectService(); |
621 | |
} |
622 | |
|
623 | |
|
624 | |
|
625 | |
|
626 | |
private static class AgendaItemInstanceChildAccessor { |
627 | |
|
628 | |
private final AgendaItemChildAccessor accessor; |
629 | |
private final AgendaItemBo instance; |
630 | |
|
631 | 0 | public AgendaItemInstanceChildAccessor(AgendaItemChildAccessor accessor, AgendaItemBo instance) { |
632 | 0 | this.accessor = accessor; |
633 | 0 | this.instance = instance; |
634 | 0 | } |
635 | |
|
636 | |
public void setChild(AgendaItemBo child) { |
637 | 0 | accessor.setChild(instance, child); |
638 | 0 | } |
639 | |
|
640 | |
public AgendaItemBo getChild() { |
641 | 0 | return accessor.getChild(instance); |
642 | |
} |
643 | |
|
644 | 0 | public AgendaItemBo getInstance() { return instance; } |
645 | |
} |
646 | |
|
647 | |
|
648 | |
|
649 | |
|
650 | |
|
651 | 0 | private static class AgendaItemChildAccessor { |
652 | |
|
653 | 0 | private enum Child { WHEN_TRUE, WHEN_FALSE, ALWAYS }; |
654 | |
|
655 | 0 | private static final AgendaItemChildAccessor whenTrue = new AgendaItemChildAccessor(Child.WHEN_TRUE); |
656 | 0 | private static final AgendaItemChildAccessor whenFalse = new AgendaItemChildAccessor(Child.WHEN_FALSE); |
657 | 0 | private static final AgendaItemChildAccessor always = new AgendaItemChildAccessor(Child.ALWAYS); |
658 | |
|
659 | |
|
660 | |
|
661 | |
|
662 | 0 | private static final AgendaItemChildAccessor [] linkedNodes = { whenTrue, whenFalse, always }; |
663 | |
|
664 | |
|
665 | |
|
666 | |
|
667 | 0 | private static final AgendaItemChildAccessor [] children = { whenTrue, whenFalse }; |
668 | |
|
669 | |
private final Child whichChild; |
670 | |
|
671 | 0 | private AgendaItemChildAccessor(Child whichChild) { |
672 | 0 | if (whichChild == null) throw new IllegalArgumentException("whichChild must be non-null"); |
673 | 0 | this.whichChild = whichChild; |
674 | 0 | } |
675 | |
|
676 | |
|
677 | |
|
678 | |
|
679 | |
public AgendaItemBo getChild(AgendaItemBo parent) { |
680 | 0 | switch (whichChild) { |
681 | 0 | case WHEN_TRUE: return parent.getWhenTrue(); |
682 | 0 | case WHEN_FALSE: return parent.getWhenFalse(); |
683 | 0 | case ALWAYS: return parent.getAlways(); |
684 | 0 | default: throw new IllegalStateException(); |
685 | |
} |
686 | |
} |
687 | |
|
688 | |
|
689 | |
|
690 | |
|
691 | |
public void setChild(AgendaItemBo parent, AgendaItemBo child) { |
692 | 0 | switch (whichChild) { |
693 | |
case WHEN_TRUE: |
694 | 0 | parent.setWhenTrue(child); |
695 | 0 | parent.setWhenTrueId(child == null ? null : child.getId()); |
696 | 0 | break; |
697 | |
case WHEN_FALSE: |
698 | 0 | parent.setWhenFalse(child); |
699 | 0 | parent.setWhenFalseId(child == null ? null : child.getId()); |
700 | 0 | break; |
701 | |
case ALWAYS: |
702 | 0 | parent.setAlways(child); |
703 | 0 | parent.setAlwaysId(child == null ? null : child.getId()); |
704 | 0 | break; |
705 | 0 | default: throw new IllegalStateException(); |
706 | |
} |
707 | 0 | } |
708 | |
} |
709 | |
|
710 | |
} |