em.getCollection( entity, collectionName, null, 100000, Level.IDS, false );
Results entities = em.getCollection(deviceRef,"users",null,100, Query.Level.REFS,false); Observable.from(entities.getEntities()) .map(new Func1<Entity, Boolean>() {
public void distribute( EntityRef group, Entity activity ) throws Exception { if ( activity == null ) { return; } em.addToCollection( group, "feed", activity ); Results r = em.getCollection( group, "users", null, 10000, Level.REFS, false ); List<EntityRef> refs = r.getRefs(); if ( refs != null ) { em.addToCollections( refs, "feed", activity ); } } }
public int calculatePasswordHistorySizeForUser( UUID userId ) throws Exception { if(logger.isTraceEnabled()){ logger.trace( "calculatePasswordHistorySizeForUser {}", userId.toString() ); } int size = 0; EntityManager em = emf.getEntityManager( smf.getManagementAppId() ); Results orgResults = em.getCollection( new SimpleEntityRef( User.ENTITY_TYPE, userId ), Schema.COLLECTION_GROUPS, null, 10000, Level.REFS, false ); if(logger.isTraceEnabled()){ logger.trace(" orgResults.size() = {}", orgResults.size()); } for ( EntityRef orgRef : orgResults.getRefs() ) { Map<Object, Object> properties = em.getDictionaryAsMap( orgRef, ORGANIZATION_PROPERTIES_DICTIONARY ); if ( properties != null ) { OrganizationInfo orgInfo = new OrganizationInfo( null, null, properties ); if(logger.isTraceEnabled()){ logger.trace( " orgInfo.getPasswordHistorySize() = {}", orgInfo.getPasswordHistorySize() ); } size = Math.max( orgInfo.getPasswordHistorySize(), size ); } } return size; }
@Override public List<UserInfo> getAdminUsersForOrganization( UUID organizationId ) throws Exception { if ( organizationId == null ) { return null; } List<UserInfo> users = new ArrayList<>(); EntityManager em = emf.getEntityManager(smf.getManagementAppId()); Results results = em.getCollection(new SimpleEntityRef(Group.ENTITY_TYPE, organizationId), "users", null, 10000, Level.ALL_PROPERTIES, false); for ( Entity entity : results.getEntities() ) { users.add( getUserInfo( smf.getManagementAppId(), entity ) ); } return users; }
@Override public Boolean call(Entity user) { boolean removed = false; try { if(!user.getUuid().equals(owner.getUuid())) { //skip current user Results devicesResults = em.getCollection(user, "devices", null, 100, Query.Level.REFS, false); List<Entity> userDevices = devicesResults.getEntities(); for (EntityRef userDevice : userDevices) { if(userDevice.getUuid().equals(deviceRef.getUuid())) { //only remove the current device from user em.removeFromCollection(user, "devices", userDevice); // TODO: may want to actually remove the device // em.delete(userDevice); } } em.removeFromCollection(deviceRef, "users", user); removed = true; } } catch (Exception e) { logger.error("Failed to delete connection " + user.toString(), e); } return removed; } }).toBlocking().lastOrDefault(null);
@Override public List<OrganizationInfo> getOrganizations( UUID startResult, int count ) throws Exception { // still need the bimap to search for existing BiMap<UUID, String> organizations = HashBiMap.create(); EntityManager em = emf.getEntityManager(smf.getManagementAppId()); Results results = em.getCollection(em.getApplicationRef(), Schema.COLLECTION_GROUPS, startResult, count, Level.ALL_PROPERTIES, false); List<OrganizationInfo> orgs = new ArrayList<>( results.size() ); OrganizationInfo orgInfo; for ( Entity entity : results.getEntities() ) { String path = ( String ) entity.getProperty( PROPERTY_PATH ); if ( organizations.containsValue( path ) ) { path += "DUPLICATE"; } orgInfo = new OrganizationInfo( entity.getUuid(), path ); orgs.add( orgInfo ); organizations.put( entity.getUuid(), path ); } return orgs; }
@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 ); }
EntityManager em = emf.getEntityManager( smf.getManagementAppId() ); EntityRef userRef = new SimpleEntityRef(User.ENTITY_TYPE, userId); Results results = em.getCollection( userRef, Schema.COLLECTION_GROUPS, null, 1000, Level.ALL_PROPERTIES, false );
public List<UUID> testEntityCollections( UUID applicationId, UUID entityId, String entityType, String collectionName, int expectedCount ) throws Exception { logger.info( "----------------------------------------------------" ); logger.info( "Checking collection " + collectionName + " for " + entityId.toString() ); EntityManager em = setup.getEmf().getEntityManager( applicationId ); Entity en = em.get( new SimpleEntityRef( entityType, entityId )); int i = 0; Results entities = em.getCollection( en, collectionName, null, 100, Level.IDS, false ); for ( UUID id : entities.getIds() ) { logger.info( ( i++ ) + " " + id.toString() ); } logger.info( "----------------------------------------------------" ); assertEquals( "Expected " + expectedCount + " connections", expectedCount, entities.getIds() != null ? entities.getIds().size() : 0 ); // return connections; return entities.getIds(); }
Results results = em.getCollection(em.getApplicationRef(), collectionName, null, expectedEntities, Query.Level.ALL_PROPERTIES, false); results = em.getCollection(em.getApplicationRef(), collectionName, lastEntityUUID, expectedEntities, Query.Level.ALL_PROPERTIES, false);
@Override public void removeAdminUserFromOrganization( UUID userId, UUID organizationId, boolean force ) throws Exception { if ( ( userId == null ) || ( organizationId == null ) ) { return; } EntityManager em = emf.getEntityManager( smf.getManagementAppId() ); try { Results collection = em.getCollection( new SimpleEntityRef( Group.ENTITY_TYPE, organizationId ), "users", null, 2, Level.IDS, false ); int size = collection.size(); if ( !force && (size == 0 || (size == 1 && collection.getId() == userId))) { throw new Exception(); } } catch ( Exception e ) { throw new UnableToLeaveOrganizationException( "Organizations must have at least one member." ); } em.removeFromCollection(new SimpleEntityRef(Group.ENTITY_TYPE, organizationId), "users", new SimpleEntityRef(User.ENTITY_TYPE, userId)); invalidateManagementAppAuthCache(); }
@Test public void testCreateAndList() throws Exception { logger.info( "EntityDaoTest.testCreateAndDelete" ); EntityManager em = app.getEntityManager(); String name = "test.thing" + UUIDUtils.newTimeUUID() + 1; Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "name", name ); properties.put( "foo", "bar" ); logger.info( "Starting entity create" ); Entity thing1 = em.create( "thing", properties ); logger.info( "Entity created" ); String name2 = "test.thing" + UUIDUtils.newTimeUUID() + 2; properties = new LinkedHashMap<String, Object>(); properties.put( "name", name2 ); properties.put( "foo", "bar" ); logger.info( "Starting entity create" ); Entity thing2 = em.create( "thing", properties ); logger.info( "Entity created" ); // now search by username, no results should be returned EntityRef appRef = em.get( new SimpleEntityRef( "application", app.getId() ) ); app.waitForQueueDrainAndRefreshIndex(); Results r = em.getCollection( appRef, "things", null, 10, Level.ALL_PROPERTIES, false ); assertEquals( 2, r.size() ); assertEquals( thing1.getUuid(), r.getEntities().get( 1 ).getUuid() ); assertEquals( thing2.getUuid(), r.getEntities().get( 0 ).getUuid() ); }
@Test public void testFilteringOfDuplicateEdges() throws Exception { logger.info( "EntityManagerIT.testFilteringOfDuplicateEdges" ); EntityManager em = app.getEntityManager(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( "name", "fluffy1" ); Entity entity = em.create( "fluffy", properties ); EntityRef appRef = new SimpleEntityRef("application", app.getId()); EntityRef entityRef = new SimpleEntityRef(entity.getType(), entity.getUuid()); assertNotNull( entity ); // create duplicate edges em.addToCollection(appRef, "fluffies", entityRef); em.addToCollection(appRef, "fluffies", entityRef); //app.waitForQueueDrainAndRefreshIndex(); Results results = em.getCollection(appRef, "fluffies", null, 10, Level.ALL_PROPERTIES, true); // we should be filtering duplicate edges so only assert 1 result back and not the # of edges assertEquals(1, results.getEntities().size()); }
@Test public void runtimeTypeCorrect() throws Exception { logger.debug( "runtimeTypeCorrect" ); EntityManager em = app.getEntityManager(); assertNotNull( em ); int size = 20; List<User> createdEntities = new ArrayList<User>(); for ( int i = 0; i < size; i++ ) { User user = new User(); user.setEmail( String.format( "test%d@usergrid.com", i ) ); user.setUsername( String.format( "test%d", i ) ); user.setName( String.format( "test%d", i ) ); User created = em.create( user ); createdEntities.add( created ); } app.waitForQueueDrainAndRefreshIndex(); Results r = em.getCollection( em.getApplicationRef(), "users", null, 50, Level.ALL_PROPERTIES, false ); logger.info( JsonUtils.mapToFormattedJsonString( r.getEntities() ) ); assertEquals( size, r.size() ); // check they're all the same before deletion for ( int i = 0; i < size; i++ ) { final Entity entity = r.getEntities().get( size - i - 1 ); assertEquals( createdEntities.get( i ).getUuid(), entity.getUuid() ); assertTrue( entity instanceof User ); } }
public void distribute( EntityRef group, EntityRef user, Entity activity ) throws Exception { if ( activity == null ) { return; } em.addToCollection( user, "feed", activity ); Results r1 = em.getCollection( group, "users", null, 10000, Level.IDS, false ); if ( ( r1 == null ) || ( r1.isEmpty() ) ) { return; } Results r2 = em.getSourceEntities(new SimpleEntityRef(user.getType(), user.getUuid()), "following", User.ENTITY_TYPE, Level.IDS); if ( ( r2 == null ) || ( r2.isEmpty() ) ) { return; } r1.and( r2 ); List<EntityRef> refs = Results.fromIdList( r1.getIds(), User.ENTITY_TYPE ).getRefs(); if ( refs != null ) { em.addToCollections( refs, "feed", activity ); } } }
List<Entity> importedThings = em.getCollection( em.getApplicationId(), "things", null, Level.ALL_PROPERTIES).getEntities();
@Override public void copyRelationships( String srcRelationName, EntityRef dstEntityRef, String dstRelationName ) throws Exception { headEntity = em.validate( headEntity ); dstEntityRef = em.validate( dstEntityRef ); CollectionInfo srcCollection = getDefaultSchema().getCollection( headEntity.getType(), srcRelationName ); CollectionInfo dstCollection = getDefaultSchema().getCollection( dstEntityRef.getType(), dstRelationName ); Results results = null; do { if ( srcCollection != null ) { results = em.getCollection( headEntity, srcRelationName, null, 5000, Level.REFS, false ); } else { results = em.getTargetEntities( headEntity, srcRelationName, null, Level.REFS ); } if ( ( results != null ) && ( results.size() > 0 ) ) { List<EntityRef> refs = results.getRefs(); for ( EntityRef ref : refs ) { if ( dstCollection != null ) { em.addToCollection( dstEntityRef, dstRelationName, ref ); } else { em.createConnection( dstEntityRef, dstRelationName, ref ); } } } } while ( ( results != null ) && ( results.hasMoreResults() ) ); }
List<Entity> importedThings = emDefaultApp.getCollection( emDefaultApp.getApplicationId(), "things", query, Level.ALL_PROPERTIES).getEntities();
app.testRequest(ServiceAction.POST, 1, "users",user1.getUuid(),"devices",device2.getUuid()).getEntity(); List device1Users = app.getEntityManager().getCollection(device1,"users",null,100, Query.Level.REFS,false).getEntities(); assertEquals(device1Users.size(),1); List user1Devices = app.getEntityManager().getCollection(user1,"devices",null,100, Query.Level.REFS,false).getEntities(); assertEquals(user1Devices.size(),2);