org.apache.olingo.odata2.api.uri.info.GetEntitySetCountUriInfo Java Examples

The following examples show how to use org.apache.olingo.odata2.api.uri.info.GetEntitySetCountUriInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JPAQueryBuilder.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
public Query build(GetEntitySetCountUriInfo uriInfo) throws ODataJPARuntimeException {
  Query query = null;
  try {
    ODataJPAQueryExtensionEntityListener listener = getODataJPAQueryEntityListener((UriInfo) uriInfo);
    if (listener != null) {
      query = listener.getQuery(uriInfo, em);
      JPQLContext jpqlContext = JPQLContext.getJPQLContext();
      query = getParameterizedQueryForListeners(jpqlContext, query);
    }
    if (query == null) {
      query = buildQuery((UriInfo) uriInfo, UriInfoType.GetEntitySetCount);
    }
  } catch (Exception e) {
    throw ODataJPARuntimeException.throwException(
        ODataJPARuntimeException.ERROR_JPQL_QUERY_CREATE, e);
  } finally {
    JPQLContext.removeJPQLContext();
    ODataExpressionParser.removePositionalParametersThreadLocal();
  }
  return query;
}
 
Example #2
Source File: Processor.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public ODataResponse countEntitySet(final GetEntitySetCountUriInfo uri_info,
      final String content_type) throws ODataException
{
   // Gets the `collection` part of the URI.
   EdmEntitySet targetES = uri_info.getTargetEntitySet();
   AbstractEntitySet entityset = Model.getEntitySet(targetES.getName());

   // Validity and security checks.
   if (!entityset.isAuthorized(Security.getCurrentUser()) ||
       uri_info.getNavigationSegments().isEmpty() && !entityset.isTopLevel())
   {
      throw new NotAllowedException();
   }

   // Builds the response.
   KeyPredicate startKP =
         (uri_info.getKeyPredicates().isEmpty()) ? null : uri_info.getKeyPredicates().get(0);

   Map<?, ?> results = Navigator.<Map>navigate(uri_info.getStartEntitySet(), startKP,
         uri_info.getNavigationSegments(), Map.class);

   FilterExpression filter = uri_info.getFilter();
   // Skip, Sort and Filter.
   if (results instanceof SubMap && (filter != null))
   {
      SubMapBuilder smb = ((SubMap) results).getSubMapBuilder();
      smb.setFilter(filter);
      results = smb.build();
   }

   return ODataResponse.entity(results.size()).build();
}
 
Example #3
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
public ODataResponse countEntitySet(final GetEntitySetCountUriInfo uriInfo, final String contentType)
    throws ODataException {
  ArrayList<Object> data = new ArrayList<Object>();
  try {
    data.addAll((List<?>) retrieveData(
        uriInfo.getStartEntitySet(),
        uriInfo.getKeyPredicates(),
        uriInfo.getFunctionImport(),
        mapFunctionParameters(uriInfo.getFunctionImportParameters()),
        uriInfo.getNavigationSegments()));
  } catch (final ODataNotFoundException e) {
    data.clear();
  }

  applySystemQueryOptions(
      uriInfo.getTargetEntitySet(),
      data,
      uriInfo.getFilter(),
      null,
      null,
      null,
      uriInfo.getSkip(),
      uriInfo.getTop());

  return ODataResponse.fromResponse(EntityProvider.writeText(String.valueOf(data.size()))).build();
}
 
Example #4
Source File: ODataJPADefaultProcessor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
public ODataResponse countEntitySet(final GetEntitySetCountUriInfo uriParserResultView, final String contentType)
    throws ODataException {
  ODataResponse oDataResponse = null;
  try {
    oDataJPAContext.setODataContext(getContext());
    long jpaEntityCount = jpaProcessor.process(uriParserResultView);
    oDataResponse = responseBuilder.build(jpaEntityCount);
  } finally {
    close();
  }
  return oDataResponse;
}
 
