1 /*
2 * $Source$
3 * $Revision$
4 *
5 * Copyright (C) 2000 William Chesters
6 *
7 * Part of Melati (http://melati.org), a framework for the rapid
8 * development of clean, maintainable web applications.
9 *
10 * Melati is free software; Permission is granted to copy, distribute
11 * and/or modify this software under the terms either:
12 *
13 * a) the GNU General Public License as published by the Free Software
14 * Foundation; either version 2 of the License, or (at your option)
15 * any later version,
16 *
17 * or
18 *
19 * b) any version of the Melati Software License, as published
20 * at http://melati.org
21 *
22 * You should have received a copy of the GNU General Public License and
23 * the Melati Software License along with this program;
24 * if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26 * GNU General Public License and visit http://melati.org to obtain the
27 * Melati Software License.
28 *
29 * Feel free to contact the Developers of Melati (http://melati.org),
30 * if you would like to work out a different arrangement than the options
31 * outlined here. It is our intention to allow Melati to be used by as
32 * wide an audience as possible.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * Contact details for copyright holder:
40 *
41 * William Chesters <williamc At paneris.org>
42 * http://paneris.org/~williamc
43 * Obrechtstraat 114, 2517VX Den Haag, The Netherlands
44 */
45
46 package org.melati.poem;
47
48 import java.util.Hashtable;
49
50 /**
51 * The quality of being searchable:
52 * <tt>yes</tt>, <tt>no</tt> or <tt>primary</tt>.
53 */
54 public final class Searchability {
55
56 /** The Id of the Searchability. */
57 public final Integer index;
58 /** Its name. */
59 private final String name;
60
61 /** Constructor. */
62 private Searchability(int index, String name) {
63 this.index = new Integer(index);
64 this.name = name;
65 }
66
67 /**
68 * The column is the only one in the table for which a
69 * special widget is provided to enter a search value.
70 * <p>
71 * There is at most one such column per table.
72 * The user is able to select matching rows without
73 * popping up a search criteria dialogue window.
74 */
75 public static final Searchability primary;
76
77 /**
78 * The column is searchable so that the user is able
79 * to enter search criteria for it included along
80 * with others when searching for rows.
81 * <p>
82 * Currently the search criteria for a column consist
83 * of at most one non-null value.
84 */
85 public static final Searchability yes;
86
87 /**
88 * The column is not searchable so the user will not
89 * be given the opportunity to enter search criteria
90 * for it.
91 */
92 public static final Searchability no;
93
94 private static int n = 0;
95
96 private static final Searchability[] searchabilities =
97 { primary = new Searchability(n++, "primary"),
98 yes = new Searchability(n++, "yes"),
99 no = new Searchability(n++, "no") };
100
101 private static final Hashtable<String, Searchability> searchabilityOfName = new Hashtable<String, Searchability>();
102
103 static {
104 for (int i = 0; i < searchabilities.length; ++i)
105 searchabilityOfName.put(searchabilities[i].name, searchabilities[i]);
106 }
107
108 /**
109 * @param index key
110 * @return the selected Searchability
111 */
112 public static Searchability forIndex(int index) {
113 return searchabilities[index];
114 }
115
116 /**
117 * @return the number of Searchabilities
118 */
119 public static int count() {
120 return searchabilities.length;
121 }
122
123 /**
124 * Thrown when an invalid {@link Searchability} level is specified,
125 * by a typing mistake in the DSD for example.
126 */
127 public static class NameUnrecognisedException
128 extends PoemException {
129 private static final long serialVersionUID = 1L;
130
131 /** The name of the requested Searchability. */
132 public String name;
133
134 /** Constructor. */
135 public NameUnrecognisedException(String name) {
136 this.name = name;
137 }
138
139 /**
140 * {@inheritDoc}
141 */
142 public String getMessage() {
143 return
144 "No searchability level found which goes by the name `" + name + "'";
145 }
146 }
147
148 /**
149 * Throws NameUnrecognisedException if not found.
150 *
151 * @param name String name of Searchability
152 * @return the Searchability
153 */
154 public static Searchability named(String name) {
155 Searchability it = (Searchability)searchabilityOfName.get(name);
156 if (it == null)
157 throw new NameUnrecognisedException(name);
158 return it;
159 }
160
161 /**
162 * @return the name and index
163 * {@inheritDoc}
164 * @see java.lang.Object#toString()
165 */
166 public String toString() {
167 return name + "/" + index;
168 }
169
170 /**
171 * @return the index
172 */
173 public Integer getIndex() {
174 return index;
175 }
176
177 /**
178 * @return the Name
179 */
180 public String getName() {
181 return name;
182 }
183
184 }