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 public TableNamingInfo getTargetTableNamingInfo() {
98 return table.dsd.tableNamingStore.tableInfoByPersistentShortName.get(typeShortName);
99 }
100
101
102
103
104
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
134
135 private String targetCast() {
136
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
143 return getTargetTableNamingInfo() == null || getTargetTableNamingInfo().superclass == null ?
144 "" : "(" + typeShortName + ")";
145 }
146
147
148
149
150
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
197
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
228
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
269
270
271
272
273
274 public void generateJavaDeclaration(Writer w) throws IOException {
275 w.write("Integer " + name);
276 }
277
278
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 }