private void buildEdmReturnType()

in odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmFunctionImport.java [228:311]


    private void buildEdmReturnType(final FunctionImport functionImport, final Method method,
        final EdmFunctionImport edmAnnotationFunctionImport) throws ODataJPAModelException {
      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 ("".equals(entitySet)) {
            throw ODataJPAModelException.throwException(ODataJPAModelException.FUNC_ENTITYSET_EXP, null);
          }
          functionImport.setEntitySet(entitySet);
        }

        Class<?> methodReturnType = method.getReturnType();
        if (methodReturnType == null || "void".equals(methodReturnType.getName())) {
          throw ODataJPAModelException.throwException(ODataJPAModelException.FUNC_RETURN_TYPE_EXP.addContent(method
              .getDeclaringClass(), method.getName()), null);
        }
        switch (returnType.type()) {
        case ENTITY:
          EntityType edmEntityType = null;
          if (returnType.isCollection() == false) {
            edmEntityType = jpaEdmEntityTypeView.searchEdmEntityType(methodReturnType.getSimpleName());
          } else {
            edmEntityType = jpaEdmEntityTypeView.searchEdmEntityType(getReturnTypeSimpleName(method));
          }

          if (edmEntityType == null) {
            throw ODataJPAModelException.throwException(ODataJPAModelException.FUNC_RETURN_TYPE_ENTITY_NOT_FOUND
                .addContent(method.getDeclaringClass(), method.getName(), methodReturnType.getSimpleName()), null);
          }
          functionReturnType.setTypeName(JPAEdmNameBuilder.build(schemaView, edmEntityType.getName()));
          break;
        case SIMPLE:
          EdmSimpleTypeKind edmSimpleTypeKind = JPATypeConverter.convertToEdmSimpleType(methodReturnType, null);
          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);
          }

          complexType = jpaEdmComplexTypeView.searchEdmComplexType(embeddableTypeName);

          if (complexType == null) {// This could occure of non JPA Embeddable Types : Extension Scenario
            List<ComplexType> complexTypeList = schemaView.getEdmSchema().getComplexTypes();
            String[] complexTypeNameParts = embeddableTypeName.split("\\.");
            String complexTypeName = complexTypeNameParts[complexTypeNameParts.length - 1];
            for (ComplexType complexType1 : complexTypeList) {
              if (complexType1.getName().equals(complexTypeName)) {
                complexType = complexType1;
                exists = true;
                break;
              }
            }
            if (exists == false) {
              throw ODataJPAModelException.throwException(ODataJPAModelException.FUNC_RETURN_TYPE_ENTITY_NOT_FOUND
                  .addContent(method.getDeclaringClass(), method.getName(), methodReturnType.getSimpleName()), null);
            }
          }
          functionReturnType.setTypeName(JPAEdmNameBuilder.build(schemaView, complexType.getName()));
          break;
        default:
          break;
        }
        functionImport.setReturnType(functionReturnType);
      }
    }