Source Code Cross Referenced for ForeignKeyGenerate.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.ForeignKey;
005:        import org.apache.commons.sql.model.Reference;
006:        import org.apache.commons.sql.model.Table;
007:
008:        import java.io.IOException;
009:        import java.io.Writer;
010:
011:        import java.util.Iterator;
012:        import java.util.List;
013:        import java.util.Map;
014:        import java.util.Vector;
015:
016:        /**
017:         * @author <a href="mailto:richard.schmidt@inform6.com">Richard Schmidt</a>
018:         *
019:         */
020:        final class ForeignKeyGenerate {
021:            public ForeignKey key;
022:            public Table table;
023:            INiceNameFormatter niceName;
024:            Map hmColumns; //key = column name, object = ColumnGenerate;
025:
026:            public ForeignKeyGenerate(Table table, ForeignKey key,
027:                    INiceNameFormatter niceName, Map columns) {
028:                this .key = key;
029:                this .table = table;
030:                this .niceName = niceName;
031:                this .hmColumns = columns;
032:            }
033:
034:            /**Never used*/
035:            public void outputStaticFields(Writer out) throws IOException {
036:            }
037:
038:            /**
039:            Need to generate something like this:
040:            public Task get_refTask(){
041:                    try {
042:                        return Task.findOrCreate(get_fldClientno(), get_fldMatterno(),
043:                            get_fldTaskno());
044:                    } catch (SException e) {
045:                        if (e.getMessage().indexOf("Null Primary key") > 0) {
046:                            return null;
047:                        }
048:
049:                        throw e;
050:                    }}
051:            public void set_refTask( Task value){
052:                      set_fldClientno( value.get_fldClientno());
053:                       set_fldMatterno( value.get_fldMatterno());
054:                       set_fldTaskno( value.get_fldTaskno());
055:            }
056:             */
057:            public void outputGettersAndSetters(Writer out) throws IOException {
058:                String foreignTableName = niceName.niceNameForTable(key
059:                        .getForeignTable());
060:                String keyName = niceName.niceNameForForeignKey(
061:                        table.getName(), key.getForeignTable());
062:
063:                //Do the getter
064:                //public Task get_refTask(){ 		
065:                out.write("   public " + foreignTableName + " get_" + keyName
066:                        + "(){\n");
067:
068:                //      try {
069:                out.write("     try{\n");
070:
071:                //return Task.findOrCreate( get_fldClientno(), get_fldMatterno(), get_fldTaskno());        
072:                //Assumes that references will be in primary key order!
073:                out.write("        return " + foreignTableName
074:                        + ".findOrCreate(");
075:
076:                Iterator itt = key.getReferences().iterator();
077:
078:                while (itt.hasNext()) {
079:                    Reference ref = (Reference) itt.next();
080:                    out.write("get_"
081:                            + niceName.niceNameForColumn(table.getName(), ref
082:                                    .getLocal()) + "()");
083:
084:                    if (itt.hasNext()) {
085:                        out.write(",");
086:                    }
087:                }
088:
089:                out.write(");\n");
090:
091:                //        } catch (SException e) {
092:                //            if (e.getMessage().indexOf("Null Primary key") > 0) {
093:                //                return null;
094:                //            }
095:                //            throw e;
096:                //    	}		
097:                out.write("     } catch (SException e) {\n");
098:                out
099:                        .write("        if (e.getMessage().indexOf(\"Null Primary key\") > 0) {\n");
100:                out.write("          return null;\n");
101:                out.write("        }\n");
102:                out.write("        throw e;\n");
103:                out.write("     }\n");
104:                out.write("   }\n");
105:
106:                //Now do the setter		
107:                //public void set_refTask( Task value){ 
108:                out.write("   public void set_" + keyName + "( "
109:                        + foreignTableName + " value){\n");
110:                itt = key.getReferences().iterator();
111:
112:                while (itt.hasNext()) {
113:                    Reference ref = (Reference) itt.next();
114:
115:                    //set_fldClientno( value.get_fldClientno());
116:                    out.write("      set_"
117:                            + niceName.niceNameForColumn(table.getName(), ref
118:                                    .getLocal())
119:                            + "( value.get_"
120:                            + niceName.niceNameForColumn(key.getForeignTable(),
121:                                    ref.getForeign()) + "());\n");
122:                }
123:
124:                ;
125:                out.write("   }\n\n");
126:            }
127:
128:            //    /**example: Department fldRefDepartment"*/
129:            //    public AParam getParameters() {
130:            //        String methodParam = niceName.niceNameForTable(key.getForeignTable()) +
131:            //            " _ref" +
132:            //            niceName.niceNameForForeignKey(table.getName(),
133:            //                key.getForeignTable());
134:            //
135:            //        String objectParam = " _ref" +
136:            //            niceName.niceNameForForeignKey(table.getName(),
137:            //                key.getForeignTable());
138:            //
139:            //        return new AParam(methodParam, objectParam);
140:            //    }
141:
142:            public boolean isPrimary() {
143:                List list = key.getReferences();
144:                Iterator itt = list.iterator();
145:
146:                while (itt.hasNext()) {
147:                    Reference ref = (Reference) itt.next();
148:                    String column = ref.getLocal();
149:
150:                    if (table.findColumn(column).isPrimaryKey() == false) {
151:                        return false;
152:                    }
153:                }
154:
155:                return true;
156:            }
157:
158:            private List primaryColumnsNotInRefernce() {
159:                List list = table.getPrimaryKeyColumns();
160:
161:                Iterator itt = key.getReferences().iterator();
162:
163:                while (itt.hasNext()) {
164:                    Reference ref = (Reference) itt.next();
165:                    Column column = table.findColumn(ref.getLocal());
166:                    list.remove(column);
167:                }
168:
169:                return list;
170:            }
171:
172:            private String foreignTableColumnName(Column column) {
173:                Iterator itt = key.getReferences().iterator();
174:
175:                while (itt.hasNext()) {
176:                    Reference ref = (Reference) itt.next();
177:                    if (ref.getLocal().equals(column.getName())) {
178:                        return ref.getForeign();
179:                    }
180:                }
181:                return "error";
182:            }
183:
184:            /**   
185:             public static Matter findOrCreate( Client _ref, String _fldMatterNo ){
186:             return findOrCreate( _ref.get_fldClientno(),_fldMatterNo);   
187:             }
188:             */
189:
190:            public void outputFindOrCreate(Writer out) throws IOException {
191:
192:                List columnsNotIn = primaryColumnsNotInRefernce();
193:
194:                //public static Matter findOrCreate( Client _ref, String _fldMatterNo ){     	
195:                out.write("   public static "
196:                        + niceName.niceNameForTable(table.getName())
197:                        + " findOrCreate( "
198:                        + niceName.niceNameForTable(key.getForeignTable())
199:                        + " _ref, ");
200:                //gerenate the method paramters 
201:                Iterator itt = columnsNotIn.iterator();
202:                while (itt.hasNext()) {
203:                    Column column = (Column) itt.next();
204:                    ColumnGenerate cg = (ColumnGenerate) hmColumns.get(column
205:                            .getName());
206:                    out.write(cg.getParameters().methodParam);
207:                    if (itt.hasNext()) {
208:                        out.write(" ,");
209:                    }
210:                }
211:                out.write("){\n");
212:
213:                //return findOrCreate( _ref.get_fldClientno(),_fldMatterNo);   		
214:                out.write("      return findOrCreate( ");
215:
216:                //iterate through the primary columns again
217:                Iterator primaryColumnsItt = table.getPrimaryKeyColumns()
218:                        .iterator();
219:                while (primaryColumnsItt.hasNext()) {
220:
221:                    Column column = (Column) primaryColumnsItt.next();
222:                    if (columnsNotIn.contains(column) == false) {
223:
224:                        //This is one of the fields in the refernece 
225:                        out.write("_ref.get_"
226:                                + niceName.niceNameForColumn(key
227:                                        .getForeignTable(),
228:                                        foreignTableColumnName(column)) + "()");
229:                    } else {
230:
231:                        //This is one of the fields passed as a parameter;
232:                        ColumnGenerate cg = (ColumnGenerate) hmColumns
233:                                .get(column.getName());
234:                        out.write(cg.getParameters().paramName);
235:                    }
236:                    if (primaryColumnsItt.hasNext()) {
237:                        out.write(", ");
238:                    }
239:                }
240:                out.write(");\n");
241:
242:                out.write("   }\n\n");
243:            }
244:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.