KickJava   Java API By Example, From Geeks To Geeks.

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


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  * MappingFieldElementImpl.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.DBMemberElement;
36 import org.netbeans.modules.dbschema.TableElement;
37 import org.netbeans.modules.dbschema.util.NameUtil;
38
39 import com.sun.jdo.api.persistence.model.*;
40 import com.sun.jdo.api.persistence.model.mapping.*;
41 import com.sun.jdo.api.persistence.model.jdo.*;
42 import com.sun.jdo.spi.persistence.utility.I18NHelper;
43 import com.sun.jdo.spi.persistence.utility.JavaTypeHelper;
44
45 /**
46  *
47  * @author Mark Munro
48  * @author Rochelle Raccah
49  * @version %I%
50  */

51 public class MappingFieldElementImpl extends MappingMemberElementImpl
52     implements MappingFieldElement
53 {
54     private ArrayList _columns; // array of member names (columns or pairs)
55

56     //@olsen: made transient to prevent from serializing into mapping files
57
private transient ArrayList _columnObjects; // array of DBMemberElement (for runtime)
58

59     private int _fetchGroup;
60     private int _properties;
61
62     /** Version field flag of the field element. */
63     private boolean _isVersion;
64
65     // leave for runtime
66
public static final int CLONE_FIELD = 1;
67     public static final int CLONE_DEEP = 2;
68     public static final int CLONE_MASK = 3;
69     public static final int LOG_ON_ACCESS = 4;
70     public static final int LOG_ON_MASK = 48;
71     public static final int LOG_ON_UPDATE = 16;
72     public static final int MOD_BI_ON_UPDATE = 32;
73     public static final int OBSERVE_ON_ACCESS = 8;
74     public static final int RECORD_ON_UPDATE = 64;
75     public static final int SEND_BEFORE_IMAGE = 128;
76     public static final int READ_ONLY = 256;
77     public static final int REF_INTEGRITY_UPDATES = 512;
78     public static final int IN_CONCURRENCY_CHECK = 1024;
79     public static final int XLATE_FIELD = 2048;
80
81     /** Create new MappingFieldElementImpl with no corresponding name or
82      * declaring class. This constructor should only be used for cloning and
83      * archiving.
84      */

85     public MappingFieldElementImpl ()
86     {
87         this(null, null);
88     }
89
90     /** Create new MappingFieldElementImpl with the corresponding name and
91      * declaring class.
92      * @param name the name of the element
93      * @param declaringClass the class to attach to
94      */

95     public MappingFieldElementImpl (String JavaDoc name,
96         MappingClassElement declaringClass)
97     {
98         super(name, declaringClass);
99         setFetchGroupInternal(GROUP_DEFAULT);
100     }
101
102 // TBD?
103
/* public boolean mapped ()
104     {
105         ArrayList columns = getColumns();
106
107         return ((columns != null) && (columns.size() > 0));
108     }
109
110     public void clear () { _columns = null; }*/

111 // end TBD
112

113     //=================== properties exposed in interface ==================
114

115     /** Determines whether this field element is read only or not.
116      * @return <code>true</code> if the field is read only,
117      * <code>false</code> otherwise
118      */

119     public boolean isReadOnly () { return getProperty(READ_ONLY); }
120
121     /** Set whether this field element is read only or not.
122      * @param flag - if <code>true</code>, the field element is marked as
123      * read only; otherwise, it is not
124      * @exception ModelException if impossible
125      */

126     public void setReadOnly (boolean flag) throws ModelException
127     {
128         Boolean JavaDoc old = JavaTypeHelper.valueOf(isReadOnly());
129         Boolean JavaDoc newFlag = JavaTypeHelper.valueOf(flag);
130
131         try
132         {
133             fireVetoableChange(PROP_READ_ONLY, old, newFlag);
134             setProperty(flag, READ_ONLY);
135             firePropertyChange(PROP_READ_ONLY, old, newFlag);
136         }
137         catch (PropertyVetoException JavaDoc e)
138         {
139             throw new ModelVetoException(e);
140         }
141     }
142
143     /** Determines whether this field element is in a concurrency check or not.
144      * @return <code>true</code> if the field is in a concurrency check,
145      * <code>false</code> otherwise
146      */

147     public boolean isInConcurrencyCheck ()
148     {
149         return getProperty(IN_CONCURRENCY_CHECK);
150     }
151
152     /** Set whether this field element is in a concurrency check or not.
153      * @param flag - if <code>true</code>, the field element is marked as
154      * being in a concurrency check; otherwise, it is not
155      * @exception ModelException if impossible
156      */

157     public void setInConcurrencyCheck (boolean flag) throws ModelException
158     {
159         Boolean JavaDoc old = JavaTypeHelper.valueOf(isInConcurrencyCheck());
160         Boolean JavaDoc newFlag = JavaTypeHelper.valueOf(flag);
161
162         try
163         {
164             fireVetoableChange(PROP_IN_CONCURRENCY_CHECK, old, newFlag);
165             setProperty(flag, IN_CONCURRENCY_CHECK);
166             firePropertyChange(PROP_IN_CONCURRENCY_CHECK, old, newFlag);
167         }
168         catch (PropertyVetoException JavaDoc e)
169         {
170             throw new ModelVetoException(e);
171         }
172     }
173
174     /** Determines whether this field element is a version field or not.
175      * @return <code>true</code> if the field is a version field,
176      * <code>false</code> otherwise
177      */

178     public boolean isVersion () { return _isVersion; }
179
180     /** Set whether this field element is a version field or not.
181      * @param flag - if <code>true</code>, the field element is marked
182      * as a version field; otherwise, it is not
183      * @exception ModelException if impossible
184      */

185     public void setVersion (boolean flag) throws ModelException
186     {
187         Boolean JavaDoc old = JavaTypeHelper.valueOf(isVersion());
188         Boolean JavaDoc newFlag = JavaTypeHelper.valueOf(flag);
189
190         try
191         {
192             fireVetoableChange(PROP_VERSION_FIELD, old, newFlag);
193             _isVersion = flag;
194             firePropertyChange(PROP_VERSION_FIELD, old, newFlag);
195         }
196         catch (PropertyVetoException JavaDoc e)
197         {
198             throw new ModelVetoException(e);
199         }
200     }
201
202     //======================= fetch group handling ======================
203

204     /** Get the fetch group of this field element.
205      * @return the fetch group, one of {@link #GROUP_DEFAULT},
206      * {@link #GROUP_NONE}, or anything less than or equal to
207      * {@link #GROUP_INDEPENDENT}
208      */

209     public int getFetchGroup () { return _fetchGroup; }
210
211     /** Set the fetch group of this field element.
212      * @param group - an integer indicating the fetch group, one of:
213      * {@link #GROUP_DEFAULT}, {@link #GROUP_NONE}, or anything less than or
214      * equal to {@link #GROUP_INDEPENDENT}
215      * @exception ModelException if impossible
216      */

217     public void setFetchGroup (int group) throws ModelException
218     {
219         Integer JavaDoc old = new Integer JavaDoc(getFetchGroup());
220         Integer JavaDoc newGroup = new Integer JavaDoc(group);
221
222         try
223         {
224             fireVetoableChange(PROP_FETCH_GROUP, old, newGroup);
225             setFetchGroupInternal(group);
226             firePropertyChange(PROP_FETCH_GROUP, old, newGroup);
227         }
228         catch (PropertyVetoException JavaDoc e)
229         {
230             throw new ModelVetoException(e);
231         }
232     }
233
234     /** Set the fetch group of this field element. Meant to be used in the
235      * constructor and by subclasses when there should be no exceptions and
236      * no property change events fired.
237      * @param group - an integer indicating the fetch group, one of:
238      * {@link #GROUP_DEFAULT}, {@link #GROUP_NONE}, or anything less than or
239      * equal to {@link #GROUP_INDEPENDENT}
240      */

241     protected void setFetchGroupInternal (int group)
242     {
243         _fetchGroup = group;
244     }
245
246     //========================== column handling ==========================
247

248     /** Returns the list of column names to which this mapping field is
249      * mapped.
250      * @return the names of the columns mapped by this mapping field
251      */

252     public ArrayList getColumns ()
253     {
254         if (_columns == null)
255             _columns = new ArrayList();
256
257         return _columns;
258     }
259
260     /** Adds a column to the list of columns mapped by this mapping field.
261      * @param column column element to be added to the mapping
262      * @exception ModelException if impossible
263      */

264     public void addColumn (DBMemberElement column) throws ModelException
265     {
266         if (column != null)
267         {
268             ArrayList columns = getColumns();
269             String JavaDoc columnName = NameUtil.getRelativeMemberName(
270                 column.getName().getFullName());
271
272             if (!columns.contains(columnName))
273             {
274                 try
275                 {
276                     fireVetoableChange(PROP_COLUMNS, null, null);
277                     columns.add(columnName);
278                     firePropertyChange(PROP_COLUMNS, null, null);
279
280                     // sync up runtime's object list too
281
_columnObjects = null;
282                 }
283                 catch (PropertyVetoException JavaDoc e)
284                 {
285                     throw new ModelVetoException(e);
286                 }
287             }
288             else
289             {
290                 // this part was blank -- do we want an error or skip here?
291
}
292         }
293         else
294         {
295             throw new ModelException(I18NHelper.getMessage(getMessages(),
296                 "mapping.element.null_argument")); // NOI18N
297
}
298     }
299
300     /** Removes a column from the list of columns mapped by this mapping field.
301      * This method overrides the one in MappingFieldElement to
302      * remove the argument from the associated columns if necessary.
303      * @param columnName the relative name of the column to be removed from
304      * the mapping
305      * @exception ModelException if impossible
306      */

307     public void removeColumn (String JavaDoc columnName) throws ModelException
308     {
309         if (columnName != null)
310         {
311             try
312             {
313                 fireVetoableChange(PROP_COLUMNS, null, null);
314
315                 if (!getColumns().remove(columnName))
316                 {
317                     throw new ModelException(
318                         I18NHelper.getMessage(getMessages(),
319                         "mapping.element.element_not_removed", // NOI18N
320
columnName));
321                 }
322
323                 firePropertyChange(PROP_COLUMNS, null, null);
324
325                 // sync up runtime's object list too
326
_columnObjects = null;
327             }
328             catch (PropertyVetoException JavaDoc e)
329             {
330                 throw new ModelVetoException(e);
331             }
332         }
333     }
334
335     protected boolean isMappedToTable (MappingTableElement table)
336     {
337         String JavaDoc tableName = table.getName();
338         Iterator iterator = getColumns().iterator();
339
340         while (iterator.hasNext())
341         {
342             String JavaDoc columnName = iterator.next().toString();
343
344             if (NameUtil.getTableName(columnName).equals(tableName))
345                 return true;
346         }
347
348         return false;
349     }
350
351     //============= extra object support for runtime ========================
352

353     /** Returns the list of columns (ColumnElements) to which this mapping
354      * field is mapped. This method should only be used by the runtime.
355      * @return the columns mapped by this mapping field
356      */

357     public ArrayList getColumnObjects ()
358     {
359         //@olsen: compute objects on access
360
if (_columnObjects == null)
361         {
362             //@olsen: calculate the column objects based on
363
// the column names as stored in _columns
364
//_columnObjects = new ArrayList();
365
_columnObjects = MappingClassElementImpl.toColumnObjects(
366                 getDeclaringClass().getDatabaseRoot(), getColumns());
367         }
368
369         return _columnObjects;
370     }
371
372     //============= delegation to PersistenceFieldElement ===========
373

374     final PersistenceFieldElement getPersistenceFieldElement ()
375     {
376         return ((MappingClassElementImpl)getDeclaringClass()).
377             getPersistenceElement().getField(getName());
378     }
379
380     /** Computes the field number of this field element.
381      * @return the field number of this field
382      */

383     public int getFieldNumber ()
384     {
385         return getPersistenceFieldElement().getFieldNumber();
386     }
387
388     /** Returns the array of concurrency groups to which this field belongs.
389      * @return the concurrency groups in which this field participates
390      * @see PersistenceClassElement#getConcurrencyGroups
391      */

392     public ConcurrencyGroupElement[] getConcurrencyGroups ()
393     {
394         return getPersistenceFieldElement().getConcurrencyGroups();
395     }
396
397     //================ convenience methods for properties bits ===============
398

399     private boolean getProperty (int propertyBit)
400     {
401         return ((getProperties() & propertyBit) > 0);
402     }
403
404     public void setProperty (boolean flag, int propertyBit)
405     {
406         _properties =
407             (flag) ? (_properties | propertyBit) : (_properties & ~propertyBit);
408     }
409
410     //================= properties not available in interface ================
411

412     public int getProperties () { return _properties;}
413
414     public boolean getLogOnAccess () { return getProperty(LOG_ON_ACCESS); }
415
416     public void setLogOnAccess (boolean flag)
417     {
418         setProperty(flag, LOG_ON_ACCESS);
419     }
420
421     public boolean getLogOnUpdate () { return getProperty(LOG_ON_UPDATE); }
422
423     public void setLogOnUpdate (boolean flag)
424     {
425         setProperty(flag, LOG_ON_UPDATE);
426     }
427
428     public boolean getObserveOnAccess ()
429     {
430         return getProperty(OBSERVE_ON_ACCESS);
431     }
432
433     public void setObserveOnAccess (boolean flag)
434     {
435         setProperty(flag, OBSERVE_ON_ACCESS);
436     }
437
438     public boolean getRecordOnUpdate ()
439     {
440         return getProperty(RECORD_ON_UPDATE);
441     }
442
443     public void setRecordOnUpdate (boolean flag)
444     {
445         setProperty(flag, RECORD_ON_UPDATE);
446     }
447
448     public boolean getModifyBeforeImageOnUpdate ()
449     {
450         return getProperty(MOD_BI_ON_UPDATE);
451     }
452
453     public void setModifyBeforeImageOnUpdate (boolean flag)
454     {
455         setProperty(flag, MOD_BI_ON_UPDATE);
456     }
457
458     public boolean getReferentialIntegrityUpdates ()
459     {
460         return getProperty(REF_INTEGRITY_UPDATES);
461     }
462
463     public void setReferentialIntegrityUpdates (boolean flag)
464     {
465         setProperty(flag, REF_INTEGRITY_UPDATES);
466     }
467
468     public boolean getSendBeforeImage ()
469     {
470         return getProperty(SEND_BEFORE_IMAGE);
471     }
472     public void setSendBeforeImage (boolean flag)
473     {
474         setProperty(flag, SEND_BEFORE_IMAGE);
475     }
476
477     public int getCloneDepth () { return (_properties & CLONE_MASK); }
478
479     public void setCloneDepth (int cloneDepth)
480     {
481         if (cloneDepth < CLONE_FIELD || cloneDepth > CLONE_DEEP)
482         {
483         }
484
485         _properties = _properties & ~CLONE_MASK | cloneDepth;
486     }
487
488     //============== extra methods for Boston -> Pilsen conversion ============
489

490     /** Boston to Pilsen conversion.
491      * This method converts the absolute column names to relative names.
492      */

493     protected void stripSchemaName ()
494     {
495         if (_columns != null) // handle _columns
496
{
497             // Use ListIterator here, because I want to replace the value
498
// stored in the ArrayList. The ListIterator returned by
499
// ArrayList.listIterator() supports the set method.
500
ListIterator i = _columns.listIterator();
501
502             while (i.hasNext())
503                 i.set(NameUtil.getRelativeMemberName((String JavaDoc)i.next()));
504         }
505     }
506 }
507
Popular Tags