Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

JspTool.java

Go to the documentation of this file.
00001 package net.threebit.utils.sosc;
00002 
00003 /*
00004         Copyright 2003 Shawn Deleurme
00005         Copyright 2003 Kevin O'Donnell
00006 
00007         This program is free software; you can redistribute it and/or modify
00008         it under the terms of the GNU General Public License as published by
00009         the Free Software Foundation; either version 2 of the License, or
00010         (at your option) any later version.
00011 
00012         This program is distributed in the hope that it will be useful,
00013         but WITHOUT ANY WARRANTY; without even the implied warranty of
00014         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015         GNU General Public License for more details.
00016 
00017         You should have received a copy of the GNU General Public License
00018         along with this program; if not, write to the Free Software
00019         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 */
00021 
00022 import com.oreilly.servlet.*;
00023 import java.io.*;
00024 import java.sql.*;
00025 import java.util.*;
00026 import javax.servlet.jsp.*;
00027 import javax.servlet.http.*;
00028 
00034 public class JspTool {
00035 
00036         private HttpServletRequest request;
00037         private HttpServletResponse response;
00038         private JspWriter out;
00039         private MultipartRequest multi = null;
00040 
00041         public int minYear = 2000;
00042         public int maxYear = 2005;
00043 
00044         public JspTool (HttpServletRequest request, HttpServletResponse response, JspWriter out) throws Exception {
00045                 this.request = request;
00046                 this.response = response;
00047                 this.out = out;
00048         }
00049 
00050         /*
00051          * Wrapper for request.getParameter(String s) but guarantees that nulls are nuver
00052          * returned. "" is returned in the place of nulls
00053          */
00054         public String getParameter (String name) {
00055                 String result;
00056                 if (multi != null) {
00057                         result = multi.getParameter(name);
00058                 }
00059                 else {
00060                         result = request.getParameter(name);
00061                 }
00062                 if (result == null) { return ""; }
00063                 return result;
00064         }
00065 
00069         public String getParameter (String name, String def) throws Exception {
00070                 String result = getParameter(name);
00071                 if (result.equals("")) {
00072                         return def;
00073                 }
00074                 return result;
00075         }
00076 
00080         public String[] getParameterValues (String name) throws Exception {
00081                 return getParameterValues(name,null);
00082         }
00083 
00087         public String[] getParameterValues (String name, String def) throws Exception {
00088                 String[] result = null;
00089                 if (multi != null) {
00090                         result = multi.getParameterValues(name);
00091                 }
00092                 else {
00093                         result = request.getParameterValues(name);
00094                 }
00095                 if (result == null || result.length == 0) {
00096                         if (def != null) { result = new String[] { def }; }
00097                         else { result = new String[] { }; }
00098                 }
00099                 return result;
00100         }
00101 
00105         public String getJspPath (javax.servlet.ServletContext application) {
00106                 return application.getRealPath(request.getRequestURI());
00107         }
00108 
00112         public String getJspBaseDir (javax.servlet.ServletContext application) {
00113                 File file = new File(application.getRealPath(request.getRequestURI()));
00114                 return file.getParent();
00115         }
00116 
00120         public MultipartRequest multipartRequest(String uploadDir, int maxSize) throws Exception {
00121                 if (multi == null) { multi = new MultipartRequest( request, uploadDir, maxSize); }
00122                 return multi;
00123         }
00124 
00128         public int[] today() {
00129                 Calendar c = Calendar.getInstance();
00130                 int year = c.get(c.YEAR);
00131                 int month = c.get(c.MONTH);
00132                 int day = c.get(c.DAY_OF_MONTH);
00133                 month ++;
00134                 return new int[] {year,month,day};
00135         }
00136 
00140         public String dateSelect (String prefix) throws Exception {
00141                 if (prefix == null) { prefix = ""; }
00142                 else { prefix = prefix + "-"; }
00143 
00144                 // Get the current date
00145                 int[] today = today();
00146 
00147                 // Determine the current valures of 'name-year', etc from the current request
00148                 int year = Integer.parseInt(getParameter(prefix+"year",""+today[0]));
00149                 int month = Integer.parseInt(getParameter(prefix+"month",""+today[1]));
00150                 int day = Integer.parseInt(getParameter(prefix+"day",""+today[2]));
00151 
00152                 StringBuffer r = new StringBuffer(1000);
00153 
00154                 // year
00155                 r.append("<select name='" + prefix + "year'>\n");
00156                 for (int x = minYear; x <= maxYear; x++) {
00157                         r.append("\t<option value='" + x + "'" + (x == year ? " selected='yes' " : "") + ">" + x + "\n");
00158                 }
00159                 r.append("</select>\n");
00160 
00161                 // month
00162                 r.append("<select name='" + prefix + "month'>\n");
00163                 for (int x = 1; x <= 12; x++) {
00164                         r.append("\t<option value='" + (x<10?"0"+x:""+x) + "'" + (x == month ? " selected='yes' " : "") + ">" + (x<10?"0"+x:""+x) + "\n");
00165                 }
00166                 r.append("</select>\n");
00167 
00168                 // day
00169                 r.append("<select name='" + prefix + "day'>\n");
00170                 for (int x = 1; x <= 31; x++) {
00171                         r.append("\t<option value='" + (x<10?"0"+x:""+x) + "'" + (x == day ? " selected='yes' " : "") + ">" + (x<10?"0"+x:""+x) + "\n");
00172                 }
00173                 r.append("</select>\n");
00174 
00175                 return r.toString();
00176         }
00177 
00181         public String dateFromForm (String prefix) throws Exception {
00182                 if (prefix == null) { prefix = ""; }
00183                 else { prefix = prefix + "-"; }
00184                 String year = getParameter(prefix+"year");
00185                 String month = getParameter(prefix+"month");
00186                 String day = getParameter(prefix+"day");
00187                 String result = year + "-" + month + "-" + day;
00188                 if (result.equals("--")) { return ""; }
00189                 return result;
00190         }
00191 
00198   public Map fixMap( Map map ) throws Exception {
00199     Map m = new HashMap ( map.size() );
00200     for ( Iterator i = map.keySet().iterator();i.hasNext(); ){
00201       String key = i.next().toString();
00202       String value = ((String[])map.get(key))[0] ;
00203       m.put ( key, value );
00204     }
00205     return m;
00206   }
00207 
00211         public String resultSetToHTMLTable (ResultSet r, String title) throws Exception {
00212                 return resultSetToHTMLTable(r,title,null,null,null,null,true);
00213         }
00214 
00218         public String resultSetToHTMLTable
00219                 (ResultSet r, String title, String tableClass, String headingClass, String thClass, String tdClass, boolean autoSum) 
00220                 throws Exception {
00221 
00222                 if (headingClass == null) { headingClass = "style='padding: 10px; text-align: left; border: 1px solid #c0c0c0;'"; }
00223                 if (tableClass == null) { tableClass = "style='border-collapse: collapse;'"; }
00224                 if (thClass == null) { thClass = "style='border: 1px solid #c0c0c0; padding: 5px;'"; }
00225                 if (tdClass == null) { tdClass = "style='padding-left: 5px; padding-right: 5px; text-align: right;'"; }
00226 
00227                 StringWriter sw = new StringWriter();
00228                 PrintWriter pw = new PrintWriter(sw);
00229                 int columnCount = r.getMetaData().getColumnCount();
00230                 String[] columnNames = new String[ columnCount ];
00231                 int[] columnTypes = new int[ columnCount ];
00232                 Object[] columnSums = new Object[ columnCount ];
00233 
00234                 pw.println("<table " + tableClass + ">");
00235                 pw.println("<tr><th colspan='" + columnCount + "' " + headingClass + ">" + title + "</th></tr>");
00236                 pw.println("<tr>");
00237                 ResultSetMetaData md = null;
00238                 for (int i=1; i <= columnCount; i++) {
00239                         try {
00240                         md = r.getMetaData();   
00241                         columnNames[i-1] = md.getColumnName(i);
00242                         columnTypes[i-1] = md.getColumnType(i);
00243                         pw.println("<th " + thClass + ">" + columnNames[i-1] + "</th>");
00244                         } catch (Exception e) {
00245                                 throw new Exception("column: " + i,e);
00246                         }
00247                 }
00248                 pw.println("</tr>");
00249 
00250                 // Do we need to advance?
00251                 if (r.getRow() == 0) { r.first(); }
00252                 // If still no valid row then report no data and exit
00253                 if (r.getRow() != 0) {
00254                         do {
00255                                 pw.println("<tr>");
00256                                 for (int i=1; i <= columnCount; i++) {
00257                                         pw.println("<td " + tdClass + ">" + r.getObject(i) + "</td>");
00258                                         if (autoSum) {
00259                                                 Object o = columnSums[i-1];
00260                                                 if (columnTypes[i-1] == Types.INTEGER) {
00261                                                         if (o == null) { o = new Integer(0); }
00262                                                         o = new Integer(((Integer) o).intValue() + r.getInt(i));
00263                                                 }
00264                                                 else if (columnTypes[i-1] == Types.NUMERIC) {
00265                                                         if (o == null) { o = new Double(0); }
00266                                                         o = new Double(((Double) o).doubleValue() + r.getDouble(i));
00267                                                 }
00268                                                 else {
00269                                                         o = "<!-- Unknown column type " + columnTypes[i-1] + " -->";
00270                                                 }
00271                                                 columnSums[i-1] = o;
00272                                         }
00273                                 }
00274                                 pw.println("</tr>");
00275                         } while (r.next());
00276                 }
00277                 else {
00278                         pw.println("<tr><th colspan='" + columnCount + "' " + headingClass + ">No Data</th></tr>");
00279                 }
00280 
00281                 if (autoSum) {
00282                         pw.println("<tr>");
00283                         for (int i=1; i <= columnCount; i++) {
00284                                 pw.println("<td " + tdClass + "><strong>" + columnSums[i-1] + "</strong></td>");
00285                         }
00286                         pw.println("</tr>");
00287                 }
00288 
00289                 pw.println("</table>");
00290                 return sw.getBuffer().toString();
00291         }
00292 
00293 
00294 
00295 }

Generated on Mon Jul 14 17:19:20 2003 for SOSC by doxygen1.2.15