KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > ejb > cmp3 > metadata > accessors > ObjectAccessor


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

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors;
23
24 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.ClassAccessor;
25 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.objects.MetadataAccessibleObject;
26
27 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.columns.MetadataJoinColumn;
28 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.columns.MetadataJoinColumns;
29 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.columns.MetadataPrimaryKeyJoinColumn;
30
31 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataConstants;
32 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataDescriptor;
33 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataLogger;
34
35 import oracle.toplink.essentials.internal.helper.DatabaseField;
36
37 import oracle.toplink.essentials.mappings.OneToOneMapping;
38
39 import java.util.List JavaDoc;
40
41 /**
42  * A single object relationship accessor.
43  *
44  * @author Guy Pelletier
45  * @since TopLink EJB 3.0 Reference Implementation
46  */

47 public abstract class ObjectAccessor extends RelationshipAccessor {
48     protected ObjectAccessor(MetadataAccessibleObject accessibleObject, ClassAccessor classAccessor) {
49         super(accessibleObject, classAccessor);
50     
51         // Override the following default settings from the parent.
52
setIsOptional(true);
53         setFetchType(MetadataConstants.EAGER);
54     }
55     
56     /**
57      * INTERNAL:
58      * Initialize a OneToOneMapping.
59      */

60     protected OneToOneMapping initOneToOneMapping() {
61         OneToOneMapping mapping = new OneToOneMapping();
62         mapping.setIsReadOnly(false);
63         mapping.setIsPrivateOwned(false);
64         mapping.setIsOptional(isOptional());
65         mapping.setAttributeName(getAttributeName());
66         mapping.setReferenceClassName(getReferenceClassName());
67         
68         // If the global weave for value holders is true, the use the value
69
// from usesIndirection. Otherwise, force it to false.
70
boolean usesIndirection = (m_project.enableLazyForOneToOne()) ? usesIndirection() : false;
71         mapping.setUsesIndirection(usesIndirection);
72         
73         // Set the getter and setter methods if access is PROPERTY and the
74
// mapping doesn't use indirection.
75
setAccessorMethods(mapping);
76         
77         // Process the cascade types.
78
processCascadeTypes(mapping);
79         
80         return mapping;
81     }
82     
83     /**
84      * INTERNAL:
85      * Process the the correct metadata join column for the owning side of a
86      * one to one mapping.
87      */

88     protected void processOwningMappingKeys(OneToOneMapping mapping) {
89         if (isOneToOnePrimaryKeyRelationship()) {
90             processOneToOnePrimaryKeyRelationship(mapping);
91         } else {
92             processOneToOneForeignKeyRelationship(mapping);
93         }
94     }
95     
96     /**
97      * INTERNAL:
98      * Process the @JoinColumn(s) for the owning side of a one to one mapping.
99      * The default pk and pk field names are used only with single primary key
100      * entities. The processor should never get as far as to use them with
101      * entities that have a composite primary key (validation exception will be
102      * thrown).
103      */

104     protected void processOneToOneForeignKeyRelationship(OneToOneMapping mapping) {
105         // If the pk field (referencedColumnName) is not specified, it
106
// defaults to the primary key of the referenced table.
107
String JavaDoc defaultPKFieldName = getReferenceDescriptor().getPrimaryKeyFieldName();
108         
109         // If the fk field (name) is not specified, it defaults to the
110
// concatenation of the following: the name of the referencing
111
// relationship property or field of the referencing entity; "_";
112
// the name of the referenced primary key column.
113
String JavaDoc defaultFKFieldName = getUpperCaseAttributeName() + "_" + defaultPKFieldName;
114             
115         // Join columns will come from a @JoinColumn(s).
116
List JavaDoc<MetadataJoinColumn> joinColumns = processJoinColumns();
117
118         // Add the source foreign key fields to the mapping.
119
for (MetadataJoinColumn joinColumn : joinColumns) {
120             DatabaseField pkField = joinColumn.getPrimaryKeyField();
121             pkField.setName(getName(pkField, defaultPKFieldName, MetadataLogger.PK_COLUMN));
122             pkField.setTableName(getReferenceDescriptor().getPrimaryTableName());
123             
124             DatabaseField fkField = joinColumn.getForeignKeyField();
125             fkField.setName(getName(fkField, defaultFKFieldName, MetadataLogger.FK_COLUMN));
126             // Set the table name if one is not already set.
127
if (fkField.getTableName().equals("")) {
128                 fkField.setTableName(m_descriptor.getPrimaryTableName());
129             }
130             
131             // Add a source foreign key to the mapping.
132
mapping.addForeignKeyField(fkField, pkField);
133             
134             // If any of the join columns is marked read-only then set the
135
// mapping to be read only.
136
if (fkField.isReadOnly()) {
137                 mapping.setIsReadOnly(true);
138             }
139         }
140     }
141     
142     /**
143      * INTERNAL:
144      * Process the primary key join columns for the owning side of a one to one
145      * mapping. The default pk and pk field names are used only with single
146      * primary key entities. The processor should never get as far as to use
147      * them with entities that have a composite primary key (validation
148      * exception will be thrown).
149      */

150     protected void processOneToOnePrimaryKeyRelationship(OneToOneMapping mapping) {
151         // Join columns will come from a @PrimaryKeyJoinColumn(s).
152
MetadataDescriptor referenceDescriptor = getReferenceDescriptor();
153         List JavaDoc<MetadataPrimaryKeyJoinColumn> primaryKeyJoinColumns = processPrimaryKeyJoinColumns(getPrimaryKeyJoinColumns(referenceDescriptor.getPrimaryTableName(), m_descriptor.getPrimaryTableName()));
154
155         // Add the source foreign key fields to the mapping.
156
for (MetadataPrimaryKeyJoinColumn primaryKeyJoinColumn : primaryKeyJoinColumns) {
157             // The default primary key name is the primary key field name of the
158
// referenced entity.
159
DatabaseField pkField = primaryKeyJoinColumn.getPrimaryKeyField();
160             pkField.setName(getName(pkField, referenceDescriptor.getPrimaryKeyFieldName(), m_logger.PK_COLUMN));
161             
162             // The default foreign key name is the primary key of the
163
// referencing entity.
164
DatabaseField fkField = primaryKeyJoinColumn.getForeignKeyField();
165             fkField.setName(getName(fkField, m_descriptor.getPrimaryKeyFieldName(), m_logger.FK_COLUMN));
166             
167             // Add a source foreign key to the mapping.
168
mapping.addForeignKeyField(fkField, pkField);
169             
170             // Mark the mapping read only
171
mapping.setIsReadOnly(true);
172         }
173     }
174     
175     /**
176      * INTERNAL:
177      * Process the @JoinColumn(s) for the owning side of a one to one mapping.
178      */

179      /* SAVED
180     protected void processOwningMappingKeys(OneToOneMapping mapping) {
181         // The default pk and pk field names are used only with single primary
182         // key entities. The processor should never get as far as to use them
183         // with entities that have a composite primary key (validation
184         // exception will be thrown).
185         String defaultFKFieldName;
186         String defaultPKFieldName;
187         List<MetadataJoinColumn> joinColumns;
188         MetadataDescriptor referenceDescriptor = getReferenceDescriptor();
189         
190         if (isOneToOnePrimaryKeyRelationship()) {
191             processOneToOnePrimaryKeyRelationship(mapping);
192             
193             // The same name as the primary key field of the referenced entity.
194             defaultPKFieldName = referenceDescriptor.getPrimaryKeyFieldName();
195             
196             // The same name as the primary key field of the referencing entity.
197             defaultFKFieldName = m_descriptor.getPrimaryKeyFieldName();
198             
199             // Join columns will come from a @PrimaryKeyJoinColumn(s).
200             // Guy here
201             //List<MetadataPrimaryJoinColumns> jColumns = processPrimaryKeyJoinColumns(getPrimaryKeyJoinColumns(referenceDescriptor.getPrimaryTableName(), getDescriptor().getPrimaryTableName()));
202             
203             joinColumns = processPrimaryKeyJoinColumns(referenceDescriptor.getPrimaryTableName(), m_descriptor.getPrimaryTableName());
204         } else {
205             // If the pk field (referencedColumnName) is not specified, it
206             // defaults to the primary key of the referenced table.
207             defaultPKFieldName = referenceDescriptor.getPrimaryKeyFieldName();
208         
209             // If the fk field (name) is not specified, it defaults to the
210             // concatenation of the following: the name of the referencing
211             // relationship property or field of the referencing entity; "_";
212             // the name of the referenced primary key column.
213             defaultFKFieldName = getUpperCaseAttributeName() + "_" + defaultPKFieldName;
214             
215             // Join columns will come from a @JoinColumn(s).
216             joinColumns = processJoinColumns();
217         }
218
219         // Add the source foreign key fields to the mapping.
220         for (MetadataJoinColumn mjc : joinColumns) {
221             DatabaseField pkField = mjc.getPrimaryKeyField();
222             pkField.setName(getName(pkField, defaultPKFieldName, MetadataLogger.PK_COLUMN));
223             
224             DatabaseField fkField = mjc.getForeignKeyField();
225             fkField.setName(getName(fkField, defaultFKFieldName, MetadataLogger.FK_COLUMN));
226             
227             // Add a source foreign key to the mapping.
228             mapping.addForeignKeyField(fkField, pkField);
229             
230             // If this is a one to one using a primary key association, or if
231             // any of the join columns is marked read-only then set the mapping
232             // to be read only.
233             if (isOneToOnePrimaryKeyRelationship() || fkField.isReadOnly()) {
234                 mapping.setIsReadOnly(true);
235             }
236         }
237     }
238      */

239 }
240
Popular Tags