Source Code Cross Referenced for TableGenerate.java in  » Database-ORM » SimpleORM » simpleorm » quickstart » 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 » Database ORM » SimpleORM » simpleorm.quickstart 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package simpleorm.quickstart;
002:
003:        import org.apache.commons.sql.model.Column;
004:        import org.apache.commons.sql.model.Database;
005:        import org.apache.commons.sql.model.ForeignKey;
006:        import org.apache.commons.sql.model.Reference;
007:        import org.apache.commons.sql.model.Table;
008:
009:        import java.io.File;
010:        import java.io.FileWriter;
011:        import java.io.IOException;
012:        import java.io.Writer;
013:
014:        import java.util.Date;
015:        import java.util.HashMap;
016:        import java.util.Iterator;
017:        import java.util.Map;
018:        import java.util.Vector;
019:
020:        /**
021:         * @author <a href="mailto:richard.schmidt@inform6.com">Richard Schmidt</a>
022:         *
023:         */
024:        final class TableGenerate {
025:            Table table;
026:            INiceNameFormatter niceName;
027:            Database db;
028:            File root;
029:
030:            /**Name of the classes and packages generated*/
031:            String classNameBase;
032:            String classNameBR;
033:            String packageName;
034:
035:            /**map of ColumnGenerate objects Keyed by ColumnName*/
036:            Map hmColumns = new HashMap();
037:
038:            /**Store the ColumnGenerate objects*/
039:            Vector vtrColumns = new Vector();
040:
041:            /**Store the primary ColumnGenerate  here*/
042:            Vector vtrPrimaryColumns = new Vector();
043:
044:            /**Store the ForeignKeygenerate objects here*/
045:            Vector vtrForeignKeys = new Vector();
046:
047:            public TableGenerate(Database db, Table table,
048:                    INiceNameFormatter niceName, String packageName, File root) {
049:                this .table = table;
050:                this .niceName = niceName;
051:                this .db = db;
052:                this .root = root;
053:                classNameBase = niceName.niceNameForTable(table.getName())
054:                        + "_gen";
055:                classNameBR = niceName.niceNameForTable(table.getName());
056:                this .packageName = packageName;
057:
058:                loadColumns();
059:                loadForeignKeys();
060:            }
061:
062:            /** put all the columns into the hash map*/
063:            public void loadColumns() {
064:                //Load up the fields
065:                Iterator columnsEnum = table.getColumns().iterator();
066:
067:                while (columnsEnum.hasNext()) {
068:                    ColumnGenerate column = new ColumnGenerate(table,
069:                            (Column) columnsEnum.next(), niceName);
070:                    hmColumns.put(column.column.getName(), column);
071:                    vtrColumns.add(column);
072:
073:                    if (column.isPrimary()) {
074:                        vtrPrimaryColumns.add(column);
075:                    }
076:                }
077:            }
078:
079:            /**Add the foreign keys to the hash map*/
080:            public void loadForeignKeys() {
081:                //Now process the foreign Keys
082:                Iterator keysEnum = table.getForeignKeys().iterator();
083:
084:                while (keysEnum.hasNext()) {
085:                    ForeignKey key = (ForeignKey) keysEnum.next();
086:                    ForeignKeyGenerate keyGen = new ForeignKeyGenerate(table,
087:                            key, niceName, hmColumns);
088:
089:                    //Now add the referenceKey
090:                    vtrForeignKeys.add(keyGen);
091:                }
092:            }
093:
094:            /**
095:             * Generates the base class file.
096:             * File will always be generated
097:             */
098:            public void generateBaseClass() throws IOException {
099:                FileWriter out = new FileWriter(new File(root, classNameBase
100:                        + ".java"));
101:
102:                //Output all the header stuff
103:                out.write("package " + packageName + ";\n");
104:                out.write("import simpleorm.core.*;\n");
105:                out.write("import simpleorm.properties.SPropertyValue;\n");
106:                out.write("import java.math.BigDecimal;\n");
107:                out.write("import java.util.Date;\n");
108:                out
109:                        .write("\n/**Base class of table "
110:                                + table.getName()
111:                                + ".<br>\n"
112:                                + "*Do not edit as will be regenerated by running SimpleORMGenerator\n"
113:                                + "*Generated on " + new Date().toString()
114:                                + "\n*/ \n\n");
115:
116:                out.write("abstract class " + classNameBase
117:                        + " extends SRecordInstance");
118:
119:                out.write(" implements java.io.Serializable {\n\n");
120:                out
121:                        .write("   public static final SRecordMeta meta =\n      new SRecordMeta("
122:                                + classNameBR
123:                                + ".class, \""
124:                                + table.getName()
125:                                + "\");\n\n");
126:
127:                //Now generate the fields
128:                out.write("//Columns in table\n");
129:
130:                for (int i = 0; i < vtrColumns.size(); i++) {
131:                    ColumnGenerate field = (ColumnGenerate) vtrColumns.get(i);
132:                    field.outputStaticFields(out);
133:                }
134:
135:                //And the getters and setters
136:                out.write("//Column getters and setters\n");
137:
138:                for (int i = 0; i < vtrColumns.size(); i++) {
139:                    ColumnGenerate field = (ColumnGenerate) vtrColumns.get(i);
140:                    field.outputGettersAndSetters(out);
141:                }
142:
143:                //The foreign keys, only getters and setters
144:                if (vtrForeignKeys.size() > 0) {
145:                    out.write("//Foreign key getters and setters\n");
146:                }
147:                for (int i = 0; i < vtrForeignKeys.size(); i++) {
148:                    ForeignKeyGenerate field = (ForeignKeyGenerate) vtrForeignKeys
149:                            .get(i);
150:                    field.outputGettersAndSetters(out);
151:                }
152:
153:                //the find or create methods
154:                out.write("//Find and create\n");
155:                genFindOrCreate(out);
156:                getRefFindOrCreate(out);
157:
158:                //now the end stuff
159:                out.write("//specializes abstract method\n");
160:                out.write("   public SRecordMeta getMeta() {\n");
161:                out.write("       return meta;\n");
162:                out.write("   }\n");
163:                out.write("}\n");
164:                out.close();
165:            }
166:
167:            /** Generate the Business Rules class.
168:             * File will only be generated if file does not exist
169:             */
170:            public void generateBRClass() throws IOException {
171:
172:                File brFile = new File(root, classNameBR + ".java");
173:
174:                if (brFile.exists() == false) {
175:                    FileWriter out = new FileWriter(brFile);
176:
177:                    //Output all the header stuff
178:                    out.write("package " + packageName + ";\n");
179:                    out.write("import simpleorm.core.*;\n");
180:                    out.write("import simpleorm.properties.SPropertyValue;\n");
181:                    out
182:                            .write("\n/**Business rules class for table "
183:                                    + table.getName()
184:                                    + ".<br>\n"
185:                                    + "*Will not be regenerated by SimpleORMGenerator, add any business rules "
186:                                    + "to this class\n*/\n\n");
187:                    out.write("public class " + classNameBR + " extends "
188:                            + classNameBase);
189:
190:                    out.write(" implements java.io.Serializable {\n\n");
191:                    out.write("}\n");
192:                    out.close();
193:                }
194:            }
195:
196:            /**Nice type casted FindOrCreate() for column parameters
197:            public static Matter findOrCreate( String _fldClientno, String _fldMatterNo ){
198:              return (Matter) meta.findOrCreate( new Object[] {new String( _fldClientno), new String( _fldMatterNo)});
199:            }    */
200:            private void genFindOrCreate(Writer out) throws IOException {
201:                Vector vtrParameters = new Vector();
202:
203:                Iterator pKeys = table.getPrimaryKeyColumns().iterator();
204:
205:                //find the column matching each primary keys and add its paramters to the list.
206:                while (pKeys.hasNext()) {
207:                    Column key = (Column) pKeys.next();
208:                    ColumnGenerate column = (ColumnGenerate) hmColumns.get(key
209:                            .getName());
210:                    vtrParameters.add(column.getParameters());
211:                }
212:
213:                //Build up a string of the paramter type and variables
214:                //eg "int _EmployeeID, SFieldReference _Department"
215:                String params = "";
216:
217:                //and a string of object params
218:                //eg "new Integer( EmployeeID), _Department
219:                String paramsVarables = "";
220:
221:                for (int i = 0; i < vtrParameters.size(); i++) {
222:                    AParam param = (AParam) vtrParameters.get(i);
223:                    params += param.methodParam;
224:                    paramsVarables += param.objectParam;
225:
226:                    if (i < (vtrParameters.size() - 1)) {
227:                        params += ", ";
228:                        paramsVarables += ", ";
229:                    }
230:                }
231:
232:                //Now we can create the final string
233:                String str = "   public static " + classNameBR
234:                        + " findOrCreate( " + params + " ){\n"
235:                        + "      return (" + classNameBR
236:                        + ") meta.findOrCreate( new Object[] {"
237:                        + paramsVarables + "});\n   }\n";
238:
239:                out.write(str);
240:            }
241:
242:            /**
243:             * Nice type casted FindOrCreate() using reference fields
244:             *
245:             *
246:             *
247:             */
248:            private void getRefFindOrCreate(Writer out) throws IOException {
249:                //Look for those foreign keys that only use primary columns 
250:                for (int i = 0; i < vtrForeignKeys.size(); i++) {
251:                    ForeignKeyGenerate key = (ForeignKeyGenerate) vtrForeignKeys
252:                            .elementAt(i);
253:
254:                    if (key.isPrimary()) {
255:                        key.outputFindOrCreate(out);
256:                    }
257:                }
258:            }
259:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.