View Javadoc

1   package com.sri.emo.wizard.creation;
2   
3   import com.jcorporate.expresso.core.controller.Input;
4   import com.jcorporate.expresso.core.dbobj.ValidValue;
5   import com.jcorporate.expresso.core.misc.StringUtil;
6   import com.sri.emo.wizard.PageMetadata;
7   import com.sri.emo.wizard.WizardException;
8   import com.sri.emo.wizard.defaults.EmoWizardPage;
9   import com.sri.emo.wizard.expressoimpl.WizardController;
10  
11  import java.io.Serializable;
12  import java.util.ArrayList;
13  import java.util.HashMap;
14  import java.util.Iterator;
15  import java.util.List;
16  import java.util.Map;
17  
18  /***
19   * Custom Part Handler Page.
20   *
21   * @author Michael Rimov
22   */
23  public class CustomPartHandlerPage extends EmoWizardPage {
24      public static final String INVALID_MARKER = "INVALID_MARKER";
25  
26      /***
27       * Constructor that builds the part handler page.
28       *
29       * @param newId        Integer
30       * @param pageMetadata PageMetadata
31       */
32      public CustomPartHandlerPage(Integer newId, PageMetadata pageMetadata) {
33          super(newId, pageMetadata, null);
34      }
35  
36      /***
37       * Sets the data associated with the page.
38       * <p/>
39       * intercepting values from form after user clicks 'next'
40       * <p/>
41       * <p/>
42       * Validate, calling
43       * super.addError("Error Message");
44       * for each failed criteria.  Sequential wizard logic will automagically
45       * detect errors and revert to original page.
46       *
47       * @param newData Is a Map of all Parameters passed into the wizard
48       *                by <tt>CompletionWizardAction</tt>
49       * @throws WizardException upon error.
50       */
51      public void setData(Serializable newData) throws WizardException {
52  
53          final Map givenData = (Map) newData;
54          final String searchPrefix = WizardController.WIZ_DATA_ID + "_";
55          CustomPartHandlerMetadata metadata = (CustomPartHandlerMetadata) getMetadata();
56  
57          // we will have a hash map of valid values only
58          HashMap prunedData = new HashMap(givenData.size());
59          for (Iterator i = givenData.keySet().iterator(); i.hasNext();) {
60              String oneKey = (String) i.next();
61              if (oneKey.startsWith(searchPrefix)) {
62                  String val = (String) givenData.get(oneKey);
63  
64                  // get key without prefix
65                  String realKey = oneKey.substring(searchPrefix.length());
66                  try {
67  
68                      // validation will fail for blank values
69                      // todo what if user enters an empty string to *erase* a value?
70                      if (metadata.getCustomHandler().validate(realKey, val)) {
71                          // only valid data
72                          prunedData.put(realKey, val);
73                      }
74                  } catch (Exception e) {
75                      throw new WizardException(e);
76                  }
77  
78              }
79          }
80  
81          // get input list from metadata
82          List inputList = metadata.getInputList();
83  
84          // convert inputlist to map
85  //        Map map = new HashMap(inputList.size());
86  //        for (Iterator iterator = inputList.iterator(); iterator.hasNext();) {
87  //            Input input = (Input) iterator.next();
88  //            map.put(input.getName(), input);
89  //        }
90  
91          ArrayList reviewListForOutputOnly = new ArrayList(inputList.size());
92  
93          // save values AND setup final confirmation page
94          for (Iterator iterator = inputList.iterator(); iterator.hasNext();) {
95              Input input = (Input) iterator.next();
96  
97              String key = input.getName();
98  
99              String entryVal = (String) prunedData.get(key);
100             if (entryVal != null) {
101                 input.setDefaultValue(entryVal);
102             }
103 
104             // need to know if input needs translation from defaultValue
105             String str = input.getLabel() + ": ";
106             String defaultValue = input.getDefaultValue();
107             if (!StringUtil.isBlankOrNull(defaultValue)) {
108                 if (input.getType().equals(Input.ATTRIBUTE_DROPDOWN)) {
109                     // translate defaultvalue into display
110                     List vvs = input.getValidValuesList();
111                     for (Iterator iterator1 = vvs.iterator(); iterator1.hasNext();) {
112                         ValidValue validValue = (ValidValue) iterator1.next();
113                         if (validValue.getKey().equals(defaultValue)) {
114                             str += validValue.getDescription();
115                             break;
116                         }
117                     }
118                 } else {
119                     str += input.getDefaultValue();
120                 }
121             }
122 
123             if (entryVal == null) {
124                 str += " <== EMPTY";
125             }
126 
127             // save display info in page for output in final confirmation page
128             reviewListForOutputOnly.add(str);
129         }
130 
131         super.setData(reviewListForOutputOnly);
132     }
133 }