KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > action > CreateMethodGenerator


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.action;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.lang.model.element.Modifier;
27 import org.netbeans.modules.j2ee.common.method.MethodModel;
28 import org.netbeans.modules.j2ee.dd.api.ejb.Entity;
29 import org.netbeans.modules.j2ee.dd.api.ejb.EntityAndSession;
30 import org.netbeans.modules.j2ee.dd.api.ejb.Session;
31 import org.netbeans.modules.j2ee.ejbcore._RetoucheUtil;
32 import org.openide.filesystems.FileObject;
33
34 /**
35  *
36  * @author Martin Adamek
37  */

38 public final class CreateMethodGenerator extends AbstractMethodGenerator {
39     
40     private CreateMethodGenerator(EntityAndSession ejb, FileObject ejbClassFileObject) {
41         super(ejb, ejbClassFileObject, null);
42     }
43     
44     public static CreateMethodGenerator create(EntityAndSession ejb, FileObject ejbClassFileObject) {
45         return new CreateMethodGenerator(ejb, ejbClassFileObject);
46     }
47     
48     public void generate(MethodModel methodModel, boolean generateLocal, boolean generateRemote) throws IOException JavaDoc {
49         if (ejb instanceof Session) {
50             generateSession(methodModel, generateLocal, generateRemote);
51         } else if (ejb instanceof Entity) {
52             generateEntity(methodModel, generateLocal, generateRemote);
53         }
54     }
55     
56     private void generateSession(MethodModel methodModel, boolean generateLocal, boolean generateRemote) throws IOException JavaDoc {
57         
58         if (!methodModel.getName().startsWith("create")) {
59             throw new IllegalArgumentException JavaDoc("The method name must have create as its prefix.");
60         }
61         
62         // local interface
63
if (generateLocal && ejb.getLocal() != null && ejb.getLocalHome() != null) {
64             List JavaDoc<String JavaDoc> exceptions = new ArrayList JavaDoc<String JavaDoc>(methodModel.getExceptions());
65             if (!methodModel.getExceptions().contains("javax.ejb.CreateException")) {
66                 exceptions.add("javax.ejb.CreateException");
67             }
68             MethodModel methodModelCopy = MethodModel.create(
69                     methodModel.getName(),
70                     ejb.getLocal(),
71                     null,
72                     methodModel.getParameters(),
73                     exceptions,
74                     Collections.<Modifier>emptySet()
75                     );
76             FileObject fileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, ejb.getLocalHome());
77             addMethod(methodModelCopy, fileObject, ejb.getLocalHome());
78         }
79         
80         // remote interface
81
if (generateRemote && ejb.getRemote() != null && ejb.getHome() != null) {
82             List JavaDoc<String JavaDoc> exceptions = exceptions = new ArrayList JavaDoc<String JavaDoc>(methodModel.getExceptions());
83             if (!methodModel.getExceptions().contains("javax.ejb.CreateException")) {
84                 exceptions.add("javax.ejb.CreateException");
85             }
86             if (!methodModel.getExceptions().contains("java.rmi.RemoteException")) {
87                 exceptions.add("java.rmi.RemoteException");
88             }
89             MethodModel methodModelCopy = MethodModel.create(
90                     methodModel.getName(),
91                     ejb.getRemote(),
92                     null,
93                     methodModel.getParameters(),
94                     exceptions,
95                     Collections.<Modifier>emptySet()
96                     );
97             FileObject fileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, ejb.getHome());
98             addMethod(methodModelCopy, fileObject, ejb.getHome());
99         }
100         
101         // ejb class
102
List JavaDoc<String JavaDoc> exceptions = new ArrayList JavaDoc<String JavaDoc>(methodModel.getExceptions());
103         if (!methodModel.getExceptions().contains("javax.ejb.CreateException")) {
104             exceptions.add("javax.ejb.CreateException");
105         }
106         MethodModel methodModelCopy = MethodModel.create(
107                 "ejbC" + methodModel.getName().substring(1),
108                 methodModel.getReturnType(),
109                 methodModel.getBody(),
110                 methodModel.getParameters(),
111                 exceptions,
112                 Collections.singleton(Modifier.PUBLIC)
113                 );
114         addMethod(methodModelCopy, ejbClassFileObject, ejb.getEjbClass());
115         
116     }
117     
118     private void generateEntity(MethodModel methodModel, boolean generateLocal, boolean generateRemote) throws IOException JavaDoc {
119         
120         if (!methodModel.getName().startsWith("create")) {
121             throw new IllegalArgumentException JavaDoc("The method name must have create as its prefix.");
122         }
123         
124         // local interface
125
if (generateLocal && ejb.getLocal() != null && ejb.getLocalHome() != null) {
126             List JavaDoc<String JavaDoc> exceptions = new ArrayList JavaDoc<String JavaDoc>(methodModel.getExceptions());
127             if (!methodModel.getExceptions().contains("javax.ejb.CreateException")) {
128                 exceptions.add("javax.ejb.CreateException");
129             }
130             MethodModel methodModelCopy = MethodModel.create(
131                     methodModel.getName(),
132                     ejb.getLocal(),
133                     null,
134                     methodModel.getParameters(),
135                     exceptions,
136                     Collections.<Modifier>emptySet()
137                     );
138             FileObject fileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, ejb.getLocalHome());
139             addMethod(methodModelCopy, fileObject, ejb.getLocalHome());
140             
141         }
142         
143         // remote interface
144
if (generateRemote && ejb.getRemote() != null && ejb.getHome() != null) {
145             List JavaDoc<String JavaDoc> exceptions = new ArrayList JavaDoc<String JavaDoc>(methodModel.getExceptions());
146             if (!methodModel.getExceptions().contains("javax.ejb.CreateException")) {
147                 exceptions.add("javax.ejb.CreateException");
148             }
149             if (!methodModel.getExceptions().contains("java.rmi.RemoteException")) {
150                 exceptions.add("java.rmi.RemoteException");
151             }
152             MethodModel methodModelCopy = MethodModel.create(
153                     methodModel.getName(),
154                     ejb.getRemote(),
155                     null,
156                     methodModel.getParameters(),
157                     exceptions,
158                     Collections.<Modifier>emptySet()
159                     );
160             FileObject fileObject = _RetoucheUtil.resolveFileObjectForClass(ejbClassFileObject, ejb.getHome());
161             addMethod(methodModelCopy, fileObject, ejb.getHome());
162         }
163         
164         // ejb class
165
List JavaDoc<String JavaDoc> exceptions = new ArrayList JavaDoc<String JavaDoc>(methodModel.getExceptions());
166         if (!methodModel.getExceptions().contains("javax.ejb.CreateException")) {
167             exceptions.add("javax.ejb.CreateException");
168         }
169         MethodModel methodModelCopy = MethodModel.create(
170                 "ejbC" + methodModel.getName().substring(1),
171                 ((Entity) ejb).getPrimKeyClass(),
172                 methodModel.getBody(),
173                 methodModel.getParameters(),
174                 exceptions,
175                 Collections.singleton(Modifier.PUBLIC)
176                 );
177         addMethod(methodModelCopy, ejbClassFileObject, ejb.getEjbClass());
178         MethodModel postCreateMethodModel = MethodModel.create(
179                 "ejbPostC" + methodModel.getName().substring(1),
180                 "void",
181                 "",
182                 methodModel.getParameters(),
183                 exceptions,
184                 Collections.singleton(Modifier.PUBLIC)
185                 );
186         addMethod(postCreateMethodModel, ejbClassFileObject, ejb.getEjbClass());
187     }
188     
189 }
190
Popular Tags