1 package com.bonevich.erj.model;
2
3 import com.bonevich.util.dependency.Dynamic;
4 import com.bonevich.java.util.ImmutableIterator;
5 import java.util.*;
6
7 /*** A class that represents ...
8 *
9 * @see Schema
10 * @see Attribute
11 * @author Jeffrey D. Bonevich <bonevich@telocity.com>
12 */
13 public final class Relation extends ModelElement
14 {
15 //////////////////////////////////////////////////////////
16 // Constants
17 public static final String NEW_RELATION_STR = "relation_";
18 public static final String EMPTY_STR = "";
19
20 //////////////////////////////////////////////////////////
21 // Attributes
22 {
23 _name = EMPTY_STR;
24 _identifier = EMPTY_STR;
25 _description = EMPTY_STR;
26 }
27
28 private int _attributeCount = 0;
29 private int _keyCount = 0;
30
31 //////////////////////////////////////////////////////////
32 // Associations
33 private Schema _schema;
34 private List _attributes = new LinkedList();
35 private List _keys = new LinkedList();
36
37 /////////////////////////////////////////////////////////
38 // Dynamic Sentries
39 private Dynamic _dyn_attributes = new Dynamic();
40 private Dynamic _dyn_keys = new Dynamic();
41
42 //////////////////////////////////////////////////////////
43 // Constructors
44 public Relation(Schema schema)
45 {
46 setSchema(schema);
47 setIdentifier(NEW_RELATION_STR + _schema.getRelationCount());
48 setName(getIdentifier());
49 }
50
51 public Schema getSchema()
52 {
53 return _schema;
54 }
55 private void setSchema(Schema schema)
56 {
57 _schema = schema;
58 }
59
60
61 //////////////////////////////////////////////////////////
62 // Operations
63 public Attribute createAttribute()
64 {
65 return createAttribute(null);
66 }
67 public Attribute createAttribute(Attribute ref)
68 {
69 // FIXME: how check that attribute identifiers do not conflict
70 _dyn_attributes.onSet();
71 ++_attributeCount;
72 Attribute attribute = new Attribute(this, ref);
73 _attributes.add(attribute);
74 return attribute;
75 }
76 public void moveAttributeUp(Attribute attribute)
77 {
78 int index = _attributes.indexOf(attribute);
79 if (index > 0)
80 {
81 _attributes.remove(index);
82 _attributes.add(--index, attribute);
83 _dyn_attributes.onSet();
84 }
85 }
86 public void moveAttributeDown(Attribute attribute)
87 {
88 int index = _attributes.indexOf(attribute);
89 if (index < _attributes.size() - 1)
90 {
91 _attributes.remove(index);
92 _attributes.add(++index, attribute);
93 _dyn_attributes.onSet();
94 }
95 }
96 public void deleteAttribute(Attribute attribute)
97 {
98 if (_attributes.remove(attribute))
99 {
100 _dyn_attributes.onSet();
101 attribute.dispose();
102 }
103 }
104 public Iterator getAttributeIterator()
105 {
106 _dyn_attributes.onGet();
107 return new ImmutableIterator(_attributes.iterator());
108 }
109 public int getAttributeCount()
110 {
111 return _attributeCount;
112 }
113 public Attribute getAttribute(String identifier)
114 {
115 Iterator attributes = getAttributeIterator();
116 while (attributes.hasNext())
117 {
118 Attribute attr = (Attribute) attributes.next();
119 if (attr.getIdentifier().equals(identifier))
120 {
121 return attr;
122 }
123 }
124 return null;
125 }
126
127 public UniqueKey createUniqueKey(Attribute[] attributes)
128 {
129 _dyn_keys.onSet();
130 ++_keyCount;
131 UniqueKey key = new UniqueKey(this, attributes);
132 _keys.add(key);
133 return key;
134 }
135 public PrimaryKey createPrimaryKey(Attribute[] attributes)
136 {
137 _dyn_keys.onSet();
138 ++_keyCount;
139 PrimaryKey key = new PrimaryKey(this, attributes);
140 // KeyConstraint.equals() will ensure that only a single
141 //PK is present; but what to do with one that already exists??
142 _keys.add(key);
143 return key;
144 }
145 public ForeignKey createForeignKey(Relation referent)
146 {
147 // See if the referent relation has a primary key;
148 // if so, make new attributes in our relation
149 Iterator keys = referent.getKeyIterator();
150 List attrList = new LinkedList();
151 while (keys.hasNext())
152 {
153 KeyConstraint key = (KeyConstraint)keys.next();
154 if (key instanceof PrimaryKey)
155 {
156 Iterator attributes = key.getAttributeIterator();
157 while (attributes.hasNext())
158 {
159 Attribute attribute = (Attribute)attributes.next();
160 Attribute newAttribute = createAttribute(attribute);
161 attrList.add(newAttribute);
162 }
163 break;
164 }
165 }
166
167 Attribute[] attrArray = new Attribute[attrList.size()];
168 attrArray = (Attribute[])attrList.toArray(attrArray);
169 return createForeignKey(referent, attrArray);
170 }
171 public ForeignKey createForeignKey(Relation referent, Attribute[] attributes)
172 {
173 _dyn_keys.onSet();
174 ++_keyCount;
175 ForeignKey key = new ForeignKey(this, attributes, referent);
176 _keys.add(key);
177
178 // All foreign keys need to be registered with the schema
179 _schema.addKey(key);
180
181 return key;
182 }
183
184 public void deleteKey(ForeignKey key)
185 {
186 deleteKey((KeyConstraint)key);
187
188 // Inform the schema it no longer needs to track this FK
189 _schema.removeKey(key);
190 }
191 public void deleteKey(KeyConstraint key)
192 {
193 if (_keys.remove(key))
194 {
195 _dyn_keys.onSet();
196 key.dispose();
197 }
198 }
199 public Iterator getKeyIterator()
200 {
201 _dyn_keys.onGet();
202 return new ImmutableIterator(_keys.iterator());
203 }
204 public int getKeyCount()
205 {
206 return _keyCount;
207 }
208
209 public void dispose()
210 {
211 Iterator keys = getKeyIterator();
212 while (keys.hasNext())
213 {
214 ((KeyConstraint) keys.next()).dispose();
215 }
216
217 Iterator attributes = getAttributeIterator();
218 while (attributes.hasNext())
219 {
220 ((Attribute) attributes.next()).dispose();
221 }
222 }
223
224 public boolean equals(Object rhs)
225 {
226 if (rhs != null && rhs instanceof Relation)
227 {
228 Relation that = (Relation)rhs;
229 if (getSchema() == that.getSchema() &&
230 that.getIdentifier().equals(getIdentifier())
231 )
232 {
233 return true;
234 }
235 }
236 return false;
237 }
238
239 public void accept(ModelElementVisitor visitor)
240 {
241 visitor.visitRelation(this);
242 }
243
244 } /* end class Relation */
This page was automatically generated by Maven