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 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     * @return the possibly null TableNamingInfo
98     */
99    public TableNamingInfo getTargetTableNamingInfo() {
100     return table.dsd.tableNamingStore.tableInfoByPersistentShortName.get(typeShortName);
101   }
102 
103  /**
104   * @param w The base table java file.
105   * @throws IOException 
106   *           if something goes wrong with the file system
107   */   
108   protected void generateColRawAccessors(Writer w) throws IOException {
109     super.generateColRawAccessors(w);
110 
111     w.write(
112       "\n" +
113       "          public Object getRaw(Persistent g)\n" +
114       "              throws AccessPoemException {\n" +
115       "            return ((" + shortestUnambiguousClassname + ")g).get" + capitalisedName + "Troid();\n" +
116       "          }\n" +
117       "\n");
118     w.write(
119       "          public void setRaw(Persistent g, Object raw)\n" +
120       "              throws AccessPoemException {\n" +
121       "            ((" + shortestUnambiguousClassname + ")g).set" + capitalisedName + "Troid((" +
122                    rawType + ")raw);\n" +
123       "          }\n");
124 
125     if (integrityFix != null) {
126       w.write(
127         "\n" +
128         "          public StandardIntegrityFix defaultIntegrityFix() {\n" +
129         "            return StandardIntegrityFix." + 
130         integrityFix + ";\n" +
131         "          }\n");
132     }
133   }
134 
135   // FIXME Forwards references to extended classes would fail here
136   // As it is this will fail with a
137   private String targetCast() {
138     // If this is irksome comment it out, but then we are relying upon hack below.
139     if (getTargetTableNamingInfo() == null)
140       throw new ParsingDSDException(lineNumber, 
141           "Reference to a type (" + typeShortName + ") which has yet to be defined. \n" + 
142           "If there are no reciprocal references then reorder definitions.");
143 
144     // FIXME HACK Forwards references to extended classes should fail here until we do two passes
145     return getTargetTableNamingInfo() == null || getTargetTableNamingInfo().superclass == null ?
146              "" : "(" + typeShortName + ")";
147   }
148 
149  /**
150   * @param w The base persistent java file.
151   * @throws IOException 
152   *           if something goes wrong with the file system
153   */   
154   public void generateBaseMethods(Writer w) throws IOException {
155     super.generateBaseMethods(w);
156 
157     String targetTableAccessorMethod = "get" + typeShortName + "Table";
158     String targetSuffix = typeShortName;
159 
160     String db = "get" + table.dsd.databaseTablesClassName + "()";
161 
162     w.write(
163       "\n /**\n"
164       + "  * Retrieves the Table Row Object ID. \n" 
165       + "  *\n"
166       + "  * Generated by " 
167       + "org.melati.poem.prepro.ReferenceFieldDef" 
168       + "#generateBaseMethods \n"
169       + "  * @throws AccessPoemException  \n" 
170       + "  *         if the current <code>AccessToken</code> \n"
171       + "  *         does not confer read access rights \n"
172       + "  * @return the TROID as an <code>Integer</code> \n"
173       + "  */\n");
174     w.write("\n" +
175       "  public Integer get" + capitalisedName + "Troid()\n" +
176       "      throws AccessPoemException {\n" +
177       "    readLock();\n" +
178       "    return get" + capitalisedName + "_unsafe();\n" +
179       "  }\n" +
180       "\n");
181     w.write(
182       "\n /**\n"
183       + "  * Sets the Table Row Object ID. \n" 
184       + "  * \n" 
185       + "  * Generated by " 
186       + "org.melati.poem.prepro.ReferenceFieldDef" 
187       + "#generateBaseMethods \n"
188       + "  * @param raw  a Table Row Object Id \n"
189       + "  * @throws AccessPoemException  \n" 
190       + "  *         if the current <code>AccessToken</code> \n"
191       + "  *         does not confer write access rights\n"
192       + "  */\n");
193     w.write(
194       "  public void set" + capitalisedName + "Troid(Integer raw)\n" +
195       "      throws AccessPoemException {\n" +
196       "    set" + capitalisedName + "(" +
197       "raw == null ? null : \n" +
198       // This cast is necessary when the target table is
199       // an "extends"
200       "        " + targetCast() +
201       db + "." + targetTableAccessorMethod + "()." +
202       "get" + targetSuffix + "Object(raw));\n" +
203       "  }\n" +
204       "\n");
205     w.write(
206       "\n /**\n"
207       + "  * Retrieves the <code>" + capitalisedName + "</code> object referred to.\n"
208       + "  *  \n"
209       + "  * Generated by " 
210       + "org.melati.poem.prepro.ReferenceFieldDef" 
211       + "#generateBaseMethods \n"
212       + "  * @throws AccessPoemException  \n" 
213       + "  *         if the current <code>AccessToken</code> \n"
214       + "  *         does not confer read access rights \n"
215       + "  * @throws NoSuchRowPoemException  \n" 
216       + "  *         if the <code>Persistent</code> has yet "
217       + "to be allocated a TROID \n"
218       + "  * @return the <code>" 
219       + capitalisedName 
220       + "</code> as a <code>" 
221       + typeShortName 
222       + "</code> \n"
223       + "  */\n");
224     w.write(
225       "  public " + typeShortName + " get" + capitalisedName + "()\n" +
226       "      throws AccessPoemException, NoSuchRowPoemException {\n" +
227       "    Integer troid = get" + capitalisedName + "Troid();\n" +
228       "    return troid == null ? null :\n" +
229       // This cast is necessary when the target table is
230       // an "extends"
231       "        " + targetCast() +
232       db + "." +
233       targetTableAccessorMethod + "()." +
234       "get" + targetSuffix + "Object(troid);\n" +
235       "  }\n" +
236       "\n");
237     w.write(
238       "\n /**\n"
239       + "  * Set the "
240       + capitalisedName
241       + ".\n" 
242       + "  * \n"
243       + "  * Generated by " 
244       + "org.melati.poem.prepro.ReferenceFieldDef" 
245       + "#generateBaseMethods \n"
246       + "  * @param cooked  a validated <code>" 
247       + typeShortName 
248       + "</code>\n"
249       + "  * @throws AccessPoemException  \n" 
250       + "  *         if the current <code>AccessToken</code> \n"
251       + "  *         does not confer write access rights \n"
252       + "  */\n");
253     w.write(
254       "  public void set" + capitalisedName + "(" + typeShortName + " cooked)\n" +
255       "      throws AccessPoemException {\n" +
256       "    _" + rootTableAccessorMethod + "().\n" + 
257       "      get" + capitalisedName + "Column().\n" +
258       "        getType().assertValidCooked(cooked);\n" +
259       "    writeLock();\n" +
260       "    if (cooked == null)\n" +
261       "      set" + capitalisedName + "_unsafe(null);\n" +
262       "    else {\n" +
263       "      cooked.existenceLock();\n" +
264       "      set" + capitalisedName + "_unsafe(cooked.troid());\n" +
265       "    }\n" +
266       "  }\n");
267   }
268 
269  /**
270   * Write out this <code>Field</code>'s java declaration string.
271   *
272   * @param w The base persistent java file.
273   * @throws IOException 
274   *           if something goes wrong with the file system
275   */   
276   public void generateJavaDeclaration(Writer w) throws IOException {
277     w.write("Integer " + name);
278   }
279 
280  /** @return the Java string for this <code>PoemType</code>. */
281   public String poemTypeJava() {
282     String targetTableAccessorMethod = "get" + typeShortName + "Table";
283     String db = "get" + table.dsd.databaseTablesClassName + "()";
284 
285     return
286         "new ReferencePoemType(" + db + ".\n" + 
287         "                                             " +
288         targetTableAccessorMethod + "(), " + isNullable() + ")";
289   }
290 
291   public void setIntegrityFix(String integrityFixIn) {
292     this.integrityFix = integrityFixIn;
293   }
294 }