KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > sqls > impl > ForeignKeyImpl


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

17 package org.apache.ws.jaxme.sqls.impl;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.ws.jaxme.sqls.Column;
24 import org.apache.ws.jaxme.sqls.ColumnSet;
25 import org.apache.ws.jaxme.sqls.ForeignKey;
26 import org.apache.ws.jaxme.sqls.Table;
27
28
29 /** <p>Implementation of a foreign key.</p>
30  *
31  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
32  */

33 public class ForeignKeyImpl extends ColumnSetImpl implements ForeignKey {
34   public static class ColumnReferenceImpl implements ForeignKey.ColumnLink {
35     private final Column localColumn, referencedColumn;
36     public ColumnReferenceImpl(Column pLocalColumn, Column pReferencedColumn) {
37       if (pLocalColumn == null) {
38          throw new NullPointerException JavaDoc("The local column must not be null.");
39       }
40       if (pReferencedColumn == null) {
41         throw new NullPointerException JavaDoc("The referenced column must not be null.");
42       }
43       localColumn = pLocalColumn;
44       referencedColumn = pReferencedColumn;
45     }
46     public Column getLocalColumn() {
47       return localColumn;
48     }
49     public Column getReferencedColumn() {
50       return referencedColumn;
51     }
52   }
53
54   private Table referencedTable;
55   private ForeignKey.Mode onDelete, onUpdate;
56   public List JavaDoc references = new ArrayList JavaDoc();
57
58   protected ForeignKeyImpl(Table pTable, Table pReferencedTable) {
59     super(pTable);
60     if (pReferencedTable == null) {
61       throw new NullPointerException JavaDoc("The referenced table must not be null.");
62     }
63     referencedTable = pReferencedTable;
64   }
65
66     public Table getReferencedTable() {
67       return referencedTable;
68     }
69
70     public void setOnDelete(ForeignKey.Mode pMode) {
71       onDelete = pMode;
72     }
73
74     public ForeignKey.Mode getOnDelete() {
75       return onDelete;
76     }
77
78     public void setOnUpdate(ForeignKey.Mode pMode) {
79       onUpdate = pMode;
80     }
81
82     public ForeignKey.Mode getOnUpdate() {
83       return onUpdate;
84     }
85
86     public void addColumnLink(Column pColumn, Column pReferencedColumn) {
87     addColumnReference(new ColumnReferenceImpl(pColumn, pReferencedColumn));
88   }
89
90   public void addColumnReference(ForeignKey.ColumnLink pReference) {
91     Column localColumn = pReference.getLocalColumn();
92     if (!getTable().equals(localColumn.getTable())) {
93       throw new IllegalStateException JavaDoc("The local column " + localColumn.getQName() +
94                                        " is not from the local table " + getTable().getQName());
95     }
96     Column referencedColumn = pReference.getReferencedColumn();
97     if (!getReferencedTable().equals(referencedColumn.getTable())) {
98       throw new IllegalStateException JavaDoc("The referenced column " + referencedColumn.getQName() +
99                                        " is not from the referenced table " + getReferencedTable().getQName());
100     }
101     references.add(pReference);
102     }
103
104     public void addColumnLink(Column.Name pName, Column.Name pReferencedName) {
105       Column localColumn = getTable().getColumn(pName);
106       if (localColumn == null) {
107         throw new NullPointerException JavaDoc("The local table " + getTable().getQName() +
108                                         " doesn't contain a column " + pName);
109       }
110       Column referencedColumn = getReferencedTable().getColumn(pReferencedName);
111       if (referencedColumn == null) {
112          throw new NullPointerException JavaDoc("The referenced table " + getReferencedTable().getName() +
113                                          " doesn't contain a column " + pReferencedName);
114       }
115       addColumnLink(localColumn, referencedColumn);
116     }
117
118     public void addColumnLink(String JavaDoc pName, String JavaDoc pReferencedName) {
119       addColumnLink(new ColumnImpl.NameImpl(pName), new ColumnImpl.NameImpl(pReferencedName));
120     }
121
122    public Iterator JavaDoc getColumnLinks() {
123       return references.iterator();
124    }
125
126     public Iterator JavaDoc getColumns() {
127       return new Iterator JavaDoc(){
128          Iterator JavaDoc inner = getColumnLinks();
129          public boolean hasNext() { return inner.hasNext(); }
130          public void remove() { inner.remove(); }
131          public Object JavaDoc next() {
132             ForeignKey.ColumnLink link = (ForeignKey.ColumnLink) inner.next();
133             return link.getLocalColumn();
134          }
135       };
136     }
137
138    public ColumnSet getReferencedColumns() {
139      return new ColumnSet(){
140        public Table getTable() { return getReferencedTable(); }
141        public Iterator JavaDoc getColumns() {
142          return new Iterator JavaDoc() {
143             private Iterator JavaDoc inner = getColumnLinks();
144             public Object JavaDoc next() {
145               return ((ColumnLink) inner.next()).getReferencedColumn();
146             }
147             public boolean hasNext() { return inner.hasNext(); }
148             public void remove() { inner.remove(); }
149          };
150        }
151      };
152    }
153 }
154
Popular Tags