View Javadoc

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