1   package com.sri.emo.wizard.defaults;
2   
3   /* ===================================================================
4    * Copyright 2002-04 SRI International.
5    * Released under the MOZILLA PUBLIC LICENSE Version 1.1
6    * which can be obtained at http://www.mozilla.org/MPL/MPL-1.1.html
7    *
8    * This program is distributed in the hope that it will be useful, but
9    * WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11   * See the MOZILLA PUBLIC LICENSE for more details.
12   * =================================================================== */
13  
14  
15  import com.jcorporate.expresso.core.db.DBConnection;
16  import com.jcorporate.expresso.core.db.DBConnectionPool;
17  import com.jcorporate.expresso.core.db.DBException;
18  import com.sri.emo.dbobj.WizDefinition;
19  import com.sri.emo.dbobj.WizStep;
20  
21  /***
22   * Class that represents data for the wizards we test. Used in unit testing
23   * only.
24   *
25   * @author Michael Rimov
26   */
27  
28  public class WizardTestModel {
29  
30      public static final String WIZ_TITLE = "Test Wizard";
31  
32      public static final String WIZ_SUMMARY = "This is a test wizard";
33  
34  
35      public static final int INDEX_TITLE = 0;
36      public static final int INDEX_HELPTEXT = 1;
37      public static final int INDEX_DIRECTIVE = 2;
38  
39      public static final String[][] WIZ_PAGES
40              = {
41                  {"Page1", "Here's some helpful text",
42                   "Enter some text"},
43                  {"Page2", "Some more help", "Step 2 Text"},
44                  {"Page3", "Some more help", "Step 3 Text"}
45              };
46  
47  
48      private String dataContext;
49  
50  
51      public WizardTestModel(String testContext) {
52          dataContext = testContext;
53      }
54  
55      public static final int NUM_WIZARDS = 3;
56  
57      private int wizardId = -1;
58  
59      private int allWizardIds[] = new int[NUM_WIZARDS];
60  
61  
62      public String getDataContext() {
63          return dataContext;
64      }
65  
66      /***
67       * Retrieve the wizard that was added to the database earlier.
68       *
69       * @return WizDefinition.
70       * @throws DBException upon error loading definition.
71       */
72      public WizDefinition getDefinedWizard() throws DBException {
73          if (wizardId == -1) {
74              throw new DBException("Model has not been reinitialized!");
75          }
76          WizDefinition def = new WizDefinition();
77          def.setField(WizDefinition.FLD_ID, wizardId);
78          def.retrieve();
79  
80          return def;
81      }
82  
83      /***
84       * Initialize the wizard test model.
85       *
86       * @return the integer value of the id of the wizard added to the
87       *         database
88       * @throws DBException upon construction error.
89       */
90      public int intialize() throws DBException {
91          DBConnection connection = DBConnectionPool.getInstance(dataContext)
92                  .getConnection("Emo Wizard Data Model Setup");
93          try {
94              connection.setAutoCommit(false);
95  
96              WizDefinition def = new WizDefinition(connection);
97              def.setConnection(connection);
98              def.deleteAll();
99              for (int i = 0; i < NUM_WIZARDS; i++) {
100                 def.setField(WizDefinition.FLD_FACTORY, com.sri.emo.wizard.selection.EmoSelectionWizardFactory.class.getName());
101                 def.setField(WizDefinition.FLD_NAME, WIZ_TITLE + i);
102                 def.setField(WizDefinition.FLD_SUMMARY, WIZ_SUMMARY);
103                 def.setField(WizDefinition.FLD_WIZARD, com.sri.emo.wizard.defaults.SequentialWizard.class.getName());
104                 def.add();
105                 allWizardIds[i] = def.getFieldInt(WizDefinition.FLD_ID);
106                 addWizardSteps(connection, allWizardIds[i]);
107                 def.clear();
108             }
109             wizardId = allWizardIds[0];
110 
111             connection.commit();
112             return wizardId;
113         } catch (Exception ex) {
114             wizardId = -1;
115             ex.printStackTrace();
116             connection.rollback();
117             throw new DBException(ex);
118         } finally {
119             connection.release();
120         }
121     }
122 
123     protected void addWizardSteps(DBConnection connection, int id) throws DBException {
124         for (int i = 0; i < WIZ_PAGES.length; i++) {
125             WizStep step = new com.sri.emo.dbobj.WizStep();
126             step.setConnection(connection);
127             step.setField(com.sri.emo.dbobj.WizStep.FLD_TITLE, WIZ_PAGES[i][INDEX_TITLE]);
128             step.setField(com.sri.emo.dbobj.WizStep.FLD_HELPTEXT, WIZ_PAGES[i][INDEX_HELPTEXT]);
129             step.setField(com.sri.emo.dbobj.WizStep.FLD_DIRECTIVE, WIZ_PAGES[i][INDEX_DIRECTIVE]);
130             step.setField(com.sri.emo.dbobj.WizStep.FLD_WIZID, id);
131 //            step.setField(com.sri.emo.dbobj.WizStep.FLD_MODEL,com.sri.emo.dbobj.WizStep.NO_SELECTION_TARGET);
132             step.setField(com.sri.emo.dbobj.WizStep.FLD_ORDER, i);
133             step.setField(WizStep.FLD_STEP_TYPE, WizStep.INSTRUCTIONS_ONLY);
134             step.add();
135         }
136     }
137 
138     /***
139      * Removes previously added items.
140      *
141      * @throws DBException
142      */
143     public void destroy() throws DBException {
144         DBConnection connection = DBConnectionPool.getInstance(dataContext)
145                 .getConnection("Emo Wizard Data Model Setup");
146 
147         try {
148             connection.setAutoCommit(false);
149             WizDefinition def = new WizDefinition(connection);
150             for (int i = 0; i < WIZ_PAGES.length; i++) {
151                 def.setId(allWizardIds[i] + "");
152                 try {
153                     def.retrieve();
154                 } catch (DBException ex1) {
155                     continue;
156                 }
157                 def.delete(); //wipe out wizard and all steps
158             }
159             connection.commit();
160             wizardId = -1;
161         } catch (DBException ex) {
162             ex.printStackTrace();
163             connection.rollback();
164             throw ex;
165         } finally {
166             connection.release();
167         }
168     }
169 }