blob: d67b73e07bcaf98b1dd73d3fef369c13419c4a58 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.j2ee.ejbcore.action;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.lang.model.element.Modifier;
import org.netbeans.modules.j2ee.core.api.support.java.method.MethodModel;
import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider;
import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans;
import org.netbeans.modules.j2ee.dd.api.ejb.Entity;
import org.netbeans.modules.j2ee.dd.api.ejb.MethodParams;
import org.netbeans.modules.j2ee.dd.api.ejb.Query;
import org.netbeans.modules.j2ee.dd.api.ejb.QueryMethod;
import org.openide.filesystems.FileObject;
/**
*
* @author Martin Adamek
*/
public class SelectMethodGenerator extends AbstractMethodGenerator {
public SelectMethodGenerator(String ejbClass, FileObject ejbClassFileObject) {
super(ejbClass, ejbClassFileObject);
}
public static SelectMethodGenerator create(String ejbClass, FileObject ejbClassFileObject) {
return new SelectMethodGenerator(ejbClass, ejbClassFileObject);
}
/**
* Generates select method.
* <p>
* <b>Should be called outside EDT.</b>
*/
public void generate(MethodModel methodModel, boolean generateLocal, boolean generateRemote, boolean isOneReturn, String ejbql) throws IOException {
if (!methodModel.getName().startsWith("ejbSelect")) {
throw new IllegalArgumentException("The select method name must have ejbSelect as its prefix.");
}
List<String> exceptions = new ArrayList<String>(methodModel.getExceptions());
if (!methodModel.getExceptions().contains("javax.ejb.FinderException")) {
exceptions.add("javax.ejb.FinderException");
}
Set<Modifier> modifiers = new HashSet<Modifier>(2);
modifiers.add(Modifier.PUBLIC);
modifiers.add(Modifier.ABSTRACT);
MethodModel methodModelCopy = MethodModel.create(
methodModel.getName(),
methodModel.getReturnType(),
null,
methodModel.getParameters(),
exceptions,
modifiers
);
addMethod(methodModelCopy, ejbClassFileObject, ejbClass);
// write query to deplyment descriptor
addQueryToXml(methodModel, ejbql);
}
private void addQueryToXml(MethodModel methodModel, String ejbql) throws IOException {
EjbJar ejbJar = DDProvider.getDefault().getDDRoot(ejbModule.getDeploymentDescriptor()); // EJB 2.1
EnterpriseBeans enterpriseBeans = ejbJar.getEnterpriseBeans();
Entity entity = (Entity) enterpriseBeans.findBeanByName(EnterpriseBeans.ENTITY, Entity.EJB_CLASS, ejbClass);
Query query = entity.newQuery();
QueryMethod queryMethod = query.newQueryMethod();
queryMethod.setMethodName(methodModel.getName());
MethodParams methodParams = queryMethod.newMethodParams();
for (MethodModel.Variable parameter : methodModel.getParameters()) {
methodParams.addMethodParam(parameter.getType());
}
queryMethod.setMethodParams(methodParams);
query.setQueryMethod(queryMethod);
query.setEjbQl(ejbql);
entity.addQuery(query);
saveXml();
}
}