KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > patterns > DTOGenerator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ejbcore.patterns;
21
22 import java.io.IOException JavaDoc;
23 import org.netbeans.modules.j2ee.dd.api.ejb.CmpField;
24 import org.netbeans.modules.j2ee.dd.api.ejb.CmrField;
25 import org.netbeans.modules.j2ee.ejbcore.EjbGenerationUtil;
26 import org.openide.filesystems.FileObject;
27
28 /**
29  * Class that generates the DTO source from Entity Bean
30  * @author blaha
31  */

32 public class DTOGenerator {
33     private static final String JavaDoc DTO_SUFIX = "DTO"; //NOI18N
34
private int indexField;
35     private DTOHelper dtoHelp;
36     
37     /* Return the string with first capital character
38      * @param str field name
39      * @return method name with capital first character
40      */

41     //TODO good candidate to Util class
42
private static String JavaDoc toFirstUpper(String JavaDoc str){
43         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(str);
44         buffer.setCharAt(0, Character.toUpperCase(buffer.charAt(0)));
45         return buffer.toString();
46     }
47     
48     /* Return DTO name from bean name
49      * @param beanName bean name
50      * @return bean name + sufix DTO
51      */

52     //TODO good candidate to Util class
53
private static String JavaDoc getDTOName(String JavaDoc beanName){
54 // return EjbGenerationUtil.getEjbNameBase(beanName) + DTO_SUFIX;
55
//TODO: RETOUCHE
56
return null;
57     }
58     
59     /* Return DTO full name from bean name
60      * @param beanName bean name
61      * @return bean full name + sufix DTO
62      */

63     //TODO good candidate to Util class
64
private static String JavaDoc getDTOFullName(String JavaDoc pkgName, String JavaDoc beanName){
65         return pkgName + "." + getDTOName(beanName);
66     }
67     
68     /* Generate DTO class
69      * @param dtoHelper <code>DTOHelper</code> class that encapsulates bean
70      * @param pkg package file object
71      * @param generateCmrFields wheteher generate CMR fields, setter and getter methods for CMR
72      */

73     public void generateDTO(DTOHelper dtoHelp,
74             FileObject pkg,
75             boolean generateCmrFields)
76             throws IOException JavaDoc {
77         String JavaDoc pkgName;
78         this.dtoHelp = dtoHelp;
79         if(pkg != null) {
80             pkgName = EjbGenerationUtil.getSelectedPackageName(pkg);
81         } else {
82             pkgName = dtoHelp.getPackage();
83             pkg = EjbGenerationUtil.getPackageFileObject(dtoHelp.getSourceGroup(),pkgName, dtoHelp.getProject());
84         }
85         String JavaDoc dtoName = dtoHelp.getEntityName();
86         //TODO: RETOUCHE
87
// b = genUtil.getDefaultBean();
88
// b.setClassname(true);
89
// b.setClassnameName(getDTOName(dtoName));
90
// b.setCommentDataEjbName(dtoHelp.getLocalName());
91
// if(pkgName != null){
92
// b.setClassnamePackage(pkgName);
93
// }
94
// b.setKey(true); // this is needed for equals() and to hashCode() meth. generation
95
// b.setKeyFullname(getDTOFullName(pkgName, dtoName));
96
// // add CMP Fields
97
// CmpField[] cmps = dtoHelp.getCmpFields();
98
// addCmpFields(cmps);
99
// // add CMR Fields
100
// if(generateCmrFields) {
101
// CmrField[] cmrs = dtoHelp.getCmrFields();
102
// if(cmrs != null) addCmrFields(cmrs);
103
// }
104
// // generate DTO
105
// genUtil.generateBeanClass(DTO_TEMPLATE, b, pkgName, pkg, true);
106
}
107     
108     /* Add Cm field (CMR or CMP) to bean
109      * @param index index of CM field in beans
110      * @param fieldName field name
111      * @param cmFieldClassname field class name, e.g. java.util.Collection, String, ...
112      */

113     private void addField(int index, String JavaDoc fieldName, String JavaDoc cmFieldClassName){
114         //TODO: RETOUCHE
115
// b.addCmField(true);
116
// b.setCmFieldMethodName(index, toFirstUpper(fieldName));
117
// b.setCmFieldClassname(index, cmFieldClassName);
118
// b.addCmFieldName(fieldName);
119
}
120     
121     /* Method adds CMP fields to the bean
122      * @param CmpField[] array of CMP fields
123      */

124     private void addCmpFields(CmpField[] cmps){
125         String JavaDoc fieldName;
126         for(int i = 0; i < cmps.length; i++){
127             if(cmps[i] != null ) {
128                 fieldName = cmps[i].getFieldName();
129                 addField(indexField, fieldName, dtoHelp.getFieldType(fieldName));
130                 //TODO: RETOUCHE
131
// b.setCmFieldInKey(indexField, fieldName);
132
indexField++;
133             }
134         }
135     }
136     
137     /* Method adds CMR fields to the bean
138      * @param CmrField[] array of CMR fields
139      */

140     private void addCmrFields(CmrField[] cmrs){
141         String JavaDoc fieldName;
142         String JavaDoc fieldClassName;
143         String JavaDoc cmFieldCmrField;
144         boolean isMultiplicity;
145         int fieldIndex;
146         
147         for(int i = 0; i < cmrs.length; i++){
148             if(cmrs[i] != null){
149                 fieldName = cmrs[i].getCmrFieldName();
150                 fieldIndex = indexField++;
151                 isMultiplicity = dtoHelp.isMultiple(cmrs[i]);
152                 
153                 if(isMultiplicity){
154                     fieldClassName = dtoHelp.getFieldType(fieldName);
155                     cmFieldCmrField = dtoHelp.getOppositeFieldType(cmrs[i]);
156                 } else {
157                     fieldClassName = getDTOName(
158                             dtoHelp.findEntityNameByLocalInt(dtoHelp.getFieldType(fieldName)));
159                     cmFieldCmrField = fieldName;
160                 }
161                 
162                 addField(fieldIndex, fieldName, fieldClassName);
163                 //TODO: RETOUCHE
164
// b.setCmFieldCmrField(fieldIndex, cmFieldCmrField);
165
// // TODO change this hack. We should change the bean.dtd or something else
166
// if(isMultiplicity) {
167
// b.setCmFieldCascadeDelete(fieldIndex,
168
// getDTOName(dtoHelp.findEntityNameByLocalInt(cmFieldCmrField)));
169
// }
170
}
171         }
172     }
173 }
174
Popular Tags