1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.tuigwaa.model.common;
17
18 import java.beans.PropertyDescriptor;
19 import java.lang.reflect.InvocationTargetException;
20 import java.security.Principal;
21 import java.sql.Timestamp;
22 import java.util.ArrayList;
23 import java.util.Calendar;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33
34 import org.apache.commons.beanutils.ConvertUtils;
35 import org.apache.commons.beanutils.DynaBean;
36 import org.apache.commons.beanutils.PropertyUtils;
37 import org.seasar.tuigwaa.model.core.TgwAttribute;
38 import org.seasar.tuigwaa.model.core.TgwEntity;
39 import org.seasar.tuigwaa.model.core.impl.BooleanAttribute;
40 import org.seasar.tuigwaa.model.core.impl.ByteArrayAttribute;
41 import org.seasar.tuigwaa.model.core.impl.DateAttribute;
42 import org.seasar.tuigwaa.model.core.impl.FileAttribute;
43 import org.seasar.tuigwaa.model.core.impl.FkAttribute;
44 import org.seasar.tuigwaa.model.core.impl.FloatAttribute;
45 import org.seasar.tuigwaa.model.core.impl.IntegerAttribute;
46 import org.seasar.tuigwaa.model.core.impl.SecurityAttribute;
47 import org.seasar.tuigwaa.model.core.impl.SelfAttribute;
48 import org.seasar.tuigwaa.model.core.impl.SetAttribute;
49 import org.seasar.tuigwaa.model.core.impl.StringAttribute;
50 import org.seasar.tuigwaa.model.core.impl.TimestampAttribute;
51 import org.seasar.tuigwaa.system.Constants;
52 import org.seasar.tuigwaa.util.TgwContext;
53 import org.seasar.tuigwaa.util.TgwNameUtils;
54 import org.seasar.tuigwaa.util.TgwResource;
55
56 public class EntityUtils {
57
58 public static final String ESCAPED_ID = "__id__";
59
60 public static final String ENTITY_BUILTIN_RESOURCE_PREFIX = "entity.builtin.";
61
62
63 private static final Map TGWTYPE2TGWATTR_MAP;
64
65
66 private static final Map JAVA2TGWATTR_MAP;
67
68
69 private static final Map TGWTYPE2JAVA_MAP;
70
71 static {
72 Map map = new HashMap();
73 map.put(TgwAttribute.TYPE_STRING, StringAttribute.class);
74 map.put(TgwAttribute.TYPE_INTEGER, IntegerAttribute.class);
75 map.put(TgwAttribute.TYPE_FLOAT, FloatAttribute.class);
76 map.put(TgwAttribute.TYPE_BOOLEAN, BooleanAttribute.class);
77 map.put(TgwAttribute.TYPE_DATE, DateAttribute.class);
78 map.put(TgwAttribute.TYPE_FILE, FileAttribute.class);
79 map.put(TgwAttribute.TYPE_FK, FkAttribute.class);
80 map.put(TgwAttribute.TYPE_SET, SetAttribute.class);
81 map.put(TgwAttribute.TYPE_SELF, SelfAttribute.class);
82 TGWTYPE2TGWATTR_MAP = Collections.unmodifiableMap(map);
83
84 Map map2 = new HashMap();
85 map2.put(int.class.getName(), IntegerAttribute.class);
86 map2.put(long.class.getName(), IntegerAttribute.class);
87 map2.put(float.class.getName(), FloatAttribute.class);
88 map2.put(double.class.getName(), FloatAttribute.class);
89 map2.put(boolean.class.getName(), BooleanAttribute.class);
90 map2.put(String.class.getName(), StringAttribute.class);
91 map2.put(Integer.class.getName(), IntegerAttribute.class);
92 map2.put(Long.class.getName(), IntegerAttribute.class);
93 map2.put(Float.class.getName(), FloatAttribute.class);
94 map2.put(Double.class.getName(), FloatAttribute.class);
95 map2.put(Boolean.class.getName(), BooleanAttribute.class);
96 map2.put(Date.class.getName(), DateAttribute.class);
97 map2.put(java.sql.Date.class.getName(), DateAttribute.class);
98 map2.put(Timestamp.class.getName(), TimestampAttribute.class);
99 map2.put(byte[].class.getName(), ByteArrayAttribute.class);
100 JAVA2TGWATTR_MAP = Collections.unmodifiableMap(map2);
101
102 Map tmp = new HashMap();
103 tmp.put(TgwAttribute.TYPE_STRING, String.class);
104 tmp.put(TgwAttribute.TYPE_INTEGER, Integer.class);
105 tmp.put(TgwAttribute.TYPE_LONG, Long.class);
106 tmp.put(TgwAttribute.TYPE_FLOAT, Float.class);
107 tmp.put(TgwAttribute.TYPE_BOOLEAN, Boolean.class);
108 tmp.put(TgwAttribute.TYPE_DATE, Date.class);
109 tmp.put(TgwAttribute.TYPE_STRINGARRAY, Integer.class);
110 tmp.put(TgwAttribute.TYPE_TIMESTAMP, Date.class);
111 tmp.put(TgwAttribute.TYPE_SET, Set.class);
112 tmp.put(TgwAttribute.TYPE_FILE, "byte[]");
113 tmp.put(TgwAttribute.TYPE_SECURITY, String.class);
114 tmp.put(TgwAttribute.TYPE_BYTEARRAY, "byte[]");
115 TGWTYPE2JAVA_MAP = Collections.unmodifiableMap(tmp);
116 }
117
118
119
120 public static Class toJavaClass(TgwAttribute attribute) {
121 return (Class) TGWTYPE2JAVA_MAP.get(attribute.getType());
122 }
123
124 public static TgwAttribute toTgwAttribute(Class clazz) {
125 return toTgwAttribute(clazz.getName());
126 }
127
128 public static TgwAttribute toTgwAttribute(String className) {
129 Class attrClass = (Class) JAVA2TGWATTR_MAP.get(className);
130 if (attrClass != null) {
131 try {
132 TgwAttribute attr = (TgwAttribute) attrClass.newInstance();
133 return attr;
134 } catch (InstantiationException e) {
135
136 } catch (IllegalAccessException e) {
137
138 }
139 }
140 return null;
141 }
142
143 public static String toEntityName(Class clazz) {
144 String[] array = clazz.getName().split("//.");
145 return array[array.length - 1].toLowerCase();
146 }
147
148 public static String toJavaType(TgwAttribute attribute) {
149 if (attribute instanceof FkAttribute) {
150 return ((FkAttribute) attribute).getRefClassName();
151 } else if (attribute instanceof SelfAttribute) {
152 return attribute.getEntity().getJavaClassName();
153 } else if (attribute instanceof FileAttribute) {
154 return "byte[]";
155 }
156 return toJavaClass(attribute).getName();
157 }
158
159
160
161 public static boolean isBuiltinAttribute(TgwAttribute attr) {
162 if (attr instanceof TimestampAttribute
163 || attr instanceof SecurityAttribute) {
164 return true;
165 }
166 return false;
167 }
168
169 public static boolean isM2MAttribute(TgwAttribute attr) {
170 if ((attr instanceof SetAttribute)
171 && SetAttribute.OPTION_M2M.equals(attr.getOption())) {
172 return true;
173 }
174 return false;
175 }
176
177 public static TgwAttribute createAttribute(String type) {
178 TgwAttribute attr = null;
179 Class clazz = (Class) TGWTYPE2TGWATTR_MAP.get(type);
180 try {
181 attr = (TgwAttribute) clazz.newInstance();
182 } catch (InstantiationException e) {
183
184 } catch (IllegalAccessException e) {
185
186 }
187 return attr;
188 }
189
190 public static void setUser(Object obj) {
191 Principal principal = TgwContext.getPrincipal();
192 if (principal == null) {
193 return;
194 }
195
196
197 if (PropertyUtils.isWriteable(obj,
198 Constants.ENTITY_BUILTIN_CREATIONUSER)) {
199 if (!hasId(obj)) {
200 setProperty(obj, Constants.ENTITY_BUILTIN_CREATIONUSER,
201 principal.getName());
202 }
203 }
204
205
206 if (PropertyUtils.isWriteable(obj,
207 Constants.ENTITY_BUILTIN_MODIFICATIONUSER)) {
208 setProperty(obj, Constants.ENTITY_BUILTIN_MODIFICATIONUSER,
209 principal.getName());
210 }
211 }
212
213 public static void setTimestamp(Object obj) {
214
215 if (PropertyUtils.isWriteable(obj,
216 Constants.ENTITY_BUILTIN_CREATIONDATE)) {
217 if (!hasId(obj)) {
218 setTimestamp(obj, Constants.ENTITY_BUILTIN_CREATIONDATE);
219 }
220 }
221
222
223 if (PropertyUtils.isWriteable(obj,
224 Constants.ENTITY_BUILTIN_MODIFICATIONDATE)) {
225 setTimestamp(obj, Constants.ENTITY_BUILTIN_MODIFICATIONDATE);
226 }
227 }
228
229 private static void setTimestamp(Object obj, String property) {
230 try {
231 Date now = new Date();
232 Class propType = PropertyUtils.getPropertyType(obj, property);
233 if (propType.equals(Date.class)) {
234 setProperty(obj, property, now);
235 } else if (propType.equals(Timestamp.class)) {
236 setProperty(obj, property, new Timestamp(now.getTime()));
237 }
238 } catch (IllegalAccessException e) {
239
240 } catch (InvocationTargetException e) {
241
242 } catch (NoSuchMethodException e) {
243
244 }
245 }
246
247 public static void addCreationUserAttribute(TgwEntity entity,
248 boolean userUniqueFlag, String attrName) {
249 SecurityAttribute attr = new SecurityAttribute();
250 attr.setName(Constants.ENTITY_BUILTIN_CREATIONUSER);
251 attr.setDisplayName(attrName);
252 attr.setUnique(userUniqueFlag);
253 entity.addField(attr);
254 }
255
256 public static void addCreationUserAttribute(TgwEntity entity,
257 boolean userUniqueFlag) {
258 addCreationUserAttribute(entity, userUniqueFlag, TgwResource
259 .getMessage(ENTITY_BUILTIN_RESOURCE_PREFIX
260 + Constants.ENTITY_BUILTIN_CREATIONUSER));
261 }
262
263 public static void addModificationUserAttribute(TgwEntity entity) {
264 addModificationUserAttribute(entity, TgwResource
265 .getMessage(ENTITY_BUILTIN_RESOURCE_PREFIX
266 + Constants.ENTITY_BUILTIN_MODIFICATIONUSER));
267 }
268
269 public static void addModificationUserAttribute(TgwEntity entity,
270 String attrDisplayName) {
271 SecurityAttribute attr = new SecurityAttribute();
272 attr.setName(Constants.ENTITY_BUILTIN_MODIFICATIONUSER);
273 attr.setDisplayName(attrDisplayName);
274 entity.addField(attr);
275 }
276
277 public static void addCreationDateAttribute(TgwEntity entity) {
278 addCreationDateAttribute(entity, TgwResource
279 .getMessage(ENTITY_BUILTIN_RESOURCE_PREFIX
280 + Constants.ENTITY_BUILTIN_CREATIONDATE));
281 }
282
283 public static void addCreationDateAttribute(TgwEntity entity,
284 String displayName) {
285 TimestampAttribute tfield = new TimestampAttribute();
286 tfield.setName(Constants.ENTITY_BUILTIN_CREATIONDATE);
287 tfield.setDisplayName(displayName);
288 entity.addField(tfield);
289 }
290
291 public static void addModificationDateAttribute(TgwEntity entity) {
292 addModificationDateAttribute(entity, TgwResource
293 .getMessage(ENTITY_BUILTIN_RESOURCE_PREFIX
294 + Constants.ENTITY_BUILTIN_MODIFICATIONDATE));
295 }
296
297 public static void addModificationDateAttribute(TgwEntity entity,
298 String displayName) {
299 TimestampAttribute tfield = new TimestampAttribute();
300 tfield.setName(Constants.ENTITY_BUILTIN_MODIFICATIONDATE);
301 tfield.setDisplayName(displayName);
302 entity.addField(tfield);
303 }
304
305 public static boolean hasId(Object bean) {
306 Long id = getId(bean);
307 return (id != null && ((Long) id).longValue() != 0l);
308 }
309
310 public static Long getEscapedId(Object bean) {
311 return (Long) getProperty(bean, ESCAPED_ID);
312 }
313
314 public static Long getId(Object bean) {
315 return getId(bean, false);
316 }
317
318 public static Long getId(Object bean, boolean isDynaProperty) {
319 String idProp = Constants.ENTITY_BUILTIN_ID;
320 if (isDynaProperty) {
321 idProp = TgwNameUtils.toDynaPropertyName(idProp);
322 }
323 return (Long) getProperty(bean, idProp);
324 }
325
326 public static void setId(Object bean, Long id) {
327 setId(bean, id, false);
328 }
329
330 public static void setId(Object bean, Long id, boolean isDynaProperty) {
331 String idProp = Constants.ENTITY_BUILTIN_ID;
332 if (isDynaProperty) {
333 idProp = TgwNameUtils.toDynaPropertyName(idProp);
334 }
335 setProperty(bean, idProp, id);
336 }
337
338 public static Object newInstanceById(TgwEntity entity, Long id) {
339 return newInstanceById(entity.getJavaClass(), id);
340 }
341
342 public static Object newInstanceById(Class clazz, Long id) {
343 if (clazz == null) {
344 return null;
345 }
346 try {
347 Object obj = clazz.newInstance();
348 setId(obj, id);
349 return obj;
350 } catch (InstantiationException e) {
351
352 } catch (IllegalAccessException e) {
353
354 }
355 return null;
356 }
357
358 public static Object getProperty(Object valueObj, TgwAttribute field) {
359 return getProperty(valueObj, field.getName());
360 }
361
362 public static void copyPropertyCollectionEscape(Object src, Object target,
363 String propertyName) {
364 Object value = getProperty(src, propertyName);
365 if (value != null && value instanceof Collection) {
366 Collection collection = (Collection) getProperty(target,
367 propertyName);
368 collection.clear();
369 collection.addAll((Collection) value);
370 } else {
371 setProperty(target, propertyName, value);
372 }
373 }
374
375 public static Object copyPropertiesCollectionEscape(Object src,
376 Object target, TgwEntity entity) {
377 Iterator itr = entity.getFieldList().iterator();
378 while (itr.hasNext()) {
379 TgwAttribute attr = (TgwAttribute) itr.next();
380 if (attr instanceof SetAttribute && !manyToManyFlag(attr)) {
381 continue;
382 }
383 copyPropertyCollectionEscape(src, target, attr.getName());
384 }
385 return target;
386 }
387
388 public static String getKeys(TgwEntity entity) {
389 return entity.getDomainName() + "." + entity.getName();
390 }
391
392 public static Object exchange(TgwEntity entity, DynaBean src) {
393 return exchange(entity.newInstance(), src, entity);
394 }
395
396 public static Object exchange(Object dest, DynaBean src, TgwEntity entity) {
397
398
399 if (PropertyUtils.isWriteable(dest, Constants.ENTITY_BUILTIN_ID)) {
400 Object value = getProperty(src, ESCAPED_ID);
401 setProperty(dest, Constants.ENTITY_BUILTIN_ID, value);
402 }
403
404 for (Iterator i = entity.getFieldIterator(); i.hasNext();) {
405 TgwAttribute attr = (TgwAttribute) i.next();
406 String propertyName = attr.getName();
407 if (!isBuiltinAttribute(attr)
408 && PropertyUtils.isWriteable(dest, propertyName)) {
409
410 String dynaPropertyName = TgwNameUtils
411 .toDynaPropertyName(propertyName);
412
413 if (!PropertyUtils.isReadable(src, dynaPropertyName)) {
414 continue;
415 }
416 Object value = getProperty(src, dynaPropertyName);
417 String attrType = attr.getType();
418
419
420 if (TgwAttribute.TYPE_STRING.equals(attrType)) {
421 String str = (String) value;
422 if (str == null || "".equals(str)) {
423 continue;
424 }
425 }
426
427 setProperty(dest, propertyName, value);
428
429 if (TgwAttribute.TYPE_DATE.equals(attrType)) {
430 exchangeDate(propertyName, (Date) value, dest);
431 }
432 }
433 }
434 return dest;
435 }
436
437 private static void exchangeDate(String name, Date date, Object dest) {
438 if (date == null) {
439 return;
440 }
441 Calendar cal = Calendar.getInstance();
442 cal.setTime(date);
443 String[] dateParts = TgwNameUtils.toDateParts(name);
444 setProperty(dest, dateParts[0], new Integer(cal.get(Calendar.YEAR)));
445 setProperty(dest, dateParts[1],
446 new Integer(cal.get(Calendar.MONTH) + 1));
447 setProperty(dest, dateParts[2], new Integer(cal.get(Calendar.DATE)));
448 }
449
450 public static Object getProperty(Object valueObj, String fieldName) {
451 if (valueObj != null) {
452 try {
453 return PropertyUtils.getProperty(valueObj, fieldName);
454 } catch (IllegalAccessException e) {
455 e.printStackTrace();
456 throw new RuntimeException(e);
457 } catch (InvocationTargetException e) {
458 e.printStackTrace();
459 throw new RuntimeException(e);
460 } catch (NoSuchMethodException e) {
461 e.printStackTrace();
462 throw new RuntimeException(e);
463 }
464 }
465 return null;
466 }
467
468 public static void setProperty(Object bean, TgwAttribute attr, Object value) {
469 setProperty(bean, attr.getName(), value);
470 }
471
472 public static void convertAndSetProperty(Object bean, TgwAttribute attr,
473 String value) {
474 Class clazz = toJavaClass(attr);
475 if (value != null && !java.lang.String.class.equals(clazz)) {
476 Object obj = ConvertUtils.convert(value, clazz);
477 setProperty(bean, attr, obj);
478 } else {
479
480 setProperty(bean, attr, value);
481 }
482 }
483
484 public static void setProperty(Object bean, String propertyName,
485 Object value) {
486 try {
487 PropertyDescriptor desc = PropertyUtils.getPropertyDescriptor(bean,
488 propertyName);
489 if (desc != null
490 && (desc.getPropertyType().isPrimitive() && value == null)) {
491 return;
492 }
493 PropertyUtils.setProperty(bean, propertyName, value);
494 } catch (IllegalAccessException e) {
495
496 } catch (InvocationTargetException e) {
497
498 } catch (NoSuchMethodException e) {
499
500 }
501 }
502
503 public static boolean needConverter(TgwAttribute attr) {
504 if (attr instanceof DateAttribute || attr instanceof FkAttribute
505 || attr instanceof SelfAttribute
506 || attr instanceof FileAttribute
507 || attr instanceof SetAttribute
508 || attr instanceof TimestampAttribute) {
509 return true;
510 }
511 return false;
512 }
513
514 public static boolean isExistFileField(TgwEntity entity) {
515 Iterator itr = entity.getFieldList().iterator();
516 while (itr.hasNext()) {
517 TgwAttribute field = (TgwAttribute) itr.next();
518 if (field instanceof FileAttribute) {
519 return true;
520 }
521 }
522 return false;
523 }
524
525 public static boolean hasAttribute(TgwEntity entity, String attrType){
526 for(Iterator itr = entity.getFieldList().iterator();itr.hasNext();){
527 TgwAttribute field = (TgwAttribute) itr.next();
528 if (field.getType().equals(attrType)) {
529 return true;
530 }
531 }
532 return false;
533 }
534
535 public static boolean hasSelfAttribute(TgwEntity entity) {
536 Iterator itr = entity.getFieldList().iterator();
537 while (itr.hasNext()) {
538 TgwAttribute field = (TgwAttribute) itr.next();
539 if (field instanceof SelfAttribute) {
540 return true;
541 }
542 }
543 return false;
544 }
545
546 public static TgwAttribute getEmailAttribute(TgwEntity entity) {
547 Iterator itr = entity.getFieldIterator();
548 while (itr.hasNext()) {
549 TgwAttribute attr = (TgwAttribute) itr.next();
550 if (isEmail(attr)) {
551 return attr;
552 }
553 }
554 return null;
555 }
556
557 public static boolean isEmail(TgwAttribute field) {
558 if (field instanceof StringAttribute
559 && ((StringAttribute) field).getMask().equals(
560 StringAttribute.MASK_EMAIL)) {
561 return true;
562 }
563 return false;
564 }
565
566 public static boolean manyToMnayFlag(TgwEntity entity) {
567 Iterator itr = entity.getFieldList().iterator();
568 while (itr.hasNext()) {
569 TgwAttribute attr = (TgwAttribute) itr.next();
570 if (manyToManyFlag(attr)) {
571 return true;
572 }
573 }
574 return false;
575 }
576
577 public static boolean manyToManyFlag(TgwAttribute attr) {
578 if (!(attr instanceof SetAttribute)) {
579 return false;
580 }
581 return manyToManyFlag((SetAttribute) attr);
582 }
583
584 public static boolean oneToManyFlag(TgwAttribute attr) {
585 if (!(attr instanceof SetAttribute)) {
586 return false;
587 }
588 return !manyToManyFlag((SetAttribute) attr);
589 }
590
591 public static boolean manyToManyFlag(SetAttribute attr) {
592 if (attr == null) {
593 return false;
594 }
595
596 TgwEntity ref = attr.getRefEntity();
597 Collection list = ref.getFieldList();
598 if (list.size() != 2) {
599 return false;
600 }
601 TgwAttribute target = getTheOtherAttribute(attr);
602 if (target instanceof FkAttribute) {
603 return true;
604 } else if (target instanceof StringAttribute
605 && "restriction".equals(target.getOption())) {
606 return true;
607 }
608 return false;
609 }
610
611 public static TgwAttribute getTheOtherAttribute(SetAttribute attr) {
612 TgwEntity ref = attr.getRefEntity();
613 if (ref == null) {
614 return null;
615 }
616 Collection list = ref.getFieldList();
617 if (list.size() != 2) {
618 return null;
619 }
620 for (Iterator i = list.iterator(); i.hasNext();) {
621 TgwAttribute anAttr = (TgwAttribute) i.next();
622 if (!anAttr.equals(attr.getInverseField())) {
623 return anAttr;
624 }
625 }
626 return null;
627 }
628
629 public static boolean contains(Set set, TgwAttribute theOtherAttr,
630 Object value) {
631 if (set == null) {
632 return false;
633 }
634
635 for (Iterator i = set.iterator(); i.hasNext();) {
636 Object obj = getProperty(i.next(), theOtherAttr);
637 if (theOtherAttr instanceof FkAttribute) {
638 String id = String.valueOf(getId(obj));
639 if (id.equals(value)) {
640 return true;
641 }
642 } else if (theOtherAttr instanceof StringAttribute) {
643 if (obj.equals(value)) {
644 return true;
645 }
646 }
647 }
648 return false;
649 }
650
651 public static void bindSelfObject(Object obj, TgwEntity entity) {
652 if (entity == null) {
653 return;
654 }
655 Iterator itr = entity.getFieldIterator();
656 while (itr.hasNext()) {
657 TgwAttribute attr = (TgwAttribute) itr.next();
658 if (attr instanceof SetAttribute) {
659 SetAttribute setAttr = (SetAttribute) attr;
660 FkAttribute fk = setAttr.getInverseField();
661 Set set = (Set) EntityUtils.getProperty(obj, setAttr);
662 if (set == null) {
663 continue;
664 }
665 for (Iterator i = set.iterator(); i.hasNext();) {
666 Object bean = i.next();
667 EntityUtils.setProperty(bean, fk, obj);
668 }
669 }
670 }
671 }
672
673 public static void removeSelfObjectTree(Object selfObj, TgwEntity entity,
674 Collection visitedEntities) {
675 if (entity == null) {
676 return;
677 }
678 if (visitedEntities == null) {
679 visitedEntities = new HashSet();
680 }
681 visitedEntities.add(entity);
682 Iterator itr = entity.getFieldIterator();
683 while (itr.hasNext()) {
684 TgwAttribute attr = (TgwAttribute) itr.next();
685 if (attr instanceof FkAttribute) {
686 FkAttribute fkAttr = (FkAttribute) attr;
687 TgwEntity refEntity = fkAttr.getRefEntity();
688 if (!visitedEntities.contains(refEntity)) {
689 SetAttribute setAttr = fkAttr.getInverseField();
690 if (setAttr != null) {
691 removeSelfObject(selfObj, fkAttr, setAttr);
692 }
693 }
694 } else if (attr instanceof SetAttribute) {
695 SetAttribute setAttr = (SetAttribute) attr;
696 TgwEntity refEntity = setAttr.getRefEntity();
697 if (!visitedEntities.contains(refEntity)) {
698 Set childrenObj = (Set) getProperty(selfObj, setAttr);
699 if (childrenObj != null) {
700 for (Iterator i = childrenObj.iterator(); i.hasNext();) {
701 Object childObj = i.next();
702 removeSelfObjectTree(childObj, refEntity,
703 visitedEntities);
704 }
705 }
706 }
707 }
708 }
709 }
710
711 private static void removeSelfObject(Object selfObj, FkAttribute fkAttr,
712 SetAttribute setAttr) {
713 Object parentObj = getProperty(selfObj, fkAttr);
714 Set set = (Set) getProperty(parentObj, setAttr);
715 if(set != null){
716 set.remove(selfObj);
717 }
718
719 }
720
721 public static List getCrossRefList(TgwEntity entity) {
722 Iterator itr = entity.getFieldList().iterator();
723 List list = new ArrayList();
724 while (itr.hasNext()) {
725 TgwAttribute attr = (TgwAttribute) itr.next();
726 if (attr instanceof FkAttribute) {
727 FkAttribute fk = (FkAttribute) attr;
728 if (fk.getInverseField() != null) {
729 list.add(fk.getInverseField());
730 }
731 }
732 }
733 return list;
734 }
735
736 /***
737 * 引数に指定したエンティティ(テーブル)のカラム名一覧を返す
738 */
739 public static String[] getColumns(TgwEntity entity) {
740
741 Collection fieldList = entity.getFieldList();
742 int fieldnum = fieldList.size();
743 String[] columns = new String[fieldnum + 1];
744 columns[0] = entity.getPrimaryKeyColumnName();
745 int idx = 1;
746 for (Iterator i = fieldList.iterator(); i.hasNext();) {
747 TgwAttribute attr = (TgwAttribute) i.next();
748 columns[idx++] = attr.getName();
749 }
750 return columns;
751 }
752
753 public static String[] getColumnTypes(TgwEntity entity) {
754
755 Collection fieldList = entity.getFieldList();
756 int fieldnum = fieldList.size();
757 String[] types = new String[fieldnum + 1];
758 types[0] = "java.lang.Long";
759 int idx = 1;
760 for (Iterator i = fieldList.iterator(); i.hasNext();) {
761 TgwAttribute attr = (TgwAttribute) i.next();
762 types[idx++] = toJavaClass(attr).getName();
763 }
764 return types;
765 }
766
767 public static boolean alreadyUserDataInserted(TgwEntity entity) {
768 SecurityAttribute attr = (SecurityAttribute) entity
769 .getField(Constants.ENTITY_BUILTIN_CREATIONUSER);
770 if (attr == null) {
771 return false;
772 }
773 if (!attr.isUnique()) {
774 return false;
775 }
776 Object obj = DataServiceUtils.loadByValue(entity,
777 Constants.ENTITY_BUILTIN_CREATIONUSER, TgwContext
778 .getPrincipal().getName());
779 return obj != null;
780 }
781 }