1 package com.sri.emo.wizard.selection;
2
3 import com.jcorporate.expresso.core.db.DBException;
4 import com.sri.emo.controller.DecisionMatrix;
5 import com.sri.emo.dbobj.Node;
6 import com.sri.emo.dbobj.WizDecisionSet;
7 import com.sri.emo.wizard.PageMetadata;
8 import com.sri.emo.wizard.Wizard;
9 import com.sri.emo.wizard.WizardException;
10 import com.sri.emo.wizard.WizardPage;
11 import com.sri.emo.wizard.defaults.EmoWizardPage;
12
13 import java.io.Serializable;
14 import java.lang.ref.WeakReference;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19
20 /***
21 * Page that displays the selected node and the choices that lead to that choice.
22 *
23 * @author Michael Rimov
24 */
25 public class DisplaySummaryPage extends EmoWizardPage {
26
27 /***
28 *
29 */
30 private static final long serialVersionUID = 1L;
31
32
33 /***
34 * The wizard owner -- must be set by the factory after the wizard
35 * is created.
36 */
37 private Wizard myOwner;
38
39
40 /***
41 * ID of the selected node.
42 */
43 transient private WeakReference cachedSelectedNodeId = null;
44
45 /***
46 * My own page id.
47 */
48 public static final Integer PAGE_ID = new Integer(-2);
49
50 /***
51 * Constructor for the summary page.
52 *
53 * @param pageMetadata PageMetadata the metadata associated with this
54 * page.
55 */
56 public DisplaySummaryPage(PageMetadata pageMetadata) {
57 super(PAGE_ID, pageMetadata, null);
58 }
59
60
61 /***
62 * Clear before redisplaying the page.
63 */
64 public synchronized void clearCachedNode() {
65 cachedSelectedNodeId = null;
66 }
67
68 protected String getCachedNodeId() {
69
70 if (cachedSelectedNodeId == null) {
71 return null;
72 } else {
73 return (String) cachedSelectedNodeId.get();
74 }
75 }
76
77 protected void setCachedNodeId(String newValue) {
78 cachedSelectedNodeId = new WeakReference(newValue);
79 }
80
81
82 /***
83 * Retrive all selection results up to this given page.
84 *
85 * @return SelectionResults[] array of selection results.
86 * @throws WizardException
87 */
88 public SelectionResults[] getSelectionResults() throws WizardException {
89
90 List stepHistory = this.getMyOwner().getStepHistory();
91 List selectionResults = new ArrayList();
92
93 for (int i = 0; i < stepHistory.size(); i++) {
94 WizardPage wizPage = (WizardPage) stepHistory.get(i);
95 assert wizPage != null:"Hit Null Wizard Page for Step index: " + i;
96
97 if (wizPage.getMetadata().isHasEntry()) {
98 selectionResults.add(new SelectionResults(i + 1, wizPage.getMetadata().getTitle(),
99 wizPage.getPrintableData()));
100 }
101 }
102
103 return (SelectionResults[]) selectionResults.toArray(new SelectionResults[selectionResults.size()]);
104 }
105
106 /***
107 * Runs the previous step history through the Decision Matrix to
108 *
109 * @return Node
110 * @throws WizardException
111 */
112 public synchronized Node getSelectedNode() throws WizardException {
113
114 try {
115
116
117 String cachedId = getCachedNodeId();
118 if (cachedId != null) {
119 Node n = new Node();
120 n.setNodeId(cachedId);
121 n.retrieve();
122 return n;
123 }
124
125 DecisionMatrix decisionMatrix = new DecisionMatrix(((Integer) getMyOwner().getId()).intValue());
126
127 WizardPage pages[] = (WizardPage[]) getMyOwner().getStepHistory().toArray(new WizardPage[myOwner.
128 getStepHistory().size()]);
129 Map decisionRow = new HashMap(pages.length);
130 for (int i = 0; i < pages.length; i++) {
131
132
133 if (pages[i].getMetadata().isHasEntry() && pages[i].getMenu() != null && pages[i].getMenu().length > 0)
134 {
135 decisionRow.put(pages[i].getId(), new Integer((String) pages[i].getData()));
136 }
137 }
138
139 WizDecisionSet decision = decisionMatrix.getDecision(decisionRow);
140 if (decision != null) {
141 Node selectedNode = decision.getDecisionNode();
142
143 this.setCachedNodeId(selectedNode.getNodeId());
144 return selectedNode;
145 } else {
146 return null;
147 }
148 } catch (DBException ex) {
149 throw new WizardException("Error processing template matching.", ex);
150 }
151
152 }
153
154 public synchronized Wizard getMyOwner() {
155 assert myOwner != null:"Parent Wizard Should have been set by wizard selection factory.";
156 return myOwner;
157 }
158
159 public synchronized void setMyOwner(Wizard myOwner) {
160 this.myOwner = myOwner;
161 }
162
163
164 /***
165 * Record Structure that provides a list of step names and their
166 * appropriate values.
167 *
168 * @author Michael Rimov
169 * @version 1.0
170 */
171 public static class SelectionResults {
172 private final String stepName;
173 private final String printableValue;
174 private final int pageNumber;
175
176 public SelectionResults(int wizardPageNumber, final String stepName, final String printableValue) {
177 this.stepName = stepName;
178 this.printableValue = printableValue;
179 pageNumber = wizardPageNumber;
180 }
181
182 public String getStepName() {
183 return stepName;
184 }
185
186 public String getStepValue() {
187 return printableValue;
188 }
189
190 public int getPageNumber() {
191 return pageNumber;
192 }
193
194 }
195
196
197 /***
198 * Retrieve the data associated with the page.
199 *
200 * @return the data instance
201 */
202 public Serializable getData() {
203 try {
204 if (getSelectedNode() == null) {
205 return null;
206 }
207 return this.getSelectedNode().getNodeId();
208 } catch (DBException ex) {
209 return new WizardException("Error querying selected node", ex);
210 }
211 }
212
213 ;
214 }