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 package org.melati.poem;
46
47 import java.util.Enumeration;
48
49 import org.melati.poem.util.MappedEnumeration;
50 import org.melati.poem.util.StringUtils;
51
52
53
54
55
56
57
58
59
60 public class StringKeyReferencePoemType extends StringKeyPoemType implements PersistentReferencePoemType, PoemType<String> {
61
62 private Table<?> targetTable;
63
64 private String targetKeyName;
65
66
67
68
69
70
71
72
73
74 public StringKeyReferencePoemType(Table<?> targetTable,
75 String targetKeyName, boolean nullable, int size) {
76 super(nullable, size);
77 if (targetTable == null)
78 throw new NullPointerException();
79 this.targetTable = targetTable;
80 if (targetKeyName == null)
81 throw new NullPointerException();
82 this.targetKeyName = targetKeyName;
83 }
84
85
86
87
88 @Override
89 public Table<?> targetTable() {
90 return targetTable;
91 }
92
93
94 public String targetKeyName() {
95 return targetKeyName;
96 }
97
98
99
100
101
102 @SuppressWarnings({ "unchecked", "rawtypes" })
103 protected Enumeration<String> _possibleRaws() {
104 return new MappedEnumeration(targetTable.selection()) {
105 public String mapped(Object p) {
106 return (String)((Persistent)p).getRaw(targetKeyName);
107 }
108 };
109 }
110
111 protected void _assertValidCooked(Object cooked)
112 throws ValidationPoemException {
113 if (!(cooked instanceof Persistent))
114 throw new TypeMismatchPoemException(cooked, this);
115
116 Persistent persistent = (Persistent)cooked;
117
118 if (persistent.getTable() != targetTable)
119 throw new ValidationPoemException(
120 this, persistent,
121 new TableMismatchPoemException(persistent, targetTable));
122 }
123
124 protected Object _cookedOfRaw(Object raw) throws NoSuchRowPoemException {
125 return targetTable.getColumn(targetKeyName).firstWhereEq(raw);
126 }
127
128
129
130
131
132 protected String _rawOfCooked(Object cooked) {
133 return (String) ((Persistent)cooked).getField(targetKeyName).getRawString();
134 }
135
136 protected String _stringOfCooked(Object cooked,
137 PoemLocale locale, int style)
138 throws PoemException {
139 return ((Persistent)cooked).displayString(locale, style);
140 }
141
142 protected boolean _canRepresent(SQLPoemType<?> other) {
143 return
144 other instanceof StringKeyReferencePoemType &&
145 ((StringKeyReferencePoemType)other).targetTable == targetTable;
146 }
147
148 protected void _saveColumnInfo(ColumnInfo columnInfo)
149 throws AccessPoemException {
150 columnInfo.setTypefactoryCode(targetTable.tableInfoID());
151 columnInfo.setSize(getSize());
152 }
153
154
155
156
157
158 public String toString() {
159 return
160 "string key reference to " + targetTable.getName() +
161 " on " + targetKeyName +
162 " (" + super.toString() + ")";
163 }
164
165
166
167
168
169 public String toDsdType() {
170 return StringUtils.capitalised(targetTable.getName() + " StringKeyReference on " + targetKeyName);
171 }
172 }