KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > tools > schemaframework > ForeignKeyConstraint


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.tools.schemaframework;
23
24 import java.io.*;
25 import java.util.*;
26 import oracle.toplink.essentials.exceptions.*;
27 import oracle.toplink.essentials.internal.sessions.AbstractSession;
28
29 /**
30  * <p>
31  * <b>Purpose</b>: Define a foreign key from one table to another.
32  * This support composite foreign keys can constraint options.
33  */

34 public class ForeignKeyConstraint implements Serializable {
35     protected String JavaDoc name;
36     protected Vector sourceFields;
37     protected Vector targetFields;
38     protected String JavaDoc targetTable;
39     protected boolean shouldCascadeOnDelete;
40
41     public ForeignKeyConstraint() {
42         this.name = "";
43         this.sourceFields = new Vector();
44         this.targetFields = new Vector();
45         this.targetTable = "";
46         this.shouldCascadeOnDelete = false;
47     }
48
49     public ForeignKeyConstraint(String JavaDoc name, String JavaDoc sourceField, String JavaDoc targetField, String JavaDoc targetTable) {
50         this();
51         this.name = name;
52         sourceFields.addElement(sourceField);
53         targetFields.addElement(targetField);
54         this.targetTable = targetTable;
55     }
56
57     public void addSourceField(String JavaDoc sourceField) {
58         getSourceFields().addElement(sourceField);
59     }
60
61     public void addTargetField(String JavaDoc targetField) {
62         getTargetFields().addElement(targetField);
63     }
64
65     /**
66      * INTERNAL:
67      * Append the database field definition string to the table creation statement.
68      */

69     public void appendDBString(Writer writer, AbstractSession session) {
70         try {
71             writer.write("FOREIGN KEY (");
72             for (Enumeration sourceEnum = getSourceFields().elements();
73                      sourceEnum.hasMoreElements();) {
74                 writer.write((String JavaDoc)sourceEnum.nextElement());
75                 if (sourceEnum.hasMoreElements()) {
76                     writer.write(", ");
77                 }
78             }
79             writer.write(") REFERENCES ");
80             writer.write(getTargetTable());
81             writer.write(" (");
82             for (Enumeration targetEnum = getTargetFields().elements();
83                      targetEnum.hasMoreElements();) {
84                 writer.write((String JavaDoc)targetEnum.nextElement());
85                 if (targetEnum.hasMoreElements()) {
86                     writer.write(", ");
87                 }
88             }
89             writer.write(")");
90             if (shouldCascadeOnDelete()) {
91                 writer.write(" ON DELETE CASCADE");
92             }
93         } catch (IOException ioException) {
94             throw ValidationException.fileError(ioException);
95         }
96     }
97
98     /**
99      * PUBLIC:
100      * Enables delete cascading on the database.
101      * This must be used carefully, i.e. only private relationships.
102      */

103     public void cascadeOnDelete() {
104         setShouldCascadeOnDelete(true);
105     }
106
107     /**
108      * PUBLIC:
109      * Disables delete cascading on the database, this is the default.
110      */

111     public void dontCascadeOnDelete() {
112         setShouldCascadeOnDelete(false);
113     }
114
115     public String JavaDoc getName() {
116         return name;
117     }
118
119     public Vector getSourceFields() {
120         return sourceFields;
121     }
122
123     public Vector getTargetFields() {
124         return targetFields;
125     }
126
127     public String JavaDoc getTargetTable() {
128         return targetTable;
129     }
130
131     public void setName(String JavaDoc name) {
132         this.name = name;
133     }
134
135     /**
136      * PUBLIC:
137      * Enables delete cascading on the database.
138      * This must be used carefully, i.e. only private relationships.
139      */

140     public void setShouldCascadeOnDelete(boolean shouldCascadeOnDelete) {
141         this.shouldCascadeOnDelete = shouldCascadeOnDelete;
142     }
143
144     public void setSourceFields(Vector sourceFields) {
145         this.sourceFields = sourceFields;
146     }
147
148     public void setTargetFields(Vector targetFields) {
149         this.targetFields = targetFields;
150     }
151
152     public void setTargetTable(String JavaDoc targetTable) {
153         this.targetTable = targetTable;
154     }
155
156     public boolean shouldCascadeOnDelete() {
157         return shouldCascadeOnDelete;
158     }
159 }
160
Popular Tags