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         */
045        public String singleRequest(String name, String value, String selector, String attributes) throws SQLException
046        {
047                StringBuffer working = new StringBuffer("<select name=\"" + name + "\" ");
048                if (attributes != null)
049                {
050                        working.append(attributes);
051                }
052                working.append(" >");
053                working.append(initialOption());
054                ResultSet rs = buildResultSet(selector);
055                while (rs.next())
056                {
057                        String temp = getValue(rs, selector);
058                        String text = getText(rs, selector);
059                        working.append("<option value=\"" + StringHelpers.escapeHTML(temp) + "\"");
060        
061                        if (temp.equalsIgnoreCase(value))
062                        {
063                                working.append(" selected=\"selected\" ");
064                        }               
065                        working.append(">");
066                        if (text != null)
067                        {
068                                working.append(StringHelpers.escapeHTML(text));
069                        }
070                        working.append("</option>");
071                }
072                working.append("</select>");
073                return new String(working);
074        }
075        /**
076         * Generate the HTML code for a SELECT tag that
077         * allows multiple values to be selected.
078         * 
079         * @param name Name attribute for SELECT tag
080         * @param values Existing values for items in tag
081         * @param selector Option to be used in creating HTML code
082         * @param attributes Additional attributes for SELECT tag
083         * @return HTML code
084         */
085        public String multipleRequest(String name, String values[], String selector, String attributes) throws SQLException
086        {
087                StringBuffer working = new StringBuffer("<select multiple=\"multiple\" name=\"" + name + "\" ");
088                if (attributes != null)
089                {
090                        working.append(attributes);
091                }
092                working.append(" >");
093                ResultSet rs = buildResultSet(selector);
094                while (rs.next())
095                {
096                        String temp = getValue(rs, selector);
097                        String text = getText(rs, selector);
098                        working.append("<option value=\"" + StringHelpers.escapeHTML(temp) + "\"");
099                        for (int i = 0; i < values.length; i++)
100                        {
101                                if (temp.equalsIgnoreCase(values[i]))
102                                {
103                                        working.append(" selected=\"selected\" ");
104                                        break;
105                                }       
106                        }
107                        working.append(">");
108                        if (text != null)
109                        {
110                                working.append(StringHelpers.escapeHTML(text));
111                        }
112                        
113                        working.append("</option>");
114                }
115                working.append("</select>");
116                return new String(working);
117        }
118        /**
119         * Determine the value for option
120         * @param rs Result set containing information for menu
121         * @param selector Optional selector for controlling HTML generation
122         * @return Text for value
123         * @throws SQLException
124         */
125        protected String getValue(ResultSet rs, String selector) throws SQLException
126        {
127                String working = rs.getString("VALUE");
128                if (rs.wasNull())
129                {
130                        return (String) null;
131                }
132                else
133                {
134                        return working;
135                }
136        }
137        /**
138         * Determine text value for the option
139         * @param rs Result set containing information
140         * @param selector Optional selector for controlling HTML generation
141         * @return Text for caption
142         * @throws SQLException
143         */
144        protected String getText(ResultSet rs, String selector) throws SQLException
145        {
146                String working = rs.getString("TEXT");
147                if (rs.wasNull())
148                {
149                        return (String) null;
150                }
151                else
152                {
153                        return working;
154                }
155        }
156        /**
157         * Creates result set that is used to generate select tag.
158         * @return Result set
159         */
160        protected ResultSet buildResultSet() throws SQLException
161        {
162                return buildResultSet((String) null);
163        }
164        /**
165         * Generate the HTML code for a SELECT tag
166         * that allows a single value to be selected.
167         * 
168         * @param name Name attribute for SELECT tag
169         * @return HTML code
170         */
171        public String singleRequest(String name) throws SQLException
172        {
173                return singleRequest(name, (String) null, (String) null, (String) null);
174        }
175        /**
176         * Generate the HTML code for a SELECT tag
177         * that allows a single value to be selected.
178         * 
179         * @param name Name attribute for SELECT tag
180         * @param value Existing value for selector
181         * @return HTML code
182         */
183        public String singleRequest(String name, String value) throws SQLException
184        {
185                return singleRequest(name, value, (String) null, (String) null);
186        }
187        /**
188         * Generate the HTML code for a SELECT tag that
189         * allows multiple values to be selected.
190         * 
191         * @param name Name attribute for SELECT tag
192         * @return HTML code
193         */
194        public String multipleRequest(String name) throws SQLException
195        {
196                return multipleRequest(name, (String[]) null, (String) null, (String) null);
197        }
198        /**
199         * Generate the HTML code for a SELECT tag that
200         * allows multiple values to be selected.
201         * 
202         * @param name Name attribute for SELECT tag
203         * @param values Existing values for items in tag
204         * @return HTML code
205         */
206        public String multipleRequest(String name, String[] values) throws SQLException
207        {
208                return multipleRequest(name, values, (String) null, (String) null);
209        }
210}