Example #5
Source File: JPAQueryBuilderTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void buildQueryCountEntitySet() {
  EdmMapping mapping = (EdmMapping) mockMapping();
  try {
    assertNotNull(builder.build((GetEntitySetCountUriInfo) mockURIInfoForEntitySetCount(mapping)));
  } catch (ODataException e) {
    fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
  }
}
 
Example #6
Source File: JPAQueryBuilderTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void buildGetEntitySetCountTest() {
  try {
    assertNotNull(builder.build((GetEntitySetCountUriInfo) mockURIInfoWithListener(false)));
  } catch (ODataException e) {
    fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
  }
}
 
Example #7
Source File: JPAProcessorImpl.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
public long process(final GetEntitySetCountUriInfo resultsView)
    throws ODataJPAModelException, ODataJPARuntimeException {

  JPAQueryBuilder queryBuilder = new JPAQueryBuilder(oDataJPAContext);
  Query query = queryBuilder.build(resultsView);
  List<?> resultList = query.getResultList();
  if (resultList != null && resultList.size() == 1) {
    return Long.valueOf(resultList.get(0).toString());
  }

  return 0;
}
 
Example #8
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
public ODataResponse countEntitySet(final GetEntitySetCountUriInfo uriInfo, final String contentType)
    throws ODataException {
  ArrayList<Object> data = new ArrayList<Object>();
  try {
    data.addAll((List<?>) retrieveData(
        uriInfo.getStartEntitySet(),
        uriInfo.getKeyPredicates(),
        uriInfo.getFunctionImport(),
        mapFunctionParameters(uriInfo.getFunctionImportParameters()),
        uriInfo.getNavigationSegments()));
  } catch (final ODataNotFoundException e) {
    data.clear();
  }

  applySystemQueryOptions(
      uriInfo.getTargetEntitySet(),
      data,
      uriInfo.getFilter(),
      null,
      null,
      null,
      uriInfo.getSkip(),
      uriInfo.getTop());

  return ODataResponse.fromResponse(EntityProvider.writeText(String.valueOf(data.size()))).build();
}
 
Example #9
Source File: ODataJPAProcessor.java    From lemonaid with MIT License 5 votes vote down vote up
@Override
public ODataResponse countEntitySet(final GetEntitySetCountUriInfo uriParserResultView, final String contentType)
		throws ODataException {
	authorization.check(READ, uriParserResultView);
	ODataResponse oDataResponse = null;
	augmentFilter((UriInfoImpl) uriParserResultView);
	try {
		oDataJPAContext.setODataContext(getContext());
		long jpaEntityCount = jpaProcessor.process(uriParserResultView);
		oDataResponse = responseBuilder.build(jpaEntityCount);
	} finally {
		close();
	}
	return oDataResponse;
}
 
Example #10
Source File: JPAQueryBuilder.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
public void getCount(GetEntitySetUriInfo uriInfo) throws ODataJPARuntimeException {
  JPAQueryInfo queryInfo = new JPAQueryInfo();
  Query query = null;
  UriInfoImpl info = (UriInfoImpl)uriInfo;
  boolean count = info.isCount();
  info.setCount(true);
  try {
    ODataJPAQueryExtensionEntityListener listener = getODataJPAQueryEntityListener((UriInfo) uriInfo);
    if (listener != null) {
      query = listener.getQuery((GetEntitySetCountUriInfo)uriInfo, em);
      if(query != null){
        JPQLContextType contextType = determineJPQLContextType(info, UriInfoType.GetEntitySetCount);
        JPQLContext jpqlContext = buildJPQLContext(contextType, info);
        JPQLStatement jpqlStatement = JPQLStatement.createBuilder(jpqlContext).build();
        jpqlContext.setJPQLStatement(jpqlStatement.toString());
        query = getParameterizedQueryForListeners(jpqlContext, query);
      }
    }
    if (query == null) {
      query = buildQuery((UriInfo) uriInfo, UriInfoType.GetEntitySetCount);
    } else {
      queryInfo.setTombstoneQuery(true);
    }
  } catch (Exception e) {
    throw ODataJPARuntimeException.throwException(
        ODataJPARuntimeException.ERROR_JPQL_QUERY_CREATE, e);
  } finally {
    JPQLContext.removeJPQLContext();
    ODataExpressionParser.removePositionalParametersThreadLocal();
  }
  queryInfo.setQuery(query);
  Query countQuery = queryInfo.getQuery();
  List<Object> countList = countQuery.getResultList();
  info.setCount(count);
  if(countList!= null && !countList.isEmpty()){
    String countNumber = countList.get(0).toString();
    Map<String, String> customQueryOptions = new HashMap<String, String>();
    customQueryOptions.put("count", countNumber);
    info.setCustomQueryOptions(customQueryOptions);
  }
}
 
