Source Code Cross Referenced for ModelEntity.java in  » ERP-CRM-Financial » ofbiz » org » ofbiz » entity » model » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ERP CRM Financial » ofbiz » org.ofbiz.entity.model 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*******************************************************************************
0002:         * Licensed to the Apache Software Foundation (ASF) under one
0003:         * or more contributor license agreements.  See the NOTICE file
0004:         * distributed with this work for additional information
0005:         * regarding copyright ownership.  The ASF licenses this file
0006:         * to you under the Apache License, Version 2.0 (the
0007:         * "License"); you may not use this file except in compliance
0008:         * with the License.  You may obtain a copy of the License at
0009:         * 
0010:         * http://www.apache.org/licenses/LICENSE-2.0
0011:         * 
0012:         * Unless required by applicable law or agreed to in writing,
0013:         * software distributed under the License is distributed on an
0014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0015:         * KIND, either express or implied.  See the License for the
0016:         * specific language governing permissions and limitations
0017:         * under the License.
0018:         *******************************************************************************/package org.ofbiz.entity.model;
0019:
0020:        import java.io.Serializable;
0021:        import java.util.Collection;
0022:        import java.util.Iterator;
0023:        import java.util.List;
0024:        import java.util.Map;
0025:        import java.util.Set;
0026:
0027:        import javolution.util.FastList;
0028:        import javolution.util.FastMap;
0029:        import org.w3c.dom.Document;
0030:        import org.w3c.dom.Element;
0031:        import org.ofbiz.base.util.Debug;
0032:        import org.ofbiz.base.util.GeneralException;
0033:        import org.ofbiz.base.util.ObjectType;
0034:        import org.ofbiz.base.util.UtilMisc;
0035:        import org.ofbiz.base.util.UtilTimer;
0036:        import org.ofbiz.base.util.UtilXml;
0037:        import org.ofbiz.base.util.UtilValidate;
0038:        import org.ofbiz.entity.GenericDelegator;
0039:        import org.ofbiz.entity.GenericEntity;
0040:        import org.ofbiz.entity.GenericEntityException;
0041:        import org.ofbiz.entity.GenericValue;
0042:        import org.ofbiz.entity.config.DatasourceInfo;
0043:        import org.ofbiz.entity.config.EntityConfigUtil;
0044:        import org.ofbiz.entity.jdbc.DatabaseUtil;
0045:
0046:        /**
0047:         * Generic Entity - Entity model class
0048:         *
0049:         */
0050:        public class ModelEntity extends ModelInfo implements  Comparable,
0051:                Serializable {
0052:
0053:            public static final String module = ModelEntity.class.getName();
0054:
0055:            /** The name of the time stamp field for locking/syncronization */
0056:            public static final String STAMP_FIELD = "lastUpdatedStamp";
0057:            public static final String STAMP_TX_FIELD = "lastUpdatedTxStamp";
0058:            public static final String CREATE_STAMP_FIELD = "createdStamp";
0059:            public static final String CREATE_STAMP_TX_FIELD = "createdTxStamp";
0060:
0061:            /** The ModelReader that created this Entity */
0062:            protected ModelReader modelReader = null;
0063:
0064:            /** The entity-name of the Entity */
0065:            protected String entityName = "";
0066:
0067:            /** The table-name of the Entity */
0068:            protected String tableName = "";
0069:
0070:            /** The package-name of the Entity */
0071:            protected String packageName = "";
0072:
0073:            /** The default-resource-name of the Entity, used with the getResource call to check for a value in a resource bundle */
0074:            protected String defaultResourceName = "";
0075:
0076:            /** The entity-name of the Entity that this Entity is dependent on, if empty then no dependency */
0077:            protected String dependentOn = "";
0078:
0079:            /** A List of the Field objects for the Entity */
0080:            protected List fields = FastList.newInstance();
0081:            protected Map fieldsMap = null;
0082:
0083:            /** A List of the Field objects for the Entity, one for each Primary Key */
0084:            protected List pks = FastList.newInstance();
0085:
0086:            /** A List of the Field objects for the Entity, one for each NON Primary Key */
0087:            protected List nopks = FastList.newInstance();
0088:
0089:            /** relations defining relationships between this entity and other entities */
0090:            protected List relations = FastList.newInstance();
0091:
0092:            /** indexes on fields/columns in this entity */
0093:            protected List indexes = FastList.newInstance();
0094:
0095:            /** map of ModelViewEntities that references this model */
0096:            protected Map viewEntities = FastMap.newInstance();
0097:
0098:            /** An indicator to specify if this entity requires locking for updates */
0099:            protected boolean doLock = false;
0100:
0101:            /** Can be used to disable automatically creating update stamp fields and populating them on inserts and updates */
0102:            protected boolean noAutoStamp = false;
0103:
0104:            /** An indicator to specify if this entity is never cached. 
0105:             * If true causes the delegator to not clear caches on write and to not get 
0106:             * from cache on read showing a warning messages to that effect 
0107:             */
0108:            protected boolean neverCache = false;
0109:
0110:            protected boolean autoClearCache = true;
0111:
0112:            // ===== CONSTRUCTORS =====
0113:            /** Default Constructor */
0114:            public ModelEntity() {
0115:            }
0116:
0117:            /** XML Constructor */
0118:            protected ModelEntity(ModelReader reader, Element entityElement,
0119:                    ModelInfo def) {
0120:                super (def);
0121:                populateFromAttributes(entityElement);
0122:                this .modelReader = reader;
0123:            }
0124:
0125:            /** XML Constructor */
0126:            public ModelEntity(ModelReader reader, Element entityElement,
0127:                    UtilTimer utilTimer, ModelInfo def) {
0128:                this (reader, entityElement, def);
0129:
0130:                if (utilTimer != null)
0131:                    utilTimer
0132:                            .timerString("  createModelEntity: before general/basic info");
0133:                this .populateBasicInfo(entityElement);
0134:
0135:                if (utilTimer != null)
0136:                    utilTimer.timerString("  createModelEntity: before fields");
0137:                List fieldElementList = UtilXml.childElementList(entityElement,
0138:                        "field");
0139:                Iterator fieldElementIter = fieldElementList.iterator();
0140:                while (fieldElementIter.hasNext()) {
0141:                    Element fieldElement = (Element) fieldElementIter.next();
0142:                    ModelField field = reader.createModelField(fieldElement);
0143:                    if (field != null) {
0144:                        field.setModelEntity(this );
0145:                        this .fields.add(field);
0146:                    }
0147:                }
0148:
0149:                // if applicable automatically add the STAMP_FIELD and STAMP_TX_FIELD fields
0150:                if ((this .doLock || !this .noAutoStamp)
0151:                        && !this .isField(STAMP_FIELD)) {
0152:                    ModelField newField = reader.createModelField(STAMP_FIELD,
0153:                            "date-time", null, false);
0154:                    newField.setIsAutoCreatedInternal(true);
0155:                    newField.setModelEntity(this );
0156:                    this .fields.add(newField);
0157:                }
0158:                if (!this .noAutoStamp && !this .isField(STAMP_TX_FIELD)) {
0159:                    ModelField newField = reader.createModelField(
0160:                            STAMP_TX_FIELD, "date-time", null, false);
0161:                    newField.setIsAutoCreatedInternal(true);
0162:                    newField.setModelEntity(this );
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:                    txIndex.setModelEntity(this );
0171:                    indexes.add(txIndex);
0172:                }
0173:
0174:                // if applicable automatically add the CREATE_STAMP_FIELD and CREATE_STAMP_TX_FIELD fields
0175:                if ((this .doLock || !this .noAutoStamp)
0176:                        && !this .isField(CREATE_STAMP_FIELD)) {
0177:                    ModelField newField = reader.createModelField(
0178:                            CREATE_STAMP_FIELD, "date-time", null, false);
0179:                    newField.setIsAutoCreatedInternal(true);
0180:                    newField.setModelEntity(this );
0181:                    this .fields.add(newField);
0182:                }
0183:                if (!this .noAutoStamp && !this .isField(CREATE_STAMP_TX_FIELD)) {
0184:                    ModelField newField = reader.createModelField(
0185:                            CREATE_STAMP_TX_FIELD, "date-time", null, false);
0186:                    newField.setIsAutoCreatedInternal(true);
0187:                    newField.setModelEntity(this );
0188:                    this .fields.add(newField);
0189:
0190:                    // also add an index for this field
0191:                    String indexName = ModelUtil.shortenDbName(this .tableName
0192:                            + "_TXCRTS", 18);
0193:                    ModelIndex txIndex = new ModelIndex(this , indexName, false);
0194:                    txIndex.addIndexField(ModelEntity.CREATE_STAMP_TX_FIELD);
0195:                    txIndex.setModelEntity(this );
0196:                    indexes.add(txIndex);
0197:                }
0198:
0199:                if (utilTimer != null)
0200:                    utilTimer
0201:                            .timerString("  createModelEntity: before prim-keys");
0202:                List pkElementList = UtilXml.childElementList(entityElement,
0203:                        "prim-key");
0204:                Iterator pkElementIter = pkElementList.iterator();
0205:                while (pkElementIter.hasNext()) {
0206:                    Element pkElement = (Element) pkElementIter.next();
0207:                    ModelField field = reader.findModelField(this , pkElement
0208:                            .getAttribute("field"));
0209:                    if (field != null) {
0210:                        this .pks.add(field);
0211:                        field.isPk = true;
0212:                    } else {
0213:                        Debug.logError(
0214:                                "[ModelReader.createModelEntity] ERROR: Could not find field \""
0215:                                        + pkElement.getAttribute("field")
0216:                                        + "\" specified in a prim-key", module);
0217:                    }
0218:                }
0219:
0220:                // now that we have the pks and the fields, make the nopks vector
0221:                this .nopks = FastList.newInstance();
0222:                for (int ind = 0; ind < this .fields.size(); ind++) {
0223:                    ModelField field = (ModelField) this .fields.get(ind);
0224:                    if (!field.isPk)
0225:                        this .nopks.add(field);
0226:                }
0227:
0228:                if (utilTimer != null)
0229:                    utilTimer
0230:                            .timerString("  createModelEntity: before relations");
0231:                this .populateRelated(reader, entityElement);
0232:                this .populateIndexes(entityElement);
0233:            }
0234:
0235:            /** DB Names Constructor */
0236:            public ModelEntity(String tableName, Map colMap,
0237:                    ModelFieldTypeReader modelFieldTypeReader,
0238:                    boolean isCaseSensitive) {
0239:                // if there is a dot in the name, remove it and everything before it, should be the schema name
0240:                this .tableName = tableName;
0241:                int dotIndex = this .tableName.indexOf(".");
0242:                if (dotIndex >= 0) {
0243:                    this .tableName = this .tableName.substring(dotIndex + 1);
0244:                }
0245:                this .entityName = ModelUtil.dbNameToClassName(this .tableName);
0246:                Iterator columnEntryIter = colMap.entrySet().iterator();
0247:                while (columnEntryIter.hasNext()) {
0248:                    Map.Entry columnEntry = (Map.Entry) columnEntryIter.next();
0249:                    DatabaseUtil.ColumnCheckInfo ccInfo = (DatabaseUtil.ColumnCheckInfo) columnEntry
0250:                            .getValue();
0251:                    ModelField newField = new ModelField(ccInfo,
0252:                            modelFieldTypeReader);
0253:                    this .fields.add(newField);
0254:                }
0255:                this .updatePkLists();
0256:            }
0257:
0258:            protected void populateBasicInfo(Element entityElement) {
0259:                this .entityName = UtilXml.checkEmpty(entityElement
0260:                        .getAttribute("entity-name"));
0261:                this .tableName = UtilXml.checkEmpty(entityElement
0262:                        .getAttribute("table-name"), ModelUtil
0263:                        .javaNameToDbName(this .entityName));
0264:                this .packageName = UtilXml.checkEmpty(entityElement
0265:                        .getAttribute("package-name"));
0266:                this .defaultResourceName = UtilXml.checkEmpty(entityElement
0267:                        .getAttribute("default-resource-name"));
0268:                this .dependentOn = UtilXml.checkEmpty(entityElement
0269:                        .getAttribute("dependent-on"));
0270:                this .doLock = UtilXml.checkBoolean(entityElement
0271:                        .getAttribute("enable-lock"), false);
0272:                this .noAutoStamp = UtilXml.checkBoolean(entityElement
0273:                        .getAttribute("no-auto-stamp"), false);
0274:                this .neverCache = UtilXml.checkBoolean(entityElement
0275:                        .getAttribute("never-cache"), false);
0276:                this .autoClearCache = UtilXml.checkBoolean(entityElement
0277:                        .getAttribute("auto-clear-cache"), true);
0278:            }
0279:
0280:            protected void populateRelated(ModelReader reader,
0281:                    Element entityElement) {
0282:                List relationElementList = UtilXml.childElementList(
0283:                        entityElement, "relation");
0284:                Iterator relationElementIter = relationElementList.iterator();
0285:                while (relationElementIter.hasNext()) {
0286:                    Element relationElement = (Element) relationElementIter
0287:                            .next();
0288:                    ModelRelation relation = reader.createRelation(this ,
0289:                            relationElement);
0290:                    if (relation != null) {
0291:                        relation.setModelEntity(this );
0292:                        this .relations.add(relation);
0293:                    }
0294:                }
0295:            }
0296:
0297:            protected void populateIndexes(Element entityElement) {
0298:                List indexElementList = UtilXml.childElementList(entityElement,
0299:                        "index");
0300:                Iterator indexElementIter = indexElementList.iterator();
0301:                while (indexElementIter.hasNext()) {
0302:                    Element indexElement = (Element) indexElementIter.next();
0303:                    ModelIndex index = new ModelIndex(this , indexElement);
0304:                    index.setModelEntity(this );
0305:                    this .indexes.add(index);
0306:                }
0307:            }
0308:
0309:            public boolean containsAllPkFieldNames(Set fieldNames) {
0310:                Iterator pksIter = this .getPksIterator();
0311:                while (pksIter.hasNext()) {
0312:                    ModelField pkField = (ModelField) pksIter.next();
0313:                    if (!fieldNames.contains(pkField.getName())) {
0314:                        return false;
0315:                    }
0316:                }
0317:                return true;
0318:            }
0319:
0320:            public void addExtendEntity(ModelReader reader,
0321:                    Element extendEntityElement) {
0322:                List fieldElementList = UtilXml.childElementList(
0323:                        extendEntityElement, "field");
0324:                Iterator fieldElementIter = fieldElementList.iterator();
0325:                while (fieldElementIter.hasNext()) {
0326:                    Element fieldElement = (Element) fieldElementIter.next();
0327:                    // TODO: should we look for existing fields of the same name here? for now just add to list...
0328:                    ModelField field = reader.createModelField(fieldElement);
0329:                    if (field != null) {
0330:                        field.setModelEntity(this );
0331:                        this .fields.add(field);
0332:                        // this will always be true for now as extend-entity fielsd are always nonpks
0333:                        if (!field.isPk)
0334:                            this .nopks.add(field);
0335:                    }
0336:                }
0337:
0338:                this .populateRelated(reader, extendEntityElement);
0339:                this .populateIndexes(extendEntityElement);
0340:            }
0341:
0342:            // ===== GETTERS/SETTERS =====
0343:
0344:            public ModelReader getModelReader() {
0345:                return modelReader;
0346:            }
0347:
0348:            /** The entity-name of the Entity */
0349:            public String getEntityName() {
0350:                return this .entityName;
0351:            }
0352:
0353:            public void setEntityName(String entityName) {
0354:                this .entityName = entityName;
0355:            }
0356:
0357:            /** The plain table-name of the Entity without a schema name prefix */
0358:            public String getPlainTableName() {
0359:                return this .tableName;
0360:            }
0361:
0362:            /** The table-name of the Entity including a Schema name if specified in the datasource config */
0363:            public String getTableName(String helperName) {
0364:                return getTableName(EntityConfigUtil
0365:                        .getDatasourceInfo(helperName));
0366:            }
0367:
0368:            /** The table-name of the Entity including a Schema name if specified in the datasource config */
0369:            public String getTableName(DatasourceInfo datasourceInfo) {
0370:                if (datasourceInfo != null && datasourceInfo.schemaName != null
0371:                        && datasourceInfo.schemaName.length() > 0) {
0372:                    return datasourceInfo.schemaName + "." + this .tableName;
0373:                } else {
0374:                    return this .tableName;
0375:                }
0376:            }
0377:
0378:            public void setTableName(String tableName) {
0379:                this .tableName = tableName;
0380:            }
0381:
0382:            /** The package-name of the Entity */
0383:            public String getPackageName() {
0384:                return this .packageName;
0385:            }
0386:
0387:            public void setPackageName(String packageName) {
0388:                this .packageName = packageName;
0389:            }
0390:
0391:            /** The default-resource-name of the Entity */
0392:            public String getDefaultResourceName() {
0393:                return this .defaultResourceName;
0394:            }
0395:
0396:            public void setDefaultResourceName(String defaultResourceName) {
0397:                this .defaultResourceName = defaultResourceName;
0398:            }
0399:
0400:            /** The entity-name of the Entity that this Entity is dependent on, if empty then no dependency */
0401:            public String getDependentOn() {
0402:                return this .dependentOn;
0403:            }
0404:
0405:            public void setDependentOn(String dependentOn) {
0406:                this .dependentOn = dependentOn;
0407:            }
0408:
0409:            /** An indicator to specify if this entity is never cached. 
0410:             * If true causes the delegator to not clear caches on write and to not get 
0411:             * from cache on read showing a warning messages to that effect 
0412:             */
0413:            public boolean getNeverCache() {
0414:                return this .neverCache;
0415:            }
0416:
0417:            public void setNeverCache(boolean neverCache) {
0418:                this .neverCache = neverCache;
0419:            }
0420:
0421:            public boolean getAutoClearCache() {
0422:                return this .autoClearCache;
0423:            }
0424:
0425:            public void setAutoClearCache(boolean autoClearCache) {
0426:                this .autoClearCache = autoClearCache;
0427:            }
0428:
0429:            /** An indicator to specify if this entity requires locking for updates */
0430:            public boolean getDoLock() {
0431:                return this .doLock;
0432:            }
0433:
0434:            public void setDoLock(boolean doLock) {
0435:                this .doLock = doLock;
0436:            }
0437:
0438:            public boolean lock() {
0439:                if (doLock && isField(STAMP_FIELD)) {
0440:                    return true;
0441:                } else {
0442:                    doLock = false;
0443:                    return false;
0444:                }
0445:            }
0446:
0447:            public void updatePkLists() {
0448:                pks = FastList.newInstance();
0449:                nopks = FastList.newInstance();
0450:                for (int i = 0; i < fields.size(); i++) {
0451:                    ModelField field = (ModelField) fields.get(i);
0452:
0453:                    if (field.isPk)
0454:                        pks.add(field);
0455:                    else
0456:                        nopks.add(field);
0457:                }
0458:            }
0459:
0460:            public boolean isField(String fieldName) {
0461:                if (fieldName == null)
0462:                    return false;
0463:                for (int i = 0; i < fields.size(); i++) {
0464:                    ModelField field = (ModelField) fields.get(i);
0465:
0466:                    if (field.name.equals(fieldName))
0467:                        return true;
0468:                }
0469:                return false;
0470:            }
0471:
0472:            public boolean areFields(Collection fieldNames) {
0473:                if (fieldNames == null)
0474:                    return false;
0475:                Iterator iter = fieldNames.iterator();
0476:
0477:                while (iter.hasNext()) {
0478:                    String fieldName = (String) iter.next();
0479:
0480:                    if (!isField(fieldName))
0481:                        return false;
0482:                }
0483:                return true;
0484:            }
0485:
0486:            public int getPksSize() {
0487:                return this .pks.size();
0488:            }
0489:
0490:            /**
0491:             * @deprecated
0492:             */
0493:            public ModelField getPk(int index) {
0494:                return (ModelField) this .pks.get(index);
0495:            }
0496:
0497:            public ModelField getOnlyPk() {
0498:                if (this .pks.size() == 1) {
0499:                    return (ModelField) this .pks.get(0);
0500:                } else {
0501:                    throw new IllegalArgumentException(
0502:                            "Error in getOnlyPk, the [" + this .getEntityName()
0503:                                    + "] entity has more than one pk!");
0504:                }
0505:            }
0506:
0507:            public Iterator getPksIterator() {
0508:                return this .pks.iterator();
0509:            }
0510:
0511:            public List getPksCopy() {
0512:                List newList = FastList.newInstance();
0513:                newList.addAll(this .pks);
0514:                return newList;
0515:            }
0516:
0517:            public String getFirstPkFieldName() {
0518:                List pkFieldNames = this .getPkFieldNames();
0519:                String idFieldName = null;
0520:                if (pkFieldNames != null && pkFieldNames.size() > 0) {
0521:                    idFieldName = (String) pkFieldNames.get(0);
0522:                }
0523:                return idFieldName;
0524:            }
0525:
0526:            public int getNopksSize() {
0527:                return this .nopks.size();
0528:            }
0529:
0530:            /**
0531:             * @deprecated
0532:             */
0533:            public ModelField getNopk(int index) {
0534:                return (ModelField) this .nopks.get(index);
0535:            }
0536:
0537:            public Iterator getNopksIterator() {
0538:                return this .nopks.iterator();
0539:            }
0540:
0541:            public List getNopksCopy() {
0542:                List newList = FastList.newInstance();
0543:                newList.addAll(this .nopks);
0544:                return newList;
0545:            }
0546:
0547:            public int getFieldsSize() {
0548:                return this .fields.size();
0549:            }
0550:
0551:            /**
0552:             * @deprecated
0553:             */
0554:            public ModelField getField(int index) {
0555:                return (ModelField) this .fields.get(index);
0556:            }
0557:
0558:            public Iterator getFieldsIterator() {
0559:                return this .fields.iterator();
0560:            }
0561:
0562:            public List getFieldsCopy() {
0563:                List newList = FastList.newInstance();
0564:                newList.addAll(this .fields);
0565:                return newList;
0566:            }
0567:
0568:            /** The col-name of the Field, the alias of the field if this is on a view-entity */
0569:            public String getColNameOrAlias(String fieldName) {
0570:                ModelField modelField = this .getField(fieldName);
0571:                String fieldString = modelField.getColName();
0572:                return fieldString;
0573:            }
0574:
0575:            public ModelField getField(String fieldName) {
0576:                if (fieldName == null)
0577:                    return null;
0578:                if (fieldsMap == null) {
0579:                    createFieldsMap();
0580:                }
0581:                ModelField modelField = (ModelField) fieldsMap.get(fieldName);
0582:                if (modelField == null) {
0583:                    // 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...
0584:                    // the purpose of the fieldsMap is for speed, but if failures are a little slower, no biggie
0585:                    createFieldsMap();
0586:                    modelField = (ModelField) fieldsMap.get(fieldName);
0587:                }
0588:                return modelField;
0589:            }
0590:
0591:            protected synchronized void createFieldsMap() {
0592:                Map tempMap = FastMap.newInstance();
0593:                for (int i = 0; i < fields.size(); i++) {
0594:                    ModelField field = (ModelField) fields.get(i);
0595:                    tempMap.put(field.name, field);
0596:                }
0597:                fieldsMap = tempMap;
0598:            }
0599:
0600:            public void addField(ModelField field) {
0601:                if (field == null)
0602:                    return;
0603:                field.setModelEntity(this );
0604:                this .fields.add(field);
0605:
0606:                if (field.isPk) {
0607:                    pks.add(field);
0608:                } else {
0609:                    nopks.add(field);
0610:                }
0611:            }
0612:
0613:            public ModelField removeField(int index) {
0614:                ModelField field = null;
0615:
0616:                field = (ModelField) fields.remove(index);
0617:                if (field == null)
0618:                    return null;
0619:
0620:                if (field.isPk) {
0621:                    pks.remove(field);
0622:                } else {
0623:                    nopks.remove(field);
0624:                }
0625:                return field;
0626:            }
0627:
0628:            public ModelField removeField(String fieldName) {
0629:                if (fieldName == null)
0630:                    return null;
0631:                ModelField field = null;
0632:
0633:                for (int i = 0; i < fields.size(); i++) {
0634:                    field = (ModelField) fields.get(i);
0635:                    if (field.name.equals(fieldName)) {
0636:                        fields.remove(i);
0637:                        if (field.isPk) {
0638:                            pks.remove(field);
0639:                        } else {
0640:                            nopks.remove(field);
0641:                        }
0642:                    }
0643:                    field = null;
0644:                }
0645:                return field;
0646:            }
0647:
0648:            public List getAllFieldNames() {
0649:                return getFieldNamesFromFieldVector(fields);
0650:            }
0651:
0652:            public List getPkFieldNames() {
0653:                return getFieldNamesFromFieldVector(pks);
0654:            }
0655:
0656:            public List getNoPkFieldNames() {
0657:                return getFieldNamesFromFieldVector(nopks);
0658:            }
0659:
0660:            public List getFieldNamesFromFieldVector(List modelFields) {
0661:                List nameList = FastList.newInstance();
0662:
0663:                if (modelFields == null || modelFields.size() <= 0)
0664:                    return nameList;
0665:                for (int i = 0; i < modelFields.size(); i++) {
0666:                    ModelField field = (ModelField) modelFields.get(i);
0667:
0668:                    nameList.add(field.name);
0669:                }
0670:                return nameList;
0671:            }
0672:
0673:            public int getRelationsSize() {
0674:                return this .relations.size();
0675:            }
0676:
0677:            public int getRelationsOneSize() {
0678:                int numRels = 0;
0679:                Iterator relationsIter = this .getRelationsIterator();
0680:                while (relationsIter.hasNext()) {
0681:                    ModelRelation modelRelation = (ModelRelation) relationsIter
0682:                            .next();
0683:                    if ("one".equals(modelRelation.getType())) {
0684:                        numRels++;
0685:                    }
0686:                }
0687:                return numRels;
0688:            }
0689:
0690:            public ModelRelation getRelation(int index) {
0691:                return (ModelRelation) this .relations.get(index);
0692:            }
0693:
0694:            public Iterator getRelationsIterator() {
0695:                return this .relations.iterator();
0696:            }
0697:
0698:            public ModelRelation getRelation(String relationName) {
0699:                if (relationName == null)
0700:                    return null;
0701:                for (int i = 0; i < relations.size(); i++) {
0702:                    ModelRelation relation = (ModelRelation) relations.get(i);
0703:                    if (relationName.equals(relation.title
0704:                            + relation.relEntityName))
0705:                        return relation;
0706:                }
0707:                return null;
0708:            }
0709:
0710:            public void addRelation(ModelRelation relation) {
0711:                relation.setModelEntity(this );
0712:                this .relations.add(relation);
0713:            }
0714:
0715:            public ModelRelation removeRelation(int index) {
0716:                return (ModelRelation) this .relations.remove(index);
0717:            }
0718:
0719:            public int getIndexesSize() {
0720:                return this .indexes.size();
0721:            }
0722:
0723:            public ModelIndex getIndex(int index) {
0724:                return (ModelIndex) this .indexes.get(index);
0725:            }
0726:
0727:            public Iterator getIndexesIterator() {
0728:                return this .indexes.iterator();
0729:            }
0730:
0731:            public ModelIndex getIndex(String indexName) {
0732:                if (indexName == null)
0733:                    return null;
0734:                for (int i = 0; i < indexes.size(); i++) {
0735:                    ModelIndex index = (ModelIndex) indexes.get(i);
0736:                    if (indexName.equals(index.getName()))
0737:                        return index;
0738:                }
0739:                return null;
0740:            }
0741:
0742:            public void addIndex(ModelIndex index) {
0743:                index.setModelEntity(this );
0744:                this .indexes.add(index);
0745:            }
0746:
0747:            public ModelIndex removeIndex(int index) {
0748:                return (ModelIndex) this .indexes.remove(index);
0749:            }
0750:
0751:            public int getViewEntitiesSize() {
0752:                return this .viewEntities.size();
0753:            }
0754:
0755:            public ModelViewEntity getViewEntity(String viewEntityName) {
0756:                return (ModelViewEntity) this .viewEntities.get(viewEntityName);
0757:            }
0758:
0759:            public Iterator getViewConvertorsIterator() {
0760:                return this .viewEntities.entrySet().iterator();
0761:            }
0762:
0763:            public void addViewEntity(ModelViewEntity view) {
0764:                this .viewEntities.put(view.getEntityName(), view);
0765:            }
0766:
0767:            public List convertToViewValues(String viewEntityName,
0768:                    GenericEntity entity) {
0769:                if (entity == null || entity == GenericEntity.NULL_ENTITY
0770:                        || entity == GenericValue.NULL_VALUE)
0771:                    return UtilMisc.toList(entity);
0772:                ModelViewEntity view = (ModelViewEntity) this .viewEntities
0773:                        .get(viewEntityName);
0774:                return view.convert(getEntityName(), entity);
0775:            }
0776:
0777:            public ModelViewEntity removeViewEntity(String viewEntityName) {
0778:                return (ModelViewEntity) this .viewEntities
0779:                        .remove(viewEntityName);
0780:            }
0781:
0782:            public ModelViewEntity removeViewEntity(ModelViewEntity viewEntity) {
0783:                return removeViewEntity(viewEntity.getEntityName());
0784:            }
0785:
0786:            public String nameString(List flds) {
0787:                return nameString(flds, ", ", "");
0788:            }
0789:
0790:            public String nameString(List flds, String separator,
0791:                    String afterLast) {
0792:                StringBuffer returnString = new StringBuffer();
0793:
0794:                if (flds.size() < 1) {
0795:                    return "";
0796:                }
0797:
0798:                int i = 0;
0799:
0800:                for (; i < flds.size() - 1; i++) {
0801:                    returnString.append(((ModelField) flds.get(i)).name);
0802:                    returnString.append(separator);
0803:                }
0804:                returnString.append(((ModelField) flds.get(i)).name);
0805:                returnString.append(afterLast);
0806:                return returnString.toString();
0807:            }
0808:
0809:            public String typeNameString(List flds) {
0810:                StringBuffer returnString = new StringBuffer();
0811:
0812:                if (flds.size() < 1) {
0813:                    return "";
0814:                }
0815:
0816:                int i = 0;
0817:
0818:                for (; i < flds.size() - 1; i++) {
0819:                    ModelField curField = (ModelField) flds.get(i);
0820:                    returnString.append(curField.type);
0821:                    returnString.append(" ");
0822:                    returnString.append(curField.name);
0823:                    returnString.append(", ");
0824:                }
0825:                ModelField curField = (ModelField) flds.get(i);
0826:                returnString.append(curField.type);
0827:                returnString.append(" ");
0828:                returnString.append(curField.name);
0829:                return returnString.toString();
0830:            }
0831:
0832:            public String fieldNameString() {
0833:                return fieldNameString(", ", "");
0834:            }
0835:
0836:            public String fieldNameString(String separator, String afterLast) {
0837:                return nameString(fields, separator, afterLast);
0838:            }
0839:
0840:            public String fieldTypeNameString() {
0841:                return typeNameString(fields);
0842:            }
0843:
0844:            public String primKeyClassNameString() {
0845:                return typeNameString(pks);
0846:            }
0847:
0848:            public String pkNameString() {
0849:                return pkNameString(", ", "");
0850:            }
0851:
0852:            public String pkNameString(String separator, String afterLast) {
0853:                return nameString(pks, separator, afterLast);
0854:            }
0855:
0856:            public String nonPkNullList() {
0857:                return fieldsStringList(fields, "null", ", ", false, true);
0858:            }
0859:
0860:            public String fieldsStringList(List flds, String eachString,
0861:                    String separator) {
0862:                return fieldsStringList(flds, eachString, separator, false,
0863:                        false);
0864:            }
0865:
0866:            public String fieldsStringList(List flds, String eachString,
0867:                    String separator, boolean appendIndex) {
0868:                return fieldsStringList(flds, eachString, separator,
0869:                        appendIndex, false);
0870:            }
0871:
0872:            public String fieldsStringList(List flds, String eachString,
0873:                    String separator, boolean appendIndex, boolean onlyNonPK) {
0874:                StringBuffer returnString = new StringBuffer();
0875:
0876:                if (flds.size() < 1) {
0877:                    return "";
0878:                }
0879:
0880:                int i = 0;
0881:
0882:                for (; i < flds.size(); i++) {
0883:                    if (onlyNonPK && ((ModelField) flds.get(i)).isPk)
0884:                        continue;
0885:                    returnString.append(eachString);
0886:                    if (appendIndex)
0887:                        returnString.append(i + 1);
0888:                    if (i < flds.size() - 1)
0889:                        returnString.append(separator);
0890:                }
0891:                return returnString.toString();
0892:            }
0893:
0894:            public String colNameString(List flds) {
0895:                return colNameString(flds, ", ", "", false);
0896:            }
0897:
0898:            public String colNameString(List flds, String separator,
0899:                    String afterLast, boolean alias) {
0900:                StringBuffer returnString = new StringBuffer();
0901:
0902:                if (flds.size() < 1) {
0903:                    return "";
0904:                }
0905:
0906:                Iterator fldsIt = flds.iterator();
0907:                while (fldsIt.hasNext()) {
0908:                    ModelField field = (ModelField) fldsIt.next();
0909:                    returnString.append(field.colName);
0910:                    if (fldsIt.hasNext()) {
0911:                        returnString.append(separator);
0912:                    }
0913:                }
0914:
0915:                returnString.append(afterLast);
0916:                return returnString.toString();
0917:            }
0918:
0919:            public String classNameString(List flds) {
0920:                return classNameString(flds, ", ", "");
0921:            }
0922:
0923:            public String classNameString(List flds, String separator,
0924:                    String afterLast) {
0925:                StringBuffer returnString = new StringBuffer();
0926:
0927:                if (flds.size() < 1) {
0928:                    return "";
0929:                }
0930:
0931:                int i = 0;
0932:
0933:                for (; i < flds.size() - 1; i++) {
0934:                    returnString.append(ModelUtil
0935:                            .upperFirstChar(((ModelField) flds.get(i)).name));
0936:                    returnString.append(separator);
0937:                }
0938:                returnString.append(ModelUtil.upperFirstChar(((ModelField) flds
0939:                        .get(i)).name));
0940:                returnString.append(afterLast);
0941:                return returnString.toString();
0942:            }
0943:
0944:            public String finderQueryString(List flds) {
0945:                StringBuffer returnString = new StringBuffer();
0946:
0947:                if (flds.size() < 1) {
0948:                    return "";
0949:                }
0950:                int i = 0;
0951:
0952:                for (; i < flds.size() - 1; i++) {
0953:                    returnString.append(((ModelField) flds.get(i)).colName);
0954:                    returnString.append(" like {");
0955:                    returnString.append(i);
0956:                    returnString.append("} AND ");
0957:                }
0958:                returnString.append(((ModelField) flds.get(i)).colName);
0959:                returnString.append(" like {");
0960:                returnString.append(i);
0961:                returnString.append("}");
0962:                return returnString.toString();
0963:            }
0964:
0965:            public String httpArgList(List flds) {
0966:                StringBuffer returnString = new StringBuffer();
0967:
0968:                if (flds.size() < 1) {
0969:                    return "";
0970:                }
0971:                int i = 0;
0972:
0973:                for (; i < flds.size() - 1; i++) {
0974:                    returnString.append("\"");
0975:                    returnString.append(tableName);
0976:                    returnString.append("_");
0977:                    returnString.append(((ModelField) flds.get(i)).colName);
0978:                    returnString.append("=\" + ");
0979:                    returnString.append(((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(((ModelField) flds.get(i)).name);
0988:                return returnString.toString();
0989:            }
0990:
0991:            public String httpArgListFromClass(List flds) {
0992:                StringBuffer returnString = new StringBuffer();
0993:
0994:                if (flds.size() < 1) {
0995:                    return "";
0996:                }
0997:
0998:                int i = 0;
0999:
1000:                for (; i < flds.size() - 1; i++) {
1001:                    returnString.append("\"");
1002:                    returnString.append(tableName);
1003:                    returnString.append("_");
1004:                    returnString.append(((ModelField) flds.get(i)).colName);
1005:                    returnString.append("=\" + ");
1006:                    returnString.append(ModelUtil.lowerFirstChar(entityName));
1007:                    returnString.append(".get");
1008:                    returnString.append(ModelUtil
1009:                            .upperFirstChar(((ModelField) flds.get(i)).name));
1010:                    returnString.append("() + \"&\" + ");
1011:                }
1012:                returnString.append("\"");
1013:                returnString.append(tableName);
1014:                returnString.append("_");
1015:                returnString.append(((ModelField) flds.get(i)).colName);
1016:                returnString.append("=\" + ");
1017:                returnString.append(ModelUtil.lowerFirstChar(entityName));
1018:                returnString.append(".get");
1019:                returnString.append(ModelUtil.upperFirstChar(((ModelField) flds
1020:                        .get(i)).name));
1021:                returnString.append("()");
1022:                return returnString.toString();
1023:            }
1024:
1025:            public String httpArgListFromClass(List flds,
1026:                    String entityNameSuffix) {
1027:                StringBuffer returnString = new StringBuffer();
1028:
1029:                if (flds.size() < 1) {
1030:                    return "";
1031:                }
1032:
1033:                int i = 0;
1034:
1035:                for (; i < flds.size() - 1; i++) {
1036:                    returnString.append("\"");
1037:                    returnString.append(tableName);
1038:                    returnString.append("_");
1039:                    returnString.append(((ModelField) flds.get(i)).colName);
1040:                    returnString.append("=\" + ");
1041:                    returnString.append(ModelUtil.lowerFirstChar(entityName));
1042:                    returnString.append(entityNameSuffix);
1043:                    returnString.append(".get");
1044:                    returnString.append(ModelUtil
1045:                            .upperFirstChar(((ModelField) flds.get(i)).name));
1046:                    returnString.append("() + \"&\" + ");
1047:                }
1048:                returnString.append("\"");
1049:                returnString.append(tableName);
1050:                returnString.append("_");
1051:                returnString.append(((ModelField) flds.get(i)).colName);
1052:                returnString.append("=\" + ");
1053:                returnString.append(ModelUtil.lowerFirstChar(entityName));
1054:                returnString.append(entityNameSuffix);
1055:                returnString.append(".get");
1056:                returnString.append(ModelUtil.upperFirstChar(((ModelField) flds
1057:                        .get(i)).name));
1058:                returnString.append("()");
1059:                return returnString.toString();
1060:            }
1061:
1062:            public String httpRelationArgList(List flds, ModelRelation relation) {
1063:                StringBuffer returnString = new StringBuffer();
1064:
1065:                if (flds.size() < 1) {
1066:                    return "";
1067:                }
1068:
1069:                int i = 0;
1070:
1071:                for (; i < flds.size() - 1; i++) {
1072:                    ModelKeyMap keyMap = relation
1073:                            .findKeyMapByRelated(((ModelField) flds.get(i)).name);
1074:
1075:                    if (keyMap != null) {
1076:                        returnString.append("\"");
1077:                        returnString.append(tableName);
1078:                        returnString.append("_");
1079:                        returnString.append(((ModelField) flds.get(i)).colName);
1080:                        returnString.append("=\" + ");
1081:                        returnString
1082:                                .append(ModelUtil
1083:                                        .lowerFirstChar(relation.mainEntity.entityName));
1084:                        returnString.append(".get");
1085:                        returnString.append(ModelUtil
1086:                                .upperFirstChar(keyMap.fieldName));
1087:                        returnString.append("() + \"&\" + ");
1088:                    } else {
1089:                        Debug
1090:                                .logWarning(
1091:                                        "-- -- ENTITYGEN ERROR:httpRelationArgList: Related Key in Key Map not found for name: "
1092:                                                + ((ModelField) flds.get(i)).name
1093:                                                + " related entity: "
1094:                                                + relation.relEntityName
1095:                                                + " main entity: "
1096:                                                + relation.mainEntity.entityName
1097:                                                + " type: " + relation.type,
1098:                                        module);
1099:                    }
1100:                }
1101:                ModelKeyMap keyMap = relation
1102:                        .findKeyMapByRelated(((ModelField) flds.get(i)).name);
1103:
1104:                if (keyMap != null) {
1105:                    returnString.append("\"");
1106:                    returnString.append(tableName);
1107:                    returnString.append("_");
1108:                    returnString.append(((ModelField) flds.get(i)).colName);
1109:                    returnString.append("=\" + ");
1110:                    returnString.append(ModelUtil
1111:                            .lowerFirstChar(relation.mainEntity.entityName));
1112:                    returnString.append(".get");
1113:                    returnString.append(ModelUtil
1114:                            .upperFirstChar(keyMap.fieldName));
1115:                    returnString.append("()");
1116:                } else {
1117:                    Debug
1118:                            .logWarning(
1119:                                    "-- -- ENTITYGEN ERROR:httpRelationArgList: Related Key in Key Map not found for name: "
1120:                                            + ((ModelField) flds.get(i)).name
1121:                                            + " related entity: "
1122:                                            + relation.relEntityName
1123:                                            + " main entity: "
1124:                                            + relation.mainEntity.entityName
1125:                                            + " type: " + relation.type, module);
1126:                }
1127:                return returnString.toString();
1128:            }
1129:
1130:            /*
1131:             public String httpRelationArgList(ModelRelation relation) {
1132:             String returnString = "";
1133:             if(relation.keyMaps.size() < 1) { return ""; }
1134:
1135:             int i = 0;
1136:             for(; i < relation.keyMaps.size() - 1; i++) {
1137:             ModelKeyMap keyMap = (ModelKeyMap)relation.keyMaps.get(i);
1138:             if(keyMap != null)
1139:             returnString = returnString + "\"" + tableName + "_" + keyMap.relColName + "=\" + " + ModelUtil.lowerFirstChar(relation.mainEntity.entityName) + ".get" + ModelUtil.upperFirstChar(keyMap.fieldName) + "() + \"&\" + ";
1140:             }
1141:             ModelKeyMap keyMap = (ModelKeyMap)relation.keyMaps.get(i);
1142:             returnString = returnString + "\"" + tableName + "_" + keyMap.relColName + "=\" + " + ModelUtil.lowerFirstChar(relation.mainEntity.entityName) + ".get" + ModelUtil.upperFirstChar(keyMap.fieldName) + "()";
1143:             return returnString;
1144:             }
1145:             */
1146:            public String typeNameStringRelatedNoMapped(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:                if (relation
1157:                        .findKeyMapByRelated(((ModelField) flds.get(i)).name) == null) {
1158:                    returnString.append(((ModelField) flds.get(i)).type);
1159:                    returnString.append(" ");
1160:                    returnString.append(((ModelField) flds.get(i)).name);
1161:                }
1162:                i++;
1163:                for (; i < flds.size(); i++) {
1164:                    if (relation
1165:                            .findKeyMapByRelated(((ModelField) flds.get(i)).name) == null) {
1166:                        if (returnString.length() > 0)
1167:                            returnString.append(", ");
1168:                        returnString.append(((ModelField) flds.get(i)).type);
1169:                        returnString.append(" ");
1170:                        returnString.append(((ModelField) flds.get(i)).name);
1171:                    }
1172:                }
1173:                return returnString.toString();
1174:            }
1175:
1176:            public String typeNameStringRelatedAndMain(List flds,
1177:                    ModelRelation relation) {
1178:                StringBuffer returnString = new StringBuffer();
1179:
1180:                if (flds.size() < 1) {
1181:                    return "";
1182:                }
1183:
1184:                int i = 0;
1185:
1186:                for (; i < flds.size() - 1; i++) {
1187:                    ModelKeyMap keyMap = relation
1188:                            .findKeyMapByRelated(((ModelField) flds.get(i)).name);
1189:
1190:                    if (keyMap != null) {
1191:                        returnString.append(keyMap.fieldName);
1192:                        returnString.append(", ");
1193:                    } else {
1194:                        returnString.append(((ModelField) flds.get(i)).name);
1195:                        returnString.append(", ");
1196:                    }
1197:                }
1198:                ModelKeyMap keyMap = relation
1199:                        .findKeyMapByRelated(((ModelField) flds.get(i)).name);
1200:
1201:                if (keyMap != null)
1202:                    returnString.append(keyMap.fieldName);
1203:                else
1204:                    returnString.append(((ModelField) flds.get(i)).name);
1205:                return returnString.toString();
1206:            }
1207:
1208:            public int compareTo(Object obj) {
1209:                ModelEntity otherModelEntity = (ModelEntity) obj;
1210:
1211:                /* This DOESN'T WORK, so forget it... using two passes
1212:                 //sort list by fk dependencies
1213:                 
1214:                 if (this.getEntityName().equals(otherModelEntity.getEntityName())) {
1215:                 return 0;
1216:                 }
1217:
1218:                 //look through relations for dependencies from this entity to the other
1219:                 Iterator relationsIter = this.getRelationsIterator();
1220:                 while (relationsIter.hasNext()) {
1221:                 ModelRelation modelRelation = (ModelRelation) relationsIter.next();
1222:
1223:                 if ("one".equals(modelRelation.getType()) && modelRelation.getRelEntityName().equals(otherModelEntity.getEntityName())) {
1224:                 //this entity is dependent on the other entity, so put that entity earlier in the list
1225:                 return -1;
1226:                 }
1227:                 }
1228:                 
1229:                 //look through relations for dependencies from the other to this entity
1230:                 Iterator otherRelationsIter = otherModelEntity.getRelationsIterator();
1231:                 while (otherRelationsIter.hasNext()) {
1232:                 ModelRelation modelRelation = (ModelRelation) otherRelationsIter.next();
1233:
1234:                 if ("one".equals(modelRelation.getType()) && modelRelation.getRelEntityName().equals(this.getEntityName())) {
1235:                 //the other entity is dependent on this entity, so put that entity later in the list
1236:                 return 1;
1237:                 }
1238:                 }
1239:                 
1240:                 return 0;
1241:                 */
1242:
1243:                return this .getEntityName().compareTo(
1244:                        otherModelEntity.getEntityName());
1245:            }
1246:
1247:            public void convertFieldMapInPlace(Map inContext,
1248:                    GenericDelegator delegator) {
1249:                Iterator modelFields = this .getFieldsIterator();
1250:                while (modelFields.hasNext()) {
1251:                    ModelField modelField = (ModelField) modelFields.next();
1252:                    String fieldName = modelField.getName();
1253:                    Object oldValue = inContext.get(fieldName);
1254:                    if (oldValue != null) {
1255:                        inContext.put(fieldName, this .convertFieldValue(
1256:                                modelField, oldValue, delegator));
1257:                    }
1258:                }
1259:            }
1260:
1261:            public Object convertFieldValue(String fieldName, Object value,
1262:                    GenericDelegator delegator) {
1263:                ModelField modelField = this .getField(fieldName);
1264:                if (modelField == null) {
1265:                    String errMsg = "Could not convert field value: could not find an entity field for the name: ["
1266:                            + fieldName
1267:                            + "] on the ["
1268:                            + this .getEntityName()
1269:                            + "] entity.";
1270:                    throw new IllegalArgumentException(errMsg);
1271:                }
1272:                return convertFieldValue(modelField, value, delegator);
1273:            }
1274:
1275:            public Object convertFieldValue(ModelField modelField,
1276:                    Object value, GenericDelegator delegator) {
1277:                if (value == null || value == GenericEntity.NULL_FIELD) {
1278:                    return null;
1279:                }
1280:                String fieldJavaType = null;
1281:                try {
1282:                    fieldJavaType = delegator.getEntityFieldType(this ,
1283:                            modelField.getType()).getJavaType();
1284:                } catch (GenericEntityException e) {
1285:                    String errMsg = "Could not convert field value: could not find Java type for the field: ["
1286:                            + modelField.getName()
1287:                            + "] on the ["
1288:                            + this .getEntityName()
1289:                            + "] entity: "
1290:                            + e.toString();
1291:                    Debug.logError(e, errMsg, module);
1292:                    throw new IllegalArgumentException(errMsg);
1293:                }
1294:                try {
1295:                    return ObjectType.simpleTypeConvert(value, fieldJavaType,
1296:                            null, null, false);
1297:                } catch (GeneralException e) {
1298:                    String errMsg = "Could not convert field value for the field: ["
1299:                            + modelField.getName()
1300:                            + "] on the ["
1301:                            + this .getEntityName()
1302:                            + "] entity to the ["
1303:                            + fieldJavaType
1304:                            + "] type for the value ["
1305:                            + value
1306:                            + "]: " + e.toString();
1307:                    Debug.logError(e, errMsg, module);
1308:                    throw new IllegalArgumentException(errMsg);
1309:                }
1310:            }
1311:
1312:            /**
1313:             * @return Returns the noAutoStamp.
1314:             */
1315:            public boolean getNoAutoStamp() {
1316:                return this .noAutoStamp;
1317:            }
1318:
1319:            /**
1320:             * @param noAutoStamp The noAutoStamp to set.
1321:             */
1322:            public void setNoAutoStamp(boolean noAutoStamp) {
1323:                this .noAutoStamp = noAutoStamp;
1324:            }
1325:
1326:            public String toString() {
1327:                return "ModelEntity[" + getEntityName() + "]";
1328:            }
1329:
1330:            public Element toXmlElement(Document document, String packageName) {
1331:                if (UtilValidate.isNotEmpty(this .getPackageName())
1332:                        && !packageName.equals(this .getPackageName())) {
1333:                    Debug.logWarning("Export EntityModel XML Element ["
1334:                            + this .getEntityName() + "] with a NEW package - "
1335:                            + packageName, module);
1336:                }
1337:
1338:                Element root = document.createElement("entity");
1339:                root.setAttribute("entity-name", this .getEntityName());
1340:                if (!this .getEntityName().equals(
1341:                        ModelUtil.dbNameToClassName(this .getPlainTableName()))
1342:                        || !ModelUtil.javaNameToDbName(this .getEntityName())
1343:                                .equals(this .getPlainTableName())) {
1344:                    root.setAttribute("table-name", this .getPlainTableName());
1345:                }
1346:                root.setAttribute("package-name", packageName);
1347:
1348:                // additional elements
1349:                if (UtilValidate.isNotEmpty(this .getDefaultResourceName())) {
1350:                    root.setAttribute("default-resource-name", this 
1351:                            .getDefaultResourceName());
1352:                }
1353:
1354:                if (UtilValidate.isNotEmpty(this .getDependentOn())) {
1355:                    root.setAttribute("dependent-on", this .getDependentOn());
1356:                }
1357:
1358:                if (this .getDoLock()) {
1359:                    root.setAttribute("enable-lock", "true");
1360:                }
1361:
1362:                if (this .getNoAutoStamp()) {
1363:                    root.setAttribute("no-auto-stamp", "true");
1364:                }
1365:
1366:                if (this .getNeverCache()) {
1367:                    root.setAttribute("never-cache", "true");
1368:                }
1369:
1370:                if (!this .getAutoClearCache()) {
1371:                    root.setAttribute("auto-clear-cache", "false");
1372:                }
1373:
1374:                if (UtilValidate.isNotEmpty(this .getTitle())) {
1375:                    root.setAttribute("title", this .getTitle());
1376:                }
1377:
1378:                if (UtilValidate.isNotEmpty(this .getCopyright())) {
1379:                    root.setAttribute("copyright", this .getCopyright());
1380:                }
1381:
1382:                if (UtilValidate.isNotEmpty(this .getAuthor())) {
1383:                    root.setAttribute("author", this .getAuthor());
1384:                }
1385:
1386:                if (UtilValidate.isNotEmpty(this .getVersion())) {
1387:                    root.setAttribute("version", this .getVersion());
1388:                }
1389:
1390:                // description element
1391:                if (UtilValidate.isNotEmpty(this .getDescription())) {
1392:                    UtilXml.addChildElementValue(root, "description", this 
1393:                            .getDescription(), document);
1394:                }
1395:
1396:                // append field elements
1397:                Iterator fieldIter = this .getFieldsIterator();
1398:                while (fieldIter != null && fieldIter.hasNext()) {
1399:                    ModelField field = (ModelField) fieldIter.next();
1400:                    if (!field.getIsAutoCreatedInternal()) {
1401:                        root.appendChild(field.toXmlElement(document));
1402:                    }
1403:                }
1404:
1405:                // append PK elements
1406:                Iterator pkIter = this .getPksIterator();
1407:                while (pkIter != null && pkIter.hasNext()) {
1408:                    ModelField pk = (ModelField) pkIter.next();
1409:                    Element pkey = document.createElement("prim-key");
1410:                    pkey.setAttribute("field", pk.getName());
1411:                    root.appendChild(pkey);
1412:                }
1413:
1414:                // append relation elements
1415:                Iterator relIter = this .getRelationsIterator();
1416:                while (relIter != null && relIter.hasNext()) {
1417:                    ModelRelation rel = (ModelRelation) relIter.next();
1418:
1419:                }
1420:
1421:                // append index elements
1422:                Iterator idxIter = this .getIndexesIterator();
1423:                while (idxIter != null && idxIter.hasNext()) {
1424:                    ModelIndex idx = (ModelIndex) idxIter.next();
1425:                    root.appendChild(idx.toXmlElement(document));
1426:
1427:                }
1428:
1429:                return root;
1430:            }
1431:
1432:            public Element toXmlElement(Document document) {
1433:                return this.toXmlElement(document, this.getPackageName());
1434:            }
1435:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.