private static Query newQueryIfNull( Query query ) { if ( query == null ) { query = new Query(); } return query; }
/** * Create a query instance from the QL. If the string is null, return an empty query * @param ql * @return */ public static Query fromQLNullSafe(final String ql){ final Query query = fromQL(ql); if(query != null){ return query; } return new Query(); }
public static Query fromQL( String ql ) throws QueryParseException { if ( StringUtils.isEmpty(ql) ) { return null; } Query query = new Query( ); query.setQl( ql ); return query; } public static Query all( ){
public static List<ServiceParameter> addParameter( List<ServiceParameter> parameters, String name ) { if ( parameters == null ) { parameters = new ArrayList<ServiceParameter>(); } if ( name == null ) { return parameters; } if ( "all".equals( name ) ) { Query query = new Query(); ServiceParameter p = new QueryParameter( query ); parameters.add( p ); return parameters; } ServiceParameter p = new NameParameter( name ); parameters.add( p ); return parameters; }
public static Query fromUUID( UUID uuid ) { Query q = new Query(); q.addIdentifier( Identifier.fromUUID( uuid ) ); return q; }
public static Query fromIdentifier( Object id ) { if (id == null) { throw new IllegalArgumentException("null identifier passed in"); } Identifier objectIdentifier = Identifier.from(id); if (objectIdentifier == null) { throw new IllegalArgumentException("Supplied id results in null Identifier"); } Query q = new Query(); q.addIdentifier( Identifier.from(id) ); return q; }
@Override public Results getTargetEntities( String connectionType, String connectedEntityType, Level level ) throws Exception { //until this is refactored properly, we will delegate to a search by query Results raw = null; Preconditions.checkNotNull( connectionType, "connectionType cannot be null" ); Query query = new Query(); query.setConnectionType( connectionType ); query.setEntityType( connectedEntityType ); query.setResultsLevel( level ); return searchTargetEntities( query ); }
@Override public ServiceResults getCollection( ServiceContext context ) throws Exception { checkPermissionsForCollection( context ); if ( getCollectionSort( context ) != null ) { return getItemsByQuery( context, new Query() ); } if (logger.isTraceEnabled()) { logger.trace("Limiting collection to {}", Query.DEFAULT_LIMIT); } int count = Query.DEFAULT_LIMIT; Results r = em.getCollection( context.getOwner(), context.getCollectionName(), null, count, Level.ALL_PROPERTIES, isCollectionReversed( context ) ); importEntities( context, r ); /* * if (r.isEmpty()) { throw new ServiceResourceNotFoundException(request); } */ return new ServiceResults( this, context, Type.COLLECTION, r, null, null ); }
@Override public Results queryJobData( Query query ) throws Exception { if ( query == null ) { query = new Query(); } String jobDataType = Schema.getDefaultSchema().getEntityType(JobData.class); return getEm().searchCollection( getEm().getApplicationRef(), Schema.defaultCollectionName(jobDataType), query ); }
protected List<EntityRef> getNotificationReceipts(EntityRef notification) throws Exception { Query query = new Query(); query.setCollection("receipts"); query.setLimit(100); PathQuery<Receipt> pathQuery = new PathQuery<Receipt>( new SimpleEntityRef(app.getEntityManager().getApplicationRef()), query ); Iterator<Receipt> it = pathQuery.iterator(app.getEntityManager()); List<EntityRef> list =new ArrayList<EntityRef>();//get all while(it.hasNext()){ Receipt receipt =it.next(); if(receipt.getNotificationUUID().equals(notification.getUuid())) { list.add(receipt); } } return list; }
@Test public void emptyQuery() throws Exception { logger.debug( "emptyQuery" ); 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(); 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() ); }
@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() ); }
protected Iterator refIterator(EntityManager em, boolean useGraph) throws Exception { if ( query.getQl() == null && query.getSingleNameOrEmailIdentifier() != null){ return new PagingResultsIterator( getHeadResults( em ), Level.REFS, null); } if ( type != null && uuid != null) { return new PagingResultsIterator( getHeadResults( em ), Level.REFS, null); } else { Query q = query; if ( query.getResultsLevel() != Level.REFS ) { // ensure REFS level q = new Query( q ); q.setResultsLevel( Level.REFS ); } if( useGraph){ return new NotificationGraphIterator( em, source.refIterator( em, true), q ); }else{ return new MultiQueryIterator( em, source.refIterator( em, false ), q ); } } }
@Test public void nameIdentifierTest() throws Exception { logger.debug( "nameIdentifierTest" ); EntityManager em = app.getEntityManager(); assertNotNull( em ); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "keywords", "blah,test,game" ); properties.put( "title", "Solitaire" ); properties.put( "name", "test" ); Entity game1 = em.create( "games", properties ); assertNotNull( game1 ); //we create 2 entities, otherwise this test will pass when it shouldn't properties.put( "name", "test2" ); Entity game2 = em.create( "game", properties ); assertNotNull( game2 ); app.waitForQueueDrainAndRefreshIndex(); // overlap Query query = new Query(); query.addIdentifier( Identifier.fromName( "test" ) ); Results r = em.searchCollection( em.getApplicationRef(), "games", query ); assertEquals( "We should only get 1 result", 1, r.size() ); assertNull( "No cursor should be present", r.getCursor() ); assertEquals( "Saved entity returned", game1, r.getEntity() ); }
/** * Validate loaded entities for geo queries * 1. load test entities * 2. validate the size of the result * 3. verify each entity has geo data */ @Test public void testGeolocationEntities() throws Exception { //1. load test entities EntityManager em = app.getEntityManager(); assertNotNull(em); //2. load test entities for (Map<String, Object> location : LOCATION_PROPERTIES) { Entity entity = em.create("store", location); assertNotNull(entity); logger.debug("Entity {} created", entity.getProperty("name")); } app.waitForQueueDrainAndRefreshIndex(); //2. validate the size of the result Query query = new Query(); Results listResults = em.searchCollection(em.getApplicationRef(), "stores", query); assertEquals("total number of 'stores'", LOCATION_PROPERTIES.size(), listResults.size()); //3. verify each entity has geo data for (Entity entity : listResults.entities) { Map location = (Map)entity.getProperty("location"); assertNotNull(location); assertNotNull(location.get("longitude")); assertNotNull(location.get("latitude")); } }
@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 ); }
@Test public void uuidIdentifierTest() throws Exception { logger.debug( "uuidIdentifierTest" ); EntityManager em = app.getEntityManager(); assertNotNull( em ); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "keywords", "blah,test,game" ); properties.put( "title", "Solitaire" ); Entity game1 = em.create( "game", properties ); assertNotNull( game1 ); //we create 2 entities, otherwise this test will pass when it shouldn't Entity game2 = em.create( "game", properties ); assertNotNull( game2 ); app.waitForQueueDrainAndRefreshIndex(); // overlap Query query = new Query(); query.addIdentifier( Identifier.fromUUID( game1.getUuid() ) ); Results r = em.searchCollection( em.getApplicationRef(), "games", query ); assertEquals( "We should only get 1 result", 1, r.size() ); assertNull( "No cursor should be present", r.getCursor() ); assertEquals( "Saved entity returned", game1, r.getEntity() ); }
@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 = new Query( query ); query.setResultsLevel( level ); query.setLimit( query.getLimit( count ) );
@Test public void emailIdentifierTest() throws Exception { logger.debug( "emailIdentifierTest" ); EntityManager em = app.getEntityManager(); assertNotNull( em ); User user = new User(); user.setUsername( "foobar" ); user.setEmail( "foobar@usergrid.org" ); Entity createUser = em.create( user ); assertNotNull( createUser ); //we create 2 entities, otherwise this test will pass when it shouldn't User user2 = new User(); user2.setUsername( "foobar2" ); user2.setEmail( "foobar2@usergrid.org" ); Entity createUser2 = em.create( user2 ); assertNotNull( createUser2 ); app.waitForQueueDrainAndRefreshIndex(); // overlap Query query = new Query(); query.addIdentifier( Identifier.fromEmail( "foobar@usergrid.org" ) ); Results r = em.searchCollection( em.getApplicationRef(), "users", query ); assertEquals( "We should only get 1 result", 1, r.size() ); assertNull( "No cursor should be present", r.getCursor() ); assertEquals( "Saved entity returned", createUser, r.getEntity() ); }