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@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.IOException;
50  import java.io.Writer;
51  
52  /**
53   * The definition of all base fields.
54   */
55  public class AtomFieldDef extends FieldDef {
56  
57    /**
58     * Constructor.
59     *
60     * @param lineNo       the line number in the DSD file
61     * @param table        the {@link TableDef} that this <code>Field</code> is
62     *                     part of
63     * @param name         the name of this field
64     * @param type         the type of this field
65     * @param displayOrder where to place this field in a list
66     * @param qualifiers   all the qualifiers of this field
67     * @throws IllegalityException if a semantic inconsistency is detected
68     */
69    public AtomFieldDef(int lineNo, TableDef table, String name,
70                        String type, int displayOrder, Vector<FieldQualifier> qualifiers)
71         throws IllegalityException {
72      super(lineNo, table, name, type, type, displayOrder, qualifiers);
73      table.addImport("org.melati.poem.ValidationPoemException", 
74                      "persistent");
75    }
76  
77   /**
78    * Write out this <code>Column</code>'s field accessors. 
79    *
80    * @param w The base table java file.
81    * @throws IOException 
82    *           if something goes wrong with the file system
83    */   
84    protected void generateColRawAccessors(Writer w) throws IOException {
85      super.generateColRawAccessors(w);
86  
87      w.write(
88        "\n" +
89        "          public Object getRaw(Persistent g)\n" +
90        "              throws AccessPoemException {\n" +
91        "            return ((" + shortestUnambiguousClassname + ")g).get" + capitalisedName + "();\n" +
92        "          }\n" +
93        "\n");
94      w.write(
95        "          public void setRaw(Persistent g, Object raw)\n" +
96        "              throws AccessPoemException {\n" +
97        "            ((" + shortestUnambiguousClassname + ")g).set" + capitalisedName +
98                       "((" + rawType + ")raw);\n" +
99        "          }\n");
100   }
101 
102  /**
103   * @param w The base persistent java file.
104   * @throws IOException 
105   *           if something goes wrong with the file system
106   */   
107   public void generateBaseMethods(Writer w) throws IOException {
108     super.generateBaseMethods(w);
109 
110     w.write(
111       "\n /**\n"
112       + "  * Retrieves the " 
113       + capitalisedName 
114       + " value, with locking, for this \n"
115       + "  * <code>" 
116       + table.nameFromDsd 
117       + "</code> <code>Persistent</code>.\n"
118       + ((description != null) ? "  * Field description: \n" 
119                                + DSD.javadocFormat(2, 3, description)
120                              : "")
121       + "  * \n"
122       + "  * Generated by "
123       + "org.melati.poem.prepro.AtomFieldDef" 
124       + "#generateBaseMethods \n"
125       + "  * @throws AccessPoemException \n"
126       + "  *         if the current <code>AccessToken</code> \n"
127       + "  *         does not confer write access rights \n"
128       + "  * @return the value of the field <code>"
129       + capitalisedName
130       + "</code> for this \n"
131       + "  *         <code>" 
132       + table.nameFromDsd 
133       + "</code> <code>Persistent</code>  \n"
134       + "  */\n");
135     w.write("\n" +
136             "  public " + typeShortName + " get" + capitalisedName + "()\n" +
137             "      throws AccessPoemException {\n" +
138             "    readLock();\n" +
139             "    return get" + capitalisedName + "_unsafe();\n" +
140             "  }\n" +
141             "\n");
142 
143     w.write(
144       "\n /**\n"
145       + "  * Sets the <code>" 
146       + capitalisedName 
147       + "</code> value, with checking, for this \n"
148       + "  * <code>" 
149       + table.nameFromDsd 
150       + "</code> <code>Persistent</code>.\n"
151       + (description != null ?   "  * Field description: \n" 
152                                + DSD.javadocFormat(2, 3, description)
153                              : "")
154       + "  * \n"
155       + "  * Generated by "
156       + "org.melati.poem.prepro.AtomFieldDef"
157       + "#generateBaseMethods  \n"
158       + "  * @param cooked  a validated <code>int</code> \n"
159       + "  * @throws AccessPoemException \n"
160       + "  *         if the current <code>AccessToken</code> \n"
161       + "  *         does not confer write access rights\n"
162       + "  * @throws ValidationPoemException \n"
163       + "  *         if the value is not valid\n"
164       + "  */\n");
165     w.write(
166             "  public void set" + capitalisedName + "(" + typeShortName + " cooked)\n" +
167             "      throws AccessPoemException, ValidationPoemException {\n" +
168             "    _" + rootTableAccessorMethod + "().get" + capitalisedName + "Column().\n" +
169             "      getType().assertValidCooked(cooked);\n" +
170             "    writeLock();\n" +
171             "    set" + capitalisedName + "_unsafe(cooked);\n" +
172             "  }\n");
173   }
174 
175  /**
176   * Write out this <code>Field</code>'s java declaration string.
177   *
178   * @param w The base persistent java file.
179   * @throws IOException 
180   *           if something goes wrong with the file system
181   */   
182   public void generateJavaDeclaration(Writer w) throws IOException {
183     w.write(typeShortName + " " + name);
184   }
185 
186  /** @return the Java string for this <code>PoemType</code>. */
187   public String poemTypeJava() {
188     return "new " + typeShortName + "PoemType(" + isNullable() + ")";
189   }
190 }