KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > model > mapping > impl > MappingTableElementImpl


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 in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
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 Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * MappingTableElementImpl.java
26  *
27  * Created on March 3, 2000, 1:11 PM
28  */

29
30 package com.sun.jdo.api.persistence.model.mapping.impl;
31
32 import java.util.*;
33 import java.beans.PropertyVetoException JavaDoc;
34
35 import org.netbeans.modules.dbschema.TableElement;
36 import org.netbeans.modules.dbschema.ColumnElement;
37 import org.netbeans.modules.dbschema.util.NameUtil;
38
39 import com.sun.jdo.api.persistence.model.ModelException;
40 import com.sun.jdo.api.persistence.model.ModelVetoException;
41 import com.sun.jdo.api.persistence.model.mapping.*;
42 import com.sun.jdo.spi.persistence.utility.I18NHelper;
43
44 /**
45  *
46  * @author Mark Munro
47  * @author Rochelle Raccah
48  * @version %I%
49  */

50 public class MappingTableElementImpl extends MappingMemberElementImpl
51     implements MappingTableElement
52 {
53     private ArrayList _key; // array of column names
54
//@olsen: made transient to prevent from serializing into mapping files
55
private transient ArrayList _keyObjects; // array of ColumnElement (for runtime)
56
private ArrayList _referencingKeys; // array of MappingReferenceKeyElement
57
private String JavaDoc _table;
58     //@olsen: made transient to prevent from serializing into mapping files
59
private transient TableElement _tableObject; // for runtime
60

61     /** Create new MappingTableElementImpl with no corresponding name or
62      * declaring class. This constructor should only be used for cloning and
63      * archiving.
64      */

65     public MappingTableElementImpl ()
66     {
67         this((String JavaDoc)null, null);
68     }
69
70     /** Create new MappingTableElementImpl with the corresponding name and
71      * declaring class.
72      * @param name the name of the element
73      * @param declaringClass the class to attach to
74      */

75     public MappingTableElementImpl (String JavaDoc name,
76         MappingClassElement declaringClass)
77     {
78         super(name, declaringClass);
79     }
80
81     /** Creates new MappingTableElementImpl with a corresponding
82      * table and declaring class.
83      * @param table table element to be used by the mapping table.
84      * @param declaringClass the class to attach to
85      */

86     public MappingTableElementImpl (TableElement table,
87         MappingClassElement declaringClass) throws ModelException
88     {
89         this(table.toString(), declaringClass);
90
91         // don't use setTable so as not to fire property change events
92
_table = getName();
93     }
94
95     //======================= table handling ===========================
96

97     /** Returns the name of the table element used by this mapping table.
98      * @return the table name for this mapping table
99      */

100     public String JavaDoc getTable () { return _table; }
101
102     /** Set the table element for this mapping table to the supplied table.
103      * @param table table element to be used by the mapping table.
104      * @exception ModelException if impossible
105      */

106     public void setTable (TableElement table) throws ModelException
107     {
108         String JavaDoc old = getTable();
109         String JavaDoc newName = table.toString();
110
111         try
112         {
113             fireVetoableChange(PROP_TABLE, old, newName);
114             _table = newName;
115             firePropertyChange(PROP_TABLE, old, newName);
116             setName(_table);
117
118             // sync up runtime's object too: force next
119
// access to getTableObject to recompute it
120
_tableObject = null;
121         }
122         catch (PropertyVetoException JavaDoc e)
123         {
124             throw new ModelVetoException(e);
125         }
126     }
127
128     /** Override method in MappingElementImpl to set the _table variable
129      * if necessary (used for unarchiving).
130      * @param name the name
131      * @exception ModelException if impossible
132      */

133     public void setName (String JavaDoc name) throws ModelException
134     {
135         super.setName(name);
136
137         if (getTable() == null)
138             _table = name;
139     }
140
141     /** Returns true if the table element used by this mapping table is equal
142      * to the supplied table.
143      * @return <code>true</code> if table elements are equal,
144      * <code>false</code> otherwise.
145      */

146     public boolean isEqual (TableElement table)
147     {
148         return ((table != null) ? getTable().equals(table.toString()) : false);
149     }
150
151     //===================== primary key handling ===========================
152

153     /** Returns the list of column names in the primary key for this
154      * mapping table.
155      * @return the names of the columns in the primary key for this mapping
156      * table
157      */

158     public ArrayList getKey ()
159     {
160         if (_key == null)
161             _key = new ArrayList();
162
163         return _key;
164     }
165
166     /** Adds a column to the primary key of columns in this mapping table.
167      * This method should only be used to manipulate the key columns of the
168      * primary table. The secondary table key columns should be manipulated
169      * using MappingReferenceKeyElement methods for pairs.
170      * @param column column element to be added
171      * @exception ModelException if impossible
172      */

173     public void addKeyColumn (ColumnElement column) throws ModelException
174     {
175         if (column != null)
176         {
177             String JavaDoc columnName = NameUtil.getRelativeMemberName(
178                 column.getName().getFullName());
179
180             if (!getKey().contains(columnName))
181                 addKeyColumnInternal(column);
182             else
183             {
184                 // this part was blank -- do we want an error or skip here?
185
}
186         }
187         else
188         {
189             throw new ModelException(I18NHelper.getMessage(getMessages(),
190                 "mapping.element.null_argument")); // NOI18N
191
}
192     }
193
194     /** Adds a column to the primary key of columns in this mapping table.
195      * This method is used internally to manipulate primary key columns
196      * that have passed the null and duplicate tests in addKeyColumn and
197      * secondary table key columns when pairs are being set up and ignoring
198      * duplicates is done at the pair level.
199      * @param column column element to be added
200      * @exception ModelException if impossible
201      */

202     protected void addKeyColumnInternal (ColumnElement column) throws ModelException
203     {
204         ArrayList key = getKey();
205         String JavaDoc columnName = NameUtil.getRelativeMemberName(
206             column.getName().getFullName());
207
208         try
209         {
210             fireVetoableChange(PROP_KEY_COLUMNS, null, null);
211             key.add(columnName);
212             firePropertyChange(PROP_KEY_COLUMNS, null, null);
213
214             // sync up runtime's object list too
215
//@olsen: rather clear objects instead of maintaining them
216
//getKeyObjects().add(column);
217
_keyObjects = null;
218         }
219         catch (PropertyVetoException JavaDoc e)
220         {
221             throw new ModelVetoException(e);
222         }
223     }
224
225     /** Removes a column from the primary key of columns in this mapping table.
226      * This method should only be used to manipulate the key columns of the
227      * primary table. The secondary table key columns should be manipulated
228      * using MappingReferenceKeyElement methods for pairs.
229      * @param columnName the relative name of the column to be removed
230      * @exception ModelException if impossible
231      */

232     public void removeKeyColumn (String JavaDoc columnName) throws ModelException
233     {
234         if (columnName != null)
235         {
236             try
237             {
238                 fireVetoableChange(PROP_KEY_COLUMNS, null, null);
239
240                 if (!getKey().remove(columnName))
241                 {
242                     throw new ModelException(
243                         I18NHelper.getMessage(getMessages(),
244                         "mapping.element.element_not_removed", // NOI18N
245
columnName));
246                 }
247
248                 firePropertyChange(PROP_KEY_COLUMNS, null, null);
249
250                 // sync up runtime's object list too
251
//@olsen: rather clear objects instead of maintaining them
252
//getKeyObjects().remove(column);
253
_keyObjects = null;
254             }
255             catch (PropertyVetoException JavaDoc e)
256             {
257                 throw new ModelVetoException(e);
258             }
259         }
260     }
261
262     //===================== reference key handling ===========================
263

264     /** Returns the list of keys (MappingReferenceKeyElements) for this
265      * mapping table. There will be keys for foreign keys and "fake" foreign
266      * keys.
267      * @return the reference key elements for this mapping table
268      */

269     public ArrayList getReferencingKeys ()
270     {
271         if (_referencingKeys == null)
272             _referencingKeys = new ArrayList();
273
274         return _referencingKeys;
275     }
276
277     /** Adds a referencing key to the list of keys in this mapping table.
278      * @param referencingKey referencing key element to be added
279      * @exception ModelException if impossible
280      */

281     public void addReferencingKey (MappingReferenceKeyElement referencingKey)
282         throws ModelException
283     {
284         try
285         {
286             fireVetoableChange(PROP_REFERENCING_KEYS, null, null);
287             getReferencingKeys().add(referencingKey);
288             firePropertyChange(PROP_REFERENCING_KEYS, null, null);
289         }
290         catch (PropertyVetoException JavaDoc e)
291         {
292             throw new ModelVetoException(e);
293         }
294     }
295
296     /** Removes the referencing key for the supplied table element from list
297      * of keys in this mapping table.
298      * @param table mapping table element for which to remove referencing keys
299      * @exception ModelException if impossible
300      */

301     public void removeReference (MappingTableElement table)
302         throws ModelException
303     {
304         if (table != null)
305         {
306             Iterator keyIterator = getReferencingKeys().iterator();
307
308             while (keyIterator.hasNext())
309             {
310                 MappingReferenceKeyElement nextKey =
311                     (MappingReferenceKeyElement)keyIterator.next();
312
313                 if (nextKey.getTable().equals(table))
314                 {
315                     try
316                     {
317                         fireVetoableChange(PROP_REFERENCING_KEYS, null, null);
318                         keyIterator.remove();
319                         firePropertyChange(PROP_REFERENCING_KEYS, null, null);
320                     }
321                     catch (PropertyVetoException JavaDoc e)
322                     {
323                         throw new ModelVetoException(e);
324                     }
325                 }
326             }
327         }
328         else
329         {
330             throw new ModelException(I18NHelper.getMessage(getMessages(),
331                 "mapping.element.null_argument")); // NOI18N
332
}
333     }
334
335     //============= extra object support for runtime ========================
336

337     /** Returns the table element (TableElement) used by this mapping
338      * table. This method should only be used by the runtime.
339      * @return the table element for this mapping table
340      */

341     public TableElement getTableObject ()
342     {
343         if (_tableObject == null)
344         {
345             String JavaDoc absoluteTableName = NameUtil.getAbsoluteTableName(
346                 getDeclaringClass().getDatabaseRoot(), _table);
347
348             _tableObject = TableElement.forName(absoluteTableName);
349         }
350
351         return _tableObject;
352     }
353
354     /** Returns the list of columns (ColumnElements) in the primary key for
355      * this mapping table. This method should only be used by the runtime.
356      * @return the column elements in the primary key for this mapping table
357      */

358     public ArrayList getKeyObjects ()
359     {
360         if (_keyObjects == null)
361         {
362             //@olsen: calculate the key objects based on
363
// the key names as stored in _key
364
//_keyObjects = new ArrayList();
365
_keyObjects = MappingClassElementImpl.toColumnObjects(
366                 getDeclaringClass().getDatabaseRoot(), getKey());
367         }
368
369         return _keyObjects;
370     }
371
372     //=============== extra set methods needed for xml archiver ==============
373

374     /** Set the name of the table element used by this mapping table. This
375      * method should only be used internally and for cloning and archiving.
376      * @param table the table name for this mapping table
377      */

378     public void setTable (String JavaDoc table)
379     {
380         _table = table;
381     }
382
383     /** Set the list of column names in the primary key for this mapping
384      * table. This method should only be used internally and for cloning
385      * and archiving.
386      * @param key the list of names of the columns in the primary key for
387      * this mapping table
388      */

389     public void setKey (ArrayList key) { _key = key; }
390
391     /** Set the list of keys (MappingReferenceKeyElements) for this mapping
392      * table. This method should only be used internally and for cloning
393      * and archiving.
394      * @param referencingKeys the list of reference key elements for this
395      * mapping table
396      */

397     public void setReferencingKeys (ArrayList referencingKeys)
398     {
399         _referencingKeys = referencingKeys;
400     }
401
402     //=============== extra method for Boston -> Pilsen conversion ============
403

404     /** Boston to Pilsen conversion. This method converts the absolute db
405      * element names to relative names. This affects the name of the
406      * MappingTableElement itself and the column names stored in _keys.
407      * The method is recursively called for all MappingReferenceKeyElements
408      * attached to this MappingTableElement.
409      */

410     protected void stripSchemaName ()
411     {
412         // handle _name
413
_name = NameUtil.getRelativeTableName(_name);
414
415         // handle _table
416
_table = NameUtil.getRelativeTableName(_table);
417
418         // handle _referencingKeys
419
// call method stripSchemaName on the MappingReferenceKeyElementImpl
420
// objects
421
if (_referencingKeys != null)
422         {
423             Iterator i = _referencingKeys.iterator();
424
425             while (i.hasNext())
426             {
427                 MappingReferenceKeyElementImpl refKey =
428                     (MappingReferenceKeyElementImpl)i.next();
429
430                 refKey.stripSchemaName();
431             }
432         }
433         
434         // handle _key
435
if (_key != null)
436         {
437             // Use ListIterator here, because I want to replace the value
438
// stored in the ArrayList. The ListIterator returned by
439
// ArrayList.listIterator() supports the set method.
440
ListIterator i = _key.listIterator();
441
442             while (i.hasNext())
443                 i.set(NameUtil.getRelativeMemberName((String JavaDoc)i.next()));
444         }
445     }
446 }
447
Popular Tags