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

1    package net.server.servlets; 
2     
3    import javax.servlet.ServletException; 
4    import javax.servlet.http.HttpServlet; 
5    import javax.servlet.http.HttpServletRequest; 
6    import javax.servlet.http.HttpServletResponse; 
7    import java.io.IOException; 
8    import java.io.PrintWriter; 
9    import java.util.ResourceBundle; 
10    
11   /** 
12    * The FormProcessorServlet class handles the processing of 
13    * the HTML pages which prompt the user for input for the Form 
14    * C student evaluation form. 
15    * 
16    * @author Robert Lysik 
17    * @version 1.00 
18    */ 
19    
20   public class FormProcessorServlet extends HttpServlet { 
21    
22       ResourceBundle rb = ResourceBundle.getBundle("LocalStrings"); 
23    
24       private CourseDataFileReader reader; // Information read in from 
25       // CSV file 
26    
27       public FormProcessorServlet() { 
28           reader = new CourseDataFileReader(); 
29       } 
30    
31       /** 
32        * This method handles the coordination of HTML page 
33        * display and form processing using parameters 
34        * extracted from the request object which is passed in 
35        * as a parameter. The value of the status parameter determines 
36        * which HTML page to display next. 
37        * 
38        * The initial state, where status is null, involves display of 
39        * a user vealidation form which prompts the user for their 
40        * user id and password. 
41        * 
42        * If the user is authorized, they are then prompted for the 
43        * course and section for which they wish to enter data for Form C. 
44        * 
45        * The Form C page is then displayed allowing the user to enter the 
46        * student assessment values for each category and student in the class. 
47        * 
48        * A confirmation page is then displayed to the user, following which, the 
49        * data is either written to file, or written to a database. 
50        * 
51        */ 
52       public void doGet(HttpServletRequest request, 
53                         HttpServletResponse response) 
54               throws IOException, ServletException { 
55           response.setContentType("text/html"); 
56           PrintWriter out = response.getWriter(); 
57           String pageStatus = request.getParameter("status"); 
58    
59           // First entry into doGet method, display user authorization form. 
60           if (pageStatus == null) { 
61               UserAuthorizationPage page = new UserAuthorizationPage(); 
62    
63               out.println(page.getHtml()); 
64           } 
65    
66           // Once the course has been selected, the user may then select 
67           // the section number for the course 
68           else if (pageStatus.equals("course_id_selected")) { 
69               String courseId = request.getParameter("course"); 
70    
71               SelectSectionPage page = 
72                       new SelectSectionPage(courseId, 
73                               reader.getSectionIds(courseId)); 
74    
75               out.println(page.getHtml()); 
76           } 
77    
78           // Using the course number and section number obtained during the 
79           // preceding steps, display the Form C student evaluation form. 
80           else if (pageStatus.equals("section_id_selected")) { 
81               String courseId = request.getParameter("course"); 
82               String sectionId = request.getParameter("section"); 
83    
84               ReviewForm page = new ReviewForm(courseId, 
85                       sectionId, 
86                       reader.getCourse(courseId, sectionId), 
87                       reader.getCourseStudents(courseId, sectionId)); 
88    
89               out.println(page.getHtml()); 
90           } 
91    
92           // Re-display the information submitted from Form C 
93           else if (pageStatus.equals("evaluation_complete")) { 
94               ConfirmationPage page = new ConfirmationPage(request); 
95    
96               out.println(page.getHtml()); 
97           } 
98    
99           // Finally, save the data generated during the form C evaluation 
100          else if (pageStatus.equals("confirmed")) { 
101              SqlSynthesizer synthesizer = new SqlSynthesizer(request); 
102   
103              synthesizer.save("c:\\statements.sql"); 
104          } 
105      } 
106   
107      /** 
108       * This method handles the coordination of HTML page 
109       * display and form processing using parameters 
110       * extracted from the request object which is passed in 
111       * as a parameter. The value of the status parameter determines 
112       * which HTML page to display next. The doPost method is used 
113       * for processing of the users id an password information 
114       * 
115       * The initial state, where status is null, involves display of 
116       * a user validation form which prompts the user for their 
117       * user id and password. 
118       * 
119       * If the user is authorized, they are then prompted for the 
120       * course and section for which they wish to enter data for Form C. 
121       * 
122       */ 
123      public void doPost(HttpServletRequest request, 
124                         HttpServletResponse response) 
125              throws IOException, ServletException { 
126          response.setContentType("text/html"); 
127          PrintWriter out = response.getWriter(); 
128          String pageStatus = request.getParameter("status"); 
129   
130          if (pageStatus.equals("authorizing")) { 
131              String userId = request.getParameter("txtUserId"); 
132              String password = request.getParameter("txtPassword"); 
133              boolean userIsAuthorized = false; 
134   
135              Authorization userAuthorization = new Authorization(); 
136              userIsAuthorized = userAuthorization.verify(userId, password); 
137   
138              if (!userIsAuthorized) 
139                  out.println("<html><body>You are not authorized for access.</body></html>"); 
140   
141              // If user is authorized to continue, display the select course 
142              // page 
143              else { 
144                  SelectCoursePage page = 
145                          new SelectCoursePage(reader.getCourseIds(), 
146                                  reader.getCourses()); 
147   
148                  out.println(page.getHtml()); 
149              } 
150          } 
151      } 
152  } 
153   
154   
155   
156