KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashSet JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.lang.model.element.Modifier;
28 import org.netbeans.modules.j2ee.common.method.MethodModel;
29 import org.netbeans.modules.j2ee.dd.api.ejb.Entity;
30 import org.netbeans.modules.j2ee.dd.api.ejb.MethodParams;
31 import org.netbeans.modules.j2ee.dd.api.ejb.Query;
32 import org.netbeans.modules.j2ee.dd.api.ejb.QueryMethod;
33 import org.openide.filesystems.FileObject;
34
35 /**
36  *
37  * @author Martin Adamek
38  */

39 public class SelectMethodGenerator extends AbstractMethodGenerator {
40     
41     public SelectMethodGenerator(Entity ejb, FileObject ejbClassFileObject, FileObject ddFileObject) {
42         super(ejb, ejbClassFileObject, ddFileObject);
43     }
44     
45     public static SelectMethodGenerator create(Entity ejb, FileObject ejbClassFileObject, FileObject ddFileObject) {
46         return new SelectMethodGenerator(ejb, ejbClassFileObject, ddFileObject);
47     }
48     
49     public void generate(MethodModel methodModel, boolean generateLocal, boolean generateRemote,
50             boolean isOneReturn, String JavaDoc ejbql) throws IOException JavaDoc {
51         
52         if (!methodModel.getName().startsWith("ejbSelect")) {
53             throw new IllegalArgumentException JavaDoc("The select method name must have ejbSelect as its prefix.");
54         }
55         
56         List JavaDoc<String JavaDoc> exceptions = new ArrayList JavaDoc<String JavaDoc>(methodModel.getExceptions());
57         if (!methodModel.getExceptions().contains("javax.ejb.FinderException")) {
58             exceptions.add("javax.ejb.FinderException");
59         }
60         Set JavaDoc<Modifier> modifiers = new HashSet JavaDoc<Modifier>(2);
61         modifiers.add(Modifier.PUBLIC);
62         modifiers.add(Modifier.ABSTRACT);
63         MethodModel methodModelCopy = MethodModel.create(
64                 methodModel.getName(),
65                 methodModel.getReturnType(),
66                 null,
67                 methodModel.getParameters(),
68                 exceptions,
69                 modifiers
70                 );
71         addMethod(methodModelCopy, ejbClassFileObject, ejb.getEjbClass());
72         
73         // write query to deplyment descriptor
74
addQueryToXml(methodModel, ejbql);
75         
76     }
77     
78     private void addQueryToXml(MethodModel methodModel, String JavaDoc ejbql) throws IOException JavaDoc {
79         Entity entity = (Entity) ejb;
80         Query query = entity.newQuery();
81         QueryMethod queryMethod = query.newQueryMethod();
82         queryMethod.setMethodName(methodModel.getName());
83         MethodParams methodParams = queryMethod.newMethodParams();
84         for (MethodModel.Variable parameter : methodModel.getParameters()) {
85             methodParams.addMethodParam(parameter.getType());
86         }
87         queryMethod.setMethodParams(methodParams);
88         query.setQueryMethod(queryMethod);
89         query.setEjbQl(ejbql);
90         entity.addQuery(query);
91         saveXml();
92     }
93
94 }
95
Popular Tags