1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.seasar.tuigwaa.model.core.impl;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import org.seasar.tuigwaa.model.common.TgwElementVisitor;
27 import org.seasar.tuigwaa.model.core.RelationAttribute;
28 import org.seasar.tuigwaa.model.core.TgwAttribute;
29 import org.seasar.tuigwaa.model.core.TgwEntity;
30 import org.seasar.tuigwaa.system.Constants;
31
32 /***
33 * @author nishioka
34 */
35 public class TgwEntityImpl extends TgwElementImpl implements TgwEntity {
36
37 private static final int DEFAULT_DEPTH = 3;
38
39 private String javaClassName;
40
41 private Class javaClass;
42
43 private String domain;
44
45 private String representativeFiled_;
46
47 private List referencedFkFieldList = new ArrayList();
48
49 private List removedAttrNames = new ArrayList();
50
51 private boolean aggregation;
52
53 private boolean hidden;
54
55 private boolean randomId;
56
57 private volatile int hashCode = 0;
58
59 private boolean importedEntity;
60
61 private String primaryKeyColumnName = Constants.ENTITY_BUILTIN_ID;
62
63 private String primaryKeyDisplayName = "ID";
64
65 private String importedClassName;
66
67 public TgwEntityImpl() {
68 this(null);
69 }
70
71 public TgwEntityImpl(String domain) {
72 this.domain = domain;
73 }
74
75 public TgwEntityImpl(String domain, String name) {
76 super();
77 this.domain = domain;
78 setName(name);
79 }
80
81
82
83 public void setRandomId(boolean randomId) {
84 this.randomId = randomId;
85 }
86
87 public boolean isRandomId() {
88 return randomId;
89 }
90
91 public void setHidden(boolean hidden) {
92 this.hidden = hidden;
93 }
94
95 public boolean isHidden() {
96 return hidden;
97 }
98
99 public boolean isImportedEntity() {
100 return importedEntity;
101 }
102
103 public void setImportedEntity(boolean importedEntity) {
104 this.importedEntity = importedEntity;
105 }
106
107 public String getJavaClassName() {
108 return javaClassName;
109 }
110
111 public void setJavaClassName(String javaClassName) {
112 this.javaClassName = javaClassName;
113 }
114
115 public void setJavaClass(Class clazz) {
116 this.javaClass = clazz;
117 setJavaClassName(clazz.getName());
118 }
119
120 public void setImportedClassName(String importedClassName) {
121 this.importedClassName = importedClassName;
122 }
123
124 public String getImportedClassName() {
125 return importedClassName;
126 }
127
128 public Class getJavaClass() {
129 return javaClass;
130 }
131
132 public String getDomainName() {
133 return domain;
134 }
135
136 public void setDomainName(String schema) {
137 this.domain = schema;
138 }
139
140 public void setAggregation(boolean aggregation) {
141 this.aggregation = aggregation;
142 }
143
144 public boolean isAggregation() {
145 return aggregation;
146 }
147
148 public void addRemovedAttrName(String attrName) {
149 removedAttrNames.add(attrName);
150 }
151
152 public List getRemovedAttrNames() {
153 return removedAttrNames;
154 }
155
156
157
158 public String getRepresentativeField() {
159 if (representativeFiled_ == null
160 || getField(representativeFiled_) instanceof SelfAttribute) {
161 for (Iterator i = getFieldIterator(); i.hasNext();) {
162 TgwAttribute field = (TgwAttribute) i.next();
163 if (!(field instanceof FkAttribute)
164 && !(field instanceof SelfAttribute)) {
165 return field.getName();
166 }
167 }
168 }
169 return representativeFiled_;
170 }
171
172 public String getRecursiveRepresentativeField() {
173 String representativeField = getRepresentativeField();
174 if (getField(representativeField) instanceof FkAttribute) {
175 FkAttribute fk = (FkAttribute) getField(representativeField);
176 String field = representativeField + "."
177 + fk.getRefEntity().getRepresentativeField();
178 return field;
179 }
180 return representativeField;
181 }
182
183 public void setRepresentativeField(String field) {
184 this.representativeFiled_ = field;
185 }
186
187 public TgwAttribute removeAttribute(String attrName) {
188 return (TgwAttribute) removeChild(attrName);
189 }
190
191 public void addField(TgwAttribute field) {
192 addChild(field);
193 field.setEntity(this);
194 if (field instanceof SetAttribute) {
195 ((SetAttribute) field).connectFkField();
196 }
197 }
198
199 public void removeReferencedFkField(FkAttribute fkField) {
200 this.referencedFkFieldList.remove(fkField);
201 }
202
203 public void addReferencedFkField(FkAttribute fkField) {
204 this.referencedFkFieldList.add(fkField);
205 }
206
207 public List getReferencedFkFieldList() {
208 return referencedFkFieldList;
209 }
210
211
212 public List getAllFieldList() {
213 return getAllFieldList(null);
214 }
215
216 public List getFirstDepthAllFieldList() {
217 return getAllFieldList(null, 1);
218 }
219
220 public List getAllFieldList(List parentFieldList) {
221 return getAllFieldList(parentFieldList, DEFAULT_DEPTH);
222 }
223
224 public List getAllFieldList(List parentFieldList, int depth) {
225
226 List list = new ArrayList();
227 if (parentFieldList != null && parentFieldList.size() > depth) {
228 return list;
229 }
230
231 for (Iterator i = getChildList().iterator(); i.hasNext();) {
232 TgwAttribute field = (TgwAttribute) i.next();
233 if (field instanceof LinkAttribute) {
234 LinkAttribute fkField = (LinkAttribute) field;
235 if (isCrossReference(parentFieldList, fkField)) {
236 continue;
237 }
238 TgwEntity refEntity = fkField.getRefEntity();
239 if (parentFieldList == null) {
240 parentFieldList = new ArrayList();
241 }
242 parentFieldList.add(field);
243 list.addAll(refEntity.getAllFieldList(parentFieldList, depth));
244 parentFieldList.remove(field);
245 } else {
246 if (parentFieldList != null && parentFieldList.size() > 0) {
247 String[] names = createPrefix(parentFieldList);
248 NestedAttribute attr = createNestedAttribute(names[0],
249 names[1], field);
250 list.add(attr);
251 } else {
252 list.add(field);
253 }
254 }
255 }
256 return list;
257 }
258
259 public String getFirstNotFkFieldName() {
260 return doFirstNotFkFieldName(this, "");
261 }
262
263 public TgwAttribute getFirstNotFkField() {
264 return doFirstNotFkField(this);
265 }
266
267 private String doFirstNotFkFieldName(TgwEntity entity, String prefix) {
268 TgwAttribute field = entity.getFirstField();
269 if (field instanceof RelationAttribute) {
270 return doFirstNotFkFieldName(((RelationAttribute) field)
271 .getRefEntity(), field.getName() + ".");
272 }
273 return prefix + field.getName();
274 }
275
276 private TgwAttribute doFirstNotFkField(TgwEntity entity) {
277 TgwAttribute field = entity.getFirstField();
278 if (field instanceof RelationAttribute) {
279 field = doFirstNotFkField(((RelationAttribute) field)
280 .getRefEntity());
281 }
282 return field;
283 }
284
285 /***
286 * 属性の表示名(仮想テーブルのカラムの表示名)でTgwAttributeを検索する
287 */
288 public TgwAttribute getAttributeByDisplayName(String displayName) {
289 if (displayName == null) {
290 return null;
291 }
292
293 int nestedIndex = displayName.indexOf(".");
294 if (nestedIndex < 0) {
295 return (TgwAttribute) getChildByDisplayName(displayName);
296 } else {
297 String attrDisplayNameName = displayName.substring(0, nestedIndex);
298 String nestedAttrDisplayNameName = displayName.substring(
299 nestedIndex + 1, displayName.length());
300 LinkAttribute fkField = (LinkAttribute) getAttributeByDisplayName(attrDisplayNameName);
301 TgwAttribute realAttr = fkField.getRefEntity()
302 .getAttributeByDisplayName(nestedAttrDisplayNameName);
303 return createNestedAttribute(fkField.getName() + ".", fkField
304 .getDisplayName()
305 + ".", realAttr);
306 }
307 }
308
309 /***
310 * 属性の名前(仮想テーブルのカラムの実名)でTgwAttributeを検索する
311 */
312 public TgwAttribute getField(String name) {
313 if (name == null) {
314 return null;
315 }
316 if (Constants.ENTITY_BUILTIN_ID.equals(name)) {
317 TgwAttribute attr = new LongAttribute();
318 attr.setName(Constants.ENTITY_BUILTIN_ID);
319 attr.setDisplayName(primaryKeyDisplayName);
320 attr.setEntity(this);
321 return attr;
322 }
323
324 int nestedIndex = name.indexOf(".");
325 if (nestedIndex < 0) {
326 return (TgwAttribute) getChild(name);
327 } else {
328 String fieldName = name.substring(0, nestedIndex);
329 String nestedFieldName = name.substring(nestedIndex + 1, name
330 .length());
331 LinkAttribute fkField = (LinkAttribute) getField(fieldName);
332 return fkField.getRefEntity().getField(nestedFieldName);
333 }
334 }
335
336 public TgwAttribute getFirstField() {
337 return (TgwAttribute) getFieldIterator().next();
338 }
339
340 public Iterator getFieldIterator() {
341 return getFieldList().iterator();
342 }
343
344 public Collection getFieldList() {
345 return getChildList();
346 }
347
348 public String getPrimaryKeyColumnName() {
349 return primaryKeyColumnName;
350 }
351
352 public void setPrimaryKeyColumnName(String primaryKeyColumnName) {
353 this.primaryKeyColumnName = primaryKeyColumnName;
354 }
355
356 public Object newInstance() {
357 try {
358 return getJavaClass().newInstance();
359 } catch (InstantiationException e) {
360
361 } catch (IllegalAccessException e) {
362
363 }
364 return null;
365 }
366
367 public boolean equals(Object obj) {
368 if (obj == null || !(obj instanceof TgwEntity)) {
369 return false;
370 }
371 String thisKey = getDomainName() + "." + getName();
372 TgwEntity thatEntity = (TgwEntity) obj;
373 String thatKey = thatEntity.getDomainName() + "."
374 + thatEntity.getName();
375 return thisKey.equals(thatKey);
376 }
377
378 public int hashCode() {
379 int seed = 17;
380 int weight = 37;
381
382 if (this.hashCode == 0) {
383 this.hashCode = seed;
384 this.hashCode += weight * this.getDomainName().hashCode();
385 if (this.getName() != null) {
386 this.hashCode += weight * this.getName().hashCode();
387 }
388 }
389 return this.hashCode;
390 }
391
392 public Object accept(TgwElementVisitor visitor, Object data) {
393 return visitor.visit(this, data);
394 }
395
396 public void resetAttributes() {
397 clearChildren();
398 }
399
400
401
402 private NestedAttribute createNestedAttribute(String parentName,
403 String parentDisplayName, TgwAttribute realAttr) {
404 NestedAttribute nestedField = new NestedAttribute();
405 nestedField.setName(parentName + realAttr.getName());
406 nestedField.setDisplayName(parentDisplayName
407 + realAttr.getDisplayName());
408 nestedField.setRealField(realAttr);
409 return nestedField;
410 }
411
412 private String[] createPrefix(List parentFieldList) {
413 StringBuffer prefix = new StringBuffer();
414 StringBuffer displayPrefix = new StringBuffer();
415 for (Iterator i = parentFieldList.iterator(); i.hasNext();) {
416 TgwAttribute field = (TgwAttribute) i.next();
417 prefix.append(field.getName() + ".");
418 displayPrefix.append(field.getDisplayName() + ".");
419 }
420 return new String[] { prefix.toString(), displayPrefix.toString() };
421 }
422
423 private boolean isCrossReference(List parentList, LinkAttribute linkAttr) {
424 if (parentList == null || parentList.size() < 1) {
425 return false;
426 }
427 TgwAttribute attr = (TgwAttribute) parentList
428 .get(parentList.size() - 1);
429 if (attr.getEntity().equals(linkAttr.getRefEntity())) {
430 return true;
431 } else {
432 return false;
433 }
434 }
435
436 public String getPrimaryKeyDisplayName() {
437 return primaryKeyDisplayName;
438 }
439
440 public void setPrimaryKeyDisplayName(String primaryKeyDisplayName) {
441 this.primaryKeyDisplayName = primaryKeyDisplayName;
442 }
443 }