1 package com.sri.emo.wizard.selection;
2
3 import com.jcorporate.expresso.core.controller.*;
4 import com.jcorporate.expresso.core.db.DBException;
5 import com.jcorporate.expresso.core.db.exception.DBRecordNotFoundException;
6 import com.sri.emo.controller.AddNodeAction;
7 import com.sri.emo.controller.NodeCloner;
8 import com.sri.emo.dbobj.Node;
9 import com.sri.emo.wizard.WizardException;
10 import com.sri.emo.wizard.WizardMonitor;
11 import com.sri.emo.wizard.WizardPage;
12 import com.sri.emo.wizard.completion.CompletionWizardSelector;
13
14 import java.io.Serializable;
15 import java.util.Map;
16
17 /***
18 * The chained selection wizard clones nodes like the selection wizard does. However,
19 * Instead of redirecting towards editing
20 *
21 * @author Michael Rimov
22 * @version 1.0
23 */
24 public class ChainedSelectionWizard extends EmoSelectionWizard {
25
26
27 /***
28 *
29 */
30 private static final long serialVersionUID = 1L;
31
32
33 /***
34 * Constructs a chained selection wizard.
35 *
36 * @param wizMonitor WizardMonitor
37 * @param steps WizardPage[]
38 */
39 public ChainedSelectionWizard(WizardMonitor wizMonitor, WizardPage[] steps) {
40 super(wizMonitor, steps);
41 }
42
43
44 /***
45 * Overrides the normal EmoSelectionWizard processing by cloning the node myself
46 * and then either redirecting to an appropriate completion wizard and
47 *
48 * @param src WizardPage the source of the event.
49 * @param data Serializable the data entered in the previous wizard page.
50 * @param additionalParams Map this wizard expects ExpressoRequest and ExpressoResponse
51 * to be loaded into the additional params.
52 * @return Object Actaully ignored. Redirects are actually issued by this
53 * function or a return to the display page is issued if there were errors.
54 * @throws WizardException for any sort of error.
55 */
56 public Object processFinish(final WizardPage src, final Serializable data, final Map additionalParams) throws
57 WizardException {
58
59 Map allData = this.getAllData();
60 String nodeId = (String) allData.get(DisplaySummaryPage.PAGE_ID);
61 try {
62 if (src.getPageErrors() == null || src.getPageErrors().getErrorCount() == 0) {
63
64 Node sourceNode = new Node();
65 sourceNode.setNodeId(nodeId);
66 sourceNode.retrieve();
67 String nameId = (String) data;
68 ExpressoRequest request = (ExpressoRequest) additionalParams.get("request");
69 ExpressoResponse response = (ExpressoResponse) additionalParams.get("response");
70 NodeCloner cloner = new NodeCloner(sourceNode, nameId);
71 ErrorCollection ec = new ErrorCollection();
72 Node result = cloner.cloneAsOrphan(ec);
73
74
75 if (ec.getErrorCount() > 0) {
76 String[] allErrors = ec.getErrorStrings();
77 for (int i = 0; i < allErrors.length; i++) {
78 src.addError(allErrors[i]);
79 }
80 } else {
81
82
83
84 CompletionWizardSelector selector = new CompletionWizardSelector();
85 Transition completionWizardTransition = selector.locateCompletionWizardForNode(nodeId,
86 result.getNodeId());
87
88
89 if (completionWizardTransition != null) {
90
91 completionWizardTransition.redirectTransition(request, response);
92
93
94 return null;
95 }
96
97
98 Transition view = AddNodeAction.getViewTrans(result.getNodeId());
99
100 view.redirectTransition(request, response);
101 }
102 return result;
103 } else {
104
105 return null;
106 }
107 } catch (DBRecordNotFoundException ex) {
108 src.addError(
109 "Error: Could no longer find Template of id: " + nodeId + " perhaps someone else has already deleted it?");
110 return null;
111 } catch (DBException ex) {
112 throw new WizardException("Error locating node", ex);
113 } catch (ControllerException ex) {
114 throw new WizardException(
115 "Template was successfully created, however we were unable to start the completion wizard for some reason.",
116 ex);
117 }
118 }
119 }