KickJava   Java API By Example, From Geeks To Geeks.

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


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
26 /*
27  * JDOConcreteBean11Generator.java
28  *
29  * Created on February 24, 2004
30  */

31
32 package com.sun.jdo.spi.persistence.support.ejb.ejbc;
33
34 import java.util.*;
35 import java.io.IOException JavaDoc;
36 import java.lang.reflect.Method JavaDoc;
37 import java.lang.reflect.Modifier JavaDoc;
38 import java.text.MessageFormat JavaDoc;
39
40 import com.sun.jdo.api.persistence.model.Model;
41 import com.sun.jdo.api.persistence.model.jdo.*;
42
43 import com.sun.jdo.spi.persistence.support.ejb.model.util.NameMapper;
44 import com.sun.jdo.spi.persistence.support.ejb.ejbqlc.JDOQLElements;
45
46 import com.sun.jdo.spi.persistence.utility.I18NHelper;
47 import com.sun.jdo.spi.persistence.utility.generator.*;
48 import com.sun.jdo.spi.persistence.utility.logging.Logger;
49  
50 /*
51  * This is the JDO specific generator for the concrete CMP beans for EJB1.1.
52  *
53  * @author Marina Vatkina
54  */

55 class JDOConcreteBean11Generator extends JDOConcreteBeanGenerator {
56
57     /**
58      * Signature with CVS keyword substitution for identifying the generated code
59      */

60     static final String JavaDoc SIGNATURE = "$RCSfile: JDOConcreteBean11Generator.java,v $ $Revision: 1.2 $"; //NOI18N
61

62     JDOConcreteBean11Generator(ClassLoader JavaDoc loader,
63                              Model model,
64                              NameMapper nameMapper)
65                              throws IOException JavaDoc {
66
67         super (loader, model, nameMapper);
68         CMP11TemplateFormatter.initHelpers();
69
70         // Add the code generation signature of the generic and 1.1-specific
71
// generator classes.
72
addCodeGeneratorClassSignature(getSignaturesOfGeneratorClasses());
73     }
74
75     /** Add interfaces to the class declarations.
76      */

77     void addInterfaces() throws IOException JavaDoc {
78         super.addInterfaces();
79         jdoHelperWriter.addInterface(CMP11TemplateFormatter.helper11Interface_);
80     }
81
82     /** Set super class for the helper class.
83      */

84     void setHelperSuperclass() throws IOException JavaDoc {
85         jdoHelperWriter.setSuperclass(CMP11TemplateFormatter.helper11Impl_);
86     }
87
88     /** Generate CMP1.1 specific methods.
89      */

90     void generateTypeSpecificMethods(PersistenceFieldElement[] allFields,
91             AbstractMethodHelper methodHelper) throws IOException JavaDoc {
92
93         super.generateTypeSpecificMethods(allFields, methodHelper);
94         generateLoadStoreMethods(allFields);
95     }
96
97     /**
98      * Generates required internal variables.
99      */

100     void generateFields() throws IOException JavaDoc {
101
102         super.generateFields();
103         CMP11TemplateFormatter.addPrivateField(
104                 CMP11TemplateFormatter.one_oneVariablesTemplate,
105                 0, concreteImplWriter);
106     }
107
108
109     /** Adds jdoLoadFields/jdoStoreFields for CMP1.1
110      */

111     void generateLoadStoreMethods(PersistenceFieldElement[] fields)
112                        throws IOException JavaDoc {
113         int i, count = ((fields != null) ? fields.length : 0);
114         StringBuffer JavaDoc lbody = new StringBuffer JavaDoc();
115         StringBuffer JavaDoc sbody = new StringBuffer JavaDoc(CMPTemplateFormatter.none_);
116
117         for (i = 0; i < count; i++) {
118             PersistenceFieldElement pfe = fields[i];
119
120             if (PersistenceFieldElement.PERSISTENT == pfe.getPersistenceType()) {
121                 FieldInfo fieldInfo = new FieldInfo(model, nameMapper, pfe, beanName, pcname);
122
123                 if (fieldInfo.isGeneratedField) {
124                 // Skip generated fields as they are not present in the bean class.
125
// A field is generated for the unknown PK class or version consistency.
126
// There are no relationship fields in CMP1.1 beans.
127
if (fieldInfo.isKey) {
128                         // This is an extra field for the unknown PK class.
129
// PK setter name is used to generate the line for ejbCreate
130
// to set the PK value in _JDOState.
131
setPKField = fieldInfo.setter;
132                     }
133                     continue;
134                 }
135
136                 // Add code to load non-DFG field if necessary.
137
loadNonDFGField(fieldInfo);
138
139                 if( fieldInfo.isByteArray) {
140                     // A byte[] CMP field should have copy-in, copy-out semantics
141
// via System.arraycopy.
142
twoParams[0] = fieldInfo.name;
143                     twoParams[1] = fieldInfo.getter;
144                     lbody.append(CMP11TemplateFormatter.l11arrayformatter.format(twoParams));
145
146                     if (isUpdateable) {
147                         threeParams[0] = fieldInfo.getter;
148                         threeParams[1] = fieldInfo.name;
149                         threeParams[2] = fieldInfo.setter;
150                         sbody.append(CMP11TemplateFormatter.s11arrayformatter.format(threeParams));
151                     }
152
153                 } else if( fieldInfo.isSerializable ) {
154                     // A special case for a Serializable CMP field (but not byte[]) -
155
// it should be serialized to/from a byte[] in PC instance.
156

157                     fourParams[0] = fieldInfo.name;
158                     fourParams[1] = fieldInfo.getter;
159                     fourParams[2] = fieldInfo.type;
160                     fourParams[3] = concreteImplName;
161                     lbody.append(CMP11TemplateFormatter.l11Serializableformatter.format(fourParams));
162
163                     if (isUpdateable) {
164                         fourParams[0] = fieldInfo.getter;
165                         fourParams[1] = fieldInfo.name;
166                         fourParams[2] = fieldInfo.setter;
167                         fourParams[3] = concreteImplName;
168                         sbody.append(CMP11TemplateFormatter.s11Serializableformatter.format(fourParams));
169                     }
170
171                 } else if (fieldInfo.requireCloneOnGetAndSet) {
172                     threeParams[0] = fieldInfo.getter;
173                     threeParams[1] = fieldInfo.type;
174                     threeParams[2] = fieldInfo.name;
175                     lbody.append(CMP11TemplateFormatter.l11copyformatter.format(threeParams));
176
177                     if (isUpdateable) {
178                         fourParams[0] = fieldInfo.getter;
179                         fourParams[1] = fieldInfo.name;
180                         fourParams[2] = fieldInfo.setter;
181                         fourParams[3] = fieldInfo.type;
182                         if (!pfe.isKey()) {
183                             sbody.append(CMP11TemplateFormatter.s11copyformatter.format(fourParams));
184                         } else {
185                             twoParams[0] = concreteImplName;
186                             twoParams[1] = fieldInfo.name;
187                             sbody.append(CMP11TemplateFormatter.assertpks11formatter.format(twoParams)).
188                                 append(CMP11TemplateFormatter.pkcopy11formatter.format(fourParams));
189                         }
190                     }
191
192                 } else {
193                     twoParams[0] = fieldInfo.name;
194                     twoParams[1] = fieldInfo.getter;
195                     lbody.append(CMP11TemplateFormatter.l11formatter.format(twoParams));
196
197                     if (isUpdateable) {
198                         threeParams[0] = fieldInfo.getter;
199                         threeParams[1] = fieldInfo.name;
200                         threeParams[2] = fieldInfo.setter;
201                         if (!pfe.isKey()) {
202                             sbody.append(CMP11TemplateFormatter.s11formatter.format(threeParams));
203                         } else {
204                             if (!fieldInfo.isPrimitive) {
205                                 twoParams[0] = concreteImplName;
206                                 twoParams[1] = fieldInfo.name;
207                                 sbody.append(
208                                     CMP11TemplateFormatter.assertpks11formatter.format(twoParams));
209                             }
210
211                             sbody.append(requireTrimOnSet(fieldInfo.type) ?
212                                 CMP11TemplateFormatter.pkstring11formatter.format(threeParams) :
213                                 CMP11TemplateFormatter.pks11formatter.format(threeParams));
214                         }
215                     }
216
217                 }
218            }
219        }
220
221        // Add jdoLoadFields
222
CMPTemplateFormatter.addGenericMethod(
223                CMP11TemplateFormatter.loadFields1_1_,
224                CMP11TemplateFormatter.getBodyAsStrings(lbody.toString()),
225                concreteImplWriter);
226
227        // Add jdoStoreFields
228
CMPTemplateFormatter.addGenericMethod(
229                CMP11TemplateFormatter.storeFields1_1_,
230                CMP11TemplateFormatter.getBodyAsStrings(sbody.toString()),
231                concreteImplWriter);
232     }
233
234     /** Returns JDOQLElements instance for this finder method.
235      * @param m the finder method as a java.lang.reflect.Method
236      * @param methodHelper the AbstractMethodHelper instance that contains
237      * all categorized methods and some other convenience methods for this bean.
238      * @return JDOQLElements instance.
239      */

240     JDOQLElements getJDOQLElements(Method JavaDoc m,
241             AbstractMethodHelper methodHelper) throws IOException JavaDoc{
242       
243         // CMP11 : get JDO query settings from DD
244
return getJDOQLElementsForCMP11(m, methodHelper);
245     }
246
247     /** Returns method body for EJBCreate method.
248      * @param createName the actual name of the method as String.
249      * @param exc a String[] of decleared exceptions for this method.
250      * @param parametersList the list of method parameters as String.
251      * @param parametersListWithSeparator the list of concatenated method
252      * parameters to be passed to another method as String.
253      * @return method body as String.
254      */

255     String JavaDoc getEJBCreateMethodBody(String JavaDoc createName,
256             String JavaDoc[] exc, String JavaDoc parametersList,
257             String JavaDoc parametersListWithSeparator) {
258
259         // For read-only beans it will throw an exception on access.
260
// For updateable beans it will be generated to do the create.
261
String JavaDoc body = CMPROTemplateFormatter.accessNotAllowedTemplate;
262
263         if (isUpdateable) {
264             // no suffixes in ejbCreate for CMP1.1 beans but we need to check what
265
// exception is available.
266
if (pkClass.equals(Object JavaDoc.class.getName())) {
267                 fiveParams[0] = pcname;
268                 fiveParams[1] = parametersList;
269                 fiveParams[2] = setPKField;
270                 fiveParams[3] = concreteImplName;
271                 fiveParams[4] = parametersListWithSeparator;
272                 body = CMP11TemplateFormatter.c11unpkformatter.format(fiveParams);
273
274             } else {
275                 sixParams[0] = pcname;
276                 sixParams[1] = parametersList;
277                 sixParams[2] = pkClass;
278                 sixParams[3] = concreteImplName;
279                 String JavaDoc s = getException(exc, CMP11TemplateFormatter.DuplicateKeyException_,
280                         CMP11TemplateFormatter.CreateException_);
281                 int l = s.lastIndexOf(CMP11TemplateFormatter.dot_);
282                 sixParams[4] = (l > 0)? s.substring(l + 1) : s;
283                 sixParams[5] = parametersListWithSeparator;
284                 body = CMP11TemplateFormatter.c11formatter.format(sixParams);
285
286             }
287         }
288         return body;
289     }
290
291     /** Returns method body for EJBPostCreate method.
292      * @param postCreateName the actual name of the method as String.
293      * @param parametersList the list of method parameters as String.
294      * @param parametersListWithSeparator the list of concatenated method
295      * parameters to be passed to another method as String.
296      * @return method body as String.
297      */

298     String JavaDoc getEJBPostCreateMethodBody(String JavaDoc postCreateName,
299             String JavaDoc parametersList, String JavaDoc parametersListWithSeparator) {
300
301         // For read-only beans it will be a no-op. For updateable
302
// beans it will be generated.
303
String JavaDoc body = CMPTemplateFormatter.none_;
304  
305         if (isUpdateable) {
306             twoParams[0] = parametersList;
307             twoParams[1] = parametersListWithSeparator;
308             body = CMP11TemplateFormatter.postc11formatter.format(twoParams);
309         }
310
311         return body;
312     }
313
314     /** Returns method body for EJBRemove method.
315      * @return method body as String.
316      */

317     String JavaDoc getEJBRemoveMethodBody() {
318
319         // For read-only beans it will throw an exception on access.
320
// For updateable beans it will be generated.
321
String JavaDoc body = CMPROTemplateFormatter.updateNotAllowedTemplate;
322  
323         if (isUpdateable) {
324             // CMP1.1 does not need any variables to substitute.
325
body = CMP11TemplateFormatter.ejbRemove1_1Template;
326         }
327
328         return body;
329     }
330
331     /** Adds other known required methods identified by properties that do
332      * not need formatting but differ between CMP types.
333      * CMP11TemplateFormatter.otherPublicMethods_ differ between CMP types.
334      */

335     void generateKnownMethods(AbstractMethodHelper methodHelper)
336                        throws IOException JavaDoc {
337
338         super.generateKnownMethods(methodHelper);
339
340         String JavaDoc[] exc = null;
341         String JavaDoc[] st = CMP11TemplateFormatter.otherPublicMethodsArray;
342         for (int i = 0; i < st.length; i++) {
343             String JavaDoc mname = st[i];
344             exc = getExceptionList(methodHelper, mname);
345
346             String JavaDoc body = CMPROTemplateFormatter.updateNotAllowedTemplate;
347             // Only ejbLoad from this list doesn't differ for read-only beans.
348
if (isUpdateable || mname.equals(CMPTemplateFormatter.ejbLoad_)) {
349                 body = CMP11TemplateFormatter.helpers.getProperty(
350                         mname + "1_1"); // NOI18N
351
} else if (mname.equals(CMPTemplateFormatter.jdoCleanAllRefs_)) {
352                 body = CMPROTemplateFormatter.jdoCleanAllRefsTemplate;
353             }
354
355             concreteImplWriter.addMethod(mname, // name
356
Modifier.PUBLIC, // modifiers
357
CMP11TemplateFormatter.void_, // returnType
358
null, // parameterNames
359
null,// parameterTypes
360
exc,// exceptions
361
CMP11TemplateFormatter.getBodyAsStrings(body), // body
362
null);// comments
363
}
364
365     }
366
367    /**
368      * Checks if the finder returns an Enumeration (for CMP1.1)
369      * @param finder Methodobject of the finder
370      * @return <code>true</code> if the finder returns a Enumeration
371      */

372     boolean isFinderReturningEnumeration(Method JavaDoc finder) {
373         return (finder.getReturnType().equals(java.util.Enumeration JavaDoc.class));
374     }
375
376     /**
377      * Generates a setIgnoreCache(true) call for a JDOQL query,
378      * in the case of a EJB 1.1 finder.
379      * @return the codefragment to set the ignoreCache flag of a JDOQL query.
380      */

381     String JavaDoc generateQueryIgnoreCache()
382     {
383         oneParam[0] = CMP11TemplateFormatter.true_;
384         return CMP11TemplateFormatter.querysetignorecacheformatter.format(oneParam);
385     }
386
387     /**
388      * Creating a JDOQLElements object for CMP11 support. For CMP11 there is no
389      * EJBQL, thus we get the filter expression and parameter declaration from
390      * the DD.
391      * @param m CMP1.1 method instance
392      * @param methodHelper the AbstractMethodHelper instance that contains
393      * all categorized methods and some other convenience methods for this bean
394      * @return a filled JDOQElementsobject for further codegeneration
395      */

396     private JDOQLElements getJDOQLElementsForCMP11(Method JavaDoc m,
397             AbstractMethodHelper methodHelper) {
398
399         String JavaDoc params = methodHelper.getJDOParameterDeclaration(m);
400         String JavaDoc variables = methodHelper.getJDOVariableDeclaration(m);
401         String JavaDoc filter = methodHelper.getJDOFilterExpression(m);
402         String JavaDoc ordering = methodHelper.getJDOOrderingSpecification(m);
403         return new JDOQLElements(
404             pcname, // use the pc class for this bean as candidateClass
405
params, // JDO parameter declarations from DD
406
variables, // JDO variables declarations from DD
407
filter, // JDO filter expression from DD
408
ordering, // JDO ordering expression from DD
409
"this", // finders return PK instances =>
410
// Project this to prevent generation of distinct
411
pcname, // finders return PK instances =>
412
// the jdo query returns a set of pc instances
413
true, // JDO query returns candidate class instances =>
414
// isPCResult = true
415
false, // not associate to aggregate function
416
null // not available and not supported for 1.1 finder
417
);
418
419     }
420
421     /**
422      * Returns the signatures of the classes and properties which are
423      * involved in the codegen.
424      * @return The signatures as a string.
425      */

426     String JavaDoc getSignaturesOfGeneratorClasses()
427     {
428         StringBuffer JavaDoc signatures = new StringBuffer JavaDoc().
429
430             // adding signature of JDOConcreteBeanGenerator
431
append(super.getSignaturesOfGeneratorClasses()).
432             append(CMPTemplateFormatter.signatureDelimiter_).
433
434             // adding signature of JDOConcreteBean11Generator
435
append(JDOConcreteBean11Generator.SIGNATURE).
436             append(CMPTemplateFormatter.signatureDelimiter_).
437
438             // adding signature of CMP11Templates.properties
439
append(CMP11TemplateFormatter.signature1_1Template);
440
441         return signatures.toString();
442     }
443
444 }
445
446
Popular Tags