KickJava   Java API By Example, From Geeks To Geeks.

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


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  * MappingReferenceKeyElementImpl.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.ArrayList JavaDoc;
33 import java.util.ListIterator JavaDoc;
34 import java.beans.PropertyVetoException JavaDoc;
35
36 import org.netbeans.modules.dbschema.*;
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.MappingTableElement;
42 import com.sun.jdo.api.persistence.model.mapping.MappingReferenceKeyElement;
43 import com.sun.jdo.spi.persistence.utility.I18NHelper;
44
45 /**
46  *
47  * @author Mark Munro
48  * @author Rochelle Raccah
49  * @version %I%
50  */

51 public class MappingReferenceKeyElementImpl extends MappingMemberElementImpl
52     implements MappingReferenceKeyElement
53 {
54     private ArrayList JavaDoc _referencingKey; // array of column names
55
private MappingTableElement _table;
56
57     /** Create new MappingReferenceKeyElementImpl with no corresponding name.
58      * This constructor should only be used for cloning and archiving.
59      */

60     public MappingReferenceKeyElementImpl ()
61     {
62         this((String JavaDoc)null);
63     }
64
65     /** Creates new MappingReferenceKeyElementImpl with the corresponding name
66      * @param name the name of the element
67      */

68     public MappingReferenceKeyElementImpl (String JavaDoc name)
69     {
70         super(name, null);
71     }
72
73     /** Creates new MappingReferenceKeyElementImpl with a corresponding
74      * mapping table.
75      * @param table mapping table element to be used with this key.
76      */

77     public MappingReferenceKeyElementImpl (MappingTableElement table)
78         throws ModelException
79     {
80         super(table.getName(), table.getDeclaringClass());
81         setTableInternal(table);
82     }
83
84     /** Get the name of this element.
85      * @return the name
86      */

87     public String JavaDoc getKeyName () { return getName(); }
88
89     /** Set the name of this element.
90      * @param name the name
91      * @throws ModelException if impossible
92      */

93     public void setKeyName (String JavaDoc name) throws ModelException
94     {
95         setName(name.toString());
96     }
97
98     //======================= table handling ===========================
99

100     /** Returns the mapping table element for this referencing key.
101      * @return the meta data table for this referencing key
102      */

103     public MappingTableElement getTable () { return _table; }
104     
105     /** Set the mapping table for this referencing key to the supplied table.
106      * @param table mapping table element to be used with this key.
107      * @exception ModelException if impossible
108      */

109     public void setTable (MappingTableElement table) throws ModelException
110     {
111         MappingTableElement old = getTable();
112
113         try
114         {
115             fireVetoableChange(PROP_TABLE, old, table);
116             setTableInternal(table);
117             firePropertyChange(PROP_TABLE, old, table);
118         }
119         catch (PropertyVetoException JavaDoc e)
120         {
121             throw new ModelVetoException(e);
122         }
123     }
124
125     /** Set the mapping table for this referencing key to the supplied table
126      * without firing any property change events.
127      * @param table mapping table element to be used with this key.
128      * @exception ModelException if impossible
129      */

130     private void setTableInternal (MappingTableElement table)
131         throws ModelException
132     {
133         if (table == null)
134         {
135             throw new ModelException(I18NHelper.getMessage(getMessages(),
136                 "mapping.element.null_argument")); // NOI18N
137
}
138
139         _table = table;
140
141         if (null == getDeclaringClass())
142             _declaringClass = table.getDeclaringClass();
143
144         if (null == getName())
145             _name = table.getName();
146     }
147
148     /** Get the declaring table. This method is provided as part of
149      * the implementation of the ReferenceKey interface but should only
150      * be used when a ReferenceKey object is used or by the runtime.
151      * @return the table that owns this reference key element, or
152      * <code>null</code> if the element is not attached to any table
153      */

154     public TableElement getDeclaringTable ()
155     {
156         ArrayList JavaDoc locals = getReferencingKey();
157
158         if ((locals != null) && (locals.size() > 0))
159         {
160             String JavaDoc absoluteName = NameUtil.getAbsoluteMemberName(
161                 getDeclaringClass().getDatabaseRoot(),
162                 locals.get(0).toString());
163
164             return TableElement.forName(NameUtil.getTableName(absoluteName));
165         }
166
167         return null;
168     }
169
170     /** Set the mapping table for this referencing key to the mapping table
171      * based on the name of the supplied table. This method is provided as
172      * part of the implementation of the ReferenceKey interface but should
173      * only be used when a ReferenceKey object is used or by the runtime.
174      * @param table table element to be used with this key.
175      */

176     public void setDeclaringTable (TableElement tableElement)
177     {
178         throw new UnsupportedOperationException JavaDoc();
179     }
180
181     /** Get the referenced table of the reference key. This method is
182      * provided as part of the implementation of the ReferenceKey interface
183      * but should only be used when a ReferenceKey object is used or by
184      * the runtime.
185      * @return the referenced table
186      */

187     public TableElement getReferencedTable ()
188     {
189         ColumnPairElement[] columnPairs = getColumnPairs();
190
191         if ((columnPairs != null) && (columnPairs.length > 0))
192             return columnPairs[0].getReferencedColumn().getDeclaringTable();
193
194         return null;
195     }
196
197     //======================= column handling ===========================
198

199     /** Returns the list of key column names in this referencing key.
200      * This method is private since API users should call the
201      * <code>getColumnPairNames</code> method.
202      * @return the names of the columns in this referencing key
203      */

204     private ArrayList JavaDoc getReferencingKey ()
205     {
206         if (_referencingKey == null)
207             _referencingKey = new ArrayList JavaDoc();
208
209         return _referencingKey;
210     }
211
212     /** Returns the list of relative column pair names in this referencing key.
213      * @return the names of the column pairs in this referencing key
214      */

215     public ArrayList JavaDoc getColumnPairNames ()
216     {
217         ArrayList JavaDoc locals = getReferencingKey();
218         ArrayList JavaDoc foreigns = getTable().getKey();
219         int i, count = ((locals != null) ? locals.size() : 0);
220         ArrayList JavaDoc pairs = new ArrayList JavaDoc();
221
222         for (i = 0; i < count; i++)
223             pairs.add(locals.get(i) + ";" + foreigns.get(i)); // NOI18N
224

225         return pairs;
226     }
227
228     /** Convenience method which takes a pair and returns its index.
229      * @param searchPairName the relative name of the column pair for
230      * which to look
231      * @return the index of the column pair or -1 if not found
232      */

233     private int getIndexOfColumnPair (String JavaDoc searchPairName)
234     {
235         ArrayList JavaDoc myPairs = getColumnPairNames();
236         int count = ((myPairs != null) ? myPairs.size() : 0);
237
238         if (count > 0)
239         {
240             int i;
241
242             for (i = 0; i < count; i++)
243             {
244                 if (myPairs.get(i).equals(searchPairName))
245                     return i;
246             }
247         }
248
249         return -1;
250     }
251
252     /** Adds a column to the list of key columns in this referencing key.
253      * This method is only called privately from addColumnPairs and assumes
254      * that the column is not <code>null</code>.
255      * @param column column element to be added
256      * @exception ModelException if impossible
257      */

258     private void addKeyColumn (ColumnElement column) throws ModelException
259     {
260         ArrayList JavaDoc referencingKey = getReferencingKey();
261         String JavaDoc columnName = NameUtil.getRelativeMemberName(
262             column.getName().getFullName());
263
264         try
265         {
266             fireVetoableChange(PROP_KEY_COLUMNS, null, null);
267             referencingKey.add(columnName);
268             firePropertyChange(PROP_KEY_COLUMNS, null, null);
269         }
270         catch (PropertyVetoException JavaDoc e)
271         {
272             throw new ModelVetoException(e);
273         }
274     }
275
276     /** Get all local columns in this reference key. This method is
277      * provided as part of the implementation of the ReferenceKey interface
278      * but should only be used when a ReferenceKey object is used or by
279      * the runtime.
280      * @return the columns
281      */

282     public ColumnElement[] getLocalColumns ()
283     {
284         ColumnPairElement[] columnPairs = getColumnPairs();
285         int i, count = ((columnPairs != null) ? columnPairs.length : 0);
286         ColumnElement[] columns = new ColumnElement[count];
287
288         for (i = 0; i < count ; i++)
289             columns[i] = columnPairs[i].getLocalColumn();
290
291         return columns;
292     }
293
294     /** Get all referenced columns in this reference key. This method is
295      * provided as part of the implementation of the ReferenceKey interface
296      * but should only be used when a ReferenceKey object is used or by
297      * the runtime.
298      * @return the columns
299      */

300     public ColumnElement[] getReferencedColumns ()
301     {
302         ColumnPairElement[] columnPairs = getColumnPairs();
303         int i, count = ((columnPairs != null) ? columnPairs.length : 0);
304         ColumnElement[] columns = new ColumnElement[count];
305
306         for (i = 0; i < count ; i++)
307             columns[i] = columnPairs[i].getReferencedColumn();
308
309         return columns;
310     }
311
312     /** Remove a column pair from the holder. This method can be used to
313      * remove a pair by name when it cannot be resolved to an actual pair.
314      * @param pairName the relative name of the column pair to remove
315      * @throws ModelException if impossible
316      */

317     public void removeColumnPair (String JavaDoc pairName) throws ModelException
318     {
319         ArrayList JavaDoc pairNames = new ArrayList JavaDoc(1);
320
321         pairNames.add(pairName);
322         removeColumnPairs(pairNames);
323     }
324
325     /** Remove some column pairs from the holder. This method can be used to
326      * remove pairs by name when they cannot be resolved to actual pairs.
327      * @param pairNames the relative names of the column pairs to remove
328      * @throws ModelException if impossible
329      */

330     public void removeColumnPairs (ArrayList JavaDoc pairNames) throws ModelException
331     {
332         ArrayList JavaDoc refKey = getReferencingKey();
333         ArrayList JavaDoc key = getTable().getKey();
334         int i, count = ((pairNames != null) ? pairNames.size() : 0);
335
336         for (i = 0; i < count ; i++)
337         {
338             String JavaDoc pairName = (String JavaDoc)pairNames.get(i);
339             int index = getIndexOfColumnPair(pairName);
340
341             if (pairName != null)
342             {
343                 try
344                 {
345                     Object JavaDoc remove1 = null, remove2 = null;
346
347                     fireVetoableChange(PROP_KEY_COLUMNS, null, null);
348
349                     remove1 = key.remove(index);
350                     remove2 = refKey.remove(index);
351
352                     if ((remove1 == null) || (remove2 == null))
353                     {
354                         // if only 1 failed, put the other one back
355
if (remove1 != null)
356                             key.add(index, remove1);
357                         else if (remove2 != null)
358                             refKey.add(index, remove2);
359
360                         throw new ModelException(I18NHelper.getMessage(
361                             getMessages(),
362                             "mapping.element.element_not_removed", // NOI18N
363
pairName));
364                     }
365
366                     firePropertyChange(PROP_KEY_COLUMNS, null, null);
367                 }
368                 catch (PropertyVetoException JavaDoc e)
369                 {
370                     throw new ModelVetoException(e);
371                 }
372             }
373         }
374     }
375
376     //============= implementation of ColumnPairElementHolder ===============
377

378     /** Add a new column pair to the holder.
379      * @param pair the pair to add
380      * @throws ModelException if impossible
381      */

382     public void addColumnPair (ColumnPairElement pair) throws ModelException
383     {
384         addColumnPairs(new ColumnPairElement[]{pair});
385     }
386
387     /** Add some new column pairs to the holder.
388      * @param pairs the column pairs to add
389      * @throws ModelException if impossible
390      */

391     public void addColumnPairs (ColumnPairElement[] pairs) throws ModelException
392     {
393         MappingTableElementImpl table = (MappingTableElementImpl)getTable();
394         int i, count = ((pairs != null) ? pairs.length : 0);
395
396         for (i = 0; i < count; i++)
397         {
398             ColumnPairElement pair = (ColumnPairElement)pairs[i];
399
400             if (pair != null)
401             {
402                 // check if entire pair matches
403
// OK only add it if it has not been added before.
404
if (getIndexOfColumnPair(NameUtil.getRelativeMemberName(
405                     pair.getName().getFullName())) == -1)
406                 {
407                     table.addKeyColumnInternal(pair.getReferencedColumn());
408                     addKeyColumn(pair.getLocalColumn());
409                 }
410                 else
411                 {
412                     // this part was blank -- do we want an error or skip here?
413
}
414             }
415             else
416             {
417                 throw new ModelException(I18NHelper.getMessage(getMessages(),
418                     "mapping.element.null_argument")); // NOI18N
419
}
420         }
421     }
422
423     /** Remove a column pair from the holder.
424      * @param pair the column pair to remove
425      * @throws ModelException if impossible
426      */

427     public void removeColumnPair (ColumnPairElement pair) throws ModelException
428     {
429         removeColumnPairs(new ColumnPairElement[]{pair});
430     }
431
432     /** Remove some column pairs from the holder.
433      * @param pairs the column pairs to remove
434      * @throws ModelException if impossible
435      */

436     public void removeColumnPairs (ColumnPairElement[] pairs)
437         throws ModelException
438     {
439         ArrayList JavaDoc pairNames = new ArrayList JavaDoc();
440         int i, count = ((pairs != null) ? pairs.length : 0);
441
442         for (i = 0; i < count ; i++)
443         {
444             ColumnPairElement pair = (ColumnPairElement)pairs[i];
445
446             pairNames.add(NameUtil.getRelativeMemberName(
447                 pair.getName().getFullName()));
448         }
449
450         removeColumnPairs(pairNames);
451     }
452
453     /** Set the column pairs for this holder.
454      * Previous column pairs are removed.
455      * @param pairs the new column pairs
456      * @throws ModelException if impossible
457      */

458     public void setColumnPairs (ColumnPairElement[] pairs) throws ModelException
459     {
460         removeColumnPairs(getColumnPairNames()); // remove the old ones
461
addColumnPairs(pairs); // add the new ones
462
}
463
464     /** Get all column pairs in this holder.
465      * @return the column pairs
466      */

467     public ColumnPairElement[] getColumnPairs ()
468     {
469         ArrayList JavaDoc pairNames = getColumnPairNames();
470         TableElement table = getDeclaringTable();
471         int i, count = ((pairNames != null) ? pairNames.size() : 0);
472         ColumnPairElement[] pairs = new ColumnPairElement[count];
473         String JavaDoc databaseRoot = getDeclaringClass().getDatabaseRoot();
474
475         for (i = 0; i < count; i++)
476         {
477             String JavaDoc absoluteName = NameUtil.getAbsoluteMemberName(
478                 databaseRoot, (String JavaDoc)pairNames.get(i));
479
480             pairs[i] = (ColumnPairElement)table.getMember(
481                 DBIdentifier.create(absoluteName));
482         }
483
484         return pairs;
485     }
486
487     /** Find a column pair by name.
488      * @param name the name of the column pair for which to look
489      * @return the column pair or <code>null</code> if not found
490      */

491     public ColumnPairElement getColumnPair (DBIdentifier name)
492     {
493         ColumnPairElement[] myPairs = getColumnPairs();
494         int count = ((myPairs != null) ? myPairs.length : 0);
495         String JavaDoc databaseRoot = getDeclaringClass().getDatabaseRoot();
496
497         if (count > 0)
498         {
499             String JavaDoc absoluteTableName = NameUtil.getAbsoluteTableName(
500                 databaseRoot, getTable().getName());
501             ColumnPairElement searchPair = (ColumnPairElement)
502                 TableElement.forName(absoluteTableName).getMember(name);
503             int i;
504
505             for (i = 0; i < count; i++)
506             {
507                 if (myPairs[i].equals(searchPair))
508                     return searchPair;
509             }
510         }
511
512         return null;
513     }
514
515     //=============== extra set methods needed for xml archiver ==============
516

517     /** Set the list of of key column names in this referencing key. This
518      * method should only be used internally and for cloning and archiving.
519      * @param referencingKey the list of names of the columns in this
520      * referencing key
521      */

522     public void setReferencingKey (ArrayList JavaDoc referencingKey)
523     {
524         _referencingKey = referencingKey;
525     }
526
527     //============== extra methods for Boston -> Pilsen conversion ============
528

529     /** Boston to Pilsen conversion.
530      * This method converts the name of this MappingReferenceKeyElement and
531      * the absolute column names stored in _referencingKey to relative names.
532      */

533     protected void stripSchemaName ()
534     {
535         // handle _name (use getRelativeTableName since the name is derived
536
// from the name of the participating table)
537
_name = NameUtil.getRelativeTableName(_name);
538
539         // handle _referencingKey
540
if (_referencingKey != null)
541         {
542             // Use ListIterator here, because I want to replace the value
543
// stored in the ArrayList. The ListIterator returned by
544
// ArrayList.listIterator() supports the set method.
545
ListIterator JavaDoc i = _referencingKey.listIterator();
546
547             while (i.hasNext())
548                 i.set(NameUtil.getRelativeMemberName((String JavaDoc)i.next()));
549         }
550     }
551 }
552
Popular Tags