View Javadoc

1   /*
2    * $Source: /usr/cvsroot/melati/maven-dsd-plugin/src/main/java/org/melati/poem/prepro/ReferenceFieldDef.java,v $
3    * $Revision: 1.32 $
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>ReferencePoemType</tt> from the DSD.
54   * 
55   * Its member variables are populated from the DSD or defaults.
56   * Its methods are used to generate the java code.
57   */ 
58  public class ReferenceFieldDef extends FieldDef {
59  
60    String integrityfix;
61    
62  
63   /**
64    * Constructor.
65    *
66    * @param lineNo       the line number in the DSD file
67    * @param table        the {@link TableDef} that this <code>Field</code> is 
68    *                     part of 
69    * @param name         the name of this field
70    * @param type         the type of this field
71    * @param displayOrder where to place this field in a list
72    * @param qualifiers   all the qualifiers of this field
73    * 
74    * @throws IllegalityException if a semantic inconsistency is detected
75    */
76    public ReferenceFieldDef(int lineNo, TableDef table, String name, int displayOrder,
77                             String type, Vector<FieldQualifier> qualifiers)
78        throws IllegalityException {
79      super(lineNo, table, name, type, "Integer", displayOrder, qualifiers);// TODO test with Troid
80      table.addImport("org.melati.poem.ReferencePoemType", 
81                        "table");
82      table.addImport("org.melati.poem.NoSuchRowPoemException", 
83                        "persistent");
84      if (integrityfix != null) {
85        table.addImport("org.melati.poem.StandardIntegrityFix", 
86                          "table");
87      }
88      // Note these do not have a '.' in and are 
89      // looked up once all tables have been processed
90      // to enable forward reference within the DSD
91      table.addImport(type,"table");
92      table.addImport(type,"persistent");
93      
94    }
95    
96    /** Due to possible forward references this could be null until we finish parse */
97    public TableNamingInfo getTargetTableNamingInfo() {
98      return table.dsd.tableNamingStore.tableInfoByPersistentShortName.get(typeShortName);
99    }
100 
101  /**
102   * @param w The base table java file.
103   * @throws IOException 
104   *           if something goes wrong with the file system
105   */   
106   protected void generateColRawAccessors(Writer w) throws IOException {
107     super.generateColRawAccessors(w);
108 
109     w.write(
110       "\n" +
111       "          public Object getRaw(Persistent g)\n" +
112       "              throws AccessPoemException {\n" +
113       "            return ((" + shortestUnambiguousClassname + ")g).get" + capitalisedName + "Troid();\n" +
114       "          }\n" +
115       "\n");
116     w.write(
117       "          public void setRaw(Persistent g, Object raw)\n" +
118       "              throws AccessPoemException {\n" +
119       "            ((" + shortestUnambiguousClassname + ")g).set" + capitalisedName + "Troid((" +
120                    rawType + ")raw);\n" +
121       "          }\n");
122 
123     if (integrityfix != null) {
124       w.write(
125         "\n" +
126         "          public StandardIntegrityFix defaultIntegrityFix() {\n" +
127         "            return StandardIntegrityFix." + 
128         integrityfix + ";\n" +
129         "          }\n");
130     }
131   }
132 
133   // FIXME Forwards references to extended classes would fail here
134   // As it is this will fail with a
135   private String targetCast() {
136     // If this is irksome comment it out, but then we are relying upon hack below.
137     if (getTargetTableNamingInfo() == null)
138       throw new ParsingDSDException(lineNumber, 
139           "Reference to a type (" + typeShortName + ") which has yet to be defined. \n" + 
140           "If there are no reciprocal references then reorder definitions.");
141 
142     // FIXME HACK Forwards references to extended classes should fail here until we do two passes
143     return getTargetTableNamingInfo() == null || getTargetTableNamingInfo().superclass == null ?
144              "" : "(" + typeShortName + ")";
145   }
146 
147  /**
148   * @param w The base persistent java file.
149   * @throws IOException 
150   *           if something goes wrong with the file system
151   */   
152   public void generateBaseMethods(Writer w) throws IOException {
153     super.generateBaseMethods(w);
154 
155     String targetTableAccessorMethod = "get" + typeShortName + "Table";
156     String targetSuffix = typeShortName;
157 
158     String db = "get" + table.dsd.databaseTablesClassName + "()";
159 
160     w.write(
161       "\n /**\n"
162       + "  * Retrieves the Table Row Object ID. \n" 
163       + "  *\n"
164       + "  * Generated by " 
165       + "org.melati.poem.prepro.ReferenceFieldDef" 
166       + "#generateBaseMethods \n"
167       + "  * @throws AccessPoemException  \n" 
168       + "  *         if the current <code>AccessToken</code> \n"
169       + "  *         does not confer read access rights \n"
170       + "  * @return the TROID as an <code>Integer</code> \n"
171       + "  */\n");
172     w.write("\n" +
173       "  public Integer get" + capitalisedName + "Troid()\n" +
174       "      throws AccessPoemException {\n" +
175       "    readLock();\n" +
176       "    return get" + capitalisedName + "_unsafe();\n" +
177       "  }\n" +
178       "\n");
179     w.write(
180       "\n /**\n"
181       + "  * Sets the Table Row Object ID. \n" 
182       + "  * \n" 
183       + "  * Generated by " 
184       + "org.melati.poem.prepro.ReferenceFieldDef" 
185       + "#generateBaseMethods \n"
186       + "  * @param raw  a Table Row Object Id \n"
187       + "  * @throws AccessPoemException  \n" 
188       + "  *         if the current <code>AccessToken</code> \n"
189       + "  *         does not confer write access rights\n"
190       + "  */\n");
191     w.write(
192       "  public void set" + capitalisedName + "Troid(Integer raw)\n" +
193       "      throws AccessPoemException {\n" +
194       "    set" + capitalisedName + "(" +
195       "raw == null ? null : \n" +
196       // This cast is necessary when the target table is
197       // an "extends"
198       "        " + targetCast() +
199       db + "." + targetTableAccessorMethod + "()." +
200       "get" + targetSuffix + "Object(raw));\n" +
201       "  }\n" +
202       "\n");
203     w.write(
204       "\n /**\n"
205       + "  * Retrieves the <code>" + capitalisedName + "</code> object referred to.\n"
206       + "  *  \n"
207       + "  * Generated by " 
208       + "org.melati.poem.prepro.ReferenceFieldDef" 
209       + "#generateBaseMethods \n"
210       + "  * @throws AccessPoemException  \n" 
211       + "  *         if the current <code>AccessToken</code> \n"
212       + "  *         does not confer read access rights \n"
213       + "  * @throws NoSuchRowPoemException  \n" 
214       + "  *         if the <code>Persistent</code> has yet "
215       + "to be allocated a TROID \n"
216       + "  * @return the <code>" 
217       + capitalisedName 
218       + "</code> as a <code>" 
219       + typeShortName 
220       + "</code> \n"
221       + "  */\n");
222     w.write(
223       "  public " + typeShortName + " get" + capitalisedName + "()\n" +
224       "      throws AccessPoemException, NoSuchRowPoemException {\n" +
225       "    Integer troid = get" + capitalisedName + "Troid();\n" +
226       "    return troid == null ? null :\n" +
227       // This cast is necessary when the target table is
228       // an "extends"
229       "        " + targetCast() +
230       db + "." +
231       targetTableAccessorMethod + "()." +
232       "get" + targetSuffix + "Object(troid);\n" +
233       "  }\n" +
234       "\n");
235     w.write(
236       "\n /**\n"
237       + "  * Set the "
238       + capitalisedName
239       + ".\n" 
240       + "  * \n"
241       + "  * Generated by " 
242       + "org.melati.poem.prepro.ReferenceFieldDef" 
243       + "#generateBaseMethods \n"
244       + "  * @param cooked  a validated <code>" 
245       + typeShortName 
246       + "</code>\n"
247       + "  * @throws AccessPoemException  \n" 
248       + "  *         if the current <code>AccessToken</code> \n"
249       + "  *         does not confer write access rights \n"
250       + "  */\n");
251     w.write(
252       "  public void set" + capitalisedName + "(" + typeShortName + " cooked)\n" +
253       "      throws AccessPoemException {\n" +
254       "    _" + tableAccessorMethod + "().\n" + 
255       "      get" + capitalisedName + "Column().\n" +
256       "        getType().assertValidCooked(cooked);\n" +
257       "    writeLock();\n" +
258       "    if (cooked == null)\n" +
259       "      set" + capitalisedName + "_unsafe(null);\n" +
260       "    else {\n" +
261       "      cooked.existenceLock();\n" +
262       "      set" + capitalisedName + "_unsafe(cooked.troid());\n" +
263       "    }\n" +
264       "  }\n");
265   }
266 
267  /**
268   * Write out this <code>Field</code>'s java declaration string.
269   *
270   * @param w The base persistent java file.
271   * @throws IOException 
272   *           if something goes wrong with the file system
273   */   
274   public void generateJavaDeclaration(Writer w) throws IOException {
275     w.write("Integer " + name);
276   }
277 
278  /** @return the Java string for this <code>PoemType</code>. */
279   public String poemTypeJava() {
280     String targetTableAccessorMethod = "get" + typeShortName + "Table";
281     String db = "get" + table.dsd.databaseTablesClassName + "()";
282 
283     return
284         "new ReferencePoemType(" + db + ".\n" + 
285         "                                             " +
286         targetTableAccessorMethod + "(), " + isNullable() + ")";
287   }
288 }