KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > tools > mapping > reversedb > DBFKRelation


1 package org.apache.ojb.tools.mapping.reversedb;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 /**
20  *
21  * @author <a HREF="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
22  * @version $Id: DBFKRelation.java,v 1.1.2.1 2005/12/21 22:32:04 tomdz Exp $
23  */

24 public class DBFKRelation implements MetadataNodeInterface, javax.swing.tree.TreeNode JavaDoc, org.apache.ojb.tools.mapping.reversedb.gui.PropertySheetModel
25 {
26   DBTable pkTable;
27   DBTable fkTable;
28   
29   private boolean bAutoRetrieve = true;
30   private boolean bAutoUpdate = false;
31   private boolean bAutoDelete = false;
32   
33   private String JavaDoc strFieldName = null;
34   
35   /* The field type is only relevant, if the parent table is the pk table
36    * and this is a collection, then it is set to Vector as default. If
37    * this is a refenece it is of the type of the target class.
38    */

39   private String JavaDoc strFieldType = null;
40   
41   private boolean enabled = true;
42   
43   boolean pkTableIsParent = true;
44   java.util.ArrayList JavaDoc alColumnPairs = new java.util.ArrayList JavaDoc();
45   /** Creates a new instance of DBFKReference */
46   public DBFKRelation (DBTable pPkTable, DBTable pFkTable, boolean ppkTableIsParent)
47   {
48     pkTable = pPkTable;
49     fkTable = pFkTable;
50     pkTableIsParent = ppkTableIsParent;
51     if (pkTableIsParent)
52     {
53       this.strFieldName = "coll" + fkTable.getClassName ();
54       this.strFieldType = "java.util.Vector";
55     }
56     else
57     {
58       this.strFieldName = "a" + pkTable.getClassName ();
59     }
60   }
61   
62   public boolean isEnabled()
63   {
64     return this.enabled;
65   }
66   
67   public void setEnabled(boolean b)
68   {
69     this.enabled = b;
70   }
71   
72   public String JavaDoc getFieldName()
73   {
74     return this.strFieldName;
75   }
76   
77   public void setFieldName(String JavaDoc s)
78   {
79     strFieldName = s;
80   }
81   
82   public String JavaDoc getFieldType()
83   {
84     if (this.isPkTableParent ()) return this.strFieldType;
85     else return this.pkTable.getFQClassName();
86   }
87   
88   public void setFieldType(String JavaDoc s)
89   {
90     if (!this.isPkTableParent()) throw new java.lang.UnsupportedOperationException JavaDoc("Cannot set Field type on this type of relation");
91     strFieldType = s;
92   }
93   
94   public boolean isPkTableParent()
95   {
96     return this.pkTableIsParent;
97   }
98     
99   public void setAutoRetrieve(boolean b)
100   {
101     this.bAutoRetrieve = b;
102   }
103   
104   public boolean getAutoRetrieve()
105   {
106     return this.bAutoRetrieve;
107   }
108
109   public void setAutoUpdate(boolean b)
110   {
111     this.bAutoUpdate = b;
112   }
113   
114   public boolean getAutoUpdate()
115   {
116     return this.bAutoUpdate;
117   }
118   
119   public void setAutoDelete(boolean b)
120   {
121     this.bAutoDelete = b;
122   }
123   
124   public boolean getAutoDelete()
125   {
126     return this.bAutoDelete;
127   }
128
129   public DBTable getFKTable()
130   {
131     return fkTable;
132   }
133   
134   public DBTable getPKTable()
135   {
136     return pkTable;
137   }
138   
139   public boolean isTreeEnabled()
140   {
141     if (this.pkTableIsParent) return this.pkTable.isTreeEnabled() && this.isEnabled();
142     else return this.fkTable.isTreeEnabled() && this.isEnabled();
143   }
144   
145   
146   public void addColumnPair(DBColumn pPkColumn, DBColumn pFkColumn)
147   {
148     alColumnPairs.add(new Object JavaDoc[]{pPkColumn, pFkColumn});
149   }
150   
151   public java.util.Iterator JavaDoc getColumnPairIterator()
152   {
153     return alColumnPairs.iterator();
154   }
155   
156   public java.util.Enumeration JavaDoc children ()
157   {
158     if (pkTableIsParent) return this.fkTable.children();
159     else return this.pkTable.children();
160   }
161   
162   public boolean getAllowsChildren ()
163   {
164     if (pkTableIsParent) return this.fkTable.getAllowsChildren();
165     else return this.pkTable.getAllowsChildren();
166   }
167   
168   public javax.swing.tree.TreeNode JavaDoc getChildAt (int param)
169   {
170     if (pkTableIsParent) return this.fkTable.getChildAt(param);
171     else return this.pkTable.getChildAt(param);
172   }
173   
174   public int getChildCount ()
175   {
176     if (pkTableIsParent) return this.fkTable.getChildCount();
177     else return this.pkTable.getChildCount();
178   }
179   
180   public int getIndex (javax.swing.tree.TreeNode JavaDoc treeNode)
181   {
182     if (pkTableIsParent) return this.fkTable.getIndex(treeNode);
183     else return this.pkTable.getIndex(treeNode);
184   }
185   
186   public javax.swing.tree.TreeNode JavaDoc getParent ()
187   {
188     if (pkTableIsParent) return this.pkTable;
189     else return this.fkTable;
190   }
191   
192   public boolean isLeaf ()
193   {
194     if (pkTableIsParent) return this.fkTable.isLeaf();
195     else return this.pkTable.isLeaf();
196   }
197
198   public String JavaDoc toString()
199   {
200     if (pkTableIsParent) return this.fkTable.toString() + " (Collection)";
201     else return this.pkTable.toString() + " (Reference)";
202   }
203   
204   
205   public Class JavaDoc getPropertySheetClass ()
206   {
207     return org.apache.ojb.tools.mapping.reversedb.gui.DBFKRelationPropertySheet.class;
208   }
209   
210   private void writeXMLReference(java.io.PrintWriter JavaDoc pw)
211   {
212     pw.println(" <reference-descriptor");
213     pw.println(" name=\"" + this.getFieldName()+ "\"" );
214     pw.println(" class-ref=\"" + this.getPKTable().getFQClassName() + "\"" );
215     pw.println(" auto-retrieve=\"" + this.getAutoRetrieve()+ "\"" );
216     pw.println(" auto-update=\"" + this.getAutoUpdate() + "\"");
217     pw.println(" auto-delete=\"" + this.getAutoDelete() + "\">");
218
219     pw.print(" <foreignkey field-ref=\"");
220     java.util.Iterator JavaDoc it = this.getColumnPairIterator();
221     // TODO: dtd dictate one refid
222
// while (it.hasNext()) strReturn += ((DBColumn)((Object[])it.next())[1]).getId() + " ";
223
if (it.hasNext()) pw.print(((DBColumn)((Object JavaDoc[])it.next())[1]).getJavaFieldName()) ;
224     pw.println("\" />");
225
226     pw.println(" </reference-descriptor>");
227   }
228   
229   private void writeXMLCollection(java.io.PrintWriter JavaDoc pw)
230   {
231     pw.println(" <collection-descriptor");
232     pw.println(" name=\"" + this.getFieldName() + "\"");
233     pw.println(" element-class-ref=\"" + this.getFKTable().getFQClassName() + "\"" );
234     pw.println(" auto-retrieve=\"" + this.getAutoRetrieve() + "\"" );
235     pw.println(" auto-update=\"" + this.getAutoUpdate() + "\"" );
236     pw.println(" auto-delete=\"" + this.getAutoDelete() + "\">");
237     pw.print(" <inverse-foreignkey field-ref=\"");
238     java.util.Iterator JavaDoc it = this.getColumnPairIterator();
239     while (it.hasNext()) pw.print(((DBColumn)((Object JavaDoc[])it.next())[1]).getJavaFieldName() + " ");
240     pw.println("\" />");
241     pw.println(" </collection-descriptor>");
242   }
243   
244   public String JavaDoc getXML()
245   {
246         java.io.StringWriter JavaDoc sw = new java.io.StringWriter JavaDoc();
247         writeXML(new java.io.PrintWriter JavaDoc(sw));
248         return sw.getBuffer().toString();
249   }
250   
251   public void writeXML(java.io.PrintWriter JavaDoc pw)
252   {
253     if (this.isPkTableParent())
254     {
255       writeXMLCollection(pw);
256     }
257     else
258     {
259       writeXMLReference(pw);
260     }
261   }
262   
263   
264   public void generateJava (java.io.File JavaDoc aFile, String JavaDoc strHeader, String JavaDoc strFooter) throws java.io.IOException JavaDoc, java.io.FileNotFoundException JavaDoc
265   {
266     throw new UnsupportedOperationException JavaDoc("Generate Java on DBFKReference is not allowed");
267   }
268   
269   public void setPackage (String JavaDoc packageName)
270   {
271     throw new UnsupportedOperationException JavaDoc("Set Package on DBFKReference is not allowed");
272   }
273
274   public String JavaDoc getJavaFieldDefinition()
275   {
276     // Only return a field definition if this Relation and both
277
// participating tables are enabled;
278
if (this.isTreeEnabled()
279         && this.getFKTable().isTreeEnabled()
280         && this.getPKTable().isTreeEnabled())
281     {
282       return " private " + this.getFieldType() + " " + this.getFieldName() + ";";
283     }
284     else return "";
285   }
286   
287   public String JavaDoc getJavaGetterSetterDefinition()
288   {
289     if (this.isTreeEnabled()
290         && this.getFKTable().isTreeEnabled()
291         && this.getPKTable().isTreeEnabled())
292     {
293       String JavaDoc strReturn = "";
294       strReturn = " public " + this.getFieldType() + " get"
295         + this.getFieldName().substring(0,1).toUpperCase()
296         + this.getFieldName().substring(1) + "()"
297         + System.getProperty("line.separator");
298
299       strReturn += " {" + System.getProperty("line.separator");
300       strReturn += " return this." + this.getFieldName() + ";" + System.getProperty("line.separator");
301       strReturn += " }" + System.getProperty("line.separator");
302       strReturn += " public void set"
303         + this.getFieldName().substring(0,1).toUpperCase()
304         + this.getFieldName().substring(1) + "(" + this.getFieldType() + " param)"
305         + System.getProperty("line.separator");
306       strReturn += " {" + System.getProperty("line.separator");
307       strReturn += " this." + this.getFieldName() + " = param;" + System.getProperty("line.separator");
308       strReturn += " }" + System.getProperty("line.separator");
309       return strReturn;
310     }
311     else return "";
312   }
313   
314   
315 }
316
317 /***************************** Changelog *****************************
318 // $Log: DBFKRelation.java,v $
319 // Revision 1.1.2.1 2005/12/21 22:32:04 tomdz
320 // Updated license
321 //
322 // Revision 1.1 2004/05/05 16:39:05 arminw
323 // fix fault
324 // wrong package structure used:
325 // org.apache.ojb.tools.reversdb
326 // org.apache.ojb.tools.reversdb2
327 //
328 // instead of
329 // org.apache.ojb.tools.mapping.reversdb
330 // org.apache.ojb.tools.mapping.reversdb2
331 //
332 // Revision 1.1 2004/05/04 13:45:00 arminw
333 // move reverseDB stuff
334 //
335 // Revision 1.8 2004/04/04 23:53:42 brianm
336 // Fixed initial copyright dates to match cvs repository
337 //
338 // Revision 1.7 2004/03/11 18:16:22 brianm
339 // ASL 2.0
340 //
341 // Revision 1.6 2003/12/12 16:37:16 brj
342 // removed unnecessary casts, semicolons etc.
343 //
344 // Revision 1.5 2003/09/10 06:45:00 mpoeschl
345 // remove duplicated license
346 //
347 // Revision 1.4 2003/06/21 10:28:44 florianbruckner
348 // implement XML generation with PrintWriter; getXML() still works and uses writeXML(java.io.PrintWriter)
349 // Ids are not used for XML generation.
350 //
351 // Revision 1.3 2003/01/28 21:42:53 florianbruckner
352 // update XML generation
353 //
354 // Revision 1.2 2002/06/17 19:34:33 jvanzyl
355 // Correcting all the package references.
356 // PR:
357 // Obtained from:
358 // Submitted by:
359 // Reviewed by:
360 //
361 // Revision 1.1.1.1 2002/06/17 18:16:51 jvanzyl
362 // Initial OJB import
363 //
364 // Revision 1.2 2002/05/16 11:47:09 florianbruckner
365 // fix CR/LF issue, change license to ASL
366 //
367 // Revision 1.1 2002/04/18 11:44:16 mpoeschl
368 //
369 // move files to new location
370 //
371 // Revision 1.3 2002/04/07 09:05:16 thma
372 // *** empty log message ***
373 //
374 // Revision 1.2 2002/03/11 17:36:05 florianbruckner
375 // fix line break issue for these files that were previously checked in with -kb
376 //
377 // Revision 1.1 2002/03/04 17:19:32 thma
378 // initial checking for Florians Reverse engineering tool
379 //
380 // Revision 1.1.1.1 2002/02/20 13:35:25 Administrator
381 // initial import
382 //
383 /***************************** Changelog *****************************/

384
Popular Tags