1     package sutut1;
2     
3     import javax.servlet.*;
4     import javax.servlet.http.*;
5     import java.io.*;
6     import java.util.*;
7     
8     import us.oh.state.common.utility.*;
9     
10    public class app2 extends HttpServlet {
11    
12    // actions
13    private static final String LOGIN = "Proceed";
14    private static final String MORE_DATA = "Enter More";
15    private static final String CHART = "Chart";
16    private static final String LOGOUT = "Cancel";
17    
18    // keys
19    private static final String ACTION = "action";
20    private static final String USERNAME = "username";
21    private static final String START_YEAR = "starting_year";
22    private static final String MORE_YEAR = "more_year";
23    private static final String SALARY_COUNT = "salary_count";
24    
25    private Properties configOptions = null;
26    
27    public void init(ServletConfig cfg) throws ServletException {
28      String confFileName = cfg.getInitParameter("configFile");
29      if ((confFileName == null) || (confFileName.length() == 0))
30        throw new ServletException("No configuration information found!");
31      configOptions = new Properties();
32      try {
33        FileInputStream propFile = new FileInputStream(confFileName);
34        configOptions.load(propFile);
35        propFile.close();
36      }
37      catch (IOException e) {
38        throw new ServletException("Can't read configuration from " + confFileName);
39      }
40      String dataFileName = configOptions.getProperty("cpidata");
41      if ((dataFileName == null) || (dataFileName.length() == 0))
42        throw new ServletException("No cpidata file name specified");
43      try {
44        CPIData = parseCPIData(dataFileName);
45      }
46      catch (IOException e) {
47        throw new ServletException("Can't read data from " + dataFileName);
48      }
49    }
50    
51    private class CPI {
52      int year;
53      double indexPercentage;
54      CPI(int year, double indexPercentage) {
55        this.year = year;
56        this.indexPercentage = indexPercentage;
57      }
58    }
59    
60    private static CPI [] CPIData = null;
61    
62    private CPI []  parseCPIData(String fileName) throws IOException {
63      LineNumberReader reader = new LineNumberReader(new FileReader(fileName));
64      Vector tmpCache = new Vector();
65      int year = 0;
66      double index = 0.0;
67      while (true) {
68        String line = reader.readLine();
69        if (line == null) break;
70        StringTokenizer column = new StringTokenizer(line, ",");
71        if ((column == null) || !column.hasMoreTokens()) continue;
72        String s = column.nextToken();
73        try {
74          year = Integer.parseInt(s);
75        }
76        catch (NumberFormatException e) {
77          continue; // not a data line, perhaps the header
78        }
79        while (column.hasMoreTokens()) {
80          s = column.nextToken();
81        }
82        // the last column is the annual index
83        try {
84          index = Double.parseDouble(s);
85        }
86        catch (NumberFormatException e) {
87          System.err.println("Bad data found at line " + reader.getLineNumber());
88        }
89        tmpCache.addElement(new CPI(year, index));
90      }
91      reader.close();
92      Enumeration enum = tmpCache.elements();
93      CPI [] data = new CPI[tmpCache.size()];
94      int i = 0;
95      while (enum.hasMoreElements()) data[i++] = (CPI)enum.nextElement();
96      return data;
97    }
98    
99    public void doGet(HttpServletRequest request, HttpServletResponse response)
100       throws ServletException, IOException {
101     HttpSession ses = request.getSession(true);
102     if (ses == null) {
103       HtmlPage page = new HtmlPage(response);
104       String s = "Sorry...";
105       page.title(s);
106       page.body();
107       page.center(true);
108       page.heading(1, s);
109       page.heading(2, "There are too many users to let you sign in");
110       page.out.println("Come back later");
111       page.close();
112       return;
113     }
114     String username = (String)ses.getValue(USERNAME);
115     if (ses.isNew() || (username == null)) signIn(request, response);
116     else acceptValues(request, response, ses);
117   }
118   
119   public void doPost(HttpServletRequest request, HttpServletResponse response)
120       throws ServletException, IOException {
121     String action = request.getParameter(ACTION);
122     HttpSession ses = request.getSession(true);
123     String username = (String)ses.getValue(USERNAME);
124     if (LOGIN.equals(action)) {
125       ses.putValue(USERNAME, request.getParameter(USERNAME));
126       try {
127         Integer year = new Integer(request.getParameter(START_YEAR));
128         ses.putValue(START_YEAR, year);
129         ses.putValue(MORE_YEAR, year);
130       }
131       catch (NumberFormatException e) {
132         ses.putValue(START_YEAR, new Integer(1985));
133         ses.putValue(MORE_YEAR, new Integer(1985));
134       }
135       acceptValues(request, response, ses);
136       return;
137     }
138     if (ses.isNew() || (username == null)) signIn(request, response);
139     else if (MORE_DATA.equals(action)) {
140       appendData(request, response, ses);
141       acceptValues(request, response, ses);
142     }
143     else if (CHART.equals(action)) {
144       makeChart(request, response, ses);
145     }
146     else {
147       ses.invalidate();
148       signIn(request, response);
149     }
150   }
151   
152   // Send the user here if there isn't any session data or it has become invalid
153   private void signIn(HttpServletRequest request, HttpServletResponse response)
154       throws ServletException, IOException {
155     HtmlPage page = new HtmlPage(response);
156     String s = "Compare salary to cost of living";
157     page.title(s);
158     page.body();
159     page.center(true);
160     page.heading(1, s);
161     page.heading(2, "Please sign in");
162     page.form(request.getServletPath());
163     page.table(0);
164     page.tField("Name:", USERNAME, "to print on report", 20, 40);
165     page.tField("Starting year:", START_YEAR, "1985", 20, 40);
166     page.row();
167       page.col();
168       page.col();
169       page.submit(ACTION, LOGIN);
170     page.table();
171     page.form();
172     page.close();
173   }
174   
175   private void acceptValues(HttpServletRequest request, HttpServletResponse response,
176       HttpSession ses) throws ServletException, IOException {
177     HtmlPage page = new HtmlPage(response);
178     String s = "Data entry - " + (String)ses.getValue(USERNAME);
179     page.title(s);
180     page.body();
181     page.center(true);
182     page.heading(1, s);
183     int count = 0;
184     int start = 0;
185     try {
186       start = ((Integer)ses.getValue(START_YEAR)).intValue();
187       count = ((Integer)ses.getValue(SALARY_COUNT)).intValue();
188     }
189     catch (Exception e) {
190     }
191     page.heading(3, count + " values entered so far, starting with " + start);
192     String servlet = request.getServletPath();
193     page.form(servlet);
194     page.table(0);
195     page.row();
196       page.col();
197       page.out.println("Year");
198       page.col();
199       page.out.println("Salary (USD)");
200     Integer moreYear = (Integer)ses.getValue(MORE_YEAR);
201     int year = 1985;
202     if (moreYear != null) year = moreYear.intValue();
203     int n = year + 5;
204     while (year < n) {
205       page.row();
206         page.col();
207         page.out.println(year);
208         page.col();
209         page.field("sal" + year, "0", 10, 10);
210       ++year;
211     }
212     ses.putValue(MORE_YEAR, new Integer(year));
213     page.table();
214     page.submit(ACTION, MORE_DATA);
215     page.form();
216     page.form(servlet, CHART, "target=_blank");
217     page.submit(ACTION, CHART);
218     page.form();
219     page.form(servlet, LOGOUT);
220     page.submit(ACTION, LOGOUT);
221     page.form();
222     page.close();
223   }
224   
225   private void appendData(HttpServletRequest request, HttpServletResponse response,
226       HttpSession ses) throws ServletException, IOException {
227     Enumeration parameterList = request.getParameterNames();
228     int count = 0;
229     try {
230       count = ((Integer)ses.getValue(SALARY_COUNT)).intValue();
231     }
232     catch (Exception e) {
233     }
234     while (parameterList.hasMoreElements()) {
235       String parm = (String)parameterList.nextElement();
236       Integer val = null;
237       try {
238         val = new Integer(request.getParameter(parm));
239       }
240       catch (NumberFormatException e) {
241         val = new Integer(0);
242       }
243       if (parm.startsWith("sal")) {
244         ++count;
245         ses.putValue(parm, val);
246         }
247     }
248     ses.putValue(SALARY_COUNT, new Integer(count));
249   }
250   
251   private void makeChart(HttpServletRequest request, HttpServletResponse response,
252       HttpSession ses) throws ServletException, IOException {
253     response.setContentType("application/pdf");
254     PdfChart chart = new PdfChart(response.getOutputStream(),
255       (int)(11.0 * PdfChart.POINTS_PER_INCH), (int)(8.5 * PdfChart.POINTS_PER_INCH));
256     chart.setTextColor(0x0);
257     chart.setFont(chart.BOLD, 18);
258     chart.println((String)ses.getValue(USERNAME));
259     chart.setFont(chart.NORMAL, 12);
260     chart.println(new Date().toString());
261     chart.drawMain(1.0, 2.0, 9.0, 5.0, 5, 4, 0xdddddd, 1.5, 0x0);
262     chart.close();
263   }
264   
265   }