1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.core.jpa.metadata; |
17 | |
|
18 | |
import java.io.Serializable; |
19 | |
import java.util.ArrayList; |
20 | |
import java.util.HashMap; |
21 | |
import java.util.LinkedHashSet; |
22 | |
import java.util.List; |
23 | |
import java.util.Map; |
24 | |
import java.util.Set; |
25 | |
|
26 | |
import org.kuali.rice.core.jpa.annotations.Sequence; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | 0 | public class EntityDescriptor implements Serializable { |
32 | |
|
33 | |
private static final long serialVersionUID = -4209120979389982233L; |
34 | |
|
35 | |
private String name; |
36 | |
private String table; |
37 | |
private Class clazz; |
38 | |
private Class idClass; |
39 | |
private Sequence sequence; |
40 | |
|
41 | 0 | private Map<String, FieldDescriptor> fieldsByName = new HashMap<String, FieldDescriptor>(); |
42 | 0 | private Map<String, FieldDescriptor> fieldsByColumnName = new HashMap<String, FieldDescriptor>(); |
43 | |
|
44 | 0 | private Set<FieldDescriptor> fields = new LinkedHashSet<FieldDescriptor>(); |
45 | 0 | private Set<FieldDescriptor> nonKeyFields = new LinkedHashSet<FieldDescriptor>(); |
46 | 0 | private Set<FieldDescriptor> primaryKeys = new LinkedHashSet<FieldDescriptor>(); |
47 | |
|
48 | |
|
49 | 0 | private Set<OneToOneDescriptor> oneToOneRelationships = new LinkedHashSet<OneToOneDescriptor>(); |
50 | 0 | private Set<OneToManyDescriptor> oneToManyRelationships = new LinkedHashSet<OneToManyDescriptor>(); |
51 | 0 | private Set<ManyToOneDescriptor> manyToOneRelationships = new LinkedHashSet<ManyToOneDescriptor>(); |
52 | 0 | private Set<ManyToManyDescriptor> manyToManyRelationships = new LinkedHashSet<ManyToManyDescriptor>(); |
53 | |
|
54 | |
|
55 | 0 | private List<ObjectDescriptor> objectRelationships = new ArrayList<ObjectDescriptor>(); |
56 | |
|
57 | |
|
58 | 0 | private List<CollectionDescriptor> collectionRelationships = new ArrayList<CollectionDescriptor>(); |
59 | |
|
60 | |
public String getName() { |
61 | 0 | return name; |
62 | |
} |
63 | |
|
64 | |
public void setName(String name) { |
65 | 0 | this.name = name; |
66 | 0 | } |
67 | |
|
68 | |
public String getTable() { |
69 | 0 | return table; |
70 | |
} |
71 | |
|
72 | |
public void setTable(String table) { |
73 | 0 | this.table = table; |
74 | 0 | } |
75 | |
|
76 | |
public Class getClazz() { |
77 | 0 | return clazz; |
78 | |
} |
79 | |
|
80 | |
public void setClazz(Class clazz) { |
81 | 0 | this.clazz = clazz; |
82 | 0 | } |
83 | |
|
84 | |
public Class getIdClass() { |
85 | 0 | return idClass; |
86 | |
} |
87 | |
|
88 | |
public void setIdClass(Class idClass) { |
89 | 0 | this.idClass = idClass; |
90 | 0 | } |
91 | |
|
92 | |
public Sequence getSequence() { |
93 | 0 | return sequence; |
94 | |
} |
95 | |
|
96 | |
public void setSequence(Sequence sequence) { |
97 | 0 | this.sequence = sequence; |
98 | 0 | } |
99 | |
|
100 | |
public FieldDescriptor getFieldByName(String name) { |
101 | 0 | return fieldsByName.get(name.toUpperCase()); |
102 | |
} |
103 | |
|
104 | |
public FieldDescriptor getFieldByColumnName(String name) { |
105 | 0 | return fieldsByColumnName.get(name.toUpperCase()); |
106 | |
} |
107 | |
|
108 | |
public void add(FieldDescriptor fieldDescriptor) { |
109 | 0 | if (fieldsByName.containsKey(fieldDescriptor.getName().toUpperCase())) { |
110 | 0 | return; |
111 | |
} |
112 | 0 | fieldsByName.put(fieldDescriptor.getName().toUpperCase(), fieldDescriptor); |
113 | 0 | fieldsByColumnName.put(fieldDescriptor.getColumn().toUpperCase(), fieldDescriptor); |
114 | 0 | fields.add(fieldDescriptor); |
115 | 0 | if (fieldDescriptor.isId()) { |
116 | 0 | primaryKeys.add(fieldDescriptor); |
117 | |
} else { |
118 | 0 | nonKeyFields.add(fieldDescriptor); |
119 | |
} |
120 | 0 | } |
121 | |
|
122 | |
public void add(OneToOneDescriptor relation) { |
123 | 0 | oneToOneRelationships.add(relation); |
124 | 0 | objectRelationships.add(relation); |
125 | 0 | } |
126 | |
|
127 | |
public void add(OneToManyDescriptor relation) { |
128 | 0 | oneToManyRelationships.add(relation); |
129 | 0 | collectionRelationships.add(relation); |
130 | 0 | } |
131 | |
|
132 | |
public void add(ManyToOneDescriptor relation) { |
133 | 0 | manyToOneRelationships.add(relation); |
134 | 0 | objectRelationships.add(relation); |
135 | 0 | } |
136 | |
|
137 | |
public void add(ManyToManyDescriptor relation) { |
138 | 0 | manyToManyRelationships.add(relation); |
139 | 0 | collectionRelationships.add(relation); |
140 | 0 | } |
141 | |
|
142 | |
|
143 | |
public ObjectDescriptor getObjectDescriptorByName(String attributeName) { |
144 | 0 | if (attributeName == null) { |
145 | 0 | return null; |
146 | |
} |
147 | 0 | for (ObjectDescriptor od : objectRelationships) { |
148 | 0 | if (od.getAttributeName().equals(attributeName)) { |
149 | 0 | return od; |
150 | |
} |
151 | |
} |
152 | 0 | return null; |
153 | |
} |
154 | |
|
155 | |
|
156 | |
public CollectionDescriptor getCollectionDescriptorByName(String attributeName) { |
157 | 0 | if (attributeName == null) { |
158 | 0 | return null; |
159 | |
} |
160 | 0 | for (CollectionDescriptor cd : collectionRelationships) { |
161 | 0 | if (cd.getAttributeName().equals(attributeName)) { |
162 | 0 | return cd; |
163 | |
} |
164 | |
} |
165 | 0 | return null; |
166 | |
} |
167 | |
|
168 | |
public String toString() { |
169 | 0 | StringBuffer sb = new StringBuffer(); |
170 | 0 | sb.append("EntityDescriptor = ["); |
171 | 0 | sb.append("\n name: ").append(name); |
172 | 0 | sb.append("\n table: ").append(table); |
173 | 0 | sb.append("\n class: ").append(clazz.getName()); |
174 | 0 | if (idClass != null) { |
175 | 0 | sb.append("\n id class: ").append(idClass.getName()); |
176 | |
} |
177 | 0 | if (sequence != null) { |
178 | 0 | sb.append("\n sequence: ").append(sequence.property()).append(" -> ").append(sequence.name()); |
179 | |
} |
180 | 0 | if (!primaryKeys.isEmpty()) { |
181 | 0 | sb.append("\n primary keys = {"); |
182 | 0 | for (FieldDescriptor pk : primaryKeys) { |
183 | 0 | sb.append("\n "); |
184 | 0 | sb.append(pk.toString()); |
185 | |
} |
186 | 0 | sb.append("\n }"); |
187 | |
} |
188 | 0 | if (!fields.isEmpty()) { |
189 | 0 | sb.append("\n fields = {"); |
190 | 0 | for (FieldDescriptor field : fields) { |
191 | 0 | sb.append("\n "); |
192 | 0 | sb.append(field.toString()); |
193 | |
} |
194 | 0 | sb.append("\n }"); |
195 | |
} |
196 | 0 | if (!oneToOneRelationships.isEmpty()) { |
197 | 0 | sb.append("\n one-to-one = {"); |
198 | 0 | for (OneToOneDescriptor relation : oneToOneRelationships) { |
199 | 0 | sb.append("\n "); |
200 | 0 | sb.append(relation.toString()); |
201 | |
} |
202 | 0 | sb.append("\n }"); |
203 | |
} |
204 | 0 | if (!manyToOneRelationships.isEmpty()) { |
205 | 0 | sb.append("\n many-to-one = {"); |
206 | 0 | for (ManyToOneDescriptor relation : manyToOneRelationships) { |
207 | 0 | sb.append("\n "); |
208 | 0 | sb.append(relation.toString()); |
209 | |
} |
210 | 0 | sb.append("\n }"); |
211 | |
} |
212 | 0 | if (!oneToManyRelationships.isEmpty()) { |
213 | 0 | sb.append("\n one-to-many = {"); |
214 | 0 | for (OneToManyDescriptor relation : oneToManyRelationships) { |
215 | 0 | sb.append("\n "); |
216 | 0 | sb.append(relation.toString()); |
217 | |
} |
218 | 0 | sb.append("\n }"); |
219 | |
} |
220 | 0 | if (!manyToManyRelationships.isEmpty()) { |
221 | 0 | sb.append("\n many-to-many = {"); |
222 | 0 | for (ManyToManyDescriptor relation : manyToManyRelationships) { |
223 | 0 | sb.append("\n "); |
224 | 0 | sb.append(relation.toString()); |
225 | |
} |
226 | 0 | sb.append("\n }"); |
227 | |
} |
228 | 0 | sb.append("\n]"); |
229 | 0 | return sb.toString(); |
230 | |
} |
231 | |
|
232 | |
public Set<FieldDescriptor> getFields() { |
233 | 0 | return this.fields; |
234 | |
} |
235 | |
|
236 | |
public Set<FieldDescriptor> getNonKeyFields() { |
237 | 0 | return this.nonKeyFields; |
238 | |
} |
239 | |
|
240 | |
public Set<FieldDescriptor> getPrimaryKeys() { |
241 | 0 | return this.primaryKeys; |
242 | |
} |
243 | |
|
244 | |
public Set<OneToOneDescriptor> getOneToOneRelationships() { |
245 | 0 | return this.oneToOneRelationships; |
246 | |
} |
247 | |
|
248 | |
public Set<OneToManyDescriptor> getOneToManyRelationships() { |
249 | 0 | return this.oneToManyRelationships; |
250 | |
} |
251 | |
|
252 | |
public Set<ManyToOneDescriptor> getManyToOneRelationships() { |
253 | 0 | return this.manyToOneRelationships; |
254 | |
} |
255 | |
|
256 | |
public Set<ManyToManyDescriptor> getManyToManyRelationships() { |
257 | 0 | return this.manyToManyRelationships; |
258 | |
} |
259 | |
|
260 | |
public List<ObjectDescriptor> getObjectRelationships() { |
261 | 0 | return this.objectRelationships; |
262 | |
} |
263 | |
|
264 | |
public List<CollectionDescriptor> getCollectionRelationships() { |
265 | 0 | return this.collectionRelationships; |
266 | |
} |
267 | |
|
268 | |
} |