1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
54
55
56
57
58 public class ReferenceFieldDef extends FieldDef {
59
60 String integrityFix;
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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);
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
89
90
91 table.addImport(type,"table");
92 table.addImport(type,"persistent");
93
94 }
95
96
97
98
99 public TableNamingInfo getTargetTableNamingInfo() {
100 return table.dsd.tableNamingStore.tableInfoByPersistentShortName.get(typeShortName);
101 }
102
103
104
105
106
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
136
137 private String targetCast() {
138
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
145 return getTargetTableNamingInfo() == null || getTargetTableNamingInfo().superclass == null ?
146 "" : "(" + typeShortName + ")";
147 }
148
149
150
151
152
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
199
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
230
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
271
272
273
274
275
276 public void generateJavaDeclaration(Writer w) throws IOException {
277 w.write("Integer " + name);
278 }
279
280
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 }