| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Table |
|
| 1.9054054054054055;1.905 |
| 1 | package org.apache.torque.engine.database.model; | |
| 2 | ||
| 3 | /* | |
| 4 | * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE | |
| 5 | * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file | |
| 6 | * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the | |
| 7 | * License. You may obtain a copy of the License at | |
| 8 | * | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 | * | |
| 11 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | |
| 12 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | |
| 13 | * specific language governing permissions and limitations under the License. | |
| 14 | */ | |
| 15 | ||
| 16 | import java.util.ArrayList; | |
| 17 | import java.util.Collections; | |
| 18 | import java.util.Hashtable; | |
| 19 | import java.util.Iterator; | |
| 20 | import java.util.List; | |
| 21 | import java.util.Map; | |
| 22 | ||
| 23 | import org.apache.commons.collections.map.ListOrderedMap; | |
| 24 | import org.apache.commons.lang.StringUtils; | |
| 25 | import org.apache.commons.logging.Log; | |
| 26 | import org.apache.commons.logging.LogFactory; | |
| 27 | import org.apache.torque.engine.EngineException; | |
| 28 | import org.xml.sax.Attributes; | |
| 29 | ||
| 30 | /** | |
| 31 | * Data about a table used in an application. | |
| 32 | * | |
| 33 | * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a> | |
| 34 | * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> | |
| 35 | * @author <a href="mailto:mpoeschl@marmot.at>Martin Poeschl</a> | |
| 36 | * @author <a href="mailto:jmcnally@collab.net>John McNally</a> | |
| 37 | * @author <a href="mailto:dlr@collab.net>Daniel Rall</a> | |
| 38 | * @author <a href="mailto:byron_foster@byron_foster@yahoo.com>Byron Foster</a> | |
| 39 | * @author <a href="mailto:monroe@dukece.com>Greg Monroe</a> | |
| 40 | * @version $Id: Table.java,v 1.1 2007-10-21 07:57:27 abyrne Exp $ | |
| 41 | */ | |
| 42 | public class Table implements IDMethod { | |
| 43 | /** Logging class from commons.logging */ | |
| 44 | 0 | private static Log log = LogFactory.getLog(Table.class); |
| 45 | ||
| 46 | // private AttributeListImpl attributes; | |
| 47 | private List columnList; | |
| 48 | private List foreignKeys; | |
| 49 | private List indices; | |
| 50 | private List unices; | |
| 51 | private List idMethodParameters; | |
| 52 | private String name; | |
| 53 | private String description; | |
| 54 | private String javaName; | |
| 55 | private String idMethod; | |
| 56 | private String javaNamingMethod; | |
| 57 | private Database tableParent; | |
| 58 | private List referrers; | |
| 59 | private List foreignTableNames; | |
| 60 | private boolean containsForeignPK; | |
| 61 | private Column inheritanceColumn; | |
| 62 | private boolean skipSql; | |
| 63 | private boolean abstractValue; | |
| 64 | private String alias; | |
| 65 | private String enterface; | |
| 66 | private String pkg; | |
| 67 | private String baseClass; | |
| 68 | private String basePeer; | |
| 69 | private Hashtable columnsByName; | |
| 70 | private Hashtable columnsByJavaName; | |
| 71 | private boolean needsTransactionInPostgres; | |
| 72 | private boolean heavyIndexing; | |
| 73 | private boolean forReferenceOnly; | |
| 74 | private Map options; | |
| 75 | ||
| 76 | /** | |
| 77 | * Default Constructor | |
| 78 | */ | |
| 79 | public Table() { | |
| 80 | 0 | this(null); |
| 81 | 0 | } |
| 82 | ||
| 83 | /** | |
| 84 | * Constructs a table object with a name | |
| 85 | * | |
| 86 | * @param name | |
| 87 | * table name | |
| 88 | */ | |
| 89 | 0 | public Table(String name) { |
| 90 | 0 | this.name = name; |
| 91 | 0 | columnList = new ArrayList(); |
| 92 | 0 | foreignKeys = new ArrayList(5); |
| 93 | 0 | indices = new ArrayList(5); |
| 94 | 0 | unices = new ArrayList(5); |
| 95 | 0 | columnsByName = new Hashtable(); |
| 96 | 0 | columnsByJavaName = new Hashtable(); |
| 97 | 0 | options = Collections.synchronizedMap(new ListOrderedMap()); |
| 98 | 0 | } |
| 99 | ||
| 100 | /** | |
| 101 | * Load the table object from an xml tag. | |
| 102 | * | |
| 103 | * @param attrib | |
| 104 | * xml attributes | |
| 105 | * @param defaultIdMethod | |
| 106 | * defined at db level | |
| 107 | */ | |
| 108 | public void loadFromXML(Attributes attrib, String defaultIdMethod) { | |
| 109 | 0 | name = attrib.getValue("name"); |
| 110 | 0 | javaName = attrib.getValue("javaName"); |
| 111 | 0 | idMethod = attrib.getValue("idMethod"); |
| 112 | ||
| 113 | // retrieves the method for converting from specified name to | |
| 114 | // a java name. | |
| 115 | 0 | javaNamingMethod = attrib.getValue("javaNamingMethod"); |
| 116 | 0 | if (javaNamingMethod == null) { |
| 117 | 0 | javaNamingMethod = getDatabase().getDefaultJavaNamingMethod(); |
| 118 | } | |
| 119 | ||
| 120 | 0 | if ("null".equals(idMethod)) { |
| 121 | 0 | idMethod = defaultIdMethod; |
| 122 | } | |
| 123 | 0 | skipSql = "true".equals(attrib.getValue("skipSql")); |
| 124 | // pkg = attrib.getValue("package"); | |
| 125 | 0 | abstractValue = "true".equals(attrib.getValue("abstract")); |
| 126 | 0 | baseClass = attrib.getValue("baseClass"); |
| 127 | 0 | basePeer = attrib.getValue("basePeer"); |
| 128 | 0 | alias = attrib.getValue("alias"); |
| 129 | 0 | heavyIndexing = "true".equals(attrib.getValue("heavyIndexing")) |
| 130 | || (!"false".equals(attrib.getValue("heavyIndexing")) && getDatabase().isHeavyIndexing()); | |
| 131 | 0 | description = attrib.getValue("description"); |
| 132 | 0 | enterface = attrib.getValue("interface"); |
| 133 | 0 | } |
| 134 | ||
| 135 | /** | |
| 136 | * <p> | |
| 137 | * A hook for the SAX XML parser to call when this table has been fully loaded from the XML, and all nested elements | |
| 138 | * have been processed. | |
| 139 | * </p> | |
| 140 | * | |
| 141 | * <p> | |
| 142 | * Performs heavy indexing and naming of elements which weren't provided with a name. | |
| 143 | * </p> | |
| 144 | */ | |
| 145 | public void doFinalInitialization() { | |
| 146 | // Heavy indexing must wait until after all columns composing | |
| 147 | // a table's primary key have been parsed. | |
| 148 | 0 | if (heavyIndexing) { |
| 149 | 0 | doHeavyIndexing(); |
| 150 | } | |
| 151 | ||
| 152 | // Name any indices which are missing a name using the | |
| 153 | // appropriate algorithm. | |
| 154 | 0 | doNaming(); |
| 155 | 0 | } |
| 156 | ||
| 157 | /** | |
| 158 | * <p> | |
| 159 | * Adds extra indices for multi-part primary key columns. | |
| 160 | * </p> | |
| 161 | * | |
| 162 | * <p> | |
| 163 | * For databases like MySQL, values in a where clause must match key part order from the left to right. So, in the | |
| 164 | * key definition <code>PRIMARY KEY (FOO_ID, BAR_ID)</code>, <code>FOO_ID</code> <i>must</i> be the first element | |
| 165 | * used in the <code>where</code> clause of the SQL query used against this table for the primary key index to be | |
| 166 | * used. This feature could cause problems under MySQL with heavily indexed tables, as MySQL currently only supports | |
| 167 | * 16 indices per table (i.e. it might cause too many indices to be created). | |
| 168 | * </p> | |
| 169 | * | |
| 170 | * <p> | |
| 171 | * See <a href="http://www.mysql.com/doc/E/X/EXPLAIN.html">the manual</a> for a better description of why heavy | |
| 172 | * indexing is useful for quickly searchable database tables. | |
| 173 | * </p> | |
| 174 | */ | |
| 175 | private void doHeavyIndexing() { | |
| 176 | 0 | if (log.isDebugEnabled()) { |
| 177 | 0 | log.debug("doHeavyIndex() called on table " + name); |
| 178 | } | |
| 179 | ||
| 180 | 0 | List pk = getPrimaryKey(); |
| 181 | 0 | int size = pk.size(); |
| 182 | ||
| 183 | try { | |
| 184 | // We start at an offset of 1 because the entire column | |
| 185 | // list is generally implicitly indexed by the fact that | |
| 186 | // it's a primary key. | |
| 187 | 0 | for (int i = 1; i < size; i++) { |
| 188 | 0 | addIndex(new Index(this, pk.subList(i, size))); |
| 189 | } | |
| 190 | 0 | } catch (EngineException e) { |
| 191 | 0 | log.error(e, e); |
| 192 | 0 | } |
| 193 | 0 | } |
| 194 | ||
| 195 | /** | |
| 196 | * Names composing objects which haven't yet been named. This currently consists of foreign-key and index entities. | |
| 197 | */ | |
| 198 | private void doNaming() { | |
| 199 | int i; | |
| 200 | int size; | |
| 201 | String name; | |
| 202 | ||
| 203 | // Assure names are unique across all databases. | |
| 204 | try { | |
| 205 | 0 | for (i = 0, size = foreignKeys.size(); i < size; i++) { |
| 206 | 0 | ForeignKey fk = (ForeignKey) foreignKeys.get(i); |
| 207 | 0 | name = fk.getName(); |
| 208 | 0 | if (StringUtils.isEmpty(name)) { |
| 209 | 0 | name = acquireConstraintName("FK", i + 1); |
| 210 | 0 | fk.setName(name); |
| 211 | } | |
| 212 | } | |
| 213 | ||
| 214 | 0 | for (i = 0, size = indices.size(); i < size; i++) { |
| 215 | 0 | Index index = (Index) indices.get(i); |
| 216 | 0 | name = index.getName(); |
| 217 | 0 | if (StringUtils.isEmpty(name)) { |
| 218 | 0 | name = acquireConstraintName("I", i + 1); |
| 219 | 0 | index.setName(name); |
| 220 | } | |
| 221 | } | |
| 222 | ||
| 223 | 0 | for (i = 0, size = unices.size(); i < size; i++) { |
| 224 | 0 | Unique unique = (Unique) unices.get(i); |
| 225 | 0 | name = unique.getName(); |
| 226 | 0 | if (StringUtils.isEmpty(name)) { |
| 227 | 0 | name = acquireConstraintName("U", i + 1); |
| 228 | 0 | unique.setName(name); |
| 229 | } | |
| 230 | } | |
| 231 | 0 | } catch (EngineException nameAlreadyInUse) { |
| 232 | 0 | log.error(nameAlreadyInUse, nameAlreadyInUse); |
| 233 | 0 | } |
| 234 | 0 | } |
| 235 | ||
| 236 | /** | |
| 237 | * Macro to a constraint name. | |
| 238 | * | |
| 239 | * @param nameType | |
| 240 | * constraint type | |
| 241 | * @param nbr | |
| 242 | * unique number for this constraint type | |
| 243 | * @return unique name for constraint | |
| 244 | * @throws EngineException | |
| 245 | */ | |
| 246 | private final String acquireConstraintName(String nameType, int nbr) throws EngineException { | |
| 247 | 0 | List inputs = new ArrayList(4); |
| 248 | 0 | inputs.add(getDatabase()); |
| 249 | 0 | inputs.add(getName()); |
| 250 | 0 | inputs.add(nameType); |
| 251 | 0 | inputs.add(new Integer(nbr)); |
| 252 | 0 | return NameFactory.generateName(NameFactory.CONSTRAINT_GENERATOR, inputs); |
| 253 | } | |
| 254 | ||
| 255 | /** | |
| 256 | * Gets the value of base class for classes produced from this table. | |
| 257 | * | |
| 258 | * @return The base class for classes produced from this table. | |
| 259 | */ | |
| 260 | public String getBaseClass() { | |
| 261 | 0 | if (isAlias() && baseClass == null) { |
| 262 | 0 | return alias; |
| 263 | 0 | } else if (baseClass == null) { |
| 264 | 0 | return getDatabase().getBaseClass(); |
| 265 | } else { | |
| 266 | 0 | return baseClass; |
| 267 | } | |
| 268 | } | |
| 269 | ||
| 270 | /** | |
| 271 | * Set the value of baseClass. | |
| 272 | * | |
| 273 | * @param v | |
| 274 | * Value to assign to baseClass. | |
| 275 | */ | |
| 276 | public void setBaseClass(String v) { | |
| 277 | 0 | this.baseClass = v; |
| 278 | 0 | } |
| 279 | ||
| 280 | /** | |
| 281 | * Get the value of basePeer. | |
| 282 | * | |
| 283 | * @return value of basePeer. | |
| 284 | */ | |
| 285 | public String getBasePeer() { | |
| 286 | 0 | if (isAlias() && basePeer == null) { |
| 287 | 0 | return alias + "Peer"; |
| 288 | 0 | } else if (basePeer == null) { |
| 289 | 0 | return getDatabase().getBasePeer(); |
| 290 | } else { | |
| 291 | 0 | return basePeer; |
| 292 | } | |
| 293 | } | |
| 294 | ||
| 295 | /** | |
| 296 | * Set the value of basePeer. | |
| 297 | * | |
| 298 | * @param v | |
| 299 | * Value to assign to basePeer. | |
| 300 | */ | |
| 301 | public void setBasePeer(String v) { | |
| 302 | 0 | this.basePeer = v; |
| 303 | 0 | } |
| 304 | ||
| 305 | /** | |
| 306 | * A utility function to create a new column from attrib and add it to this table. | |
| 307 | * | |
| 308 | * @param attrib | |
| 309 | * xml attributes for the column to add | |
| 310 | * @return the added column | |
| 311 | */ | |
| 312 | public Column addColumn(Attributes attrib) { | |
| 313 | 0 | Column col = new Column(); |
| 314 | 0 | col.setTable(this); |
| 315 | 0 | col.setCorrectGetters(false); |
| 316 | 0 | col.loadFromXML(attrib); |
| 317 | 0 | addColumn(col); |
| 318 | 0 | return col; |
| 319 | } | |
| 320 | ||
| 321 | /** | |
| 322 | * Adds a new column to the column list and set the parent table of the column to the current table | |
| 323 | * | |
| 324 | * @param col | |
| 325 | * the column to add | |
| 326 | */ | |
| 327 | public void addColumn(Column col) { | |
| 328 | 0 | col.setTable(this); |
| 329 | 0 | if (col.isInheritance()) { |
| 330 | 0 | inheritanceColumn = col; |
| 331 | } | |
| 332 | 0 | columnList.add(col); |
| 333 | 0 | columnsByName.put(col.getName(), col); |
| 334 | 0 | columnsByJavaName.put(col.getJavaName(), col); |
| 335 | 0 | col.setPosition(columnList.size()); |
| 336 | 0 | needsTransactionInPostgres |= col.requiresTransactionInPostgres(); |
| 337 | 0 | } |
| 338 | ||
| 339 | /** | |
| 340 | * A utility function to create a new foreign key from attrib and add it to this table. | |
| 341 | * | |
| 342 | * @param attrib | |
| 343 | * the xml attributes | |
| 344 | * @return the created ForeignKey | |
| 345 | */ | |
| 346 | public ForeignKey addForeignKey(Attributes attrib) { | |
| 347 | 0 | ForeignKey fk = new ForeignKey(); |
| 348 | 0 | fk.loadFromXML(attrib); |
| 349 | 0 | addForeignKey(fk); |
| 350 | 0 | return fk; |
| 351 | } | |
| 352 | ||
| 353 | /** | |
| 354 | * Gets the column that subclasses of the class representing this table can be produced from. | |
| 355 | */ | |
| 356 | public Column getChildrenColumn() { | |
| 357 | 0 | return inheritanceColumn; |
| 358 | } | |
| 359 | ||
| 360 | /** | |
| 361 | * Get the objects that can be created from this table. | |
| 362 | */ | |
| 363 | public List getChildrenNames() { | |
| 364 | 0 | if (inheritanceColumn == null || !inheritanceColumn.isEnumeratedClasses()) { |
| 365 | 0 | return null; |
| 366 | } | |
| 367 | 0 | List children = inheritanceColumn.getChildren(); |
| 368 | 0 | List names = new ArrayList(children.size()); |
| 369 | 0 | for (int i = 0; i < children.size(); i++) { |
| 370 | 0 | names.add(((Inheritance) children.get(i)).getClassName()); |
| 371 | } | |
| 372 | 0 | return names; |
| 373 | } | |
| 374 | ||
| 375 | /** | |
| 376 | * Adds the foreign key from another table that refers to this table. | |
| 377 | * | |
| 378 | * @param fk | |
| 379 | * A foreign key refering to this table | |
| 380 | */ | |
| 381 | public void addReferrer(ForeignKey fk) { | |
| 382 | 0 | if (referrers == null) { |
| 383 | 0 | referrers = new ArrayList(5); |
| 384 | } | |
| 385 | 0 | referrers.add(fk); |
| 386 | 0 | } |
| 387 | ||
| 388 | /** | |
| 389 | * Get list of references to this table. | |
| 390 | * | |
| 391 | * @return A list of references to this table | |
| 392 | */ | |
| 393 | public List getReferrers() { | |
| 394 | 0 | return referrers; |
| 395 | } | |
| 396 | ||
| 397 | /** | |
| 398 | * Set whether this table contains a foreign PK | |
| 399 | * | |
| 400 | * @param b | |
| 401 | */ | |
| 402 | public void setContainsForeignPK(boolean b) { | |
| 403 | 0 | containsForeignPK = b; |
| 404 | 0 | } |
| 405 | ||
| 406 | /** | |
| 407 | * Determine if this table contains a foreign PK | |
| 408 | */ | |
| 409 | public boolean getContainsForeignPK() { | |
| 410 | 0 | return containsForeignPK; |
| 411 | } | |
| 412 | ||
| 413 | /** | |
| 414 | * A list of tables referenced by foreign keys in this table | |
| 415 | * | |
| 416 | * @return A list of tables | |
| 417 | */ | |
| 418 | public List getForeignTableNames() { | |
| 419 | 0 | if (foreignTableNames == null) { |
| 420 | 0 | foreignTableNames = new ArrayList(1); |
| 421 | } | |
| 422 | 0 | return foreignTableNames; |
| 423 | } | |
| 424 | ||
| 425 | /** | |
| 426 | * Adds a new FK to the FK list and set the parent table of the column to the current table | |
| 427 | * | |
| 428 | * @param fk | |
| 429 | * A foreign key | |
| 430 | */ | |
| 431 | public void addForeignKey(ForeignKey fk) { | |
| 432 | 0 | fk.setTable(this); |
| 433 | 0 | foreignKeys.add(fk); |
| 434 | ||
| 435 | 0 | if (foreignTableNames == null) { |
| 436 | 0 | foreignTableNames = new ArrayList(5); |
| 437 | } | |
| 438 | 0 | if (!foreignTableNames.contains(fk.getForeignTableName())) { |
| 439 | 0 | foreignTableNames.add(fk.getForeignTableName()); |
| 440 | } | |
| 441 | 0 | } |
| 442 | ||
| 443 | /** | |
| 444 | * Return true if the column requires a transaction in Postgres | |
| 445 | */ | |
| 446 | public boolean requiresTransactionInPostgres() { | |
| 447 | 0 | return needsTransactionInPostgres; |
| 448 | } | |
| 449 | ||
| 450 | /** | |
| 451 | * A utility function to create a new id method parameter from attrib and add it to this table. | |
| 452 | */ | |
| 453 | public IdMethodParameter addIdMethodParameter(Attributes attrib) { | |
| 454 | 0 | IdMethodParameter imp = new IdMethodParameter(); |
| 455 | 0 | imp.loadFromXML(attrib); |
| 456 | 0 | addIdMethodParameter(imp); |
| 457 | 0 | return imp; |
| 458 | } | |
| 459 | ||
| 460 | /** | |
| 461 | * Adds a new ID method parameter to the list and sets the parent table of the column associated with the supplied | |
| 462 | * parameter to this table. | |
| 463 | * | |
| 464 | * @param imp | |
| 465 | * The column to add as an ID method parameter. | |
| 466 | */ | |
| 467 | public void addIdMethodParameter(IdMethodParameter imp) { | |
| 468 | 0 | imp.setTable(this); |
| 469 | 0 | if (idMethodParameters == null) { |
| 470 | 0 | idMethodParameters = new ArrayList(2); |
| 471 | } | |
| 472 | 0 | idMethodParameters.add(imp); |
| 473 | 0 | } |
| 474 | ||
| 475 | /** | |
| 476 | * Adds a new index to the index list and set the parent table of the column to the current table | |
| 477 | */ | |
| 478 | public void addIndex(Index index) { | |
| 479 | 0 | index.setTable(this); |
| 480 | 0 | indices.add(index); |
| 481 | 0 | } |
| 482 | ||
| 483 | /** | |
| 484 | * A utility function to create a new index from attrib and add it to this table. | |
| 485 | */ | |
| 486 | public Index addIndex(Attributes attrib) { | |
| 487 | 0 | Index index = new Index(); |
| 488 | 0 | index.loadFromXML(attrib); |
| 489 | 0 | addIndex(index); |
| 490 | 0 | return index; |
| 491 | } | |
| 492 | ||
| 493 | /** | |
| 494 | * Adds a new Unique to the Unique list and set the parent table of the column to the current table | |
| 495 | */ | |
| 496 | public void addUnique(Unique unique) { | |
| 497 | 0 | unique.setTable(this); |
| 498 | 0 | unices.add(unique); |
| 499 | 0 | } |
| 500 | ||
| 501 | /** | |
| 502 | * A utility function to create a new Unique from attrib and add it to this table. | |
| 503 | * | |
| 504 | * @param attrib | |
| 505 | * the xml attributes | |
| 506 | */ | |
| 507 | public Unique addUnique(Attributes attrib) { | |
| 508 | 0 | Unique unique = new Unique(); |
| 509 | 0 | unique.loadFromXML(attrib); |
| 510 | 0 | addUnique(unique); |
| 511 | 0 | return unique; |
| 512 | } | |
| 513 | ||
| 514 | /** | |
| 515 | * Get the name of the Table | |
| 516 | */ | |
| 517 | public String getName() { | |
| 518 | 0 | return name; |
| 519 | } | |
| 520 | ||
| 521 | /** | |
| 522 | * Set the name of the Table | |
| 523 | */ | |
| 524 | public void setName(String newName) { | |
| 525 | 0 | name = newName; |
| 526 | 0 | } |
| 527 | ||
| 528 | /** | |
| 529 | * Get the description for the Table | |
| 530 | */ | |
| 531 | public String getDescription() { | |
| 532 | 0 | return description; |
| 533 | } | |
| 534 | ||
| 535 | /** | |
| 536 | * Set the description for the Table | |
| 537 | * | |
| 538 | * @param newDescription | |
| 539 | * description for the Table | |
| 540 | */ | |
| 541 | public void setDescription(String newDescription) { | |
| 542 | 0 | description = newDescription; |
| 543 | 0 | } |
| 544 | ||
| 545 | /** | |
| 546 | * Get name to use in Java sources | |
| 547 | */ | |
| 548 | public String getJavaName() { | |
| 549 | 0 | if (javaName == null) { |
| 550 | 0 | List inputs = new ArrayList(2); |
| 551 | 0 | inputs.add(name); |
| 552 | 0 | inputs.add(javaNamingMethod); |
| 553 | try { | |
| 554 | 0 | javaName = NameFactory.generateName(NameFactory.JAVA_GENERATOR, inputs); |
| 555 | 0 | } catch (EngineException e) { |
| 556 | 0 | log.error(e, e); |
| 557 | 0 | } |
| 558 | } | |
| 559 | 0 | return javaName; |
| 560 | } | |
| 561 | ||
| 562 | /** | |
| 563 | * Set name to use in Java sources | |
| 564 | */ | |
| 565 | public void setJavaName(String javaName) { | |
| 566 | 0 | this.javaName = javaName; |
| 567 | 0 | } |
| 568 | ||
| 569 | /** | |
| 570 | * Get the method for generating pk's | |
| 571 | */ | |
| 572 | public String getIdMethod() { | |
| 573 | 0 | if (idMethod == null) { |
| 574 | 0 | return IDMethod.NO_ID_METHOD; |
| 575 | } else { | |
| 576 | 0 | return idMethod; |
| 577 | } | |
| 578 | } | |
| 579 | ||
| 580 | /** | |
| 581 | * Set the method for generating pk's | |
| 582 | */ | |
| 583 | public void setIdMethod(String idMethod) { | |
| 584 | 0 | this.idMethod = idMethod; |
| 585 | 0 | } |
| 586 | ||
| 587 | /** | |
| 588 | * Skip generating sql for this table (in the event it should not be created from scratch). | |
| 589 | * | |
| 590 | * @return value of skipSql. | |
| 591 | */ | |
| 592 | public boolean isSkipSql() { | |
| 593 | 0 | return (skipSql || isAlias() || isForReferenceOnly()); |
| 594 | } | |
| 595 | ||
| 596 | /** | |
| 597 | * Set whether this table should have its creation sql generated. | |
| 598 | * | |
| 599 | * @param v | |
| 600 | * Value to assign to skipSql. | |
| 601 | */ | |
| 602 | public void setSkipSql(boolean v) { | |
| 603 | 0 | this.skipSql = v; |
| 604 | 0 | } |
| 605 | ||
| 606 | /** | |
| 607 | * JavaName of om object this entry references. | |
| 608 | * | |
| 609 | * @return value of external. | |
| 610 | */ | |
| 611 | public String getAlias() { | |
| 612 | 0 | return alias; |
| 613 | } | |
| 614 | ||
| 615 | /** | |
| 616 | * Is this table specified in the schema or is there just a foreign key reference to it. | |
| 617 | * | |
| 618 | * @return value of external. | |
| 619 | */ | |
| 620 | public boolean isAlias() { | |
| 621 | 0 | return (alias != null); |
| 622 | } | |
| 623 | ||
| 624 | /** | |
| 625 | * Set whether this table specified in the schema or is there just a foreign key reference to it. | |
| 626 | * | |
| 627 | * @param v | |
| 628 | * Value to assign to alias. | |
| 629 | */ | |
| 630 | public void setAlias(String v) { | |
| 631 | 0 | this.alias = v; |
| 632 | 0 | } |
| 633 | ||
| 634 | /** | |
| 635 | * Interface which objects for this table will implement | |
| 636 | * | |
| 637 | * @return value of interface. | |
| 638 | */ | |
| 639 | public String getInterface() { | |
| 640 | 0 | return enterface; |
| 641 | } | |
| 642 | ||
| 643 | /** | |
| 644 | * Interface which objects for this table will implement | |
| 645 | * | |
| 646 | * @param v | |
| 647 | * Value to assign to interface. | |
| 648 | */ | |
| 649 | public void setInterface(String v) { | |
| 650 | 0 | this.enterface = v; |
| 651 | 0 | } |
| 652 | ||
| 653 | /** | |
| 654 | * When a table is abstract, it marks the business object class that is generated as being abstract. If you have a | |
| 655 | * table called "FOO", then the Foo BO will be <code>public abstract class Foo</code> This helps support class | |
| 656 | * hierarchies | |
| 657 | * | |
| 658 | * @return value of abstractValue. | |
| 659 | */ | |
| 660 | public boolean isAbstract() { | |
| 661 | 0 | return abstractValue; |
| 662 | } | |
| 663 | ||
| 664 | /** | |
| 665 | * When a table is abstract, it marks the business object class that is generated as being abstract. If you have a | |
| 666 | * table called "FOO", then the Foo BO will be <code>public abstract class Foo</code> This helps support class | |
| 667 | * hierarchies | |
| 668 | * | |
| 669 | * @param v | |
| 670 | * Value to assign to abstractValue. | |
| 671 | */ | |
| 672 | public void setAbstract(boolean v) { | |
| 673 | 0 | this.abstractValue = v; |
| 674 | 0 | } |
| 675 | ||
| 676 | /** | |
| 677 | * Get the value of package. | |
| 678 | * | |
| 679 | * @return value of package. | |
| 680 | */ | |
| 681 | public String getPackage() { | |
| 682 | 0 | if (pkg != null) { |
| 683 | 0 | return pkg; |
| 684 | } else { | |
| 685 | 0 | return this.getDatabase().getPackage(); |
| 686 | } | |
| 687 | } | |
| 688 | ||
| 689 | /** | |
| 690 | * Set the value of package. | |
| 691 | * | |
| 692 | * @param v | |
| 693 | * Value to assign to package. | |
| 694 | */ | |
| 695 | public void setPackage(String v) { | |
| 696 | 0 | this.pkg = v; |
| 697 | 0 | } |
| 698 | ||
| 699 | /** | |
| 700 | * Returns a List containing all the columns in the table | |
| 701 | * | |
| 702 | * @return a List containing all the columns | |
| 703 | */ | |
| 704 | public List getColumns() { | |
| 705 | 0 | return columnList; |
| 706 | } | |
| 707 | ||
| 708 | /** | |
| 709 | * Utility method to get the number of columns in this table | |
| 710 | */ | |
| 711 | public int getNumColumns() { | |
| 712 | 0 | return columnList.size(); |
| 713 | } | |
| 714 | ||
| 715 | /** | |
| 716 | * Returns a List containing all the FKs in the table | |
| 717 | * | |
| 718 | * @return a List containing all the FKs | |
| 719 | */ | |
| 720 | public List getForeignKeys() { | |
| 721 | 0 | return foreignKeys; |
| 722 | } | |
| 723 | ||
| 724 | /** | |
| 725 | * Returns a Collection of parameters relevant for the chosen id generation method. | |
| 726 | */ | |
| 727 | public List getIdMethodParameters() { | |
| 728 | 0 | return idMethodParameters; |
| 729 | } | |
| 730 | ||
| 731 | /** | |
| 732 | * A name to use for creating a sequence if one is not specified. | |
| 733 | * | |
| 734 | * @return name of the sequence | |
| 735 | */ | |
| 736 | public String getSequenceName() { | |
| 737 | 0 | String result = null; |
| 738 | 0 | if (getIdMethod().equals(NATIVE)) { |
| 739 | 0 | List idMethodParams = getIdMethodParameters(); |
| 740 | 0 | if (idMethodParams == null) { |
| 741 | 0 | result = getName() + "_SEQ"; |
| 742 | } else { | |
| 743 | 0 | result = ((IdMethodParameter) idMethodParams.get(0)).getValue(); |
| 744 | } | |
| 745 | } | |
| 746 | 0 | return result; |
| 747 | } | |
| 748 | ||
| 749 | /** | |
| 750 | * Returns a List containing all the indices in the table | |
| 751 | * | |
| 752 | * @return A List containing all the indices | |
| 753 | */ | |
| 754 | public List getIndices() { | |
| 755 | 0 | return indices; |
| 756 | } | |
| 757 | ||
| 758 | /** | |
| 759 | * Returns a List containing all the UKs in the table | |
| 760 | * | |
| 761 | * @return A List containing all the UKs | |
| 762 | */ | |
| 763 | public List getUnices() { | |
| 764 | 0 | return unices; |
| 765 | } | |
| 766 | ||
| 767 | /** | |
| 768 | * Returns a specified column. | |
| 769 | * | |
| 770 | * @param name | |
| 771 | * name of the column | |
| 772 | * @return Return a Column object or null if it does not exist. | |
| 773 | */ | |
| 774 | public Column getColumn(String name) { | |
| 775 | 0 | return (Column) columnsByName.get(name); |
| 776 | } | |
| 777 | ||
| 778 | /** | |
| 779 | * Returns a specified column. | |
| 780 | * | |
| 781 | * @param javaName | |
| 782 | * java name of the column | |
| 783 | * @return Return a Column object or null if it does not exist. | |
| 784 | */ | |
| 785 | public Column getColumnByJavaName(String javaName) { | |
| 786 | 0 | return (Column) columnsByJavaName.get(javaName); |
| 787 | } | |
| 788 | ||
| 789 | /** | |
| 790 | * Return the first foreign key that includes col in it's list of local columns. Eg. Foreign key (a,b,c) refrences | |
| 791 | * tbl(x,y,z) will be returned of col is either a,b or c. | |
| 792 | * | |
| 793 | * @param col | |
| 794 | * column name included in the key | |
| 795 | * @return Return a Column object or null if it does not exist. | |
| 796 | */ | |
| 797 | public ForeignKey getForeignKey(String col) { | |
| 798 | 0 | ForeignKey firstFK = null; |
| 799 | 0 | for (Iterator iter = foreignKeys.iterator(); iter.hasNext();) { |
| 800 | 0 | ForeignKey key = (ForeignKey) iter.next(); |
| 801 | 0 | if (key.getLocalColumns().contains(col)) { |
| 802 | 0 | if (firstFK == null) { |
| 803 | 0 | firstFK = key; |
| 804 | } else { | |
| 805 | // System.out.println(col+" is in multiple FKs. This is not" | |
| 806 | // + " being handled properly."); | |
| 807 | // throw new IllegalStateException("Cannot call method if " + | |
| 808 | // "column is referenced multiple times"); | |
| 809 | } | |
| 810 | } | |
| 811 | 0 | } |
| 812 | 0 | return firstFK; |
| 813 | } | |
| 814 | ||
| 815 | /** | |
| 816 | * Returns true if the table contains a specified column | |
| 817 | * | |
| 818 | * @param col | |
| 819 | * the column | |
| 820 | * @return true if the table contains the column | |
| 821 | */ | |
| 822 | public boolean containsColumn(Column col) { | |
| 823 | 0 | return columnList.contains(col); |
| 824 | } | |
| 825 | ||
| 826 | /** | |
| 827 | * Returns true if the table contains a specified column | |
| 828 | * | |
| 829 | * @param name | |
| 830 | * name of the column | |
| 831 | * @return true if the table contains the column | |
| 832 | */ | |
| 833 | public boolean containsColumn(String name) { | |
| 834 | 0 | return (getColumn(name) != null); |
| 835 | } | |
| 836 | ||
| 837 | /** | |
| 838 | * Set the parent of the table | |
| 839 | * | |
| 840 | * @param parent | |
| 841 | * the parant database | |
| 842 | */ | |
| 843 | public void setDatabase(Database parent) { | |
| 844 | 0 | tableParent = parent; |
| 845 | 0 | } |
| 846 | ||
| 847 | /** | |
| 848 | * Get the parent of the table | |
| 849 | * | |
| 850 | * @return the parant database | |
| 851 | */ | |
| 852 | public Database getDatabase() { | |
| 853 | 0 | return tableParent; |
| 854 | } | |
| 855 | ||
| 856 | /** | |
| 857 | * Flag to determine if code/sql gets created for this table. Table will be skipped, if return true. | |
| 858 | * | |
| 859 | * @return value of forReferenceOnly. | |
| 860 | */ | |
| 861 | public boolean isForReferenceOnly() { | |
| 862 | 0 | return forReferenceOnly; |
| 863 | } | |
| 864 | ||
| 865 | /** | |
| 866 | * Flag to determine if code/sql gets created for this table. Table will be skipped, if set to true. | |
| 867 | * | |
| 868 | * @param v | |
| 869 | * Value to assign to forReferenceOnly. | |
| 870 | */ | |
| 871 | public void setForReferenceOnly(boolean v) { | |
| 872 | 0 | this.forReferenceOnly = v; |
| 873 | 0 | } |
| 874 | ||
| 875 | /** | |
| 876 | * Returns a XML representation of this table. | |
| 877 | * | |
| 878 | * @return XML representation of this table | |
| 879 | */ | |
| 880 | @Override | |
| 881 | public String toString() { | |
| 882 | 0 | StringBuffer result = new StringBuffer(); |
| 883 | ||
| 884 | 0 | result.append("<table name=\"").append(name).append('\"'); |
| 885 | ||
| 886 | 0 | if (javaName != null) { |
| 887 | 0 | result.append(" javaName=\"").append(javaName).append('\"'); |
| 888 | } | |
| 889 | ||
| 890 | 0 | if (idMethod != null) { |
| 891 | 0 | result.append(" idMethod=\"").append(idMethod).append('\"'); |
| 892 | } | |
| 893 | ||
| 894 | 0 | if (skipSql) { |
| 895 | 0 | result.append(" skipSql=\"").append(new Boolean(skipSql)).append('\"'); |
| 896 | } | |
| 897 | ||
| 898 | 0 | if (abstractValue) { |
| 899 | 0 | result.append(" abstract=\"").append(new Boolean(abstractValue)).append('\"'); |
| 900 | } | |
| 901 | ||
| 902 | 0 | if (baseClass != null) { |
| 903 | 0 | result.append(" baseClass=\"").append(baseClass).append('\"'); |
| 904 | } | |
| 905 | ||
| 906 | 0 | if (basePeer != null) { |
| 907 | 0 | result.append(" basePeer=\"").append(basePeer).append('\"'); |
| 908 | } | |
| 909 | ||
| 910 | 0 | result.append(">\n"); |
| 911 | ||
| 912 | 0 | if (columnList != null) { |
| 913 | 0 | for (Iterator iter = columnList.iterator(); iter.hasNext();) { |
| 914 | 0 | result.append(iter.next()); |
| 915 | } | |
| 916 | } | |
| 917 | ||
| 918 | 0 | if (foreignKeys != null) { |
| 919 | 0 | for (Iterator iter = foreignKeys.iterator(); iter.hasNext();) { |
| 920 | 0 | result.append(iter.next()); |
| 921 | } | |
| 922 | } | |
| 923 | ||
| 924 | 0 | if (idMethodParameters != null) { |
| 925 | 0 | Iterator iter = idMethodParameters.iterator(); |
| 926 | 0 | while (iter.hasNext()) { |
| 927 | 0 | result.append(iter.next()); |
| 928 | } | |
| 929 | } | |
| 930 | ||
| 931 | 0 | result.append("</table>\n"); |
| 932 | ||
| 933 | 0 | return result.toString(); |
| 934 | } | |
| 935 | ||
| 936 | /** | |
| 937 | * Returns the collection of Columns which make up the single primary key for this table. | |
| 938 | * | |
| 939 | * @return A list of the primary key parts. | |
| 940 | */ | |
| 941 | public List getPrimaryKey() { | |
| 942 | 0 | List pk = new ArrayList(columnList.size()); |
| 943 | ||
| 944 | 0 | Iterator iter = columnList.iterator(); |
| 945 | 0 | while (iter.hasNext()) { |
| 946 | 0 | Column col = (Column) iter.next(); |
| 947 | 0 | if (col.isPrimaryKey()) { |
| 948 | 0 | pk.add(col); |
| 949 | } | |
| 950 | 0 | } |
| 951 | 0 | return pk; |
| 952 | } | |
| 953 | ||
| 954 | /** | |
| 955 | * Determine whether this table has a primary key. | |
| 956 | * | |
| 957 | * @return Whether this table has any primary key parts. | |
| 958 | */ | |
| 959 | public boolean hasPrimaryKey() { | |
| 960 | 0 | return (getPrimaryKey().size() > 0); |
| 961 | } | |
| 962 | ||
| 963 | /** | |
| 964 | * Returns all parts of the primary key, separated by commas. | |
| 965 | * | |
| 966 | * @return A CSV list of primary key parts. | |
| 967 | */ | |
| 968 | public String printPrimaryKey() { | |
| 969 | 0 | return printList(columnList); |
| 970 | } | |
| 971 | ||
| 972 | /** | |
| 973 | * Returns the elements of the list, separated by commas. | |
| 974 | * | |
| 975 | * @param list | |
| 976 | * a list of Columns | |
| 977 | * @return A CSV list. | |
| 978 | */ | |
| 979 | private String printList(List list) { | |
| 980 | 0 | StringBuffer result = new StringBuffer(); |
| 981 | 0 | boolean comma = false; |
| 982 | 0 | for (Iterator iter = list.iterator(); iter.hasNext();) { |
| 983 | 0 | Column col = (Column) iter.next(); |
| 984 | 0 | if (col.isPrimaryKey()) { |
| 985 | 0 | if (comma) { |
| 986 | 0 | result.append(','); |
| 987 | } else { | |
| 988 | 0 | comma = true; |
| 989 | } | |
| 990 | 0 | result.append(col.getName()); |
| 991 | } | |
| 992 | 0 | } |
| 993 | 0 | return result.toString(); |
| 994 | } | |
| 995 | ||
| 996 | /** | |
| 997 | * Force all columns to set the correctGetters property. | |
| 998 | * | |
| 999 | * @param value | |
| 1000 | * The new value of the correctGetters property. | |
| 1001 | * @since 3.2 | |
| 1002 | */ | |
| 1003 | public void setCorrectGetters(Boolean value) { | |
| 1004 | 0 | boolean correctGetters = value != null && value.booleanValue(); |
| 1005 | 0 | for (Iterator it = columnList.iterator(); it.hasNext();) { |
| 1006 | 0 | Column col = (Column) it.next(); |
| 1007 | 0 | col.setCorrectGetters(correctGetters); |
| 1008 | 0 | } |
| 1009 | 0 | } |
| 1010 | ||
| 1011 | /** | |
| 1012 | * Add an XML Specified option key/value pair to this element's option set. | |
| 1013 | * | |
| 1014 | * @param key | |
| 1015 | * the key of the option. | |
| 1016 | * @param value | |
| 1017 | * the value of the option. | |
| 1018 | */ | |
| 1019 | public void addOption(String key, String value) { | |
| 1020 | 0 | options.put(key, value); |
| 1021 | 0 | } |
| 1022 | ||
| 1023 | /** | |
| 1024 | * Get the value that was associated with this key in an XML option element. | |
| 1025 | * | |
| 1026 | * @param key | |
| 1027 | * the key of the option. | |
| 1028 | * @return The value for the key or a null. | |
| 1029 | */ | |
| 1030 | public String getOption(String key) { | |
| 1031 | 0 | return (String) options.get(key); |
| 1032 | } | |
| 1033 | ||
| 1034 | /** | |
| 1035 | * Gets the full ordered hashtable array of items specified by XML option statements under this element. | |
| 1036 | * <p> | |
| 1037 | * | |
| 1038 | * Note, this is not thread save but since it's only used for generation which is single threaded, there should be | |
| 1039 | * minimum danger using this in Velocity. | |
| 1040 | * | |
| 1041 | * @return An Map of all options. Will not be null but may be empty. | |
| 1042 | */ | |
| 1043 | public Map getOptions() { | |
| 1044 | 0 | return options; |
| 1045 | } | |
| 1046 | ||
| 1047 | @Override | |
| 1048 | public void empty() { | |
| 1049 | ||
| 1050 | 0 | } |
| 1051 | } |