View Javadoc

1   /***
2    *  The contents of this file are subject to the Mozilla Public
3    *  License Version 1.1 (the "License"); you may not use this file
4    *  except in compliance with the License. You may obtain a copy of
5    *  the License at http://www.mozilla.org/MPL/
6    *
7    *  Software distributed under the License is distributed on an "AS
8    *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9    *  implied. See the License for the specific language governing
10   *  rights and limitations under the License.
11   *
12   *  The Original Code is pow2toolkit library.
13   *
14   *  The Initial Owner of the Original Code is
15   *  Power Of Two S.R.L. (www.pow2.com)
16   *
17   *  Portions created by Power Of Two S.R.L. are
18   *  Copyright (C) Power Of Two S.R.L.
19   *  All Rights Reserved.
20   *
21   * Contributor(s):
22   */
23  
24  package com.pow2.dao;
25  
26  import java.sql.*;
27  
28  import com.pow2.util.Util;
29  
30  
31  /***
32   *  HTML Data driven element builder
33   *
34   * @author     Luca Fossato
35   * @version    $Id: HTMLDAO.java,v 1.1.1.1 2004/08/31 20:22:46 foxat Exp $
36   */
37  public class HTMLDAO extends AbstractDAO
38  {
39    /*** an handle to the unique HTMLDAO instance. */
40    private static HTMLDAO instance = null;
41  
42  
43    /***  Default private constructor. */
44    protected HTMLDAO()
45    {
46      super();
47    }
48  
49  
50    /***
51     *  Get the unique instance of HTMLDAO class.
52     *
53     * @return    the instance of HTMLDAO class
54     */
55    public static synchronized HTMLDAO instance()
56    {
57      if (instance == null)
58        instance = new HTMLDAO();
59  
60      return instance;
61    }
62  
63  
64    /***
65     *  Get the String representation of an HTML select <code>options</code> list
66     *  retrieving the option <code>value</code> and <code>description</code>
67     *  parameters from the input table.
68     *  <br>
69     *  The <code>options</code> list is ordered by the <code>descField</code>
70     *  field vale.
71     *
72     * @param  table          the table name where to retrieve the records
73     * @param  idField        the name of the value field
74     * @param  descField      the name of the description field
75     * @param  selectedIndex  the value used to set the <code>selected</code>
76     *                        attribute to the option element whose value matches
77     *                        this entry
78     * @return                The selectOptions value
79     * @exception  Exception  Description of the Exception
80     */
81    public String getSelectOptions(String table,
82                                   String idField,
83                                   String descField,
84                                   String selectedIndex) throws Exception
85    {
86      return getSelectOptions(table, idField, descField, null, descField, selectedIndex);
87    }
88  
89  
90    /***
91     *  Get the String representation of an HTML select <code>options</code> list
92     *  retrieving the option <code>value</code> and <code>description</code>
93     *  parameters from the input table.
94     *  <br>
95     *  The <code>options</code> list is ordered by the <code>descField</code>
96     *  field vale.
97     *
98     * @param  table          the table name where to retrieve the records
99     * @param  idField        the name of the value field
100    * @param  descField      the name of the description field
101    * @param  condition      the <code>SQL WHERE</code> condition
102    * @param  selectedIndex  the value used to set the <code>selected</code>
103    *                        attribute to the option element whose value matches
104    *                        this entry
105    * @return                The selectOptions value
106    * @exception  Exception  Description of the Exception
107    */
108   public String getSelectOptions(String table,
109                                  String idField,
110                                  String descField,
111                                  String condition,
112                                  String selectedIndex) throws Exception
113   {
114     return getSelectOptions(table, idField, descField, condition, descField, selectedIndex);
115   }
116 
117 
118   /***
119    *  Get the String representation of an HTML select <code>options</code> list
120    *  retrieving the option <code>value</code> and <code>description</code>
121    *  parameters from the input table.
122    *
123    * @param  table          the table name where to retrieve the records
124    * @param  idField        the name of the value field
125    * @param  descField      the name of the description field
126    * @param  condition      the <code>SQL WHERE</code> condition
127    * @param  orderBy        the <code>SQL ORDERBY</code> statement
128    * @param  selectedIndex  the value used to set the <code>selected</code>
129    *                       attribute to the option element whose value matches
130    *                       this entry
131    * @return                The selectOptions value
132    * @exception  Exception  Description of the Exception
133    */
134   public String getSelectOptions(String table,
135                                  String idField,
136                                  String descField,
137                                  String condition,
138                                  String orderBy,
139                                  String selectedIndex) throws Exception
140   {
141     Connection        con   = null;
142     PreparedStatement ps    = null;
143     ResultSet         rs    = null;
144     int               i     = 0;
145     boolean           found = false;
146     StringBuffer      qry   = new StringBuffer();
147     StringBuffer      sb    = new StringBuffer();
148 
149     qry.append("SELECT ")
150         .append(idField)
151         .append(", ")
152         .append(descField)
153         .append(" FROM ")
154         .append(table);
155 
156     if (!Util.isNull(condition))
157       qry.append(" WHERE ").append(condition);
158 
159     if (!Util.isNull(orderBy))
160       qry.append(" ORDER BY ").append(orderBy);
161 
162     //cat.debug("::getSelectOptions - cnd = [" + qry + "]");
163 
164     try
165     {
166       con = getConnection();
167       ps = con.prepareStatement(qry.toString());
168       rs = ps.executeQuery();
169 
170       while (rs.next())
171       {
172         String idValue = rs.getString(idField);
173 
174         sb.append("<OPTION ");
175 
176         if ((selectedIndex != null) && selectedIndex.compareTo(idValue) == 0)
177           sb.append("selected ");
178 
179         sb.append(" value=\"")
180             .append(idValue)
181             .append("\">")
182             .append(rs.getString(descField))
183             .append("</option>\n");
184       }
185     }
186     finally
187     {
188       closeResources(rs, ps, con, true);
189     }
190 
191     return sb.toString();
192   }
193 }