View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.database.util;
17  
18  import java.sql.ResultSetMetaData;
19  import java.sql.SQLException;
20  import java.util.HashMap;
21  import java.util.HashSet;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import org.seasar.tuigwaa.database.DatabaseInfo;
28  import org.seasar.tuigwaa.database.DatabaseInfo.ForeignKeyInfo;
29  import org.seasar.tuigwaa.model.common.EntityUtils;
30  import org.seasar.tuigwaa.model.core.TgwAttribute;
31  import org.seasar.tuigwaa.model.core.TgwEntity;
32  import org.seasar.tuigwaa.model.core.impl.FkAttribute;
33  import org.seasar.tuigwaa.model.core.impl.TgwEntityImpl;
34  import org.seasar.tuigwaa.system.Constants;
35  
36  
37  public class EntityFactory {
38  
39  	// private Log log = LogFactory.getLog(getClass());
40  
41  	private String schema;
42  
43  	private String domainName;
44  
45  	private DatabaseInfo dbInfo;
46  
47  	private Map createdEntityMap = new HashMap();
48  
49  	private Set unreadableEntities = new HashSet();
50  
51  	static {
52  
53  	}
54  
55  	public EntityFactory(String domainName, String schema, DatabaseInfo dbInfo) {
56  		this.domainName = domainName;
57  		this.schema = schema;
58  		this.dbInfo = dbInfo;
59  	}
60  
61  	public Map getCreatedEntityMap() {
62  		return createdEntityMap;
63  	}
64  
65  	public void addEntity(ResultSetMetaData metadata, String entityName) {
66  		List pks = dbInfo.getPrimaryKeys(null, schema, entityName);
67  		Map fkMap = dbInfo.getForeignKeyInfoMap(null, schema, entityName);
68  
69  		/***
70  		 * 主キーが複数あるテーブルは無視する
71  		 */
72  		if (pks.size() != 1) {
73  			removeEntity(entityName);
74  			return;
75  		}
76  
77  		TgwEntity entity = getEntity(domainName, entityName);
78  
79  		try {
80  			int num = metadata.getColumnCount();
81  
82  			for (int i = 1; i <= num; i++) {
83  
84  				String columnName = metadata.getColumnName(i);
85  				String columnClassName = metadata.getColumnClassName(i);
86  
87  				//該当するカラムが主キーだったら
88  				if (pks.contains(columnName)) {
89  					entity.setPrimaryKeyColumnName(columnName);
90  					entity.setPrimaryKeyDisplayName(columnName);
91  					continue;
92  				//"id"という主キーでないカラムは無視する
93  				} else if (Constants.ENTITY_BUILTIN_ID.equals(columnName)) {
94  					continue;
95  				}
96  
97  				ForeignKeyInfo fkinfo = (ForeignKeyInfo) fkMap.get(columnName);
98  
99  				TgwAttribute attr = (fkinfo == null) ? createAttribute(
100 						columnName, columnClassName) : createFkAttribute(
101 						fkinfo, columnName);
102 
103 				if (attr != null) {
104 					entity.addField(attr);
105 				}
106 			}
107 		} catch (SQLException e) {
108 			e.printStackTrace();
109 		}
110 
111 		if (entity.getFieldList().size() == 0) {
112 			removeEntity(entityName);
113 		}
114 	}
115 
116 	private TgwAttribute createFkAttribute(ForeignKeyInfo fkinfo,
117 			String columnName) {
118 
119 		String refTableName = fkinfo.getReferenceTableName();
120 		String refEntityName = refTableName.toLowerCase();
121 		if (!unreadableEntities.contains(refEntityName)) {
122 			TgwEntity refEntity = getEntity(domainName, refEntityName);
123 			FkAttribute fk = new FkAttribute();
124 			bindName(fk, columnName);
125 			fk.setRefEntity(refEntity);
126 			return fk;
127 		}
128 		return null;
129 
130 	}
131 
132 	private TgwAttribute createAttribute(String columnName,
133 			String columnClassName) throws SQLException {
134 		TgwAttribute attr = EntityUtils.toTgwAttribute(columnClassName);
135 		if (attr == null) {
136 			return null;
137 		}
138 		bindName(attr, columnName);
139 		return attr;
140 	}
141 
142 	private void bindName(TgwAttribute attr, String columnName) {
143 		String attrName = columnName.toLowerCase();
144 		attr.setName(attrName);
145 		attr.setDisplayName(attrName);
146 	}
147 
148 	private TgwEntity getEntity(String domainName, String entityName) {
149 		TgwEntity entity = (TgwEntity) createdEntityMap.get(entityName);
150 		if (entity == null) {
151 			entity = new TgwEntityImpl(domainName, entityName);
152 			entity.setDisplayName(entityName);
153 			entity.setImportedEntity(true);
154 			createdEntityMap.put(entityName, entity);
155 		}
156 		return entity;
157 	}
158 
159 	private void removeEntity(String entityName) {
160 		unreadableEntities.add(entityName);
161 		TgwEntity entity = (TgwEntity) createdEntityMap.remove(entityName);
162 		if (entity != null) {
163 			List list = entity.getReferencedFkFieldList();
164 			for (Iterator i = list.iterator(); i.hasNext();) {
165 				FkAttribute fk = (FkAttribute) i.next();
166 				TgwEntity refEntity = fk.getEntity();
167 				refEntity.removeAttribute(fk.getName());
168 			}
169 		}
170 	}
171 }