KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > ejb > ejbc > EjbConversionHelper


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  * EjbConversionHelper.java
26  *
27  * Created on March 19, 2002
28  */

29
30 package com.sun.jdo.spi.persistence.support.ejb.ejbc;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Set JavaDoc;
38
39 import com.sun.enterprise.deployment.CMRFieldInfo;
40 import com.sun.enterprise.deployment.EjbBundleDescriptor;
41 import com.sun.enterprise.deployment.PersistenceDescriptor;
42 import com.sun.enterprise.deployment.PersistentFieldInfo;
43 import com.sun.enterprise.deployment.RelationshipDescriptor;
44 import com.sun.enterprise.deployment.RelationRoleDescriptor;
45
46 import com.sun.enterprise.deployment.IASEjbCMPEntityDescriptor;
47
48 import com.sun.jdo.api.persistence.mapping.ejb.ConversionHelper;
49
50 import com.sun.jdo.spi.persistence.support.ejb.model.util.NameMapper;
51
52 /*
53  * This class implements ConversionHelper interface by using data from
54  * IASEjbBundleDescriptor.
55  *
56  * @author Shing Wai Chan
57  */

58 public class EjbConversionHelper implements ConversionHelper {
59
60     private NameMapper nameMapper = null;
61     private EjbBundleDescriptor bundle = null;
62     private HashMap JavaDoc ejbDescMap = new HashMap JavaDoc();
63     private HashMap JavaDoc ejbFieldMap = new HashMap JavaDoc();
64     private HashMap JavaDoc ejbKeyMap = new HashMap JavaDoc();
65     private HashMap JavaDoc ejbPerDescMap = new HashMap JavaDoc();
66     private HashMap JavaDoc ejbRelMap = new HashMap JavaDoc();
67     boolean generateFields = true;
68     boolean ensureValidation = true;
69
70     public EjbConversionHelper(NameMapper nameMapper) {
71         this.nameMapper = nameMapper;
72         this.bundle = nameMapper.getBundleDescriptor();
73
74         Iterator JavaDoc iter = bundle.getEjbs().iterator();
75         while (iter.hasNext()) {
76             Object JavaDoc desc = iter.next();
77             if (desc instanceof IASEjbCMPEntityDescriptor) {
78                 IASEjbCMPEntityDescriptor ejbDesc =
79                         (IASEjbCMPEntityDescriptor)desc;
80
81                 String JavaDoc ejbName = ejbDesc.getName();
82                 //collect all ejbdesc
83
ejbDescMap.put(ejbName, ejbDesc);
84
85                 //collect PersistenceDescriptor
86
PersistenceDescriptor pers = ejbDesc.getPersistenceDescriptor();
87                 ejbPerDescMap.put(ejbName, pers);
88
89                 //collect pers fields
90
Collection JavaDoc pFields = ejbDesc.getPersistentFields();
91                 HashMap JavaDoc fieldMap = new HashMap JavaDoc();
92                 Iterator JavaDoc fIter = pFields.iterator();
93                 while (fIter.hasNext()) {
94                     String JavaDoc fieldName = ((PersistentFieldInfo)fIter.next()).name;
95                     fieldMap.put(fieldName, fieldName);
96                 }
97                 ejbFieldMap.put(ejbName, fieldMap);
98
99                 //collect pseudo cmr fields
100
List JavaDoc pseudoFields = nameMapper.getGeneratedRelationshipsForEjbName(ejbName);
101                 Iterator JavaDoc pIter = pseudoFields.iterator();
102                 while (pIter.hasNext()) {
103                     addField(ejbName, (String JavaDoc)pIter.next());
104                 }
105
106                 //collect all keys
107
Collection JavaDoc pKeys = ejbDesc.getPrimaryKeyFields();
108                 HashMap JavaDoc pKeyMap = new HashMap JavaDoc();
109                 Iterator JavaDoc kIter = pKeys.iterator();
110                 while (kIter.hasNext()) {
111                     String JavaDoc fieldName = ((PersistentFieldInfo)kIter.next()).name;
112                     pKeyMap.put(fieldName, fieldName);
113                 }
114                 ejbKeyMap.put(ejbName, pKeyMap);
115             }
116         }
117
118         //collect relationship
119
Set JavaDoc rels = bundle.getRelationships();
120         Iterator JavaDoc relIter = rels.iterator();
121         while (relIter.hasNext()) {
122             RelationshipDescriptor rel = (RelationshipDescriptor)relIter.next();
123             RelationRoleDescriptor source = rel.getSource();
124             RelationRoleDescriptor sink = rel.getSink();
125
126             //collect source RelationshipDescriptor
127
String JavaDoc sourceEjbName = source.getOwner().getName();
128             ArrayList JavaDoc sourceRels = (ArrayList JavaDoc)ejbRelMap.get(sourceEjbName);
129             if (sourceRels == null) {
130                 sourceRels = new ArrayList JavaDoc();
131                 ejbRelMap.put(sourceEjbName, sourceRels);
132             }
133             sourceRels.add(rel);
134
135             //collect source cmr field
136
String JavaDoc sourceCMRField = source.getCMRField();
137             if (sourceCMRField != null) {
138                 addField(sourceEjbName, sourceCMRField);
139             }
140
141             //collect sink RelationshipDescriptor
142
String JavaDoc sinkEjbName = sink.getOwner().getName();
143             ArrayList JavaDoc sinkRels = (ArrayList JavaDoc)ejbRelMap.get(sinkEjbName);
144             if (sinkRels == null) {
145                 sinkRels = new ArrayList JavaDoc();
146                 ejbRelMap.put(sinkEjbName, sinkRels);
147             }
148             sinkRels.add(rel);
149
150             //collect sink cmr field
151
String JavaDoc sinkCMRField = sink.getCMRField();
152             if (sinkCMRField != null) {
153                 addField(sinkEjbName, sinkCMRField);
154             }
155         }
156     }
157
158     //---- implements interface ConversionHelper ----
159

160     public String JavaDoc getMappedClassName(String JavaDoc ejbName) {
161         return nameMapper.getPersistenceClassForEjbName(ejbName);
162     }
163
164     /**
165      * If {@link #generateFields} is <code>true</code>, then this method will
166      * check if the field is one of the cmp + cmr + pseudo cmr fields, otherwise
167      * the method will check if the field is one of the cmp + cmr fields.
168      * @param ejbName The ejb-name element for the bean
169      * @param fieldName The name of a container managed field in the named bean
170      * @return <code>true</code> if the bean contains the field, otherwise
171      * return <code>false</code>
172      */

173     public boolean hasField(String JavaDoc ejbName, String JavaDoc fieldName) {
174         if (!generateFields && isGeneratedRelationship(ejbName, fieldName))
175             return false;
176         else {
177             HashMap JavaDoc fieldMap = (HashMap JavaDoc)ejbFieldMap.get(ejbName);
178             return (fieldMap != null) ?
179                 (fieldMap.get(fieldName) != null) : false;
180         }
181     }
182
183     /**
184      * If {@link #generateFields} is <code>true</code>, then this method will
185      * return an array of cmp + cmr + pseudo cmr fields, otherwise
186      * the method will return an array of cmp + cmr fields.
187      * @param ejbName The ejb-name element for the bean
188      * @param fieldName The name of a container managed field in the named bean
189      * @return an array of fields in the ejb bean
190      */

191     public Object JavaDoc[] getFields(String JavaDoc ejbName) {
192         HashMap JavaDoc fieldMap = (HashMap JavaDoc)ejbFieldMap.get(ejbName);
193         if (fieldMap != null) {
194             List JavaDoc fields = new ArrayList JavaDoc(fieldMap.keySet());
195             if (!generateFields) {
196                 fields.removeAll(getGeneratedRelationships(ejbName));
197             }
198             return fields.toArray();
199         }
200         return null;
201     }
202
203     /**
204      * The boolean argument candidate is ignored in this case.
205      */

206     public boolean isKey(String JavaDoc ejbName, String JavaDoc fieldName, boolean candidate) {
207         HashMap JavaDoc keyMap = (HashMap JavaDoc)ejbKeyMap.get(ejbName);
208         return (keyMap != null) ? (keyMap.get(fieldName) != null) : false;
209     }
210
211     /**
212      * This API will only be called from MappingFile when multiplicity is Many
213      * on the other role.
214      */

215     public String JavaDoc getRelationshipFieldType(String JavaDoc ejbName, String JavaDoc fieldName) {
216         if (isGeneratedRelationship(ejbName, fieldName)) {
217             return java.util.Collection JavaDoc.class.getName();
218         } else {
219             PersistenceDescriptor pers =
220                 (PersistenceDescriptor)ejbPerDescMap.get(ejbName);
221             return pers.getCMRFieldReturnType(fieldName);
222         }
223     }
224
225     /**
226      * getMultiplicity of the other role on the relationship
227      * Please note that multiplicity is JDO style
228      */

229     public String JavaDoc getMultiplicity(String JavaDoc ejbName, String JavaDoc fieldName) {
230         RelationRoleDescriptor oppRole = getRelationRoleDescriptor(ejbName,
231                 fieldName, false);
232         return (oppRole.getIsMany()) ? MANY : ONE;
233     }
234
235     public String JavaDoc getRelationshipFieldContent(String JavaDoc ejbName, String JavaDoc fieldName) {
236         RelationRoleDescriptor oppRole = getRelationRoleDescriptor(ejbName,
237                 fieldName, false);
238         return oppRole.getOwner().getName();
239     }
240
241     /**
242      * This method return the fieldName of relation role on the other end.
243      */

244     public String JavaDoc getInverseFieldName(String JavaDoc ejbName, String JavaDoc fieldName) {
245         RelationRoleDescriptor oppRole = getRelationRoleDescriptor(ejbName,
246                 fieldName, false);
247         String JavaDoc inverseName = oppRole.getCMRField();
248
249         // if we are generating relationships, check for a generated inverse
250
if ((generateFields) && (inverseName == null))
251             inverseName = nameMapper.getGeneratedFieldForEjbField(
252                 ejbName, fieldName)[1];
253
254         return inverseName;
255     }
256
257     /**
258      * Returns flag whether the mapping conversion should apply the default
259      * strategy for dealing with unknown primary key classes. This method will
260      * only be called when {@link #generateFields} returns <code>true</code>.
261      * @param ejbName The value of the ejb-name element for a bean.
262      * @return <code>true</code> to apply the default unknown PK Class Strategy,
263      * <code>false</code> otherwise
264      */

265     public boolean applyDefaultUnknownPKClassStrategy(String JavaDoc ejbName) {
266         IASEjbCMPEntityDescriptor ejbDesc =
267                 (IASEjbCMPEntityDescriptor)ejbDescMap.get(ejbName);
268         String JavaDoc keyClassName = ejbDesc.getPrimaryKeyClassName();
269         return keyClassName != null &&
270                 keyClassName.equals(Object JavaDoc.class.getName());
271     }
272   
273     /**
274      * Returns the name used for generated primary key fields.
275      * @return a string for key field name
276      */

277     public String JavaDoc getGeneratedPKFieldName() {
278         return nameMapper.GENERATED_KEY_FIELD_NAME;
279     }
280
281     /**
282      * Returns the prefix used for generated version fields.
283      * @return a string for version field name prefix
284      */

285     public String JavaDoc getGeneratedVersionFieldNamePrefix() {
286         return nameMapper.GENERATED_VERSION_FIELD_PREFIX;
287     }
288
289     public boolean relatedObjectsAreDeleted(String JavaDoc beanName, String JavaDoc fieldName) {
290         RelationRoleDescriptor oppRole = getRelationRoleDescriptor(beanName, fieldName, false);
291         return oppRole.getCascadeDelete();
292     }
293
294     /**
295      * Returns the flag whether the mapping conversion should generate
296      * relationship fields and primary key fields to support run-time.
297      * The version field is always created even {@link #generateFields} is
298      * <code>false</code> because it holds version column information.
299      * @return <code>true</code> to generate fields in the dot-mapping file
300      * (if they are not present).
301      */

302     public boolean generateFields() {
303         return generateFields;
304     }
305
306     /**
307      * Sets the flag whether the mapping conversion should generate relationship
308      * fields, primary key fields, and version fields to support run-time.
309      * @param generateFields a flag which indicates whether fields should be
310      * generated
311      */

312     public void setGenerateFields(boolean generateFields) {
313         this.generateFields = generateFields;
314     }
315
316     /** Returns the flag whether the mapping conversion should validate
317      * all fields against schema columns.
318      * @return <code>true</code> to validate all the fields in the dot-mapping
319      * file.
320      */

321     public boolean ensureValidation() {
322         return ensureValidation;
323     }
324
325     /**
326      * Sets the flag whether the mapping conversion should validate all fields
327      * against schema columns.
328      * @param isValidating a boolean of indicating validating fields or not
329      */

330     public void setEnsureValidation(boolean isValidating) {
331         ensureValidation = isValidating;
332     }
333
334     /**
335      * Returns <code>true</code> if the field is generated. There are three
336      * types of generated fields: generated relationships, unknown primary key
337      * fields, and version consistency fields.
338      * @param ejbName The ejb-name element for the bean
339      * @param fieldName The name of a container managed field in the named bean
340      * @return <code>true</code> if the field is generated; <code>false</code>
341      * otherwise.
342      */

343
344     public boolean isGeneratedField(String JavaDoc ejbName, String JavaDoc fieldName) {
345         return nameMapper.isGeneratedField(ejbName, fieldName);
346     }
347
348     public boolean isGeneratedRelationship(String JavaDoc ejbName, String JavaDoc fieldName) {
349         return nameMapper.isGeneratedEjbRelationship(ejbName, fieldName);
350     }
351
352     /**
353      * Returns a list of generated relationship field names.
354      * @param ejbName The ejb-name element for the bean
355      * @return a list of generated relationship field names
356      */

357     public List JavaDoc getGeneratedRelationships(String JavaDoc ejbName) {
358         return nameMapper.getGeneratedRelationshipsForEjbName(ejbName);
359  
360     }
361
362     //-------------------------------------
363
private RelationRoleDescriptor getRelationRoleDescriptor(String JavaDoc ejbName,
364             String JavaDoc cmrFieldName, boolean self) {
365         String JavaDoc myEjbName = ejbName;
366         String JavaDoc myCMRFieldName = cmrFieldName;
367         boolean myself = self;
368         if (isGeneratedRelationship(ejbName, cmrFieldName)) {
369             String JavaDoc[] nfPair = nameMapper.getEjbFieldForGeneratedField(
370                     ejbName, cmrFieldName);
371             myEjbName = nfPair[0];
372             myCMRFieldName = nfPair[1];
373             myself = !self;
374         }
375         return getRealRelationRoleDescriptor(myEjbName, myCMRFieldName, myself);
376     }
377
378     private RelationRoleDescriptor getRealRelationRoleDescriptor(
379             String JavaDoc ejbName, String JavaDoc cmrFieldName, boolean self) {
380         ArrayList JavaDoc rels = (ArrayList JavaDoc)ejbRelMap.get(ejbName);
381         for (int i = 0; i < rels.size(); i++) {
382             RelationshipDescriptor rel = (RelationshipDescriptor)rels.get(i);
383             RelationRoleDescriptor source = rel.getSource();
384             RelationRoleDescriptor sink = rel.getSink();
385             if (ejbName.equals(source.getOwner().getName()) &&
386                     cmrFieldName.equals(source.getCMRField())) {
387                 return (self) ? source : sink;
388             } else if (ejbName.equals(sink.getOwner().getName()) &&
389                     cmrFieldName.equals(sink.getCMRField())) {
390                 return (self) ? sink : source;
391             }
392         }
393         throw new IllegalArgumentException JavaDoc();
394     }
395
396     private void addField(String JavaDoc ejbName, String JavaDoc fieldName) {
397         HashMap JavaDoc fieldMap = (HashMap JavaDoc)ejbFieldMap.get(ejbName);
398         if (fieldMap == null) {
399             fieldMap = new HashMap JavaDoc();
400             ejbFieldMap.put(ejbName, fieldMap);
401         }
402         fieldMap.put(fieldName, fieldName);
403     }
404 }
405
Popular Tags