1 package com.explosion.expfmodules.rdbmsconn.dbom;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Map;
8
9 import org.apache.log4j.LogManager;
10 import org.apache.log4j.Logger;
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 /***
29 * @author Stephen Cowx
30 */
31
32 public class DBEntity
33 {
34
35 public static final int TYPE_OTHER = 0;
36 public static final int TYPE_SCHEMA = 1;
37 public static final int TYPE_CATALOG = 2;
38 public static final int TYPE_TABLE = 3;
39 public static final int TYPE_VIEW = 4;
40 public static final int TYPE_SYSTEM_TABLE = 5;
41 public static final int TYPE_SYNONYM = 6;
42
43 private String catalogName;
44 private String schemaName;
45 private String entityName;
46 private String ownerName;
47 private int entityType;
48 private Map columns = new HashMap();
49 private List columnsList = new ArrayList();
50 private List pseudoColumns = new ArrayList();
51
52 private DBEntityBuffer dbEntityBuffer = new DBEntityBuffer();
53
54 private static Logger log = LogManager.getLogger(DBEntity.class);
55
56 public DBEntity(String catalogName, String schemaName, String entityName, int entityType)
57 {
58 this.catalogName = catalogName;
59 this.schemaName = schemaName;
60 this.entityName = check(entityName);
61 this.entityType = entityType;
62 }
63
64 /***
65 * This method clears the metadata for this object so that it can be refreshed from the
66 * database. This does not clear the buffered incofmration like sorts and things like
67 * that. It only clears column names etc.
68 */
69 public void clearMetadata()
70 {
71 columns = new HashMap();
72 columnsList = new ArrayList();
73 pseudoColumns = new ArrayList();
74 }
75
76 private String check(String name)
77 {
78 if (name != null)
79 return name.replaceAll("/","//");
80 else
81 return name;
82 }
83
84 public String getCatalogName()
85 {
86 return catalogName;
87 }
88
89 public String getSchemaName()
90 {
91 return schemaName;
92 }
93
94 public String getEntityName()
95 {
96 return entityName;
97 }
98
99 public String getOwnerName()
100 {
101 return ownerName;
102 }
103
104 public int getEntityType()
105 {
106 return entityType;
107 }
108
109 public DBEntityBuffer getDBEntityBuffer()
110 {
111 return dbEntityBuffer;
112 }
113
114 public void setCatalogName(String newCatalogName)
115 {
116 this.catalogName = newCatalogName;
117 }
118
119 public void setSchemaName(String newSchemaName)
120 {
121 this.schemaName = newSchemaName;
122 }
123
124 public void setEntityName(String newEntityName)
125 {
126 this.entityName = newEntityName;
127 }
128
129 public void setOwnerName(String ownerName)
130 {
131 this.ownerName = ownerName;
132 }
133
134 public void setEntityType(int newEntityType)
135 {
136 this.entityType = newEntityType;
137 }
138
139 public void setDBEntityBuffer(DBEntityBuffer dbEntityBuffer)
140 {
141 this.dbEntityBuffer = dbEntityBuffer;
142 }
143
144 /***
145 * Returns the name of this entity (not a fully qualified name)
146 * @see java.lang.Object#toString()
147 * @return
148 */
149 public String toString()
150 {
151 return entityName;
152 }
153
154 /***
155 * Returns a List of the columns for this entity.
156 * @return
157 */
158 public List getColumns()
159 {
160 return this.columnsList;
161 }
162
163 /***
164 * Adds a column to the Map of columns for this Entity
165 *
166 * @param column
167 * @return
168 */
169 public void addColumn(DBEntityColumn column) throws IllegalArgumentException
170 {
171 columnsList.add(column);
172 columns.put(column.getColumnName(), column);
173 }
174
175 /***
176 * Returns the DBEntityColumn for this entity
177 *
178 * @param columnName
179 * @return
180 */
181 public DBEntityColumn getColumn(String columnName)
182 {
183 return (DBEntityColumn) columns.get(columnName);
184 }
185
186 /***
187 * Returns a MNap of PrimaryKeys for this column
188 *
189 * @return
190 */
191 public List getPrimaryKeyColumns()
192 {
193 List list = new ArrayList();
194 for (Iterator i = columnsList.iterator(); i.hasNext();)
195 {
196 DBEntityColumn column = (DBEntityColumn) i.next();
197 if (column.isPrimaryKey())
198 list.add(column);
199 }
200 return list;
201
202 }
203
204 /***
205 * Returns a Lsit of BestRowIDentifiers for entity
206 * This is as it has been returned from the metadata object of the driver
207 *
208 * @return
209 */
210 public List getBestRowIdentifierColumns()
211 {
212 List list = new ArrayList();
213 for (Iterator i = columnsList.iterator(); i.hasNext();)
214 {
215 DBEntityColumn column = (DBEntityColumn) i.next();
216 if (column.isBestRowIdentifier())
217 list.add(column);
218 }
219 return list;
220
221 }
222
223 /***
224 * Returns a Map of ForeignKeys for this column
225 *
226 * @return
227 */
228 public List getForeignKeyColumns()
229 {
230 List list = new ArrayList();
231 for (Iterator i = columnsList.iterator(); i.hasNext();)
232 {
233 DBEntityColumn column = (DBEntityColumn) i.next();
234 if (column.isForeignKey())
235 list.add(column);
236 }
237 return list;
238 }
239
240 /***
241 * This method will work out the best means of identifying a particular row in a table.
242 * It will first try the getBestRowIdentifier() method
243 * If that fails then it will look for primary keys
244 * If that fails it will return all columns in a hope for the best sort of way
245 *
246 * This is the BEST and RECOMMENDED way of obtaining a quique set of columns for your
247 * application.
248 *
249 * Although you can use the other methods (e.g. getPrimaryKeyColumns() ) independantly
250 * try not to as this will provide you a more unified behaviour throughout your application
251 */
252 public List getDerivedIdentityColumns()
253 {
254 List list = getBestRowIdentifierColumns();
255 if (list.size() < 1)
256 {
257 list = getPrimaryKeyColumns();
258 if (list.size() < 1)
259 {
260 list = getColumns();
261 log.debug("Using all columns as choice for IdentityColumns.");
262 }
263 else
264 {
265 log.debug("Using Primary Key Columns as choice for IdentityColumns.");
266 }
267 }
268 else
269 {
270 log.debug("Using BestRowIdentifierColumns as choice for IdentityColumns.");
271 }
272
273 return list;
274 }
275
276 }