public Query withReversed( boolean reversed ) { setReversed( reversed ); return this; }
@Override public Results getCollection( String collectionName, UUID startResult, int count, Level resultsLevel, boolean reversed ) throws Exception { final String ql; if (startResult != null ) { // UUID timestamp is a different measure than 'created' field on entities Calendar uuidEpoch = Calendar.getInstance(TimeZone.getTimeZone("UTC")); uuidEpoch.clear(); uuidEpoch.set(1582, 9, 15, 0, 0, 0); // 9 = October long epochMillis = uuidEpoch.getTime().getTime(); long time = (startResult.timestamp() / 10000L) + epochMillis; if ( !reversed ) { ql = "select * where created > " + time; } else { ql = "select * where created < " + time; } } else { ql = "select *"; } Query query = Query.fromQL( ql ); if(query == null ){ throw new RuntimeException("Unable to get data for collection: "+collectionName); } query.setLimit( count ); query.setReversed( reversed ); return searchCollection( collectionName, query ); }
q.setReversed( reversed );
@Test public void emptyQueryReverse() throws Exception { logger.debug( "emptyQueryReverse" ); EntityManager em = app.getEntityManager(); assertNotNull( em ); String firstName = "firstName" + UUIDUtils.newTimeUUID(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "username", "edanuff" ); properties.put( "email", "ed@anuff.com" ); properties.put( "firstname", firstName ); Entity user = em.create( "user", properties ); assertNotNull( user ); properties = new LinkedHashMap<String, Object>(); properties.put( "username", "djacobs" ); properties.put( "email", "djacobs@gmail.com" ); Entity user2 = em.create( "user", properties ); assertNotNull( user2 ); app.waitForQueueDrainAndRefreshIndex(); // EntityRef Query query = new Query(); query.setReversed( true ); Results r = em.searchCollection( em.getApplicationRef(), "users", query ); assertEquals( 2, r.size() ); Entity returned = r.getEntities().get( 0 ); assertEquals( user2.getUuid(), returned.getUuid() ); returned = r.getEntities().get( 1 ); assertEquals( user.getUuid(), returned.getUuid() ); }
@Override public ServiceResults putItemsByQuery( ServiceContext context, Query query ) throws Exception { checkPermissionsForCollection( context ); if ( context.moreParameters() ) { return getItemsByQuery( context, query ); } query = new Query( query ); query.setResultsLevel( Level.ALL_PROPERTIES ); query.setLimit( 1000 ); if ( !query.isReversedSet() ) { query.setReversed( isCollectionReversed( context ) ); } Results r = em.searchCollection( context.getOwner(), context.getCollectionName(), query ); if ( r.isEmpty() ) { throw new ServiceResourceNotFoundException( context ); } updateEntities( context, r ); return new ServiceResults( this, context, Type.COLLECTION, r, null, null ); }
@Override public ServiceResults deleteItemsByQuery( ServiceContext context, Query query ) throws Exception { checkPermissionsForCollection( context ); if ( context.moreParameters() ) { return getItemsByQuery( context, query ); } query = new Query( query ); query.setResultsLevel( Level.ALL_PROPERTIES ); query.setLimit( query.getLimit() ); if ( !query.isReversedSet() ) { query.setReversed( isCollectionReversed( context ) ); } Results r = em.searchCollection( context.getOwner(), context.getCollectionName(), query ); importEntities( context, r ); for ( Entity entity : r ) { prepareToDelete( context, entity ); } for ( Entity entity : r ) { em.removeFromCollection( context.getOwner(), context.getCollectionName(), entity ); } return new ServiceResults( this, context, Type.COLLECTION, r, null, null ); }
query.setReversed( isCollectionReversed( context ) );