001: package org.apache.ojb.tools.mapping.reversedb;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: *
020: * @author <a href="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
021: * @version $Id: DBFKRelation.java,v 1.1.2.1 2005/12/21 22:32:04 tomdz Exp $
022: */
023: public class DBFKRelation implements MetadataNodeInterface,
024: javax.swing.tree.TreeNode,
025: org.apache.ojb.tools.mapping.reversedb.gui.PropertySheetModel {
026: DBTable pkTable;
027: DBTable fkTable;
028:
029: private boolean bAutoRetrieve = true;
030: private boolean bAutoUpdate = false;
031: private boolean bAutoDelete = false;
032:
033: private String strFieldName = null;
034:
035: /* The field type is only relevant, if the parent table is the pk table
036: * and this is a collection, then it is set to Vector as default. If
037: * this is a refenece it is of the type of the target class.
038: */
039: private String strFieldType = null;
040:
041: private boolean enabled = true;
042:
043: boolean pkTableIsParent = true;
044: java.util.ArrayList alColumnPairs = new java.util.ArrayList();
045:
046: /** Creates a new instance of DBFKReference */
047: public DBFKRelation(DBTable pPkTable, DBTable pFkTable,
048: boolean ppkTableIsParent) {
049: pkTable = pPkTable;
050: fkTable = pFkTable;
051: pkTableIsParent = ppkTableIsParent;
052: if (pkTableIsParent) {
053: this .strFieldName = "coll" + fkTable.getClassName();
054: this .strFieldType = "java.util.Vector";
055: } else {
056: this .strFieldName = "a" + pkTable.getClassName();
057: }
058: }
059:
060: public boolean isEnabled() {
061: return this .enabled;
062: }
063:
064: public void setEnabled(boolean b) {
065: this .enabled = b;
066: }
067:
068: public String getFieldName() {
069: return this .strFieldName;
070: }
071:
072: public void setFieldName(String s) {
073: strFieldName = s;
074: }
075:
076: public String getFieldType() {
077: if (this .isPkTableParent())
078: return this .strFieldType;
079: else
080: return this .pkTable.getFQClassName();
081: }
082:
083: public void setFieldType(String s) {
084: if (!this .isPkTableParent())
085: throw new java.lang.UnsupportedOperationException(
086: "Cannot set Field type on this type of relation");
087: strFieldType = s;
088: }
089:
090: public boolean isPkTableParent() {
091: return this .pkTableIsParent;
092: }
093:
094: public void setAutoRetrieve(boolean b) {
095: this .bAutoRetrieve = b;
096: }
097:
098: public boolean getAutoRetrieve() {
099: return this .bAutoRetrieve;
100: }
101:
102: public void setAutoUpdate(boolean b) {
103: this .bAutoUpdate = b;
104: }
105:
106: public boolean getAutoUpdate() {
107: return this .bAutoUpdate;
108: }
109:
110: public void setAutoDelete(boolean b) {
111: this .bAutoDelete = b;
112: }
113:
114: public boolean getAutoDelete() {
115: return this .bAutoDelete;
116: }
117:
118: public DBTable getFKTable() {
119: return fkTable;
120: }
121:
122: public DBTable getPKTable() {
123: return pkTable;
124: }
125:
126: public boolean isTreeEnabled() {
127: if (this .pkTableIsParent)
128: return this .pkTable.isTreeEnabled() && this .isEnabled();
129: else
130: return this .fkTable.isTreeEnabled() && this .isEnabled();
131: }
132:
133: public void addColumnPair(DBColumn pPkColumn, DBColumn pFkColumn) {
134: alColumnPairs.add(new Object[] { pPkColumn, pFkColumn });
135: }
136:
137: public java.util.Iterator getColumnPairIterator() {
138: return alColumnPairs.iterator();
139: }
140:
141: public java.util.Enumeration children() {
142: if (pkTableIsParent)
143: return this .fkTable.children();
144: else
145: return this .pkTable.children();
146: }
147:
148: public boolean getAllowsChildren() {
149: if (pkTableIsParent)
150: return this .fkTable.getAllowsChildren();
151: else
152: return this .pkTable.getAllowsChildren();
153: }
154:
155: public javax.swing.tree.TreeNode getChildAt(int param) {
156: if (pkTableIsParent)
157: return this .fkTable.getChildAt(param);
158: else
159: return this .pkTable.getChildAt(param);
160: }
161:
162: public int getChildCount() {
163: if (pkTableIsParent)
164: return this .fkTable.getChildCount();
165: else
166: return this .pkTable.getChildCount();
167: }
168:
169: public int getIndex(javax.swing.tree.TreeNode treeNode) {
170: if (pkTableIsParent)
171: return this .fkTable.getIndex(treeNode);
172: else
173: return this .pkTable.getIndex(treeNode);
174: }
175:
176: public javax.swing.tree.TreeNode getParent() {
177: if (pkTableIsParent)
178: return this .pkTable;
179: else
180: return this .fkTable;
181: }
182:
183: public boolean isLeaf() {
184: if (pkTableIsParent)
185: return this .fkTable.isLeaf();
186: else
187: return this .pkTable.isLeaf();
188: }
189:
190: public String toString() {
191: if (pkTableIsParent)
192: return this .fkTable.toString() + " (Collection)";
193: else
194: return this .pkTable.toString() + " (Reference)";
195: }
196:
197: public Class getPropertySheetClass() {
198: return org.apache.ojb.tools.mapping.reversedb.gui.DBFKRelationPropertySheet.class;
199: }
200:
201: private void writeXMLReference(java.io.PrintWriter pw) {
202: pw.println(" <reference-descriptor");
203: pw.println(" name=\"" + this .getFieldName() + "\"");
204: pw.println(" class-ref=\""
205: + this .getPKTable().getFQClassName() + "\"");
206: pw.println(" auto-retrieve=\"" + this .getAutoRetrieve()
207: + "\"");
208: pw.println(" auto-update=\"" + this .getAutoUpdate() + "\"");
209: pw.println(" auto-delete=\"" + this .getAutoDelete() + "\">");
210:
211: pw.print(" <foreignkey field-ref=\"");
212: java.util.Iterator it = this .getColumnPairIterator();
213: // TODO: dtd dictate one refid
214: // while (it.hasNext()) strReturn += ((DBColumn)((Object[])it.next())[1]).getId() + " ";
215: if (it.hasNext())
216: pw.print(((DBColumn) ((Object[]) it.next())[1])
217: .getJavaFieldName());
218: pw.println("\" />");
219:
220: pw.println(" </reference-descriptor>");
221: }
222:
223: private void writeXMLCollection(java.io.PrintWriter pw) {
224: pw.println(" <collection-descriptor");
225: pw.println(" name=\"" + this .getFieldName() + "\"");
226: pw.println(" element-class-ref=\""
227: + this .getFKTable().getFQClassName() + "\"");
228: pw.println(" auto-retrieve=\"" + this .getAutoRetrieve()
229: + "\"");
230: pw.println(" auto-update=\"" + this .getAutoUpdate() + "\"");
231: pw.println(" auto-delete=\"" + this .getAutoDelete() + "\">");
232: pw.print(" <inverse-foreignkey field-ref=\"");
233: java.util.Iterator it = this .getColumnPairIterator();
234: while (it.hasNext())
235: pw.print(((DBColumn) ((Object[]) it.next())[1])
236: .getJavaFieldName()
237: + " ");
238: pw.println("\" />");
239: pw.println(" </collection-descriptor>");
240: }
241:
242: public String getXML() {
243: java.io.StringWriter sw = new java.io.StringWriter();
244: writeXML(new java.io.PrintWriter(sw));
245: return sw.getBuffer().toString();
246: }
247:
248: public void writeXML(java.io.PrintWriter pw) {
249: if (this .isPkTableParent()) {
250: writeXMLCollection(pw);
251: } else {
252: writeXMLReference(pw);
253: }
254: }
255:
256: public void generateJava(java.io.File aFile, String strHeader,
257: String strFooter) throws java.io.IOException,
258: java.io.FileNotFoundException {
259: throw new UnsupportedOperationException(
260: "Generate Java on DBFKReference is not allowed");
261: }
262:
263: public void setPackage(String packageName) {
264: throw new UnsupportedOperationException(
265: "Set Package on DBFKReference is not allowed");
266: }
267:
268: public String getJavaFieldDefinition() {
269: // Only return a field definition if this Relation and both
270: // participating tables are enabled;
271: if (this .isTreeEnabled() && this .getFKTable().isTreeEnabled()
272: && this .getPKTable().isTreeEnabled()) {
273: return " private " + this .getFieldType() + " "
274: + this .getFieldName() + ";";
275: } else
276: return "";
277: }
278:
279: public String getJavaGetterSetterDefinition() {
280: if (this .isTreeEnabled() && this .getFKTable().isTreeEnabled()
281: && this .getPKTable().isTreeEnabled()) {
282: String strReturn = "";
283: strReturn = " public " + this .getFieldType() + " get"
284: + this .getFieldName().substring(0, 1).toUpperCase()
285: + this .getFieldName().substring(1) + "()"
286: + System.getProperty("line.separator");
287:
288: strReturn += " {" + System.getProperty("line.separator");
289: strReturn += " return this." + this .getFieldName()
290: + ";" + System.getProperty("line.separator");
291: strReturn += " }" + System.getProperty("line.separator");
292: strReturn += " public void set"
293: + this .getFieldName().substring(0, 1).toUpperCase()
294: + this .getFieldName().substring(1) + "("
295: + this .getFieldType() + " param)"
296: + System.getProperty("line.separator");
297: strReturn += " {" + System.getProperty("line.separator");
298: strReturn += " this." + this .getFieldName()
299: + " = param;"
300: + System.getProperty("line.separator");
301: strReturn += " }" + System.getProperty("line.separator");
302: return strReturn;
303: } else
304: return "";
305: }
306:
307: }
308:
309: /***************************** Changelog *****************************
310: // $Log: DBFKRelation.java,v $
311: // Revision 1.1.2.1 2005/12/21 22:32:04 tomdz
312: // Updated license
313: //
314: // Revision 1.1 2004/05/05 16:39:05 arminw
315: // fix fault
316: // wrong package structure used:
317: // org.apache.ojb.tools.reversdb
318: // org.apache.ojb.tools.reversdb2
319: //
320: // instead of
321: // org.apache.ojb.tools.mapping.reversdb
322: // org.apache.ojb.tools.mapping.reversdb2
323: //
324: // Revision 1.1 2004/05/04 13:45:00 arminw
325: // move reverseDB stuff
326: //
327: // Revision 1.8 2004/04/04 23:53:42 brianm
328: // Fixed initial copyright dates to match cvs repository
329: //
330: // Revision 1.7 2004/03/11 18:16:22 brianm
331: // ASL 2.0
332: //
333: // Revision 1.6 2003/12/12 16:37:16 brj
334: // removed unnecessary casts, semicolons etc.
335: //
336: // Revision 1.5 2003/09/10 06:45:00 mpoeschl
337: // remove duplicated license
338: //
339: // Revision 1.4 2003/06/21 10:28:44 florianbruckner
340: // implement XML generation with PrintWriter; getXML() still works and uses writeXML(java.io.PrintWriter)
341: // Ids are not used for XML generation.
342: //
343: // Revision 1.3 2003/01/28 21:42:53 florianbruckner
344: // update XML generation
345: //
346: // Revision 1.2 2002/06/17 19:34:33 jvanzyl
347: // Correcting all the package references.
348: // PR:
349: // Obtained from:
350: // Submitted by:
351: // Reviewed by:
352: //
353: // Revision 1.1.1.1 2002/06/17 18:16:51 jvanzyl
354: // Initial OJB import
355: //
356: // Revision 1.2 2002/05/16 11:47:09 florianbruckner
357: // fix CR/LF issue, change license to ASL
358: //
359: // Revision 1.1 2002/04/18 11:44:16 mpoeschl
360: //
361: // move files to new location
362: //
363: // Revision 1.3 2002/04/07 09:05:16 thma
364: // *** empty log message ***
365: //
366: // Revision 1.2 2002/03/11 17:36:05 florianbruckner
367: // fix line break issue for these files that were previously checked in with -kb
368: //
369: // Revision 1.1 2002/03/04 17:19:32 thma
370: // initial checking for Florians Reverse engineering tool
371: //
372: // Revision 1.1.1.1 2002/02/20 13:35:25 Administrator
373: // initial import
374: //
375: /***************************** Changelog *****************************/
|