0001: /*
0002: * $Id: ModelEntity.java,v 1.12 2004/02/06 22:14:09 jonesde Exp $
0003: *
0004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
0005: *
0006: * Permission is hereby granted, free of charge, to any person obtaining a
0007: * copy of this software and associated documentation files (the "Software"),
0008: * to deal in the Software without restriction, including without limitation
0009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0010: * and/or sell copies of the Software, and to permit persons to whom the
0011: * Software is furnished to do so, subject to the following conditions:
0012: *
0013: * The above copyright notice and this permission notice shall be included
0014: * in all copies or substantial portions of the Software.
0015: *
0016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
0017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
0019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
0020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
0021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
0022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0023: */
0024: package org.ofbiz.entity.model;
0025:
0026: import java.util.ArrayList;
0027: import java.util.Collection;
0028: import java.util.HashMap;
0029: import java.util.Hashtable;
0030: import java.util.Iterator;
0031: import java.util.List;
0032: import java.util.Map;
0033:
0034: import org.ofbiz.base.util.Debug;
0035: import org.ofbiz.base.util.UtilTimer;
0036: import org.ofbiz.base.util.UtilXml;
0037: import org.ofbiz.entity.config.EntityConfigUtil;
0038: import org.ofbiz.entity.jdbc.DatabaseUtil;
0039: import org.w3c.dom.Element;
0040: import org.w3c.dom.NodeList;
0041:
0042: /**
0043: * Generic Entity - Entity model class
0044: *
0045: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
0046: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
0047: * @version $Revision: 1.12 $
0048: * @since 2.0
0049: */
0050: public class ModelEntity implements Comparable {
0051:
0052: public static final String module = ModelEntity.class.getName();
0053:
0054: /** The name of the time stamp field for locking/syncronization */
0055: public static final String STAMP_FIELD = "lastUpdatedStamp";
0056: public static final String STAMP_TX_FIELD = "lastUpdatedTxStamp";
0057: public static final String CREATE_STAMP_FIELD = "createdStamp";
0058: public static final String CREATE_STAMP_TX_FIELD = "createdTxStamp";
0059:
0060: /** The ModelReader that created this Entity */
0061: protected ModelReader modelReader = null;
0062:
0063: /** The entity-name of the Entity */
0064: protected String entityName = "";
0065:
0066: /** The table-name of the Entity */
0067: protected String tableName = "";
0068:
0069: /** The package-name of the Entity */
0070: protected String packageName = "";
0071:
0072: /** The default-resource-name of the Entity, used with the getResource call to check for a value in a resource bundle */
0073: protected String defaultResourceName = "";
0074:
0075: /** The entity-name of the Entity that this Entity is dependent on, if empty then no dependency */
0076: protected String dependentOn = "";
0077:
0078: // Strings to go in the comment header.
0079: /** The title for documentation purposes */
0080: protected String title = "";
0081:
0082: /** The description for documentation purposes */
0083: protected String description = "";
0084:
0085: /** The copyright for documentation purposes */
0086: protected String copyright = "";
0087:
0088: /** The author for documentation purposes */
0089: protected String author = "";
0090:
0091: /** The version for documentation purposes */
0092: protected String version = "";
0093:
0094: /** A List of the Field objects for the Entity */
0095: protected List fields = new ArrayList();
0096: protected Map fieldsMap = null;
0097:
0098: /** A List of the Field objects for the Entity, one for each Primary Key */
0099: protected List pks = new ArrayList();
0100:
0101: /** A List of the Field objects for the Entity, one for each NON Primary Key */
0102: protected List nopks = new ArrayList();
0103:
0104: /** relations defining relationships between this entity and other entities */
0105: protected List relations = new ArrayList();
0106:
0107: /** indexes on fields/columns in this entity */
0108: protected List indexes = new ArrayList();
0109:
0110: /** An indicator to specify if this entity requires locking for updates */
0111: protected boolean doLock = false;
0112:
0113: /** Can be used to disable automatically creating update stamp fields and populating them on inserts and updates */
0114: protected boolean noAutoStamp = false;
0115:
0116: /** An indicator to specify if this entity is never cached.
0117: * If true causes the delegator to not clear caches on write and to not get
0118: * from cache on read showing a warning messages to that effect
0119: */
0120: protected boolean neverCache = false;
0121:
0122: // ===== CONSTRUCTORS =====
0123: /** Default Constructor */
0124: public ModelEntity() {
0125: }
0126:
0127: /** XML Constructor */
0128: public ModelEntity(ModelReader reader, Element entityElement,
0129: Element docElement, UtilTimer utilTimer,
0130: Hashtable docElementValues) {
0131: this .modelReader = reader;
0132:
0133: if (utilTimer != null)
0134: utilTimer
0135: .timerString(" createModelEntity: before general/basic info");
0136: this .populateBasicInfo(entityElement, docElement,
0137: docElementValues);
0138:
0139: if (utilTimer != null)
0140: utilTimer.timerString(" createModelEntity: before fields");
0141: NodeList fieldList = entityElement
0142: .getElementsByTagName("field");
0143: for (int i = 0; i < fieldList.getLength(); i++) {
0144: ModelField field = reader.createModelField(
0145: (Element) fieldList.item(i), docElement,
0146: docElementValues);
0147: if (field != null)
0148: this .fields.add(field);
0149: }
0150:
0151: // if applicable automatically add the STAMP_FIELD and STAMP_TX_FIELD fields
0152: if ((this .doLock || !this .noAutoStamp)
0153: && !this .isField(STAMP_FIELD)) {
0154: ModelField newField = reader.createModelField(STAMP_FIELD,
0155: "date-time", null, false);
0156: newField.setIsAutoCreatedInternal(true);
0157: this .fields.add(newField);
0158: }
0159: if (!this .noAutoStamp && !this .isField(STAMP_TX_FIELD)) {
0160: ModelField newField = reader.createModelField(
0161: STAMP_TX_FIELD, "date-time", null, false);
0162: newField.setIsAutoCreatedInternal(true);
0163: this .fields.add(newField);
0164:
0165: // also add an index for this field
0166: String indexName = ModelUtil.shortenDbName(this .tableName
0167: + "_TXSTMP", 18);
0168: ModelIndex txIndex = new ModelIndex(this , indexName, false);
0169: txIndex.addIndexField(ModelEntity.STAMP_TX_FIELD);
0170: indexes.add(txIndex);
0171: }
0172:
0173: // if applicable automatically add the CREATE_STAMP_FIELD and CREATE_STAMP_TX_FIELD fields
0174: if ((this .doLock || !this .noAutoStamp)
0175: && !this .isField(CREATE_STAMP_FIELD)) {
0176: ModelField newField = reader.createModelField(
0177: CREATE_STAMP_FIELD, "date-time", null, false);
0178: newField.setIsAutoCreatedInternal(true);
0179: this .fields.add(newField);
0180: }
0181: if (!this .noAutoStamp && !this .isField(CREATE_STAMP_TX_FIELD)) {
0182: ModelField newField = reader.createModelField(
0183: CREATE_STAMP_TX_FIELD, "date-time", null, false);
0184: newField.setIsAutoCreatedInternal(true);
0185: this .fields.add(newField);
0186:
0187: // also add an index for this field
0188: String indexName = ModelUtil.shortenDbName(this .tableName
0189: + "_TXCRTS", 18);
0190: ModelIndex txIndex = new ModelIndex(this , indexName, false);
0191: txIndex.addIndexField(ModelEntity.CREATE_STAMP_TX_FIELD);
0192: indexes.add(txIndex);
0193: }
0194:
0195: if (utilTimer != null)
0196: utilTimer
0197: .timerString(" createModelEntity: before prim-keys");
0198: NodeList pkList = entityElement
0199: .getElementsByTagName("prim-key");
0200: for (int i = 0; i < pkList.getLength(); i++) {
0201: ModelField field = reader.findModelField(this ,
0202: ((Element) pkList.item(i)).getAttribute("field"));
0203: if (field != null) {
0204: this .pks.add(field);
0205: field.isPk = true;
0206: } else {
0207: Debug.logError(
0208: "[ModelReader.createModelEntity] ERROR: Could not find field \""
0209: + ((Element) pkList.item(i))
0210: .getAttribute("field")
0211: + "\" specified in a prim-key", module);
0212: }
0213: }
0214:
0215: // now that we have the pks and the fields, make the nopks vector
0216: this .nopks = new ArrayList();
0217: for (int ind = 0; ind < this .fields.size(); ind++) {
0218: ModelField field = (ModelField) this .fields.get(ind);
0219: if (!field.isPk)
0220: this .nopks.add(field);
0221: }
0222:
0223: if (utilTimer != null)
0224: utilTimer
0225: .timerString(" createModelEntity: before relations");
0226: this .populateRelated(reader, entityElement);
0227: this .populateIndexes(entityElement);
0228: }
0229:
0230: /** DB Names Constructor */
0231: public ModelEntity(String tableName, List colList,
0232: ModelFieldTypeReader modelFieldTypeReader,
0233: boolean isCaseSensitive) {
0234: // if there is a dot in the name, remove it and everything before it, should be the schema name
0235: this .tableName = tableName;
0236: int dotIndex = this .tableName.indexOf(".");
0237: if (dotIndex >= 0) {
0238: this .tableName = this .tableName.substring(dotIndex + 1);
0239: }
0240: this .entityName = ModelUtil.dbNameToClassName(this .tableName);
0241: Iterator columns = colList.iterator();
0242: while (columns.hasNext()) {
0243: DatabaseUtil.ColumnCheckInfo ccInfo = (DatabaseUtil.ColumnCheckInfo) columns
0244: .next();
0245: ModelField newField = new ModelField(ccInfo,
0246: modelFieldTypeReader);
0247:
0248: this .fields.add(newField);
0249: }
0250: this .updatePkLists();
0251: }
0252:
0253: protected void populateBasicInfo(Element entityElement,
0254: Element docElement, Hashtable docElementValues) {
0255: this .entityName = UtilXml.checkEmpty(entityElement
0256: .getAttribute("entity-name"));
0257: this .tableName = UtilXml.checkEmpty(entityElement
0258: .getAttribute("table-name"), ModelUtil
0259: .javaNameToDbName(this .entityName));
0260: this .packageName = UtilXml.checkEmpty(entityElement
0261: .getAttribute("package-name"));
0262: this .defaultResourceName = UtilXml.checkEmpty(entityElement
0263: .getAttribute("default-resource-name"));
0264: this .dependentOn = UtilXml.checkEmpty(entityElement
0265: .getAttribute("dependent-on"));
0266: this .doLock = UtilXml.checkBoolean(entityElement
0267: .getAttribute("enable-lock"), false);
0268: this .noAutoStamp = UtilXml.checkBoolean(entityElement
0269: .getAttribute("no-auto-stamp"), false);
0270: this .neverCache = UtilXml.checkBoolean(entityElement
0271: .getAttribute("never-cache"), false);
0272:
0273: if (docElementValues == null) {
0274: this .title = UtilXml.checkEmpty(entityElement
0275: .getAttribute("title"), UtilXml.childElementValue(
0276: docElement, "title"), "None");
0277: this .description = UtilXml.checkEmpty(UtilXml
0278: .childElementValue(entityElement, "description"),
0279: UtilXml
0280: .childElementValue(docElement,
0281: "description"), "None");
0282: this .copyright = UtilXml
0283: .checkEmpty(
0284: entityElement.getAttribute("copyright"),
0285: UtilXml.childElementValue(docElement,
0286: "copyright"),
0287: "Copyright (c) 2001 The Open For Business Project - www.ofbiz.org");
0288: this .author = UtilXml.checkEmpty(entityElement
0289: .getAttribute("author"), UtilXml.childElementValue(
0290: docElement, "author"), "None");
0291: this .version = UtilXml.checkEmpty(entityElement
0292: .getAttribute("version"), UtilXml
0293: .childElementValue(docElement, "version"), "1.0");
0294: } else {
0295: if (!docElementValues.containsKey("title"))
0296: docElementValues.put("title", UtilXml
0297: .childElementValue(docElement, "title"));
0298: if (!docElementValues.containsKey("description"))
0299: docElementValues.put("description", UtilXml
0300: .childElementValue(docElement, "description"));
0301: if (!docElementValues.containsKey("copyright"))
0302: docElementValues.put("copyright", UtilXml
0303: .childElementValue(docElement, "copyright"));
0304: if (!docElementValues.containsKey("author"))
0305: docElementValues.put("author", UtilXml
0306: .childElementValue(docElement, "author"));
0307: if (!docElementValues.containsKey("version"))
0308: docElementValues.put("version", UtilXml
0309: .childElementValue(docElement, "version"));
0310: this .title = UtilXml.checkEmpty(entityElement
0311: .getAttribute("title"), (String) docElementValues
0312: .get("title"), "None");
0313: this .description = UtilXml.checkEmpty(UtilXml
0314: .childElementValue(entityElement, "description"),
0315: (String) docElementValues.get("description"),
0316: "None");
0317: this .copyright = UtilXml
0318: .checkEmpty(
0319: entityElement.getAttribute("copyright"),
0320: (String) docElementValues.get("copyright"),
0321: "Copyright (c) 2001 The Open For Business Project - www.ofbiz.org");
0322: this .author = UtilXml.checkEmpty(entityElement
0323: .getAttribute("author"), (String) docElementValues
0324: .get("author"), "None");
0325: this .version = UtilXml.checkEmpty(entityElement
0326: .getAttribute("version"), (String) docElementValues
0327: .get("version"), "1.0");
0328: }
0329: }
0330:
0331: protected void populateRelated(ModelReader reader,
0332: Element entityElement) {
0333: NodeList relationList = entityElement
0334: .getElementsByTagName("relation");
0335: for (int i = 0; i < relationList.getLength(); i++) {
0336: Element relationElement = (Element) relationList.item(i);
0337: if (relationElement.getParentNode() == entityElement) {
0338: ModelRelation relation = reader.createRelation(this ,
0339: relationElement);
0340: if (relation != null)
0341: this .relations.add(relation);
0342: }
0343: }
0344: }
0345:
0346: protected void populateIndexes(Element entityElement) {
0347: NodeList indexList = entityElement
0348: .getElementsByTagName("index");
0349: for (int i = 0; i < indexList.getLength(); i++) {
0350: Element indexElement = (Element) indexList.item(i);
0351: if (indexElement.getParentNode() == entityElement) {
0352: ModelIndex index = new ModelIndex(this , indexElement);
0353: this .indexes.add(index);
0354: }
0355: }
0356: }
0357:
0358: // ===== GETTERS/SETTERS =====
0359:
0360: public ModelReader getModelReader() {
0361: return modelReader;
0362: }
0363:
0364: /** The entity-name of the Entity */
0365: public String getEntityName() {
0366: return this .entityName;
0367: }
0368:
0369: public void setEntityName(String entityName) {
0370: this .entityName = entityName;
0371: }
0372:
0373: /** The plain table-name of the Entity without a schema name prefix */
0374: public String getPlainTableName() {
0375: return this .tableName;
0376: }
0377:
0378: /** The table-name of the Entity including a Schema name if specified in the datasource config */
0379: public String getTableName(String helperName) {
0380: return getTableName(EntityConfigUtil
0381: .getDatasourceInfo(helperName));
0382: }
0383:
0384: /** The table-name of the Entity including a Schema name if specified in the datasource config */
0385: public String getTableName(
0386: EntityConfigUtil.DatasourceInfo datasourceInfo) {
0387: if (datasourceInfo != null && datasourceInfo.schemaName != null
0388: && datasourceInfo.schemaName.length() > 0) {
0389: return datasourceInfo.schemaName + "." + this .tableName;
0390: } else {
0391: return this .tableName;
0392: }
0393: }
0394:
0395: public void setTableName(String tableName) {
0396: this .tableName = tableName;
0397: }
0398:
0399: /** The package-name of the Entity */
0400: public String getPackageName() {
0401: return this .packageName;
0402: }
0403:
0404: public void setPackageName(String packageName) {
0405: this .packageName = packageName;
0406: }
0407:
0408: /** The default-resource-name of the Entity */
0409: public String getDefaultResourceName() {
0410: return this .defaultResourceName;
0411: }
0412:
0413: public void setDefaultResourceName(String defaultResourceName) {
0414: this .defaultResourceName = defaultResourceName;
0415: }
0416:
0417: /** The entity-name of the Entity that this Entity is dependent on, if empty then no dependency */
0418: public String getDependentOn() {
0419: return this .dependentOn;
0420: }
0421:
0422: public void setDependentOn(String dependentOn) {
0423: this .dependentOn = dependentOn;
0424: }
0425:
0426: // Strings to go in the comment header.
0427: /** The title for documentation purposes */
0428: public String getTitle() {
0429: return this .title;
0430: }
0431:
0432: public void setTitle(String title) {
0433: this .title = title;
0434: }
0435:
0436: /** The description for documentation purposes */
0437: public String getDescription() {
0438: return this .description;
0439: }
0440:
0441: public void setDescription(String description) {
0442: this .description = description;
0443: }
0444:
0445: /** The copyright for documentation purposes */
0446: public String getCopyright() {
0447: return this .copyright;
0448: }
0449:
0450: public void setCopyright(String copyright) {
0451: this .copyright = copyright;
0452: }
0453:
0454: /** The author for documentation purposes */
0455: public String getAuthor() {
0456: return this .author;
0457: }
0458:
0459: public void setAuthor(String author) {
0460: this .author = author;
0461: }
0462:
0463: /** The version for documentation purposes */
0464: public String getVersion() {
0465: return this .version;
0466: }
0467:
0468: public void setVersion(String version) {
0469: this .version = version;
0470: }
0471:
0472: /** An indicator to specify if this entity is never cached.
0473: * If true causes the delegator to not clear caches on write and to not get
0474: * from cache on read showing a warning messages to that effect
0475: */
0476: public boolean getNeverCache() {
0477: return this .neverCache;
0478: }
0479:
0480: public void setNeverCache(boolean neverCache) {
0481: this .neverCache = neverCache;
0482: }
0483:
0484: /** An indicator to specify if this entity requires locking for updates */
0485: public boolean getDoLock() {
0486: return this .doLock;
0487: }
0488:
0489: public void setDoLock(boolean doLock) {
0490: this .doLock = doLock;
0491: }
0492:
0493: public boolean lock() {
0494: if (doLock && isField(STAMP_FIELD)) {
0495: return true;
0496: } else {
0497: doLock = false;
0498: return false;
0499: }
0500: }
0501:
0502: public void updatePkLists() {
0503: pks = new ArrayList();
0504: nopks = new ArrayList();
0505: for (int i = 0; i < fields.size(); i++) {
0506: ModelField field = (ModelField) fields.get(i);
0507:
0508: if (field.isPk)
0509: pks.add(field);
0510: else
0511: nopks.add(field);
0512: }
0513: }
0514:
0515: public boolean isField(String fieldName) {
0516: if (fieldName == null)
0517: return false;
0518: for (int i = 0; i < fields.size(); i++) {
0519: ModelField field = (ModelField) fields.get(i);
0520:
0521: if (field.name.equals(fieldName))
0522: return true;
0523: }
0524: return false;
0525: }
0526:
0527: public boolean areFields(Collection fieldNames) {
0528: if (fieldNames == null)
0529: return false;
0530: Iterator iter = fieldNames.iterator();
0531:
0532: while (iter.hasNext()) {
0533: String fieldName = (String) iter.next();
0534:
0535: if (!isField(fieldName))
0536: return false;
0537: }
0538: return true;
0539: }
0540:
0541: public int getPksSize() {
0542: return this .pks.size();
0543: }
0544:
0545: public ModelField getPk(int index) {
0546: return (ModelField) this .pks.get(index);
0547: }
0548:
0549: public Iterator getPksIterator() {
0550: return this .pks.iterator();
0551: }
0552:
0553: public List getPksCopy() {
0554: return new ArrayList(this .pks);
0555: }
0556:
0557: public int getNopksSize() {
0558: return this .nopks.size();
0559: }
0560:
0561: public ModelField getNopk(int index) {
0562: return (ModelField) this .nopks.get(index);
0563: }
0564:
0565: public Iterator getNopksIterator() {
0566: return this .nopks.iterator();
0567: }
0568:
0569: public List getNopksCopy() {
0570: return new ArrayList(this .nopks);
0571: }
0572:
0573: public int getFieldsSize() {
0574: return this .fields.size();
0575: }
0576:
0577: public ModelField getField(int index) {
0578: return (ModelField) this .fields.get(index);
0579: }
0580:
0581: public Iterator getFieldsIterator() {
0582: return this .fields.iterator();
0583: }
0584:
0585: public List getFieldsCopy() {
0586: return new ArrayList(this .fields);
0587: }
0588:
0589: /** The col-name of the Field, the alias of the field if this is on a view-entity */
0590: public String getColNameOrAlias(String fieldName) {
0591: ModelField modelField = this .getField(fieldName);
0592: String fieldString = modelField.getColName();
0593: return fieldString;
0594: }
0595:
0596: public ModelField getField(String fieldName) {
0597: if (fieldName == null)
0598: return null;
0599: if (fieldsMap == null) {
0600: createFieldsMap();
0601: }
0602: ModelField modelField = (ModelField) fieldsMap.get(fieldName);
0603: if (modelField == null) {
0604: // sometimes weird things happen and this getField method is called before the fields are all populated, so before moving on just re-create the fieldsMap again real quick...
0605: // the purpose of the fieldsMap is for speed, but if failures are a little slower, no biggie
0606: createFieldsMap();
0607: modelField = (ModelField) fieldsMap.get(fieldName);
0608: }
0609: return modelField;
0610: }
0611:
0612: protected synchronized void createFieldsMap() {
0613: Map tempMap = new HashMap(fields.size());
0614: for (int i = 0; i < fields.size(); i++) {
0615: ModelField field = (ModelField) fields.get(i);
0616: tempMap.put(field.name, field);
0617: }
0618: fieldsMap = tempMap;
0619: }
0620:
0621: public void addField(ModelField field) {
0622: if (field == null)
0623: return;
0624: this .fields.add(field);
0625:
0626: if (field.isPk) {
0627: pks.add(field);
0628: } else {
0629: nopks.add(field);
0630: }
0631: }
0632:
0633: public ModelField removeField(int index) {
0634: ModelField field = null;
0635:
0636: field = (ModelField) fields.remove(index);
0637: if (field == null)
0638: return null;
0639:
0640: if (field.isPk) {
0641: pks.remove(field);
0642: } else {
0643: nopks.remove(field);
0644: }
0645: return field;
0646: }
0647:
0648: public ModelField removeField(String fieldName) {
0649: if (fieldName == null)
0650: return null;
0651: ModelField field = null;
0652:
0653: for (int i = 0; i < fields.size(); i++) {
0654: field = (ModelField) fields.get(i);
0655: if (field.name.equals(fieldName)) {
0656: fields.remove(i);
0657: if (field.isPk) {
0658: pks.remove(field);
0659: } else {
0660: nopks.remove(field);
0661: }
0662: }
0663: field = null;
0664: }
0665: return field;
0666: }
0667:
0668: public List getAllFieldNames() {
0669: return getFieldNamesFromFieldVector(fields);
0670: }
0671:
0672: public List getPkFieldNames() {
0673: return getFieldNamesFromFieldVector(pks);
0674: }
0675:
0676: public List getNoPkFieldNames() {
0677: return getFieldNamesFromFieldVector(nopks);
0678: }
0679:
0680: public List getFieldNamesFromFieldVector(List modelFields) {
0681: List nameList = new ArrayList(modelFields.size());
0682:
0683: if (modelFields == null || modelFields.size() <= 0)
0684: return nameList;
0685: for (int i = 0; i < modelFields.size(); i++) {
0686: ModelField field = (ModelField) modelFields.get(i);
0687:
0688: nameList.add(field.name);
0689: }
0690: return nameList;
0691: }
0692:
0693: public int getRelationsSize() {
0694: return this .relations.size();
0695: }
0696:
0697: public ModelRelation getRelation(int index) {
0698: return (ModelRelation) this .relations.get(index);
0699: }
0700:
0701: public Iterator getRelationsIterator() {
0702: return this .relations.iterator();
0703: }
0704:
0705: public ModelRelation getRelation(String relationName) {
0706: if (relationName == null)
0707: return null;
0708: for (int i = 0; i < relations.size(); i++) {
0709: ModelRelation relation = (ModelRelation) relations.get(i);
0710: if (relationName.equals(relation.title
0711: + relation.relEntityName))
0712: return relation;
0713: }
0714: return null;
0715: }
0716:
0717: public void addRelation(ModelRelation relation) {
0718: this .relations.add(relation);
0719: }
0720:
0721: public ModelRelation removeRelation(int index) {
0722: return (ModelRelation) this .relations.remove(index);
0723: }
0724:
0725: public int getIndexesSize() {
0726: return this .indexes.size();
0727: }
0728:
0729: public ModelIndex getIndex(int index) {
0730: return (ModelIndex) this .indexes.get(index);
0731: }
0732:
0733: public Iterator getIndexesIterator() {
0734: return this .indexes.iterator();
0735: }
0736:
0737: public ModelIndex getIndex(String indexName) {
0738: if (indexName == null)
0739: return null;
0740: for (int i = 0; i < indexes.size(); i++) {
0741: ModelIndex index = (ModelIndex) indexes.get(i);
0742: if (indexName.equals(index.getName()))
0743: return index;
0744: }
0745: return null;
0746: }
0747:
0748: public void addIndex(ModelIndex index) {
0749: this .indexes.add(index);
0750: }
0751:
0752: public ModelIndex removeIndex(int index) {
0753: return (ModelIndex) this .indexes.remove(index);
0754: }
0755:
0756: public String nameString(List flds) {
0757: return nameString(flds, ", ", "");
0758: }
0759:
0760: public String nameString(List flds, String separator,
0761: String afterLast) {
0762: StringBuffer returnString = new StringBuffer();
0763:
0764: if (flds.size() < 1) {
0765: return "";
0766: }
0767:
0768: int i = 0;
0769:
0770: for (; i < flds.size() - 1; i++) {
0771: returnString.append(((ModelField) flds.get(i)).name);
0772: returnString.append(separator);
0773: }
0774: returnString.append(((ModelField) flds.get(i)).name);
0775: returnString.append(afterLast);
0776: return returnString.toString();
0777: }
0778:
0779: public String typeNameString(List flds) {
0780: StringBuffer returnString = new StringBuffer();
0781:
0782: if (flds.size() < 1) {
0783: return "";
0784: }
0785:
0786: int i = 0;
0787:
0788: for (; i < flds.size() - 1; i++) {
0789: ModelField curField = (ModelField) flds.get(i);
0790: returnString.append(curField.type);
0791: returnString.append(" ");
0792: returnString.append(curField.name);
0793: returnString.append(", ");
0794: }
0795: ModelField curField = (ModelField) flds.get(i);
0796: returnString.append(curField.type);
0797: returnString.append(" ");
0798: returnString.append(curField.name);
0799: return returnString.toString();
0800: }
0801:
0802: public String fieldNameString() {
0803: return fieldNameString(", ", "");
0804: }
0805:
0806: public String fieldNameString(String separator, String afterLast) {
0807: return nameString(fields, separator, afterLast);
0808: }
0809:
0810: public String fieldTypeNameString() {
0811: return typeNameString(fields);
0812: }
0813:
0814: public String primKeyClassNameString() {
0815: return typeNameString(pks);
0816: }
0817:
0818: public String pkNameString() {
0819: return pkNameString(", ", "");
0820: }
0821:
0822: public String pkNameString(String separator, String afterLast) {
0823: return nameString(pks, separator, afterLast);
0824: }
0825:
0826: public String nonPkNullList() {
0827: return fieldsStringList(fields, "null", ", ", false, true);
0828: }
0829:
0830: public String fieldsStringList(List flds, String eachString,
0831: String separator) {
0832: return fieldsStringList(flds, eachString, separator, false,
0833: false);
0834: }
0835:
0836: public String fieldsStringList(List flds, String eachString,
0837: String separator, boolean appendIndex) {
0838: return fieldsStringList(flds, eachString, separator,
0839: appendIndex, false);
0840: }
0841:
0842: public String fieldsStringList(List flds, String eachString,
0843: String separator, boolean appendIndex, boolean onlyNonPK) {
0844: StringBuffer returnString = new StringBuffer();
0845:
0846: if (flds.size() < 1) {
0847: return "";
0848: }
0849:
0850: int i = 0;
0851:
0852: for (; i < flds.size(); i++) {
0853: if (onlyNonPK && ((ModelField) flds.get(i)).isPk)
0854: continue;
0855: returnString.append(eachString);
0856: if (appendIndex)
0857: returnString.append(i + 1);
0858: if (i < flds.size() - 1)
0859: returnString.append(separator);
0860: }
0861: return returnString.toString();
0862: }
0863:
0864: public String colNameString(List flds) {
0865: return colNameString(flds, ", ", "", false);
0866: }
0867:
0868: public String colNameString(List flds, String separator,
0869: String afterLast, boolean alias) {
0870: StringBuffer returnString = new StringBuffer();
0871:
0872: if (flds.size() < 1) {
0873: return "";
0874: }
0875:
0876: Iterator fldsIt = flds.iterator();
0877: while (fldsIt.hasNext()) {
0878: ModelField field = (ModelField) fldsIt.next();
0879: returnString.append(field.colName);
0880: if (fldsIt.hasNext()) {
0881: returnString.append(separator);
0882: }
0883: }
0884:
0885: returnString.append(afterLast);
0886: return returnString.toString();
0887: }
0888:
0889: public String classNameString(List flds) {
0890: return classNameString(flds, ", ", "");
0891: }
0892:
0893: public String classNameString(List flds, String separator,
0894: String afterLast) {
0895: StringBuffer returnString = new StringBuffer();
0896:
0897: if (flds.size() < 1) {
0898: return "";
0899: }
0900:
0901: int i = 0;
0902:
0903: for (; i < flds.size() - 1; i++) {
0904: returnString.append(ModelUtil
0905: .upperFirstChar(((ModelField) flds.get(i)).name));
0906: returnString.append(separator);
0907: }
0908: returnString.append(ModelUtil.upperFirstChar(((ModelField) flds
0909: .get(i)).name));
0910: returnString.append(afterLast);
0911: return returnString.toString();
0912: }
0913:
0914: public String finderQueryString(List flds) {
0915: StringBuffer returnString = new StringBuffer();
0916:
0917: if (flds.size() < 1) {
0918: return "";
0919: }
0920: int i = 0;
0921:
0922: for (; i < flds.size() - 1; i++) {
0923: returnString.append(((ModelField) flds.get(i)).colName);
0924: returnString.append(" like {");
0925: returnString.append(i);
0926: returnString.append("} AND ");
0927: }
0928: returnString.append(((ModelField) flds.get(i)).colName);
0929: returnString.append(" like {");
0930: returnString.append(i);
0931: returnString.append("}");
0932: return returnString.toString();
0933: }
0934:
0935: public String httpArgList(List flds) {
0936: StringBuffer returnString = new StringBuffer();
0937:
0938: if (flds.size() < 1) {
0939: return "";
0940: }
0941: int i = 0;
0942:
0943: for (; i < flds.size() - 1; i++) {
0944: returnString.append("\"");
0945: returnString.append(tableName);
0946: returnString.append("_");
0947: returnString.append(((ModelField) flds.get(i)).colName);
0948: returnString.append("=\" + ");
0949: returnString.append(((ModelField) flds.get(i)).name);
0950: returnString.append(" + \"&\" + ");
0951: }
0952: returnString.append("\"");
0953: returnString.append(tableName);
0954: returnString.append("_");
0955: returnString.append(((ModelField) flds.get(i)).colName);
0956: returnString.append("=\" + ");
0957: returnString.append(((ModelField) flds.get(i)).name);
0958: return returnString.toString();
0959: }
0960:
0961: public String httpArgListFromClass(List flds) {
0962: StringBuffer returnString = new StringBuffer();
0963:
0964: if (flds.size() < 1) {
0965: return "";
0966: }
0967:
0968: int i = 0;
0969:
0970: for (; i < flds.size() - 1; i++) {
0971: returnString.append("\"");
0972: returnString.append(tableName);
0973: returnString.append("_");
0974: returnString.append(((ModelField) flds.get(i)).colName);
0975: returnString.append("=\" + ");
0976: returnString.append(ModelUtil.lowerFirstChar(entityName));
0977: returnString.append(".get");
0978: returnString.append(ModelUtil
0979: .upperFirstChar(((ModelField) flds.get(i)).name));
0980: returnString.append("() + \"&\" + ");
0981: }
0982: returnString.append("\"");
0983: returnString.append(tableName);
0984: returnString.append("_");
0985: returnString.append(((ModelField) flds.get(i)).colName);
0986: returnString.append("=\" + ");
0987: returnString.append(ModelUtil.lowerFirstChar(entityName));
0988: returnString.append(".get");
0989: returnString.append(ModelUtil.upperFirstChar(((ModelField) flds
0990: .get(i)).name));
0991: returnString.append("()");
0992: return returnString.toString();
0993: }
0994:
0995: public String httpArgListFromClass(List flds,
0996: String entityNameSuffix) {
0997: StringBuffer returnString = new StringBuffer();
0998:
0999: if (flds.size() < 1) {
1000: return "";
1001: }
1002:
1003: int i = 0;
1004:
1005: for (; i < flds.size() - 1; i++) {
1006: returnString.append("\"");
1007: returnString.append(tableName);
1008: returnString.append("_");
1009: returnString.append(((ModelField) flds.get(i)).colName);
1010: returnString.append("=\" + ");
1011: returnString.append(ModelUtil.lowerFirstChar(entityName));
1012: returnString.append(entityNameSuffix);
1013: returnString.append(".get");
1014: returnString.append(ModelUtil
1015: .upperFirstChar(((ModelField) flds.get(i)).name));
1016: returnString.append("() + \"&\" + ");
1017: }
1018: returnString.append("\"");
1019: returnString.append(tableName);
1020: returnString.append("_");
1021: returnString.append(((ModelField) flds.get(i)).colName);
1022: returnString.append("=\" + ");
1023: returnString.append(ModelUtil.lowerFirstChar(entityName));
1024: returnString.append(entityNameSuffix);
1025: returnString.append(".get");
1026: returnString.append(ModelUtil.upperFirstChar(((ModelField) flds
1027: .get(i)).name));
1028: returnString.append("()");
1029: return returnString.toString();
1030: }
1031:
1032: public String httpRelationArgList(List flds, ModelRelation relation) {
1033: StringBuffer returnString = new StringBuffer();
1034:
1035: if (flds.size() < 1) {
1036: return "";
1037: }
1038:
1039: int i = 0;
1040:
1041: for (; i < flds.size() - 1; i++) {
1042: ModelKeyMap keyMap = relation
1043: .findKeyMapByRelated(((ModelField) flds.get(i)).name);
1044:
1045: if (keyMap != null) {
1046: returnString.append("\"");
1047: returnString.append(tableName);
1048: returnString.append("_");
1049: returnString.append(((ModelField) flds.get(i)).colName);
1050: returnString.append("=\" + ");
1051: returnString
1052: .append(ModelUtil
1053: .lowerFirstChar(relation.mainEntity.entityName));
1054: returnString.append(".get");
1055: returnString.append(ModelUtil
1056: .upperFirstChar(keyMap.fieldName));
1057: returnString.append("() + \"&\" + ");
1058: } else {
1059: Debug
1060: .logWarning(
1061: "-- -- ENTITYGEN ERROR:httpRelationArgList: Related Key in Key Map not found for name: "
1062: + ((ModelField) flds.get(i)).name
1063: + " related entity: "
1064: + relation.relEntityName
1065: + " main entity: "
1066: + relation.mainEntity.entityName
1067: + " type: " + relation.type,
1068: module);
1069: }
1070: }
1071: ModelKeyMap keyMap = relation
1072: .findKeyMapByRelated(((ModelField) flds.get(i)).name);
1073:
1074: if (keyMap != null) {
1075: returnString.append("\"");
1076: returnString.append(tableName);
1077: returnString.append("_");
1078: returnString.append(((ModelField) flds.get(i)).colName);
1079: returnString.append("=\" + ");
1080: returnString.append(ModelUtil
1081: .lowerFirstChar(relation.mainEntity.entityName));
1082: returnString.append(".get");
1083: returnString.append(ModelUtil
1084: .upperFirstChar(keyMap.fieldName));
1085: returnString.append("()");
1086: } else {
1087: Debug
1088: .logWarning(
1089: "-- -- ENTITYGEN ERROR:httpRelationArgList: Related Key in Key Map not found for name: "
1090: + ((ModelField) flds.get(i)).name
1091: + " related entity: "
1092: + relation.relEntityName
1093: + " main entity: "
1094: + relation.mainEntity.entityName
1095: + " type: " + relation.type, module);
1096: }
1097: return returnString.toString();
1098: }
1099:
1100: /*
1101: public String httpRelationArgList(ModelRelation relation) {
1102: String returnString = "";
1103: if(relation.keyMaps.size() < 1) { return ""; }
1104:
1105: int i = 0;
1106: for(; i < relation.keyMaps.size() - 1; i++) {
1107: ModelKeyMap keyMap = (ModelKeyMap)relation.keyMaps.get(i);
1108: if(keyMap != null)
1109: returnString = returnString + "\"" + tableName + "_" + keyMap.relColName + "=\" + " + ModelUtil.lowerFirstChar(relation.mainEntity.entityName) + ".get" + ModelUtil.upperFirstChar(keyMap.fieldName) + "() + \"&\" + ";
1110: }
1111: ModelKeyMap keyMap = (ModelKeyMap)relation.keyMaps.get(i);
1112: returnString = returnString + "\"" + tableName + "_" + keyMap.relColName + "=\" + " + ModelUtil.lowerFirstChar(relation.mainEntity.entityName) + ".get" + ModelUtil.upperFirstChar(keyMap.fieldName) + "()";
1113: return returnString;
1114: }
1115: */
1116: public String typeNameStringRelatedNoMapped(List flds,
1117: ModelRelation relation) {
1118: StringBuffer returnString = new StringBuffer();
1119:
1120: if (flds.size() < 1) {
1121: return "";
1122: }
1123:
1124: int i = 0;
1125:
1126: if (relation
1127: .findKeyMapByRelated(((ModelField) flds.get(i)).name) == null) {
1128: returnString.append(((ModelField) flds.get(i)).type);
1129: returnString.append(" ");
1130: returnString.append(((ModelField) flds.get(i)).name);
1131: }
1132: i++;
1133: for (; i < flds.size(); i++) {
1134: if (relation
1135: .findKeyMapByRelated(((ModelField) flds.get(i)).name) == null) {
1136: if (returnString.length() > 0)
1137: returnString.append(", ");
1138: returnString.append(((ModelField) flds.get(i)).type);
1139: returnString.append(" ");
1140: returnString.append(((ModelField) flds.get(i)).name);
1141: }
1142: }
1143: return returnString.toString();
1144: }
1145:
1146: public String typeNameStringRelatedAndMain(List flds,
1147: ModelRelation relation) {
1148: StringBuffer returnString = new StringBuffer();
1149:
1150: if (flds.size() < 1) {
1151: return "";
1152: }
1153:
1154: int i = 0;
1155:
1156: for (; i < flds.size() - 1; i++) {
1157: ModelKeyMap keyMap = relation
1158: .findKeyMapByRelated(((ModelField) flds.get(i)).name);
1159:
1160: if (keyMap != null) {
1161: returnString.append(keyMap.fieldName);
1162: returnString.append(", ");
1163: } else {
1164: returnString.append(((ModelField) flds.get(i)).name);
1165: returnString.append(", ");
1166: }
1167: }
1168: ModelKeyMap keyMap = relation
1169: .findKeyMapByRelated(((ModelField) flds.get(i)).name);
1170:
1171: if (keyMap != null)
1172: returnString.append(keyMap.fieldName);
1173: else
1174: returnString.append(((ModelField) flds.get(i)).name);
1175: return returnString.toString();
1176: }
1177:
1178: public int compareTo(Object obj) {
1179: ModelEntity otherModelEntity = (ModelEntity) obj;
1180:
1181: /* This DOESN'T WORK, so forget it... using two passes
1182: //sort list by fk dependencies
1183:
1184: if (this.getEntityName().equals(otherModelEntity.getEntityName())) {
1185: return 0;
1186: }
1187:
1188: //look through relations for dependencies from this entity to the other
1189: Iterator relationsIter = this.getRelationsIterator();
1190: while (relationsIter.hasNext()) {
1191: ModelRelation modelRelation = (ModelRelation) relationsIter.next();
1192:
1193: if ("one".equals(modelRelation.getType()) && modelRelation.getRelEntityName().equals(otherModelEntity.getEntityName())) {
1194: //this entity is dependent on the other entity, so put that entity earlier in the list
1195: return -1;
1196: }
1197: }
1198:
1199: //look through relations for dependencies from the other to this entity
1200: Iterator otherRelationsIter = otherModelEntity.getRelationsIterator();
1201: while (otherRelationsIter.hasNext()) {
1202: ModelRelation modelRelation = (ModelRelation) otherRelationsIter.next();
1203:
1204: if ("one".equals(modelRelation.getType()) && modelRelation.getRelEntityName().equals(this.getEntityName())) {
1205: //the other entity is dependent on this entity, so put that entity later in the list
1206: return 1;
1207: }
1208: }
1209:
1210: return 0;
1211: */
1212:
1213: return this .getEntityName().compareTo(
1214: otherModelEntity.getEntityName());
1215: }
1216:
1217: /**
1218: * @return Returns the noAutoStamp.
1219: */
1220: public boolean getNoAutoStamp() {
1221: return this .noAutoStamp;
1222: }
1223:
1224: /**
1225: * @param noAutoStamp The noAutoStamp to set.
1226: */
1227: public void setNoAutoStamp(boolean noAutoStamp) {
1228: this.noAutoStamp = noAutoStamp;
1229: }
1230:
1231: }
|