1

我正在与 Olingo 合作,我想说一切都很好,除了一件事:还没有找到一种获得功能的方法,归因于 @EdmFunctionImport,工作正常。

我使用的是纯注释方法,而不是 JPA,所以我没有此处描述的 ODataJPAContext 。我假设AnnotationEdmProvider edmProvider = new AnnotationEdmProvider(AnnotationInstances.MODEL_PACKAGE);应该找到我的函数,用@EdmFunctionImport 属性描述。

谷歌对此一无所知。可能有人有这样的经历吗?

4

2 回答 2

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

于 2015-08-05T10:13:57.983 回答
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();
    }
}
于 2015-08-06T11:38:21.633 回答