public ODataResponse build()

in odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/ODataJPAResponseBuilderDefault.java [295:370]


  public ODataResponse build(final GetFunctionImportUriInfo resultsView, final List<Object> resultList,
      final String contentType) throws ODataJPARuntimeException,
      ODataNotFoundException {

    ODataResponse odataResponse = null;

    if (resultList != null) {
      JPAEntityParser jpaResultParser = new JPAEntityParser();
      EdmType edmType = null;
      EdmFunctionImport functionImport = null;
      List<Map<String, Object>> edmEntityList = null;
      Object result = null;
      try {
      
        functionImport = resultsView.getFunctionImport();
        edmType = functionImport.getReturnType().getType();
        
        final List<SelectItem> selectedItems = resultsView.getSelect();
        List<ArrayList<NavigationPropertySegment>> expandList = null;
        EntityProviderWriteProperties feedProperties = null;

        if (edmType.getKind().equals(EdmTypeKind.ENTITY) || edmType.getKind().equals(EdmTypeKind.COMPLEX)) {
	    	if (selectedItems != null && !selectedItems.isEmpty()) {
	            edmEntityList =
	                jpaResultParser.parse2EdmEntityList(resultList, 
	                		buildSelectItemList(selectedItems, (EdmStructuralType) edmType));
	          } else {
	              edmEntityList = jpaResultParser.parse2EdmEntityList(resultList, (EdmStructuralType) edmType);
	          }
	    	expandList = resultsView.getExpand();
	        if (expandList != null && !expandList.isEmpty()) {
	          int count = 0;
	          List<EdmNavigationProperty> edmNavPropertyList = constructListofNavProperty(expandList);
	          for (Object jpaEntity : resultList) {
	            Map<String, Object> relationShipMap = edmEntityList.get(count);
	            HashMap<String, Object> navigationMap =
	                jpaResultParser.parse2EdmNavigationValueMap(jpaEntity, edmNavPropertyList);
	            relationShipMap.putAll(navigationMap);
	            count++;
	          }
	        }
	        if (functionImport.getReturnType().getMultiplicity().equals(EdmMultiplicity.MANY)) {
	        	result = edmEntityList;
	        } else {
	        	result = edmEntityList.get(0);
	        }
	    	feedProperties = 
	        		getEntityProviderProperties(oDataJPAContext, resultsView, edmEntityList);


        } else if (edmType.getKind().equals(EdmTypeKind.SIMPLE)) {
          result = resultList.get(0);
          feedProperties =
                  EntityProviderWriteProperties.serviceRoot(
                		  oDataJPAContext.getODataContext().getPathInfo().getServiceRoot())
                      .build();
        }
        
        odataResponse =
            EntityProvider.writeFunctionImport(contentType, resultsView.getFunctionImport(), result, feedProperties);
        odataResponse = ODataResponse.fromResponse(odataResponse).status(HttpStatusCodes.OK).build();

      } catch (EdmException e) {
        throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
      } catch (EntityProviderException e) {
        throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
      } catch (ODataException e) {
        throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
      }

    } else {
      throw new ODataNotFoundException(ODataHttpException.COMMON);
    }

    return odataResponse;
  }