/Users/lyon/j4p/src/net/server/servlets/FormCSQL.java

1    package net.server.servlets; 
2     
3    import java.util.*; 
4     
5     
6    /** 
7     *  FormCSQL  Class 
8     */ 
9     
10   public class FormCSQL { 
11    
12       private SQLBeanie dbw = new SQLBeanie(); 
13    
14       FormCDataParser fc; 
15    
16    
17       /** 
18        * FormCSQL   Constructor 
19        */ 
20    
21       public FormCSQL(FormCDataParser _fc) { 
22           fc = _fc; 
23       } 
24    
25    
26       /** 
27        * Creates a FormC.sql outfile with the sequel insert statements 
28    
29        */ 
30    
31       public void generateFormCSQL(String[] fieldNames, String[] fieldValues) { 
32    
33           int courseKeyIndex = fc.getCourseKeyIndex(); 
34           String students[] = 
35                   fc.getCourseStudentsIdByKey(courseKeyIndex); 
36    
37           String studentsData[][] = getData(students, fieldNames, fieldValues); 
38    
39           createFormCSql(getFormCSqlRows(courseKeyIndex, studentsData)); 
40    
41       } 
42    
43    
44       /** 
45        * getData    Method 
46        * 
47    
48        * @return    String[][] 
49        */ 
50    
51       private String[][] getData(String students[], String fields[], 
52                                  String fValues[]) { 
53    
54           int noOfRows = students.length; 
55           int noOfColumns = 9;    // student id + 8 codes 
56    
57           String data[][] = new String[noOfRows][noOfColumns]; 
58    
59           for (int i = 0; i < data.length; i++) { 
60               for (int j = 0; j < data[i].length; j++) { 
61                   switch (j) { 
62                       case 0: 
63                           data[i][j] = students[i]; 
64                           break; 
65    
66                       default: 
67                           for (int k = 0; k < fields.length; k++) { 
68                               if (fields[k].equals("cbo" + (i) + (j - 1))) { 
69                                   data[i][j] = fValues[k]; 
70                                   break; 
71                               } 
72                           } 
73                           break; 
74                   } 
75               } 
76    
77           } 
78    
79           return data; 
80       } 
81    
82    
83       /** 
84        * getFormCSqlRows        Method 
85        * 
86        * @return              String[] 
87        */ 
88    
89       private String[] getFormCSqlRows(int index, String[][] studentsData) { 
90    
91           Vector sqlRows = new Vector(); 
92    
93           for (int i = 0; i < studentsData.length; i++) { 
94               getFormCSqlRow(sqlRows, getFormCRow(index, studentsData[i])); 
95           } 
96    
97           return (vectorToString(sqlRows)); 
98       } 
99    
100   
101      /** 
102       * getFormCSqlRow         Method 
103       * 
104   
105       */ 
106   
107      private void getFormCSqlRow(Vector vSql, Vector vData) { 
108   
109   
110          for (Enumeration e = vData.elements(); e.hasMoreElements();) { 
111              String strObj[] = (String[]) e.nextElement(); 
112   
113   
114              String sql = "Insert into all_forms values ("; 
115   
116   
117  //deh to Umy this is messy !!! 
118  /* 
119              for (int i = 0; i < strObj.length; i++) { 
120                  if (i == 0) { 
121                      sql = sql + strObj[i] + ","; 
122                  } 
123                  else if(i < (strObj.length - 1)) { 
124                      sql = sql + Html.quote(strObj[i]) + ","; 
125                  } 
126                  else { 
127                      sql = sql + strObj[i] + ")"; 
128                      vSql.add(sql); 
129                  } 
130              } 
131          } 
132  */ 
133  //deh's way 
134              int i;  //Want this variable to last a little longer than the for loop 
135   
136              for (i = 0; i < strObj.length - 1; i++) 
137                  sql = sql + Html.singleQuote(strObj[i]) + ","; 
138   
139  // Last item doesn't need a comma but a parenthesis and a semi-colon 
140              sql = sql + Html.singleQuote(strObj[i]) + ");"; 
141              vSql.add(sql); 
142  //deh 
143  //          System.out.println(sql); 
144  //deh 
145          } 
146      } 
147   
148   
149      /** 
150       * getFormCRow            Method 
151       * 
152       * @return              Vector 
153       */ 
154   
155      private Vector getFormCRow(int index, String[] data) { 
156   
157          String formCId = "1"; 
158   
159          Vector rows = new Vector(); 
160   
161          for (int i = 0; i < data.length; i++) { 
162   
163              List formcRow = new ArrayList(); 
164              formcRow.add(data[0]); 
165              formcRow.add(fc.getCourseNoByKey(index)); 
166              formcRow.add(fc.getCourseSectionByKey(index)); 
167              formcRow.add(fc.getCourseTermByKey(index)); 
168              formcRow.add(fc.getCourseYearByKey(index)); 
169              formcRow.add(formCId); 
170   
171              switch (i) { 
172                  case 0: 
173                      break; 
174                  default: 
175                      formcRow.add("" + i); 
176                      formcRow.add(data[i]); 
177                      rows.add(objectToString(formcRow)); 
178              } 
179          } 
180   
181          return rows; 
182      } 
183   
184   
185      /** 
186       * Returns an array of String Object 
187       * 
188       * @return              String[] 
189       */ 
190   
191      public String[] objectToString(List l) { 
192          String str[] = new String[l.size()]; 
193   
194          Iterator it = l.iterator(); 
195   
196          for (int i = 0; it.hasNext(); i++) { 
197              str[i] = (String) it.next(); 
198          } 
199   
200          return str; 
201      } 
202   
203      /** 
204       * Returns an array of String Object 
205       * 
206       * @return              String[] 
207       */ 
208   
209      public String[] vectorToString(Vector v) { 
210   
211          String str[] = new String[v.size()]; 
212          int i = 0; 
213   
214          for (Enumeration e = v.elements(); e.hasMoreElements(); i++) { 
215              str[i] = (String) e.nextElement(); 
216          } 
217   
218          return str; 
219      } 
220   
221      /** 
222       * createFormCSql           Method 
223       * 
224       */ 
225   
226      private void createFormCSql(String[] sqlRows) { 
227   
228   
229          dbw.open(); 
230   
231  //      try { 
232   
233  // This code is only for writing SQL to a file 
234   
235  //          BufferedWriter bw = FileUtil.openOutputFile(new File("c:\\FormC.Sql")); 
236   
237          for (int i = 0; i < sqlRows.length; i++) { 
238              dbw.insert(sqlRows[i]); 
239  // The following is diagnostic for looking at the generated sql commands. 
240  //              System.out.println(sqlRows[i]); 
241  //              FileUtil.println(bw, sqlRows[i]); 
242  //          } 
243  //          FileUtil.close(bw); 
244  //      } 
245  //      catch (FileNotFoundException fnf) { 
246  //          fnf.printStackTrace(); 
247  //      } 
248  //      catch (IOException io) { 
249  //          io.printStackTrace(); 
250  //      } 
251  //      catch (Exception e) { 
252  //          e.printStackTrace(); 
253          } 
254   
255          dbw.close(); 
256   
257      } 
258  } 
259