Example #11
Source File: ODataJPADefaultProcessorTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private GetEntitySetCountUriInfo getEntitySetCountUriInfo() {
  return getLocalUriInfo();
}
 
Example #12
Source File: JPAProcessorMockAbstract.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public long process(GetEntitySetCountUriInfo requestView) throws ODataJPAModelException, ODataJPARuntimeException {
  // TODO Auto-generated method stub
  return 0;
}
 
Example #13
Source File: JPAProcessorImplTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private GetEntitySetCountUriInfo getEntitySetCountUriInfo() {
  return getLocalUriInfo();
}
 
Example #14
Source File: JPAProcessorImplTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public Query getQuery(GetEntitySetCountUriInfo uriInfo, EntityManager em) {
  return query;
}
 
Example #15
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public ODataResponse countEntityLinks(final GetEntitySetLinksCountUriInfo uriInfo, final String contentType)
    throws ODataException {
  return countEntitySet((GetEntitySetCountUriInfo) uriInfo, contentType);
}
 
Example #16
Source File: JPAQueryBuilderTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public Query getQuery(GetEntitySetCountUriInfo uriInfo, EntityManager em) {
  return query;
}
 
Example #17
Source File: BatchHandlerTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public ODataResponse countEntitySet(GetEntitySetCountUriInfo uriInfo, String contentType) throws ODataException {
  // this method is not needed.
  return null;
}
 
Example #18
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public ODataResponse countEntityLinks(final GetEntitySetLinksCountUriInfo uriInfo, final String contentType)
    throws ODataException {
  return countEntitySet((GetEntitySetCountUriInfo) uriInfo, contentType);
}
 
Example #19
Source File: ODataSingleProcessor.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
/**
 * @see EntitySetProcessor
 */
@Override
public ODataResponse countEntitySet(final GetEntitySetCountUriInfo uriInfo, final String contentType)
    throws ODataException {
  throw new ODataNotImplementedException();
}
 
Example #20
Source File: ODataJPAQueryExtensionEntityListener.java    From olingo-odata2 with Apache License 2.0 2 votes vote down vote up
/**
 * Override this method to build JPA Query for OData request - GetEntitySet Count; SELECT COUNT(*)
 * @param uriInfo is a reference to OData request
 * @param em is a reference to {@link javax.persistence.EntityManager}
 * @return an instance of type {@link javax.persistence.Query}
 */
public Query getQuery(GetEntitySetCountUriInfo uriInfo, EntityManager em) throws ODataJPARuntimeException {
  return null;
}
 
Example #21
Source File: EntitySetProcessor.java    From olingo-odata2 with Apache License 2.0 2 votes vote down vote up
/**
 * Counts the number of requested entities.
 * @param uriInfo information about the request URI
 * @param contentType the content type of the response
 * @return an {@link ODataResponse} object
 * @throws ODataException
 */
ODataResponse countEntitySet(GetEntitySetCountUriInfo uriInfo, String contentType) throws ODataException;
 
Example #22
Source File: JPAProcessor.java    From olingo-odata2 with Apache License 2.0 votes vote down vote up
/**
 * Processes OData request for fetching Entity count. The method returns JPA Entity count
 * 
 * @param requestView
 * OData request for counting an entity set
 * @return long value representing count of JPA entity set
 * 
 * @throws ODataJPAModelException
 * @throws ODataJPARuntimeException
 */

public long process(GetEntitySetCountUriInfo requestView)
    throws ODataJPAModelException, ODataJPARuntimeException;