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

1    package net.server.servlets; 
2     
3    import java.io.BufferedReader; 
4    import java.util.Arrays; 
5    import java.util.Enumeration; 
6    import java.util.Vector; 
7     
8     
9    /** 
10    *  FormCDataParser Class 
11    */ 
12    
13   public class FormCDataParser implements FormCDataColumns { 
14    
15       /** 
16        * Components 
17        */ 
18    
19       private Vector vData; 
20       private Vector term = new Vector(); 
21       private Vector course = new Vector(); 
22       private Vector section = new Vector(); 
23       private Vector instructor = new Vector(); 
24       private Vector courseKey = new Vector(); 
25       private Vector courseStudents = new Vector(); 
26    
27       private int courseKeyIndex = 0; 
28       private int courseKeyIndexArray[]; 
29       private int courseKeyIndexLength; 
30    
31       /** 
32        * FormCDataParser() Constructor 
33        */ 
34    
35       public FormCDataParser(BufferedReader br) { 
36           CsvLineConsumer cv = new CsvLineConsumer(br, columnOrder); 
37           vData = cv.getVData(); 
38           processData(); 
39    
40           //deh 
41           courseKeyIndexArray = new int[20]; 
42    
43       } 
44    
45       /** 
46        * Returns Term Array 
47        * 
48        * @return          String[] 
49        */ 
50    
51       public String[] getTerm() { 
52           Object[] objTerm = term.toArray(); 
53           Arrays.sort(objTerm); 
54    
55           String[] strTerm = new String[objTerm.length]; 
56    
57           for (int i = 0; i < strTerm.length; i++) { 
58               strTerm[i] = (String) objTerm[i]; 
59           } 
60    
61           return strTerm; 
62       } 
63    
64       /** 
65        * Returns Courses Array 
66        * 
67        * @return          String[] 
68        */ 
69    
70       public String[] getCourse() { 
71           Object[] objCourse = course.toArray(); 
72           Arrays.sort(objCourse); 
73    
74           String[] strCourse = new String[objCourse.length]; 
75    
76           for (int i = 0; i < strCourse.length; i++) { 
77               strCourse[i] = (String) objCourse[i]; 
78           } 
79    
80           return strCourse; 
81       } 
82    
83       /** 
84        * Returns Section Array 
85        * 
86        * @return          String[] 
87        */ 
88    
89       public String[] getSection() { 
90           Object[] objSection = section.toArray(); 
91           Arrays.sort(objSection); 
92    
93           String[] strSection = new String[objSection.length]; 
94    
95           for (int i = 0; i < strSection.length; i++) { 
96               strSection[i] = (String) objSection[i]; 
97           } 
98    
99           return strSection; 
100      } 
101   
102      /** 
103       * Returns Instructor Array 
104       * 
105       * @return          String[] 
106       */ 
107   
108      public String[] getInstructor() { 
109          Object[] objInstructor = instructor.toArray(); 
110          Arrays.sort(objInstructor); 
111   
112          String[] strInstructor = new String[objInstructor.length]; 
113   
114          for (int i = 0; i < strInstructor.length; i++) { 
115              strInstructor[i] = (String) objInstructor[i]; 
116              System.out.println("Instr is " + strInstructor[i]); 
117          } 
118   
119          return strInstructor; 
120      } 
121   
122      /** 
123       * Returns true/false 
124       * 
125       * @return                  boolean 
126       * 
127       * Notes: The UserName is the Lastname and is the CourseKey against which information is built for the 
128       *        Survey 
129       */ 
130   
131      public boolean isCourseKeyByLoginUser(String user) { 
132   
133  // deh put code in here to return multiple courses 
134  // CourseKeyIndexLength is set to -1, so the first item will be at the 0th place in the CourseKeyIndexArray 
135          courseKeyIndexLength = -1; 
136   
137          int index = 0; 
138          boolean hasCourse = false; 
139   
140          for (Enumeration e = (getCourseKey()).elements(); 
141               e.hasMoreElements(); index++) { 
142   
143              String strData[] = (String[]) e.nextElement(); 
144   
145   
146              if ((strData[COURSE_KEY_INSTR]).regionMatches(true, 3, user, 0, 
147                      user.length())) { 
148                  courseKeyIndexLength++; 
149                  courseKeyIndexArray[courseKeyIndexLength] = index; 
150   
151                  hasCourse = true; 
152              } 
153          } 
154   
155          return hasCourse; 
156      } 
157   
158      /** 
159       * Returns true/false 
160       * 
161       * @return          boolean 
162       */ 
163   
164      public boolean isCourseKey(String s) { 
165   
166          int index = 0; 
167   
168          for (Enumeration e = (getCourseKey()).elements(); 
169               e.hasMoreElements(); index++) { 
170   
171              String strData[] = (String[]) e.nextElement(); 
172   
173              if (s.equals(strData[COURSE_KEY])) { 
174                  courseKeyIndex = index; 
175                  return true; 
176              } 
177          } 
178   
179          return false; 
180      } 
181   
182   
183      /** 
184       * Returns Course Key Index 
185       * 
186       * @return          int 
187       */ 
188   
189      public int getCourseKeyIndex() { 
190   
191          return courseKeyIndex; 
192      } 
193   
194      /** 
195       * Returns Course Key Index Array 
196       * 
197       * @return          Array of ints 
198       */ 
199   
200      public int[] getCourseKeyIndexArray() { 
201   
202          return courseKeyIndexArray; 
203      } 
204   
205      /** 
206       * Returns Course Key Index Array Length 
207       * 
208       * @return          Length of the Array of int getCourseKeyIndexArray[] 
209       */ 
210   
211      public int getCourseKeyIndexLength() { 
212          System.out.println("The courseKeyIndexLength here is " + courseKeyIndexLength); 
213          return courseKeyIndexLength; 
214      } 
215   
216   
217      /** 
218       * Returns Course Key 
219       * 
220       * @return          Vector 
221       */ 
222   
223      public Vector getCourseKey() { 
224   
225          return courseKey; 
226      } 
227   
228      /** 
229       * Returns Course Key value 
230       * 
231       * @return          String 
232       */ 
233   
234      public String getCourseKeyValue(int keyIndex, int column) { 
235   
236          String courseElements[] = (String[]) (courseKey.elementAt(keyIndex)); 
237   
238  //      for (int i =0; i< courseElements.length; i++){ 
239  //      System.out.println("\ncourse element is " + courseElements[i]); 
240  //      } 
241   
242          return courseElements[column]; 
243   
244      } 
245   
246      /** 
247       * Returns Course Key 
248       * 
249       * @return                String 
250       */ 
251   
252      public String getCourseKeyByKey(int keyIndex) { 
253   
254          return getCourseKeyValue(keyIndex, COURSE_KEY); 
255   
256      } 
257   
258      /** 
259       * Return Term of the Course 
260       * 
261       * @return                String 
262       */ 
263   
264      public String getCourseTermByKey(int keyIndex) { 
265   
266          return getCourseKeyValue(keyIndex, COURSE_KEY_TERM); 
267   
268      } 
269   
270   
271      /** 
272       * Returns Course No 
273       * 
274       * @return                String 
275       */ 
276   
277      public String getCourseNoByKey(int keyIndex) { 
278   
279          return getCourseKeyValue(keyIndex, COURSE_KEY_COURSE_NO); 
280   
281      } 
282   
283      /** 
284       * Returns Course Name 
285       * 
286       * @return                String 
287       */ 
288   
289      public String getCourseNameByKey(int keyIndex) { 
290   
291          return getCourseKeyValue(keyIndex, COURSE_KEY_COURSE_NAME); 
292   
293      } 
294   
295      /** 
296       * Returns Course Section 
297       * 
298       * @return                String 
299       */ 
300   
301      public String getCourseSectionByKey(int keyIndex) { 
302   
303          return getCourseKeyValue(keyIndex, COURSE_KEY_SECTION); 
304   
305      } 
306   
307      /** 
308       * Returns Course Year 
309       * 
310       * @return                String 
311       */ 
312   
313      public String getCourseYearByKey(int keyIndex) { 
314   
315          return getCourseKeyValue(keyIndex, COURSE_KEY_YEAR); 
316   
317      } 
318   
319      /** 
320       * Returns Course Instructor 
321       * 
322       * @return                String 
323       */ 
324   
325      public String getCourseInstructorByKey(int keyIndex) { 
326   
327          return getCourseKeyValue(keyIndex, COURSE_KEY_INSTR); 
328   
329      } 
330   
331   
332      /** 
333       * Returns Course Students 
334       * 
335       * @return          Vector 
336       */ 
337   
338      public Vector getCourseStudents() { 
339   
340          return courseStudents; 
341      } 
342   
343   
344      /** 
345       * Returns Course Students array 
346       * 
347       * @return               String[] 
348       */ 
349   
350      public String[] getCourseStudentsByKey(int keyIndex, int column) { 
351   
352          Vector sv = (Vector) (getCourseStudents()).elementAt(keyIndex); 
353          String students[] = new String[sv.size()]; 
354   
355          int i = 0; 
356   
357          for (Enumeration e = sv.elements(); 
358               e.hasMoreElements(); i++) { 
359   
360              String strObj[] = (String[]) e.nextElement(); 
361              students[i] = strObj[column]; 
362   
363          } 
364   
365          return students; 
366      } 
367   
368   
369      /** 
370       * Returns Course Students array 
371       * 
372       * @return               String[] 
373       */ 
374   
375      public String[] getCourseStudentsByKey(int keyIndex) { 
376   
377          return getCourseStudentsByKey(keyIndex, S_STUDENT_NAME); 
378   
379      } 
380   
381      /** 
382       * Returns Course Students array 
383       * 
384       * @return                 String[] 
385       */ 
386   
387      public String[] getCourseStudentsIdByKey(int keyIndex) { 
388   
389          return getCourseStudentsByKey(keyIndex, S_STUDENT_ID); 
390   
391      } 
392   
393      /** 
394       * processData Method 
395       * 
396       */ 
397   
398      private void processData() { 
399   
400          //This takes in the CSV file line by line in it's indiviual columns 
401   
402          int count = vData.size(); 
403   
404          for (int i = 0; i < count; i++) { 
405              String data[] = (String[]) vData.elementAt(i); 
406              splitColumns(data); 
407          } 
408   
409      } 
410   
411      /** 
412       * splitColumns Method 
413       */ 
414   
415      private void splitColumns(String data[]) { 
416          StringBuffer courseBuffer = new StringBuffer(); 
417          StringBuffer courseKeyBuffer = new StringBuffer(); 
418          StringBuffer studentBuffer = new StringBuffer(); 
419          String courseKeyArray[] = new String[COURSE_KEY_ARRAY_SIZE]; 
420          String studentsArray[] = new String[STUDENTS_ARRAY_SIZE]; 
421   
422          addElement(term, data[IND_TERM]); 
423   
424          courseBuffer.append(data[IND_COURSE_NO]); 
425          courseBuffer.append(" "); 
426          courseBuffer.append(data[IND_COURSE_NAME]); 
427          addElement(course, courseBuffer.toString()); 
428   
429          addElement(section, data[IND_SECTION]); 
430   
431          courseKeyBuffer.append(data[IND_TERM]); 
432          courseKeyBuffer.append(" "); 
433          courseKeyBuffer.append(courseBuffer.toString()); 
434          courseKeyBuffer.append(" "); 
435          courseKeyBuffer.append(data[IND_SECTION]); 
436   
437          courseKeyArray[0] = courseKeyBuffer.toString(); 
438          courseKeyArray[1] = data[IND_TERM]; 
439          courseKeyArray[2] = data[IND_COURSE_NO]; 
440          courseKeyArray[3] = data[IND_COURSE_NAME]; 
441          courseKeyArray[4] = data[IND_SECTION]; 
442          courseKeyArray[5] = data[IND_YEAR]; 
443          courseKeyArray[6] = data[IND_INSTR] + data[IND_INSTR_NAME]; 
444   
445  //      This addElement adds the courseKeyArray to courseKey if it doesn't already exist 
446          addElement(courseKey, courseStudents, courseKeyArray); 
447  //      System.out.println("Course Key " + courseKey ); 
448   
449   
450          studentBuffer.append(data[IND_FIRST_NAME]); 
451          studentBuffer.append(" "); 
452          studentBuffer.append(data[IND_MIDDLE_NAME]); 
453          studentBuffer.append(" "); 
454          studentBuffer.append(data[IND_LAST_NAME]); 
455   
456          studentsArray[0] = data[IND_STUDENT_ID]; 
457          studentsArray[1] = studentBuffer.toString(); 
458   
459  //This adds the students to the detail which is 
460          // the CourseKeyArray(really holds the students in here) 
461          addElement(courseKey, courseStudents, 
462                  courseKeyArray, studentsArray); 
463   
464   
465      } 
466   
467   
468      /** 
469       * addElement     Method 
470       * 
471       */ 
472   
473      private void addElement(Vector v, String data) { 
474          boolean dataExists = false; 
475   
476          for (Enumeration e = v.elements(); e.hasMoreElements();) { 
477              if (data.equals(e.nextElement())) { 
478                  dataExists = true; 
479                  break; 
480              } 
481          } 
482   
483   
484          if (!dataExists) { 
485              v.addElement(data); 
486          } 
487      } 
488   
489      /** 
490       * addElement     Method 
491       * 
492       */ 
493   
494      private void addElement(Vector v, String data[]) { 
495          String strData[]; 
496          boolean dataExists = false; 
497   
498          for (Enumeration e = v.elements(); e.hasMoreElements();) { 
499              strData = (String[]) e.nextElement(); 
500              if (data[0].equals(strData[0])) { 
501                  dataExists = true; 
502                  break; 
503              } 
504          } 
505   
506   
507          if (!dataExists) { 
508              v.addElement(data); 
509          } 
510      } 
511   
512   
513      /** 
514       * addElement     Method 
515       * 
516       */ 
517   
518      private void addElement(Vector key, Vector detail, String data[]) { 
519          String strData[]; 
520          boolean dataExists = false; 
521   
522          for (Enumeration ke = key.elements(); ke.hasMoreElements();) { 
523              strData = (String[]) ke.nextElement(); 
524              if (data[0].equals(strData[0])) { 
525                  dataExists = true; 
526                  break; 
527              } 
528          } 
529   
530   
531          if (!dataExists) { 
532              key.addElement(data); 
533              detail.addElement(new Vector()); 
534          } 
535      } 
536   
537   
538      /** 
539       * addElement     Method 
540       * 
541       */ 
542   
543      private void addElement(Vector vkey, Vector detail, 
544                              String skey[], String data[]) { 
545   
546          String strData[]; 
547          Enumeration ke = vkey.elements(); 
548   
549          for (int i = 0; ke.hasMoreElements(); i++) { 
550              strData = (String[]) ke.nextElement(); 
551              if (skey[0].equals(strData[0])) { 
552                  Vector sv = (Vector) detail.elementAt(i); 
553                  addElement(sv, data); 
554                  break; 
555              } 
556          } 
557   
558      } 
559   
560  } 
561