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