我正在和Olingo合作,我会说一切都很好,除了一件事:没有找到一种获得功能的方法,归功于@EdmFunctionImport,工作。
我使用的是纯注释方法,而不是JPA,所以我没有ODataJPAContext,因为它描述的是here。我假设AnnotationEdmProvider edmProvider = new AnnotationEdmProvider(AnnotationInstances.MODEL_PACKAGE);
应该找到我的函数,用@EdmFunctionImport属性描述。
谷歌对此一无所知。可能有人有这样的经历吗?
答案 0 :(得分:0)
到目前为止,对Olingo的注释处理器扩展功能导入没有任何官方支持。
但是,如果您通过问题列表,可以使用一些代码贡献,这需要对annotation-core.jar进行一些更改以启用函数导入。你可以在这里参考链接: https://issues.apache.org/jira/browse/OLINGO-651?jql=project%20%3D%20OLINGO%20AND%20component%20%3D%20odata2-annotation%20AND%20resolution%20%3D%20Unresolved%20ORDER%20BY%20due%20ASC%2C%20priority%20DESC%2C%20created%20ASC
答案 1 :(得分:0)
谢谢!我几乎以同样的方式使它工作得更早,但我的代码有点复杂,因为它使用了返回和参数类型解析。 作为基础,我使用了JPA函数支持的方法。 这是我的代码(通过前面的链接获取第一部分):
2)在容器构建器类
中添加一个新方法public ContainerBuilder addFunctionImport(
final FunctionImport functionImport) {
functionImports.add(functionImport);
return this;
}
3)修改了AnnotationEdmProvider类中的handleEntityContainer方法,如下所示。
private void handleEntityContainer(final Class<?> aClass) {
EdmEntityType entityType = aClass.getAnnotation(EdmEntityType.class);
if (entityType != null) {
FullQualifiedName typeName = createFqnForEntityType(aClass);
String containerName = ANNOTATION_HELPER
.extractContainerName(aClass);
ContainerBuilder builder = containerName2ContainerBuilder
.get(containerName);
if (builder == null) {
builder = ContainerBuilder.init(typeName.getNamespace(),
containerName);
containerName2ContainerBuilder.put(containerName, builder);
}
EdmEntitySet entitySet = aClass.getAnnotation(EdmEntitySet.class);
if (entitySet != null) {
builder.addEntitySet(createEntitySet(typeName, aClass));
}
buildFunctionImports(aClass, builder);
}
}
private void buildFunctionImports(Class aClass, ContainerBuilder builder) {
for (int i = 0; i < aClass.getMethods().length; i++) {
Method method = aClass.getMethods()[i];
EdmFunctionImport edmAnnotationFunctionImport = method
.getAnnotation(EdmFunctionImport.class);
if (edmAnnotationFunctionImport != null
&& edmAnnotationFunctionImport.returnType() != null) {
try {
FunctionImport fi = new FunctionImport();
fi.setEntitySet(edmAnnotationFunctionImport.entitySet());
fi.setHttpMethod(edmAnnotationFunctionImport.httpMethod().name());
fi.setName(edmAnnotationFunctionImport.name());
builder.addFunctionImport(fi);
builder.addFunctionImport(buildEdmFunctionImport(method,
edmAnnotationFunctionImport));
} catch (ODataAnnotationException e) {
}
}
}
}
private FunctionImport buildEdmFunctionImport(final Method method,
final EdmFunctionImport edmAnnotationFunctionImport)
throws ODataAnnotationException {
if (edmAnnotationFunctionImport != null
&& edmAnnotationFunctionImport.returnType() != null) {
FunctionImport functionImport = new FunctionImport();
if (edmAnnotationFunctionImport.name().equals("")) {
functionImport.setName(method.getName());
} else {
functionImport.setName(edmAnnotationFunctionImport.name());
}
functionImport.setHttpMethod(edmAnnotationFunctionImport
.httpMethod().name().toString());
buildEdmReturnType(functionImport, method,
edmAnnotationFunctionImport);
buildEdmParameter(functionImport, method);
return functionImport;
}
return null;
}
private void buildEdmParameter(final FunctionImport functionImport,
final Method method) throws ODataAnnotationException {
Annotation[][] annotations = method.getParameterAnnotations();
Class<?>[] parameterTypes = method.getParameterTypes();
List<FunctionImportParameter> funcImpList = new ArrayList<FunctionImportParameter>();
int j = 0;
for (Annotation[] annotationArr : annotations) {
Class<?> parameterType = parameterTypes[j++];
for (Annotation element : annotationArr) {
if (element instanceof EdmFunctionImportParameter) {
EdmFunctionImportParameter annotation = (EdmFunctionImportParameter) element;
FunctionImportParameter functionImportParameter = new FunctionImportParameter();
if (annotation.name().equals("")) {
throw new ODataAnnotationException(
"annotation.name().equals(\"\")");
} else {
functionImportParameter.setName(annotation.name());
}
functionImportParameter.setType(TypeBuilder
.getEdmSimpleType(parameterType));
Facets facets = new Facets();
if (annotation.facets().maxLength() > 0) {
facets.setMaxLength(annotation.facets().maxLength());
}
if (annotation.facets().nullable() == false) {
facets.setNullable(false);
} else {
facets.setNullable(true);
}
if (annotation.facets().precision() > 0) {
facets.setPrecision(annotation.facets().precision());
}
if (annotation.facets().scale() >= 0) {
facets.setScale(annotation.facets().scale());
}
functionImportParameter.setFacets(facets);
funcImpList.add(functionImportParameter);
}
}
}
if (!funcImpList.isEmpty()) {
functionImport.setParameters(funcImpList);
}
}
private void buildEdmReturnType(final FunctionImport functionImport,
final Method method,
final EdmFunctionImport edmAnnotationFunctionImport)
throws ODataAnnotationException {
ReturnType returnType = edmAnnotationFunctionImport.returnType();
if (returnType != null) {
org.apache.olingo.odata2.api.edm.provider.ReturnType functionReturnType = new org.apache.olingo.odata2.api.edm.provider.ReturnType();
if (returnType.isCollection()) {
functionReturnType.setMultiplicity(EdmMultiplicity.MANY);
} else {
functionReturnType.setMultiplicity(EdmMultiplicity.ONE);
}
if (returnType.type() == ReturnType.Type.ENTITY) {
String entitySet = edmAnnotationFunctionImport.entitySet();
if (entitySet.equals("")) {
throw new ODataAnnotationException("entitySet.equals(\"\")");
}
functionImport.setEntitySet(entitySet);
}
Class<?> methodReturnType = method.getReturnType();
if (methodReturnType == null
|| methodReturnType.getName().equals("void")) {
throw new ODataAnnotationException("Wrong return type");
}
switch (returnType.type()) {
case ENTITY:
EntityType edmEntityType = null;
String returnTypeName = null;
if (returnType.isCollection() == false) {
returnTypeName = methodReturnType.getSimpleName();
} else {
returnTypeName = getReturnTypeSimpleName(method);
}
for (Class<?> clazz : annotatedClasses) {
if (returnTypeName.equals(clazz.getName())) {
edmEntityType = getEntityType(createFqnForEntityType(clazz));
break;
}
}
if (edmEntityType == null) {
throw new ODataAnnotationException(
"Unable to find an entity type");
}
functionReturnType.setTypeName(edmEntityType.getBaseType());
break;
case SIMPLE:
EdmSimpleTypeKind edmSimpleTypeKind = TypeBuilder
.getEdmSimpleType(methodReturnType);
functionReturnType.setTypeName(edmSimpleTypeKind
.getFullQualifiedName());
break;
case COMPLEX:
String embeddableTypeName = null;
ComplexType complexType = null;
boolean exists = false;
if (returnType.isCollection() == false) {
embeddableTypeName = methodReturnType.getName();
} else {
embeddableTypeName = getReturnTypeName(method);
}
FullQualifiedName fqn = null;
for (Class<?> clazz : annotatedClasses) {
if (embeddableTypeName.equals(clazz.getName())) {
fqn = ANNOTATION_HELPER.extractComplexTypeFqn(clazz);
break;
}
}
if (fqn == null) {
throw new ODataAnnotationException(
"Unable to find an complex type");
}
functionReturnType.setTypeName(fqn);
break;
default:
break;
}
functionImport.setReturnType(functionReturnType);
}
}
private String getReturnTypeName(final Method method) {
try {
ParameterizedType pt = (ParameterizedType) method
.getGenericReturnType();
Type t = pt.getActualTypeArguments()[0];
return ((Class<?>) t).getName();
} catch (ClassCastException e) {
return method.getReturnType().getName();
}
}
private String getReturnTypeSimpleName(final Method method) {
try {
ParameterizedType pt = (ParameterizedType) method
.getGenericReturnType();
Type t = pt.getActualTypeArguments()[0];
return ((Class<?>) t).getSimpleName();
} catch (ClassCastException e) {
return method.getReturnType().getSimpleName();
}
}