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

1    package net.server.servlets; 
2     
3    import java.io.FileNotFoundException; 
4    import java.io.FileReader; 
5    import java.io.IOException; 
6    import java.util.Vector; 
7     
8    /** 
9     * The CourseDataFileReader class encapsulates the 
10    * functionality required to read information from 
11    * the course CSV file, extract the data and 
12    * populate an array of Course objects which can 
13    * then be used to more easilay manipulate the 
14    * course information stored in the CSV file. 
15    * 
16    * @author Robert Lysik 
17    * @version 1.00 
18    */ 
19   class CourseDataFileReader { 
20       public Course[] courses; 
21    
22       /** 
23        * The CourseDataFileReader constructor opens 
24        * the CSV file and extracts the data. This 
25        * data is parsed and formatted in order to 
26        * construct an array of Course objects which 
27        * can then be more easily manipulated. 
28        */ 
29       CourseDataFileReader() { 
30           Vector fileRowVector; 
31           Vector courseVector; 
32    
33           // Extract a vector of rows from the CSV file 
34           fileRowVector = readCourseDataFile(); 
35    
36           // Parse through each row of the file and construct an 
37           // array of Course objects. 
38           courseVector = 
39           processCourseDataFileRows(fileRowVector); 
40           courses = 
41           new Course[courseVector.size()]; 
42           for (int index = 0; 
43                index < courseVector.size(); 
44                index++) 
45               courses[index] = 
46               (Course) courseVector.get(index); 
47       } 
48    
49       /** 
50        * This function takes a vector of row data 
51        * obtained from the CSV course file and 
52        * returns a vecor of Course objects which are 
53        * the result of parsing this data for Course 
54        * related information. Each course has an id, 
55        * a section id, a term, a year, an instructor 
56        * and a list of Students. 
57        * 
58        * @param fileRowVector a vector of rows 
59        *                      obtained from the CSV 
60        *                      file 
61        */ 
62       private Vector processCourseDataFileRows( 
63               Vector fileRowVector) { 
64           CsvLineParser parser = new CsvLineParser(); 
65           Vector courseVector = new Vector(); 
66           String[] values; 
67           boolean courseFound = false; 
68           String courseId = new String(); 
69           String sectionId = new String(); 
70           String term = new String(); 
71           String year = new String(); 
72           String name = new String(); 
73           for (int fileRowIndex = 1; 
74                fileRowIndex < fileRowVector.size(); 
75                fileRowIndex++) { 
76               values = 
77               parser.getTokens( 
78                       (String) fileRowVector.get( 
79                               fileRowIndex)); 
80               courseId = values[1]; 
81               sectionId = values[2]; 
82               term = values[4]; 
83               year = values[5]; 
84               name = values[8] + " " + values[9]; 
85               courseFound = 
86               courseExists(courseId, 
87                            sectionId, 
88                            courseVector); 
89               if (!courseFound) { 
90                   Vector studentVector = new Vector(); 
91                   String[] valArray; 
92                   for (int index = 1; 
93                        index < 
94                        fileRowVector.size(); 
95                        index++) { 
96                       valArray = 
97                       parser.getTokens( 
98                               (String) fileRowVector.get( 
99                                       index)); 
100                      if ((courseId.equals( 
101                              valArray[1])) && 
102                          (sectionId.equals( 
103                                  valArray[2]))) { 
104                          Student student = new Student( 
105                                  valArray[15], 
106                                  valArray[16], 
107                                  valArray[17], 
108                                  Integer.parseInt( 
109                                          valArray[12])); 
110                          studentVector.add( 
111                                  student); 
112                      } 
113                  } 
114                  Student[] students = new Student[studentVector.size()]; 
115                  for (int index = 0; 
116                       index < 
117                       studentVector.size(); 
118                       index++) 
119                      students[index] = 
120                      (Student) studentVector.get( 
121                              index); 
122                  Course course = new Course( 
123                          courseId, 
124                          sectionId, 
125                          term, 
126                          year, 
127                          name, 
128                          students); 
129                  courseVector.add(course); 
130              } 
131          } 
132          return courseVector; 
133      } 
134   
135      /** 
136       * This function is used to determine if a 
137       * specific course exists in the array of 
138       * Course objects. 
139       * 
140       * @param courseId     the course id 
141       * @param sectionId    the section id of the 
142       *                     course 
143       * @param courseVector the vector of Course 
144       *                     objects to check for 
145       *                     the existence of the 
146       *                     course within. 
147       */ 
148      private boolean courseExists(String courseId, 
149                                   String sectionId, 
150                                   Vector courseVector) { 
151          boolean exists = false; 
152          Course course; 
153          String course_id = new String(); 
154          String section_id = new String(); 
155          for (int index = 0; 
156               index < courseVector.size(); 
157               index++) { 
158              course = 
159              (Course) courseVector.get(index); 
160              course_id = course.getCourseId(); 
161              section_id = course.getSectionId(); 
162              if ((courseId.equals(course_id)) && 
163                  (sectionId.equals(section_id))) { 
164                  exists = true; 
165                  break; 
166              } 
167          } 
168          return exists; 
169      } 
170   
171      /** 
172       * This function reads the course data CSV 
173       * file and returns a Vector of row data. 
174       */ 
175      private Vector readCourseDataFile() { 
176          FileReader reader = null; 
177          Vector fileRowVector = new Vector(); 
178          try { 
179              reader = 
180              new FileReader("c:\\formc.csv"); 
181          } catch (FileNotFoundException fnfe) { 
182              fnfe.printStackTrace(); 
183          } 
184          try { 
185              String row = new String(); 
186              char buffer = ' '; 
187              int readValue = 0; 
188              readValue = reader.read(); 
189              while (readValue != -1) { 
190                  buffer = (char) readValue; 
191                  while ((buffer != '\n') && 
192                         (readValue != -1)) { 
193                      row += buffer; 
194                      readValue = reader.read(); 
195                      buffer = (char) readValue; 
196                  } 
197                  fileRowVector.add(row); 
198                  row = ""; 
199                  readValue = reader.read(); 
200              } 
201              reader.close(); 
202          } catch (IOException ioe) { 
203              ioe.printStackTrace(); 
204          } 
205          return fileRowVector; 
206      } 
207   
208      /** 
209       * This function returns the array of Course 
210       * objects obtained by parsing the course data 
211       * CSV file. 
212       */ 
213      public Course[] getCourses() { 
214          return courses; 
215      } 
216   
217      /** 
218       * This function returns the Course object 
219       * which matches the course id and section id 
220       * passed in as parameters. 
221       * 
222       * @param courseId  the course id 
223       * @param sectionId the section id of the 
224       *                  course 
225       */ 
226      public Course getCourse(String courseId, 
227                              String sectionId) { 
228          boolean courseFound = false; 
229          int index = 0; 
230          Course course = null; 
231          while ((!courseFound) && 
232                 (index < courses.length)) { 
233              if (courseId.equals( 
234                      courses[index].getCourseId()) && 
235                  (sectionId.equals( 
236                          courses[index].getSectionId()))) 
237                  courseFound = true; 
238              else 
239                  index++; 
240          } 
241          if (courseFound) 
242              return courses[index]; 
243          return course; 
244      } 
245   
246      /** 
247       * This function returns an array of course 
248       * ids which correspond to all the unique 
249       * course ids of the array of Course objects. 
250       */ 
251      public String[] getCourseIds() { 
252          String[] courseIds = new String[courses.length]; 
253          for (int index = 0; 
254               index < courses.length; 
255               index++) 
256              courseIds[index] = 
257              courses[index].getCourseId(); 
258          return courseIds; 
259      } 
260   
261      /** 
262       * This function returns an array of section 
263       * ids which correspond to the course id 
264       * passed in as a parameter. 
265       * 
266       * @param courseId the course id 
267       */ 
268      public String[] getSectionIds( 
269              String courseId) { 
270          Vector sectionVector = new Vector(); 
271          for (int index = 0; 
272               index < courses.length; 
273               index++) 
274              if (courseId.equals( 
275                      courses[index].getCourseId())) 
276                  sectionVector.add( 
277                          courses[index].getSectionId()); 
278          String[] sectionIds = new String[sectionVector.size()]; 
279          for (int index = 0; 
280               index < sectionVector.size(); 
281               index++) 
282              sectionIds[index] = 
283              (String) sectionVector.get(index); 
284          return sectionIds; 
285      } 
286   
287      /** 
288       * This function returns the name of the 
289       * instructor for the Course which matches the 
290       * course id and section id passed in as 
291       * parameters. 
292       * 
293       * @param courseId  the course id 
294       * @param sectionId the section id of the 
295       *                  course 
296       */ 
297      public String getCourseInstructor( 
298              String courseId, 
299              String sectionId) { 
300          int index = 0; 
301          boolean courseFound = false; 
302          String instructor = new String(); 
303          while ((!courseFound) && 
304                 (index < courses.length)) { 
305              if (courseId.equals( 
306                      courses[index].getCourseId()) && 
307                  (sectionId.equals( 
308                          courses[index].getSectionId()))) 
309                  courseFound = true; 
310              else 
311                  index++; 
312          } 
313          if (courseFound) 
314              instructor = 
315              courses[index].getInstructor(); 
316          return instructor; 
317      } 
318   
319      /** 
320       * This function returns the an array of 
321       * Students which are associated with the 
322       * Course that matches the course id and 
323       * section id passed in as parameters. 
324       * 
325       * @param courseId  the course id 
326       * @param sectionId the section id of the 
327       *                  course 
328       */ 
329      public Student[] getCourseStudents( 
330              String courseId, 
331              String sectionId) { 
332          int index = 0; 
333          boolean courseFound = false; 
334          Student[] empty = null; 
335          while ((!courseFound) && 
336                 (index < courses.length)) { 
337              if (courseId.equals( 
338                      courses[index].getCourseId()) && 
339                  (sectionId.equals( 
340                          courses[index].getSectionId()))) 
341                  courseFound = true; 
342              else 
343                  index++; 
344          } 
345          if (courseFound) 
346              return courses[index].getStudents(); 
347          return empty; 
348      } 
349  }