View Javadoc

1   /*
2    * $Source: /usr/cvsroot/melati/maven-dsd-plugin/src/main/java/org/melati/poem/prepro/DisplayLevelFieldDef.java,v $
3    * $Revision: 1.16 $
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 a <tt>DisplayLevelPoemType</tt> from the DSD.
54   * A <tt>DisplayLevel</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 DisplayLevelFieldDef 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 DisplayLevelFieldDef(int lineNo, TableDef table, String name, int displayOrder,
75                                Vector<FieldQualifier> qualifiers) throws IllegalityException {
76      super(lineNo, table, name, "DisplayLevel", "Integer", displayOrder, qualifiers);
77      table.addImport("org.melati.poem.DisplayLevelPoemType", 
78                      "table");
79      table.addImport("org.melati.poem.DisplayLevel", 
80                      "table");
81      table.addImport("org.melati.poem.DisplayLevel", 
82                      "persistent");
83    }
84  
85   /**
86    * @param w The base table java file.
87    *
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   *
112   * @throws IOException 
113   *           if something goes wrong with the file system
114   */   
115   public void generateBaseMethods(Writer w) throws IOException {
116     super.generateBaseMethods(w);
117 
118 //    String targetTableAccessorMethod = "get" + type + "Table";
119 //    String targetSuffix = type;
120 
121     w.write(
122       "\n /**\n"
123       + "  * Retrieves the " 
124       + capitalisedName 
125       + " index value \n"
126       + "  * of this <code>Persistent</code>.\n" 
127       + "  * \n"
128       + ((description != null) ? "  * Field description: \n" 
129                                  + DSD.javadocFormat(2, 3, description)
130                                  + "  * \n"
131                                : "")
132       + "  * Generated by " 
133       + "org.melati.poem.prepro.DisplayLevelFieldDef" 
134       + "#generateBaseMethods \n"
135       + "  * @throws AccessPoemException \n"
136       + "  *         if the current <code>AccessToken</code> \n"
137       + "  *         does not confer read access rights\n"
138       + "  * @return the " + rawType + " " + name + "\n"
139       + "  */\n");
140     w.write("\n" +
141             "  public Integer get" + capitalisedName + "Index()\n" +
142             "      throws AccessPoemException {\n" +
143             "    readLock();\n" +
144             "    return get" + capitalisedName + "_unsafe();\n" +
145             "  }\n" +
146             "\n");
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.DisplayLevelFieldDef" 
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(
167       "  public void set" + capitalisedName + "Index(Integer raw)\n" +
168       "      throws AccessPoemException {\n" +
169       "    " + tableAccessorMethod + "().get" + capitalisedName + "Column()." +
170                 "getType().assertValidRaw(raw);\n" +
171       "    writeLock();\n" +
172       "    set" + capitalisedName + "_unsafe(raw);\n" +
173       "  }\n" +
174       "\n");
175     w.write(
176       "\n /**\n"
177       + "  * Retrieves the " 
178       + capitalisedName 
179       + " value \n"
180       + "  * of this <code>Persistent</code>.\n" 
181       + ((description != null) ? "  * Field description: \n" 
182                                  + DSD.javadocFormat(2, 3, description)
183                                  + "  * \n"
184                                : "")
185       + "  * \n"
186       + "  * Generated by " 
187       + "org.melati.poem.prepro.DisplayLevelFieldDef" 
188       + "#generateBaseMethods \n"
189       + "  * @throws AccessPoemException \n"
190       + "  *         if the current <code>AccessToken</code> \n"
191       + "  *         does not confer read access rights\n"
192       + "  * @return the " + typeShortName + "\n"
193       + "  */\n");
194     w.write(
195             "  public " + typeShortName + " get" + capitalisedName + "()\n" +
196             "      throws AccessPoemException {\n" +
197             "    Integer index = get" + capitalisedName + "Index();\n" +
198             "    return index == null ? null :\n" +
199             "        DisplayLevel.forIndex(index.intValue());\n" +
200             "  }\n" +
201             "\n");
202     w.write(
203       "\n /**\n"
204       + "  * Sets the <code>" 
205       + capitalisedName 
206       + "</code> value, with checking, for the " 
207       + "<code>Persistent</code> argument.\n"
208       + ((description != null) ? "  * Field description: \n" 
209                                  + DSD.javadocFormat(2, 3, description)
210                                  + "  * \n"
211                                : "")
212       + "  * \n"
213       + "  * Generated by " 
214       + "org.melati.poem.prepro.DisplayLevelFieldDef" 
215       + "#generateBaseMethods \n"
216       + "  * @param cooked  the value to set \n"
217       + "  * @throws AccessPoemException \n"
218       + "  *         if the current <code>AccessToken</code> \n"
219       + "  *         does not confer write access rights\n"
220       + "  */\n");
221     w.write(
222       "  public void set" + capitalisedName + "(" + typeShortName + " cooked)\n" +
223       "      throws AccessPoemException {\n" +
224       "    set" + capitalisedName + 
225       "Index(cooked == null ? null : cooked.index);\n" +
226       "  }\n");
227   }
228 
229  /**
230   * Write out this <code>Field</code>'s java declaration string.
231   *
232   * @param w The base persistent java file.
233   * @throws IOException 
234   *           if something goes wrong with the file system
235   */   
236   public void generateJavaDeclaration(Writer w) throws IOException {
237     w.write("Integer " + name);
238   }
239 
240  /** @return the Java string for this <code>PoemType</code>. */
241   public String poemTypeJava() {
242     return "new DisplayLevelPoemType()";
243   }
244 }