001package bradleyross.j2ee.servlets;
002import java.sql.ResultSet;
003import java.sql.SQLException;
004import bradleyross.library.database.DatabaseProperties;
005import bradleyross.library.helpers.StringHelpers;
006/**
007 * Generate the code for a pull-down list to be placed
008 * in a web page.
009 * 
010 * @author Bradley Ross
011 *
012 */
013public abstract class PulldownList 
014{
015        /**
016         * Object containing database connection properties.
017         * 
018         * <p>The value should be set by the constructor.</p>
019         */
020        protected DatabaseProperties data;
021        /**
022         * Creates result set that is used to generate select tag.
023         * @param selector Option value that can be used to control generation of tag
024         * @return Result set
025         */
026        protected abstract ResultSet buildResultSet(String selector);
027        /**
028         * Used to generate initial option in select tag.
029         * @return HTML code for initial option tag
030         */
031        protected String initialOption()
032        {
033                return new String();
034        }
035        /**
036         * Generate the HTML code for a SELECT tag
037         * that allows a single value to be selected.
038         * 
039         * @param name Name attribute for SELECT tag
040         * @param value Existing value for selector
041         * @param selector Option to be used in creating HTML code
042         * @param attributes Code for additional attributes for SELECT tag
043         * @return HTML code
044         * @throws SQLException if database error
045         */
046        public String singleRequest(String name, String value, String selector, String attributes) throws SQLException
047        {
048                StringBuffer working = new StringBuffer("<select name=\"" + name + "\" ");
049                if (attributes != null)
050                {
051                        working.append(attributes);
052                }
053                working.append(" >");
054                working.append(initialOption());
055                ResultSet rs = buildResultSet(selector);
056                while (rs.next())
057                {
058                        String temp = getValue(rs, selector);
059                        String text = getText(rs, selector);
060                        working.append("<option value=\"" + StringHelpers.escapeHTML(temp) + "\"");
061        
062                        if (temp.equalsIgnoreCase(value))
063                        {
064                                working.append(" selected=\"selected\" ");
065                        }               
066                        working.append(">");
067                        if (text != null)
068                        {
069                                working.append(StringHelpers.escapeHTML(text));
070                        }
071                        working.append("</option>");
072                }
073                working.append("</select>");
074                return new String(working);
075        }
076        /**
077         * Generate the HTML code for a SELECT tag that
078         * allows multiple values to be selected.
079         * 
080         * @param name Name attribute for SELECT tag
081         * @param values Existing values for items in tag
082         * @param selector Option to be used in creating HTML code
083         * @param attributes Additional attributes for SELECT tag
084         * @return HTML code
085         * @throws SQLException if database problems
086         */
087        public String multipleRequest(String name, String values[], String selector, String attributes) throws SQLException
088        {
089                StringBuffer working = new StringBuffer("<select multiple=\"multiple\" name=\"" + name + "\" ");
090                if (attributes != null)
091                {
092                        working.append(attributes);
093                }
094                working.append(" >");
095                ResultSet rs = buildResultSet(selector);
096                while (rs.next())
097                {
098                        String temp = getValue(rs, selector);
099                        String text = getText(rs, selector);
100                        working.append("<option value=\"" + StringHelpers.escapeHTML(temp) + "\"");
101                        for (int i = 0; i < values.length; i++)
102                        {
103                                if (temp.equalsIgnoreCase(values[i]))
104                                {
105                                        working.append(" selected=\"selected\" ");
106                                        break;
107                                }       
108                        }
109                        working.append(">");
110                        if (text != null)
111                        {
112                                working.append(StringHelpers.escapeHTML(text));
113                        }
114                        
115                        working.append("</option>");
116                }
117                working.append("</select>");
118                return new String(working);
119        }
120        /**
121         * Determine the value for option
122         * @param rs Result set containing information for menu
123         * @param selector Optional selector for controlling HTML generation
124         * @return Text for value
125         * @throws SQLException  if database problems
126         */
127        protected String getValue(ResultSet rs, String selector) throws SQLException
128        {
129                String working = rs.getString("VALUE");
130                if (rs.wasNull())
131                {
132                        return (String) null;
133                }
134                else
135                {
136                        return working;
137                }
138        }
139        /**
140         * Determine text value for the option
141         * @param rs Result set containing information
142         * @param selector Optional selector for controlling HTML generation
143         * @return Text for caption
144         * @throws SQLException if database problems
145         */
146        protected String getText(ResultSet rs, String selector) throws SQLException
147        {
148                String working = rs.getString("TEXT");
149                if (rs.wasNull())
150                {
151                        return (String) null;
152                }
153                else
154                {
155                        return working;
156                }
157        }
158        /**
159         * Creates result set that is used to generate select tag.
160         * @return Result set
161         * @throws SQLException if database problems
162         */
163        protected ResultSet buildResultSet() throws SQLException
164        {
165                return buildResultSet((String) null);
166        }
167        /**
168         * Generate the HTML code for a SELECT tag
169         * that allows a single value to be selected.
170         * 
171         * @param name Name attribute for SELECT tag
172         * @return HTML code
173         * @throws SQLException if database errors
174         */
175        public String singleRequest(String name) throws SQLException
176        {
177                return singleRequest(name, (String) null, (String) null, (String) null);
178        }
179        /**
180         * Generate the HTML code for a SELECT tag
181         * that allows a single value to be selected.
182         * 
183         * @param name Name attribute for SELECT tag
184         * @param value Existing value for selector
185         * @return HTML code
186         * @throws SQLException if database errors
187         */
188        public String singleRequest(String name, String value) throws SQLException
189        {
190                return singleRequest(name, value, (String) null, (String) null);
191        }
192        /**
193         * Generate the HTML code for a SELECT tag that
194         * allows multiple values to be selected.
195         * 
196         * @param name Name attribute for SELECT tag
197         * @return HTML code
198         * @throws SQLException if database problems
199         */
200        public String multipleRequest(String name) throws SQLException
201        {
202                return multipleRequest(name, (String[]) null, (String) null, (String) null);
203        }
204        /**
205         * Generate the HTML code for a SELECT tag that
206         * allows multiple values to be selected.
207         * 
208         * @param name Name attribute for SELECT tag
209         * @param values Existing values for items in tag
210         * @return HTML code
211         * @throws SQLException if database problems
212         */
213        public String multipleRequest(String name, String[] values) throws SQLException
214        {
215                return multipleRequest(name, values, (String) null, (String) null);
216        }
217}