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 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 * <caption> 60 * Field summary for SQL table <code>ValueInfo</code> 61 * </caption> 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 * See 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 * See 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 @SuppressWarnings("rawtypes") 132 public SQLPoemType<?> getType() { 133 if (poemType == null) { 134 PoemTypeFactory f = getTypefactory(); 135 136 if (f == null) { // FIXME Test this, remove if possible 137 // this can happen before we have been fully initialised 138 // it's convenient to return the "most general" type available ... 139 return (SQLPoemType)StringPoemType.nullableInstance; 140 } 141 poemType = f.typeOf(getDatabase(), toTypeParameter()); 142 } 143 144 return poemType; 145 } 146 147 /** 148 * @param nullableP whether nullable 149 * @return A type 150 */ 151 @SuppressWarnings({ "unchecked", "rawtypes" }) 152 private SQLPoemType getRangeEndType(boolean nullableP) { 153 // FIXME a little inefficient, but rarely used; also relies on BasePoemType 154 // should have interface for ranged type ... 155 156 SQLPoemType t = getType(); 157 158 if (t instanceof BasePoemType) { 159 BasePoemType unrangedType = (BasePoemType)((BasePoemType)t).clone(); 160 unrangedType.setRawRange(null, null); 161 return (SQLPoemType)unrangedType.withNullable(nullableP); 162 } 163 else 164 return null; 165 } 166 167 @SuppressWarnings({ "rawtypes", "unchecked" }) 168 private FieldAttributes<?> fieldAttributesRenamedAs(FieldAttributes<?> c, 169 PoemType<?> type) { 170 return new BaseFieldAttributes( 171 c.getName(), c.getDisplayName(), c.getDescription(), type, 172 width == null ? 12 : width.intValue(), 173 height == null ? 1 : height.intValue(), 174 renderinfo, 175 false, // indexed 176 usereditable == null ? true : usereditable.booleanValue(), 177 true // usercreateable 178 ); 179 } 180 181 /** 182 * @param c a FieldAttributes eg a Field 183 * @return a new FieldAttributes 184 */ 185 public FieldAttributes<?> fieldAttributesRenamedAs(FieldAttributes<?> c) { 186 return fieldAttributesRenamedAs(c, getType()); 187 } 188 189 /** 190 * @param c a Column with a Range 191 * @return a Field representing the end of the Range 192 */ 193 @SuppressWarnings({ "unchecked", "rawtypes" }) 194 private Field<String> rangeEndField(Column<?> c) { 195 SQLPoemType<?> unrangedType = getRangeEndType(c.getType().getNullable()); 196 197 if (unrangedType == null) 198 return null; 199 else { 200 Object raw; 201 try { 202 raw = unrangedType.rawOfString((String)c.getRaw_unsafe(this)); 203 } 204 catch (Exception e) { 205 c.setRaw_unsafe(this, null); 206 raw = null; 207 throw new AppBugPoemException("Found a bad entry for " + c + " in " + 208 getTable().getName() + "/" + troid() + ": " + 209 "solution is to null it out ...", e); 210 } 211 212 return new Field( 213 raw, 214 fieldAttributesRenamedAs(c, unrangedType)); 215 } 216 } 217 218 /** 219 * {@inheritDoc} 220 * @see org.melati.poem.generated.ValueInfoBase#getRangelow_stringField() 221 */ 222 public Field<String> getRangelow_stringField() { 223 Field<String> it = rangeEndField(getValueInfoTable().getRangelow_stringColumn()); 224 return it != null ? it : super.getRangelow_stringField(); 225 } 226 227 /** 228 * {@inheritDoc} 229 * @see org.melati.poem.generated.ValueInfoBase#getRangelimit_stringField() 230 */ 231 public Field<String> getRangelimit_stringField() { 232 Field<String> it = (Field<String>)rangeEndField(getValueInfoTable().getRangelimit_stringColumn()); 233 return it != null ? it : super.getRangelimit_stringField(); 234 } 235 236 /** 237 * {@inheritDoc} 238 * @see org.melati.poem.generated.ValueInfoBase#setRangelow_string(java.lang.String) 239 */ 240 public void setRangelow_string(String value) { 241 boolean nullableL = getValueInfoTable().getRangelow_stringColumn(). 242 getType().getNullable(); 243 PoemType<?> unrangedType = getRangeEndType(nullableL); 244 if (unrangedType != null) 245 value = unrangedType.stringOfRaw(unrangedType.rawOfString(value)); 246 super.setRangelow_string(value); 247 } 248 249 /** 250 * {@inheritDoc} 251 * @see org.melati.poem.generated.ValueInfoBase#setRangelimit_string(java.lang.String) 252 */ 253 public void setRangelimit_string(String value) { 254 boolean nullableL = getValueInfoTable().getRangelimit_stringColumn(). 255 getType().getNullable(); 256 PoemType<?> unrangedType = getRangeEndType(nullableL); 257 if (unrangedType != null) 258 value = unrangedType.stringOfRaw(unrangedType.rawOfString(value)); 259 super.setRangelimit_string(value); 260 } 261 }