KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import java.util.ArrayList JavaDoc;
24 import javax.lang.model.element.Element;
25 import javax.lang.model.element.TypeElement;
26 import org.netbeans.api.java.source.CompilationController;
27 import org.netbeans.api.project.FileOwnerQuery;
28 import org.netbeans.api.project.Project;
29 import org.netbeans.api.project.SourceGroup;
30 import org.netbeans.modules.j2ee.common.Util;
31 import org.netbeans.modules.j2ee.dd.api.ejb.CmpField;
32 import org.netbeans.modules.j2ee.dd.api.ejb.CmrField;
33 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
34 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider;
35 import org.netbeans.modules.j2ee.dd.api.ejb.Ejb;
36 import org.netbeans.modules.j2ee.dd.api.ejb.EjbRelation;
37 import org.netbeans.modules.j2ee.dd.api.ejb.EjbRelationshipRole;
38 import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans;
39 import org.netbeans.modules.j2ee.dd.api.ejb.Entity;
40 import org.netbeans.modules.j2ee.dd.api.ejb.Relationships;
41 import org.openide.ErrorManager;
42 import org.openide.filesystems.FileObject;
43
44 /**
45  * DTO helper class
46  * @author blaha
47  */

48 public class DTOHelper {
49     private final Project project;
50     private EjbJar ejbJar;
51     private Entity entity;
52     private final TypeElement classElm;
53     private EnterpriseBeans beans;
54     private final FileObject fileObject;
55     
56     /** Create new instance of Data Transfer Object helper class
57      * @param me <code>MemberElement</code> that represents entity implementation class
58      */

59     public DTOHelper(CompilationController controller, Element feature) {
60         classElm = (TypeElement) feature.getEnclosingElement();
61         fileObject = controller.getFileObject();
62         project = FileOwnerQuery.getOwner(fileObject);
63         
64         org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbModule = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(fileObject);
65         
66         
67         DDProvider provider = DDProvider.getDefault();
68         try{
69             ejbJar = provider.getMergedDDRoot(ejbModule.getMetadataUnit());
70             beans = ejbJar.getEnterpriseBeans();
71             
72             entity = (Entity) beans.findBeanByName(
73                     EnterpriseBeans.ENTITY,
74                     Ejb.EJB_CLASS, classElm.getQualifiedName().toString());
75         }catch(java.io.IOException JavaDoc ex) {
76             // IO error while reading DD
77
ErrorManager.getDefault().notify(ex);
78         }
79     }
80     
81     /** Get CMR fields for entity bean
82      * @return CmrField[] array
83      */

84     public CmrField[] getCmrFields() {
85         ArrayList JavaDoc<CmrField> cmrFields = new ArrayList JavaDoc<CmrField>();
86         EjbRelation[] ejbRelations = getRelation();
87         if(ejbRelations != null){
88             for (EjbRelation ejbRelation : ejbRelations) {
89                 EjbRelationshipRole role = ejbRelation.getEjbRelationshipRole();
90                 if(isUseEjb(role)) {
91                     cmrFields.add(role.getCmrField());
92                 }
93                 role = ejbRelation.getEjbRelationshipRole2();
94                 if(isUseEjb(role)) {
95                     cmrFields.add(role.getCmrField());
96                 }
97             }
98         }
99         return cmrFields.toArray(new CmrField[0]);
100     }
101     
102     /* Get all relations in EJB jar
103      * @return EjbRelation[]
104      */

105     private EjbRelation[] getRelation() {
106         Relationships relation = ejbJar.getSingleRelationships();
107         return (relation != null) ? relation.getEjbRelation() : null;
108     }
109     
110     /* Test whether the CMR field is multiple relation
111      * @param cmrField
112      * @return true whether the field is multiple
113      */

114     public boolean isMultiple(CmrField cmrField) {
115         boolean isMultiple = false;
116         EjbRelation[] ejbRelations = getRelation();
117         if(ejbRelations != null){
118             for (EjbRelation ejbRelation : ejbRelations) {
119                 EjbRelationshipRole role = ejbRelation.getEjbRelationshipRole();
120                 if(cmrField == role.getCmrField() &&
121                         role.MULTIPLICITY_MANY.equals(ejbRelation.getEjbRelationshipRole2().
122                         getMultiplicity())) {
123                     isMultiple = true;
124                 }
125                 role = ejbRelation.getEjbRelationshipRole2();
126                 if(cmrField == role.getCmrField() &&
127                         role.MULTIPLICITY_MANY.equals(ejbRelation.getEjbRelationshipRole().
128                         getMultiplicity())) {
129                     isMultiple = true;
130                 }
131             }
132         }
133         return isMultiple;
134     }
135     
136     /* Get field type of opposite field in releation
137      * @param CmrField
138      * @return Field type of opposite field
139      */

140     public String JavaDoc getOppositeFieldType(CmrField cmrField){
141         String JavaDoc ejbName;
142         String JavaDoc cmrFieldType2 = "";
143         EjbRelation[] ejbRelations = getRelation();
144         if(ejbRelations != null) {
145             for (EjbRelation ejbRelation : ejbRelations) {
146                 EjbRelationshipRole role = ejbRelation.getEjbRelationshipRole();
147                 if(cmrField == role.getCmrField()) {
148                     ejbName = ejbRelation.getEjbRelationshipRole2().getRelationshipRoleSource().getEjbName();
149                     cmrFieldType2 = findLocalIntNameByEntityName(ejbName);
150                     break;
151                 }
152                 role = ejbRelation.getEjbRelationshipRole2();
153                 if(cmrField == role.getCmrField()) {
154                     ejbName = ejbRelation.getEjbRelationshipRole().getRelationshipRoleSource().getEjbName();
155                     cmrFieldType2 = findLocalIntNameByEntityName(ejbName);
156                     break;
157                 }
158             }
159         }
160         return cmrFieldType2;
161     }
162     
163     /* Find local interface name by Entity name
164      * @param entityName name
165      * @return local interface name
166      */

167     public String JavaDoc findLocalIntNameByEntityName(String JavaDoc entityName) {
168         Entity[] ents = beans.getEntity();
169         String JavaDoc localInterfaceName = "";
170         for(int i = 0; i < ents.length; i++) {
171             if(entityName.equals(ents[i].getEjbName())) {
172                 localInterfaceName = ents[i].getLocal();
173                 break;
174             }
175         }
176         return localInterfaceName;
177     }
178     
179     /* Find entity name by local interface name
180      * @param interf local interface name
181      * @return entity name
182      */

183     public String JavaDoc findEntityNameByLocalInt(String JavaDoc interf) {
184         Entity[] ents = beans.getEntity();
185         String JavaDoc ejbName = "";
186         for(int i = 0; i < ents.length; i++) {
187             if(interf.equals(ents[i].getLocal())) {
188                 ejbName = ents[i].getEjbName();
189                 break;
190             }
191         }
192         return ejbName;
193     }
194     
195     /* Check whether the releation role belong to the entity
196      * @param role in relationship
197      * @return true whether relation role belong to the entity bean
198      */

199     private boolean isUseEjb(EjbRelationshipRole role) {
200         return role != null &&
201                 role.getRelationshipRoleSource() != null &&
202                 role.getRelationshipRoleSource().getEjbName().equals(getEntityName());
203     }
204     
205     /* Get all CMP fields in entity bean
206      * @return CmpField array
207      */

208     public CmpField[] getCmpFields() {
209         return entity.getCmpField();
210     }
211     
212     /* Get field type of CMP field, e.g. String, int, ...
213      * @param fieldName field name
214      * @return field type
215      */

216     public String JavaDoc getFieldType(String JavaDoc fieldName) {
217         String JavaDoc returnType = "";
218         //TODO: RETOUCHE
219
// returnType = EntityMethodController.getGetterMethod(
220
// classElm, fieldName).getType().getName();
221
return returnType;
222     }
223     
224     /* Get entity name
225      * @return entity name
226      */

227     //TODO method could be moved to some Utils method
228
public String JavaDoc getEntityName() {
229         return entity.getEjbName();
230     }
231     
232     /* Get local interface name
233      * @return local interface name
234      */

235     public String JavaDoc getLocalName(){
236         return entity.getLocal();
237     }
238     
239     /* Get full name of entity bean, e.g org.netbeans.entity.XXXX
240      * @return full entity bean name
241      */

242     public String JavaDoc getFullName() {
243         return classElm.getQualifiedName().toString();
244     }
245     
246     /* Get package of entity bean class
247      * @return package name
248      */

249     public String JavaDoc getPackage(){
250         return getFullName().substring(0,
251                 getFullName().lastIndexOf('.'));
252     }
253     
254     /* Get project where the entity bean is located
255      * @return project
256      */

257     public Project getProject() {
258         return project;
259     }
260     
261     /* Get source root where the entity bean is located
262      * @return source root
263      */

264     public SourceGroup getSourceGroup(){
265         SourceGroup entityFolder = null;
266         SourceGroup[] folders= Util.getJavaSourceGroups(project);
267
268         for(int i = 0; i < folders.length; i++){
269             if(folders[i].contains(fileObject)){
270                 entityFolder = folders[i];
271             }
272          }
273         return entityFolder;
274     }
275 }
276
Popular Tags