1 17 package org.apache.ws.jaxme.sqls.impl; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 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 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 ("The local column must not be null."); 39 } 40 if (pReferencedColumn == null) { 41 throw new NullPointerException ("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 references = new ArrayList (); 57 58 protected ForeignKeyImpl(Table pTable, Table pReferencedTable) { 59 super(pTable); 60 if (pReferencedTable == null) { 61 throw new NullPointerException ("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 ("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 ("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 ("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 ("The referenced table " + getReferencedTable().getName() + 113 " doesn't contain a column " + pReferencedName); 114 } 115 addColumnLink(localColumn, referencedColumn); 116 } 117 118 public void addColumnLink(String pName, String pReferencedName) { 119 addColumnLink(new ColumnImpl.NameImpl(pName), new ColumnImpl.NameImpl(pReferencedName)); 120 } 121 122 public Iterator getColumnLinks() { 123 return references.iterator(); 124 } 125 126 public Iterator getColumns() { 127 return new Iterator (){ 128 Iterator inner = getColumnLinks(); 129 public boolean hasNext() { return inner.hasNext(); } 130 public void remove() { inner.remove(); } 131 public Object 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 getColumns() { 142 return new Iterator () { 143 private Iterator inner = getColumnLinks(); 144 public Object 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 |