View Javadoc
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.prepro;
47  
48  import java.util.Vector;
49  import java.io.Writer;
50  import java.io.IOException;
51  
52  /**
53   * A definition of a <tt>ColumnTypePoemType</tt>.
54   * 
55   * A <tt>ColumnType</tt> is a metadata field, is used in <tt>Poem.dsd</tt> 
56   * and is not usually used in normal DSD files.
57   */ 
58  public class ColumnTypeFieldDef extends FieldDef {
59  
60   /**
61    * Constructor.
62    *
63    * @param lineNo       the line number in the DSD file
64    * @param table        the {@link TableDef} that this <code>Field</code> is 
65    *                     part of 
66    * @param name         the name of this field
67    * @param displayOrder where to place this field in a list
68    * @param qualifiers   all the qualifiers of this field
69    * 
70    * @throws IllegalityException if a semantic inconsistency is detected
71    */
72    public ColumnTypeFieldDef(int lineNo, TableDef table, String name, int displayOrder,
73                              Vector<FieldQualifier> qualifiers) throws IllegalityException {
74      super(lineNo, table, name, "PoemTypeFactory", "Integer", displayOrder, qualifiers);
75      table.addImport("org.melati.poem.PoemTypeFactory", 
76                      "persistent");
77      table.addImport("org.melati.poem.PoemTypeFactory", 
78                      "table");
79      table.addImport("org.melati.poem.ColumnTypePoemType", 
80                      "table");
81    }
82  
83   /**
84    * @param w The base table java file.
85    * @throws IOException 
86    *           if something goes wrong with the file system
87    */   
88    protected void generateColRawAccessors(Writer w) throws IOException {
89      super.generateColRawAccessors(w);
90  
91      w.write(
92        "\n" +
93        "          public Object getRaw(Persistent g)\n" +
94        "              throws AccessPoemException {\n" +
95        "            return ((" + shortestUnambiguousClassname + ")g).get" + capitalisedName + "Code();\n" +
96        "          }\n" +
97        "\n");
98      w.write(
99        "          public void setRaw(Persistent g, Object raw)\n" +
100       "              throws AccessPoemException {\n" +
101       "            ((" + shortestUnambiguousClassname + ")g).set" + capitalisedName + "Code((" +
102                        rawType + ")raw);\n" +
103       "          }\n");
104   }
105 
106  /**
107   * @param w The base persistent java file.
108   * @throws IOException 
109   *           if something goes wrong with the file system
110   */   
111   public void generateBaseMethods(Writer w) throws IOException {
112     super.generateBaseMethods(w);
113 
114     //String targetTableAccessorMethod = "get" + type + "Table";
115     //String targetSuffix = type;
116 
117     w.write(
118       "\n /**\n"
119       + "  * Retrieves the <code>" 
120       + capitalisedName 
121       + "</code> value as an <code>Integer</code> for this " 
122       + "<code>Column</code> of the <code>"
123       + table.nameFromDsd 
124       + "</code> <code>Table</code>.\n"
125       + ((description != null) ? "  * Field description: \n" 
126                                  + DSD.javadocFormat(2, 3, description)
127                                  + "  * \n"
128                                : "")
129       + "  * \n"
130       + "  * Generated by " 
131       + "org.melati.poem.prepro.ColumnTypeFieldDef" 
132       + "#generateBaseMethods \n"
133       + "  * @throws AccessPoemException \n"
134       + "  *         if the current <code>AccessToken</code> \n"
135       + "  *         does not confer read access rights\n"
136       + "  * @return the <code>" + capitalisedName 
137       + "</code> value of this <code>Column</code>\n"
138       + "  */\n");
139     w.write("\n" +
140             "  public Integer get" + capitalisedName + "Code()\n" +
141             "      throws AccessPoemException {\n" +
142             "    readLock();\n" +
143             "    return get" + capitalisedName + "_unsafe();\n" +
144             "  }\n");
145     w.write(
146       "\n /**\n"
147       + "  * Sets the <code>Integer</code> <code>" 
148       + capitalisedName 
149       + "</code> value  for this <code>" 
150       + table.nameFromDsd
151       + "</code> <code>Column</code> of the <code>"
152       + table.nameFromDsd 
153       + "</code> <code>Table</code>.\n" 
154       + ((description != null) ? "  * Field description: \n" 
155                                  + DSD.javadocFormat(2, 3, description)
156                                  + "  * \n"
157                                : "")
158       + "  * \n"
159       + "  * Generated by " 
160       + "org.melati.poem.prepro.ColumnTypeFieldDef" 
161       + "#generateBaseMethods \n"
162       + "  * @param raw the value to set \n"
163       + "  * @throws AccessPoemException \n"
164       + "  *         if the current <code>AccessToken</code> \n"
165       + "  *         does not confer write access rights\n"
166       + "  */\n");
167     w.write("\n" +
168             "  public void set" + capitalisedName + "Code(Integer raw)\n" +
169             "      throws AccessPoemException {\n" +
170             "    " + rootTableAccessorMethod + "().get" + capitalisedName + "Column()." +
171                      "getType().assertValidRaw(raw);\n" +
172             "    writeLock();\n" +
173             "    set" + capitalisedName + "_unsafe(raw);\n" +
174             "  }\n" +
175             "\n");
176     w.write(
177       "\n /**\n"
178       + "  * Retrieves the <code>" 
179       + capitalisedName 
180       + "</code> value as an <code>" + typeShortName + "</code> for this " 
181       + "<code>Column</code> of the <code>"
182       + table.nameFromDsd 
183       + "</code> <code>Table</code>.\n"
184       + ((description != null) ? "  * Field description: \n" 
185                                  + DSD.javadocFormat(2, 3, description)
186                                  + "  * \n"
187                                : "")
188       + "  * \n"
189       + "  * Generated by " 
190       + "org.melati.poem.prepro.ColumnTypeFieldDef" 
191       + "#generateBaseMethods \n"
192       + "  * @throws AccessPoemException \n"
193       + "  *         if the current <code>AccessToken</code> \n"
194       + "  *         does not confer read access rights\n"
195       + "  * @return the <code>" + capitalisedName 
196       + "</code> value of this <code>Column</code>\n"
197       + "  */\n");
198     w.write("  public " + typeShortName + " get" + capitalisedName + "()\n" +
199             "      throws AccessPoemException {\n" +
200             "    Integer code = get" + capitalisedName + "Code();\n" +
201             "    return code == null ? null :\n" +
202             "        PoemTypeFactory.forCode(getDatabase(), " + 
203             "code.intValue());\n" +
204             "  }\n" +
205             "\n");
206     w.write(
207       "\n /**\n"
208       + "  * Sets the <code>" + typeShortName + "</code> <code>" 
209       + capitalisedName 
210       + "</code> value  for this <code>" 
211       + table.nameFromDsd
212       + "</code> <code>Column</code> of the <code>"
213       + table.nameFromDsd 
214       + "</code> <code>Table</code>.\n" 
215       + ((description != null) ? "  * Field description: \n" 
216                                  + DSD.javadocFormat(2, 3, description)
217                                  + "  * \n"
218                                : "")
219       + "  * \n"
220       + "  * Generated by " 
221       + "org.melati.poem.prepro.ColumnTypeFieldDef" 
222       + "#generateBaseMethods \n"
223       + "  * @param cooked the value to set \n"
224       + "  * @throws AccessPoemException \n"
225       + "  *         if the current <code>AccessToken</code> \n"
226       + "  *         does not confer write access rights\n"
227       + "  */\n");
228     w.write("  public void set" + capitalisedName + "(" + typeShortName + " cooked)\n" +
229             "      throws AccessPoemException {\n" +
230             "    set" + capitalisedName + "Code(cooked == null ? null : " +
231             "cooked.getCode());\n" +
232             "  }\n");
233   }
234 
235  /**
236   * @param w whatever is being written to
237   * @throws IOException 
238   *           if something goes wrong with the file system
239   */   
240   public void generateJavaDeclaration(Writer w) throws IOException {
241     w.write("Integer " + name);
242   }
243 
244  /** @return the Java string for this <code>PoemType</code>. */
245   public String poemTypeJava() {
246       return "new ColumnTypePoemType(getDatabase())";
247   }
248 }