1 /*
2 * $Source: /usr/cvsroot/melati/poem/src/main/java/org/melati/poem/ValueInfo.java,v $
3 * $Revision: 1.17 $
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 org.melati.poem.generated.ValueInfoBase;
49
50 /**
51 * Abstract persistent generated from Poem.dsd
52 * and extended to cover {@link Setting} and {@link ColumnInfo}.
53 *
54 * Melati POEM generated, programmer modifiable stub
55 * for a <code>Persistent</code> <code>ValueInfo</code> object.
56 *
57 *
58 * <table>
59 * <tr><th colspan='3'>
60 * Field summary for SQL table <code>ValueInfo</code>
61 * </th></tr>
62 * <tr><th>Name</th><th>Type</th><th>Description</th></tr>
63 * <tr><td> displayname </td><td> String </td><td> A user-friendly name for
64 * the field </td></tr>
65 * <tr><td> description </td><td> String </td><td> A brief description of the
66 * field's function </td></tr>
67 * <tr><td> usereditable </td><td> Boolean </td><td> Whether it makes sense
68 * for the user to update the field's value </td></tr>
69 * <tr><td> typefactory </td><td> PoemTypeFactory </td><td> The field's
70 * Melati type </td></tr>
71 * <tr><td> nullable </td><td> Boolean </td><td> Whether the field can be
72 * empty </td></tr>
73 * <tr><td> size </td><td> Integer </td><td> For character fields, the
74 * maximum number of characters that can be stored, (-1 for unlimited)
75 * </td></tr>
76 * <tr><td> width </td><td> Integer </td><td> A sensible width for text boxes
77 * used for entering the field, where appropriate </td></tr>
78 * <tr><td> height </td><td> Integer </td><td> A sensible height for text
79 * boxes used for entering the field, where appropriate </td></tr>
80 * <tr><td> precision </td><td> Integer </td><td> Precision (total number of
81 * digits) for fixed-point numbers </td></tr>
82 * <tr><td> scale </td><td> Integer </td><td> Scale (number of digits after
83 * the decimal) for fixed-point numbers </td></tr>
84 * <tr><td> renderinfo </td><td> String </td><td> The name of the Melati
85 * templet (if not the default) to use for input controls for the field
86 * </td></tr>
87 * <tr><td> rangelow_string </td><td> String </td><td> The low end of the
88 * range of permissible values for the field </td></tr>
89 * <tr><td> rangelimit_string </td><td> String </td><td> The (exclusive)
90 * limit of the range of permissible values for the field </td></tr>
91 * </table>
92 *
93 * @generator org.melati.poem.prepro.TableDef#generateMainJava
94 */
95 public class ValueInfo extends ValueInfoBase {
96
97 /**
98 * Constructor
99 * for a <code>Persistent</code> <code>ValueInfo</code> object.
100 *
101 * @generator org.melati.poem.prepro.TableDef#generateMainJava
102 */
103 public ValueInfo() { }
104
105 // programmer's domain-specific code here
106
107 /**
108 * @return a Type Parameter based upon our size and nullability
109 */
110 public PoemTypeFactory.Parameter toTypeParameter() {
111 final Boolean nullableL = getNullable_unsafe();
112 final Integer sizeL = getSize_unsafe();
113 return
114 new PoemTypeFactory.Parameter() {
115 public boolean getNullable() {
116 return nullableL == null || nullableL.booleanValue();
117 }
118
119 public int getSize() {
120 return sizeL == null ? -1 : sizeL.intValue();
121 }
122 };
123 }
124
125 private SQLPoemType poemType = null;
126
127 /**
128 * NOTE A type cannot be changed once initialised.
129 * @return our SQLPoemType
130 */
131 public SQLPoemType getType() {
132 if (poemType == null) {
133 PoemTypeFactory f = getTypefactory();
134
135 if (f == null) {
136 // this can happen before we have been fully initialised
137 // it's convenient to return the "most general" type available ...
138 return StringPoemType.nullableInstance;
139 }
140 poemType = f.typeOf(getDatabase(), toTypeParameter());
141 }
142
143 return poemType;
144 }
145
146 /**
147 * @param nullableP whether nullable
148 * @return A type
149 */
150 private SQLPoemType getRangeEndType(boolean nullableP) {
151 // FIXME a little inefficient, but rarely used; also relies on BasePoemType
152 // should have interface for ranged type ...
153
154 SQLPoemType t = getType();
155
156 if (t instanceof BasePoemType) {
157 BasePoemType unrangedType = (BasePoemType)((BasePoemType)t).clone();
158 unrangedType.setRawRange(null, null);
159 return (SQLPoemType)unrangedType.withNullable(nullableP);
160 }
161 else
162 return null;
163 }
164
165 private FieldAttributes fieldAttributesRenamedAs(FieldAttributes c,
166 PoemType type) {
167 return new BaseFieldAttributes(
168 c.getName(), c.getDisplayName(), c.getDescription(), type,
169 width == null ? 12 : width.intValue(),
170 height == null ? 1 : height.intValue(),
171 renderinfo,
172 false, // indexed
173 usereditable == null ? true : usereditable.booleanValue(),
174 true // usercreateable
175 );
176 }
177
178 /**
179 * @param c a FieldAttributes eg a Field
180 * @return a new FieldAttributes
181 */
182 public FieldAttributes fieldAttributesRenamedAs(FieldAttributes c) {
183 return fieldAttributesRenamedAs(c, getType());
184 }
185
186 /**
187 * @param c a Column with a Range
188 * @return a Field representing the end of the Range
189 */
190 private Field rangeEndField(Column c) {
191 SQLPoemType unrangedType = getRangeEndType(c.getType().getNullable());
192
193 if (unrangedType == null)
194 return null;
195 else {
196 Object raw;
197 try {
198 raw = unrangedType.rawOfString((String)c.getRaw_unsafe(this));
199 }
200 catch (Exception e) {
201 c.setRaw_unsafe(this, null);
202 raw = null;
203 throw new AppBugPoemException("Found a bad entry for " + c + " in " +
204 getTable().getName() + "/" + troid() + ": " +
205 "solution is to null it out ...", e);
206 }
207
208 return new Field(
209 raw,
210 fieldAttributesRenamedAs(c, unrangedType));
211 }
212 }
213
214 /**
215 * {@inheritDoc}
216 * @see org.melati.poem.generated.ValueInfoBase#getRangelow_stringField()
217 */
218 public Field getRangelow_stringField() {
219 Field it = rangeEndField(getValueInfoTable().getRangelow_stringColumn());
220 return it != null ? it : super.getRangelow_stringField();
221 }
222
223 /**
224 * {@inheritDoc}
225 * @see org.melati.poem.generated.ValueInfoBase#getRangelimit_stringField()
226 */
227 public Field getRangelimit_stringField() {
228 Field it = rangeEndField(getValueInfoTable().getRangelimit_stringColumn());
229 return it != null ? it : super.getRangelimit_stringField();
230 }
231
232 /**
233 * {@inheritDoc}
234 * @see org.melati.poem.generated.ValueInfoBase#setRangelow_string(java.lang.String)
235 */
236 public void setRangelow_string(String value) {
237 boolean nullableL = getValueInfoTable().getRangelow_stringColumn().
238 getType().getNullable();
239 PoemType unrangedType = getRangeEndType(nullableL);
240 if (unrangedType != null)
241 value = unrangedType.stringOfRaw(unrangedType.rawOfString(value));
242 super.setRangelow_string(value);
243 }
244
245 /**
246 * {@inheritDoc}
247 * @see org.melati.poem.generated.ValueInfoBase#setRangelimit_string(java.lang.String)
248 */
249 public void setRangelimit_string(String value) {
250 boolean nullableL = getValueInfoTable().getRangelimit_stringColumn().
251 getType().getNullable();
252 PoemType unrangedType = getRangeEndType(nullableL);
253 if (unrangedType != null)
254 value = unrangedType.stringOfRaw(unrangedType.rawOfString(value));
255 super.setRangelimit_string(value);
256 }
257 }