1 package com.sri.emo.wizard.completion;
2
3 import com.jcorporate.expresso.core.db.DBException;
4 import com.sri.emo.dbobj.Node;
5 import com.sri.emo.wizard.PageMetadata;
6 import com.sri.emo.wizard.ValidValueWithComment;
7 import com.sri.emo.wizard.WizardException;
8 import com.sri.emo.wizard.expressoimpl.WizardController;
9
10 import java.io.Serializable;
11 import java.util.*;
12
13 /***
14 * Wizard page that handles the selection of various relations available.
15 *
16 * @author Michael Rimov
17 * @version 1.0
18 */
19 public class RelationWizardPage extends CompletionPartsPage {
20
21 /***
22 *
23 */
24 private static final long serialVersionUID = 1L;
25
26 /***
27 * The prefix of all ids that this class culls from the parameters. They are
28 * of the form of WIZ_DATA_ID + "_" + SomeNumber
29 */
30 public static final String PARAM_PREFIX = WizardController.WIZ_DATA_ID;
31
32 /***
33 * The total number of available nodes.
34 */
35 private int numAvailableRelations = -1;
36
37 /***
38 * Standard constructor for a wizard page.
39 *
40 * @param newId Integer
41 * @param pageMetadata PageMetadata
42 */
43 public RelationWizardPage(Integer newId, PageMetadata pageMetadata) {
44 super(newId, pageMetadata);
45
46 assert pageMetadata instanceof RelationPageMetadata
47 : "Completion Wizard Relation Page must have metadata of type 'RelationPageMetadata";
48 }
49
50 /***
51 * Intercepts the setData call (which is a Map), grabs the parameters
52 * we're interested in, and calls the super.setData() with a Set of all
53 * relations that are listed as 'checked'.
54 *
55 * @param newData the new data for the page. (Should be a map)
56 * @throws WizardException upon error.
57 */
58 public void setData(final Serializable newData) throws WizardException {
59
60 assert getNumAvailableRelations() != -1 : "Number of available relations must be initialized";
61
62 if (! (newData instanceof Map)) {
63 throw new IllegalArgumentException(
64 "Internal Error: Incoming data was: " + newData.getClass().getName() + " and we needed a java.util.Map");
65 }
66
67 HashSet prunedData = null;
68 if (newData == null) {
69 prunedData = new HashSet();
70 } else {
71 final Map givenData = (Map) newData;
72 final String searchPrefix = PARAM_PREFIX + "_";
73
74 prunedData = new HashSet(givenData.size());
75 for (Iterator i = givenData.keySet().iterator(); i.hasNext();) {
76 String oneKey = (String) i.next();
77 if (oneKey.startsWith(searchPrefix)) {
78 prunedData.add(givenData.get(oneKey));
79 }
80 }
81
82 RelationPageMetadata metadata = getRelationPageMetadata();
83
84 if (metadata.getMaxEntries() != null
85 && metadata.getMaxEntries().intValue() < prunedData.size()) {
86 this.addError("Please only choose a maximum of: " + metadata.getMaxEntries().toString() + " choices");
87 }
88 }
89
90 super.setData(prunedData);
91 }
92
93
94 public Map getSelectedRelations() throws WizardException {
95 Set relationKeys = (Set) this.getData();
96 if (relationKeys == null) {
97 return null;
98 }
99
100
101 Map returnValue = new HashMap();
102 try {
103 for (Iterator i = relationKeys.iterator(); i.hasNext();) {
104 String oneKey = (String) i.next();
105 Node n = new Node();
106 n.setNodeId(oneKey);
107 n.retrieve();
108 returnValue.put(oneKey, n);
109 }
110 } catch (DBException ex) {
111 throw new WizardException("Error mapping node ids to actual nodes.", ex);
112 }
113
114
115 return returnValue;
116 }
117
118 /***
119 * Retrieve the menu.
120 *
121 * @return ValidValueWithComment[]
122 * @throws DBException
123 */
124 public ValidValueWithComment[] getRelatedNodesMenu() throws DBException {
125
126 RelationPageMetadata metadata = (RelationPageMetadata) this.getMetadata();
127
128
129
130 Node n = new Node();
131 n.setNodeId(metadata.getNodeId());
132 n.retrieve();
133 Node[] relatedNodes = n.getRelatedNodes(metadata.getPartNodeRelation(), metadata.getNodeType());
134 ValidValueWithComment[] selectionMenu = new ValidValueWithComment[relatedNodes.length];
135
136 numAvailableRelations = relatedNodes.length;
137
138 Set checkedData = (Set) this.getData();
139 if (checkedData == null) {
140
141
142 checkedData = new HashSet();
143 }
144
145 for (int relatedIndex = 0; relatedIndex < relatedNodes.length; relatedIndex++) {
146
147 selectionMenu[relatedIndex] = new ValidValueWithComment(relatedNodes[relatedIndex].
148 getNodeId(), relatedNodes[relatedIndex].getNodeTitle(),
149 relatedNodes[relatedIndex].getNodeAnnotationRaw());
150
151 if (checkedData.contains(selectionMenu[relatedIndex].getKey())) {
152 selectionMenu[relatedIndex].setSelected(true);
153 } else {
154 selectionMenu[relatedIndex].setSelected(false);
155 }
156 }
157
158
159 return selectionMenu;
160 }
161
162
163 /***
164 * Type safe page getter.
165 *
166 * @return RelationPageMetadata
167 */
168 protected RelationPageMetadata getRelationPageMetadata() {
169 return (RelationPageMetadata) this.getMetadata();
170 }
171
172 public void setNumAvailableRelations(int numAvailableRelations) {
173 this.numAvailableRelations = numAvailableRelations;
174 }
175
176 public int getNumAvailableRelations() {
177 return numAvailableRelations;
178 }
179 }