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.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashSet;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Set;
24
25 import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
26 import org.seasar.tuigwaa.model.ModelService;
27 import org.seasar.tuigwaa.model.core.TgwAttribute;
28 import org.seasar.tuigwaa.model.core.TgwDomain;
29 import org.seasar.tuigwaa.model.core.TgwEntity;
30 import org.seasar.tuigwaa.model.core.impl.FkAttribute;
31 import org.seasar.tuigwaa.util.TgwUtils;
32
33
34 public class DomainUtils {
35
36 public static Collection getEntityTree(TgwDomain domain) {
37 return null;
38 }
39
40 public static Collection getReferencedEntities(TgwEntity entity) {
41 Set inverseRefSet = new HashSet();
42
43 entity.accept(ADD_INVERSEREF_ENTITY_VISITOR, inverseRefSet);
44 return inverseRefSet;
45 }
46
47 public static Collection getReferenceEntities(TgwEntity entity) {
48 throw new UnsupportedOperationException();
49 }
50
51 public static Collection getCrossReferenceEntities(TgwEntity entity) {
52 Set refSet = new HashSet();
53 addCrossReferenceEntities(entity, refSet);
54 return refSet;
55 }
56
57 private static void addCrossReferenceEntities(TgwEntity entity,
58 Collection entities) {
59 Iterator itr = entity.getFieldList().iterator();
60 while (itr.hasNext()) {
61 TgwAttribute attr = (TgwAttribute) itr.next();
62 if (attr instanceof FkAttribute
63 && ((FkAttribute) attr).getInverseField() != null) {
64 TgwEntity refEntity = ((FkAttribute) attr).getRefEntity();
65 if (!entities.contains(refEntity)) {
66 entities.add(refEntity);
67 addCrossReferenceEntities(refEntity, entities);
68 }
69 }
70 }
71 }
72
73 public static List getSortedTalbeNames(String schema){
74 List list = sortByReference(schema);
75 List ret = new ArrayList();
76 if(list == null){
77 return ret;
78 }
79 for(Iterator i=list.iterator();i.hasNext();){
80 String tableName = TgwUtils.toTableName((TgwEntity)i.next());
81 ret.add(tableName);
82 }
83 return ret;
84 }
85
86 public static List sortByReference(String domainName) {
87 TgwDomain domain = ((ModelService) SingletonS2ContainerFactory
88 .getContainer().getComponent(ModelService.class))
89 .getDomain(domainName);
90 return sortByReference(domain);
91 }
92
93 public static List sortByNoReference(TgwDomain domain) {
94 List entityList = domain.getEntityList();
95 return sortByNoReference(entityList);
96 }
97
98 public static List sortByReference(TgwDomain domain) {
99 List entityList = domain.getEntityList();
100 return sortByNoReference(entityList);
101 }
102
103 public static List sortByReference(Collection entities) {
104 List sortedList = new ArrayList();
105 sortByReference(sortedList, entities);
106 return sortedList;
107 }
108
109 public static List sortByNoReference(Collection entities) {
110 List sortedList = new ArrayList();
111 sortByNoReference(sortedList, entities);
112 return sortedList;
113 }
114
115 private static void sortByNoReference(List sortedList, Collection entityList) {
116 List notSortedList = new ArrayList();
117 for (Iterator i = entityList.iterator(); i.hasNext();) {
118 TgwEntity entity = (TgwEntity) i.next();
119 if (Boolean.TRUE.equals(entity.accept(SORT_VISITOR, sortedList))) {
120 sortedList.add(entity);
121 } else {
122 notSortedList.add(entity);
123 }
124 }
125 if (notSortedList.size() > 0) {
126 sortByNoReference(sortedList, notSortedList);
127 }
128 }
129
130 private static void sortByReference(List sortedList, Collection entityList) {
131 List notSortedList = new ArrayList();
132 for (Iterator i = entityList.iterator(); i.hasNext();) {
133 TgwEntity entity = (TgwEntity) i.next();
134 if (hasReferenced(entity, sortedList)) {
135 sortedList.add(entity);
136 } else {
137 notSortedList.add(entity);
138 }
139 }
140 if (notSortedList.size() > 0) {
141 sortByReference(sortedList, notSortedList);
142 }
143 }
144
145 private static boolean hasReferenced(TgwEntity entity, List sortedList) {
146 List referencedList = entity.getReferencedFkFieldList();
147 boolean flag = true;
148 for (Iterator i = referencedList.iterator(); i.hasNext();) {
149 FkAttribute fk = (FkAttribute) i.next();
150 TgwEntity refedEntity = fk.getEntity();
151 if (!sortedList.contains(refedEntity)) {
152 return false;
153 }
154 }
155 return flag;
156 }
157
158 private static TgwElementVisitor SORT_VISITOR = new DefaultTuigwaaElementVisitor() {
159
160 /***
161 * @return if true , the entity has no foreign key or an entity of the
162 * foreign key has already inserted into sorted set.
163 */
164 public Object visit(TgwEntity entity, Object sortedList) {
165 boolean flag = true;
166 Collection col = entity.getFieldList();
167 for (Iterator i = col.iterator(); i.hasNext();) {
168 TgwAttribute attr = (TgwAttribute) i.next();
169 if (attr instanceof FkAttribute) {
170 if (Boolean.FALSE.equals(attr.accept(this, sortedList))) {
171 return Boolean.FALSE;
172 }
173 }
174 }
175 return new Boolean(flag);
176 }
177
178 public Object visit(FkAttribute attr, Object data) {
179 TgwEntity refEntity = attr.getRefEntity();
180 List sortedList = (List) data;
181 if (!sortedList.contains(refEntity)) {
182 return Boolean.FALSE;
183 }
184 return refEntity.accept(this, data);
185 }
186
187 };
188
189 private static final TgwElementVisitor ADD_INVERSEREF_ENTITY_VISITOR = new DefaultTuigwaaElementVisitor() {
190
191 public Object visit(FkAttribute attr, Object obj) {
192 Set entityList = (Set) obj;
193 TgwEntity entity = attr.getEntity();
194 entityList.add(entity);
195 entity.accept(this, entityList);
196 return null;
197 }
198
199 public Object visit(TgwEntity entity, Object obj) {
200 Set entityList = (Set) obj;
201 List list = entity.getReferencedFkFieldList();
202 for (Iterator i = list.iterator(); i.hasNext();) {
203 FkAttribute fkFiled = (FkAttribute) i.next();
204 fkFiled.accept(this, entityList);
205 }
206 return null;
207 }
208 };
209 }