/** * Persists the connection for this entity. */ private void saveConnections( Entity entity, EntityManager em, JsonGenerator jg ) throws Exception { jg.writeFieldName( "connections" ); jg.writeStartObject(); Set<String> connectionTypes = em.getConnectionTypes( entity ); for ( String connectionType : connectionTypes ) { jg.writeFieldName( connectionType ); jg.writeStartArray(); Results results = em.getTargetEntities( new SimpleEntityRef(entity.getType(), entity.getUuid()), connectionType, null, Level.IDS); List<ConnectionRef> connections = results.getConnections(); for ( ConnectionRef connectionRef : connections ) { jg.writeObject( connectionRef.getTargetRefs().getUuid() ); } jg.writeEndArray(); } jg.writeEndObject(); }
@Override public BiMap<UUID, String> getApplicationsForOrganization( UUID organizationGroupId ) throws Exception { if ( organizationGroupId == null ) { return null; } final BiMap<UUID, String> applications = HashBiMap.create(); final EntityManager em = emf.getEntityManager(smf.getManagementAppId()); // query for application_info entities final Results results = em.getTargetEntities( new SimpleEntityRef(Group.ENTITY_TYPE, organizationGroupId), ORG_APP_RELATIONSHIP, CpNamingUtils.APPLICATION_INFO, Level.ALL_PROPERTIES); final PagingResultsIterator itr = new PagingResultsIterator( results ); String entityName; while ( itr.hasNext() ) { final Entity entity = ( Entity ) itr.next(); entityName = entity.getName(); if ( entityName != null ) { entityName = entityName.toLowerCase(); } // make sure we return applicationId and not the application_info UUID UUID applicationId = entity.getUuid(); applications.put( applicationId, entityName ); } return applications; }
public Map<String, Map<String, List<UUID>>> testEntityConnections( UUID applicationId, UUID entityId, String connectionType, String entityType, int expectedCount ) throws Exception { logger.info( "----------------------------------------------------" ); logger.info( "Checking connections for " + entityId.toString() ); EntityManager em = setup.getEmf().getEntityManager( applicationId ); Entity en = em.get( new SimpleEntityRef( entityType, entityId)); Results results = em.getTargetEntities(en, connectionType, null, Level.REFS); logger.info( "----------------------------------------------------" ); assertEquals( "Expected " + expectedCount + " connections", expectedCount, results.getConnections().size() ); // return connections; return null; }
private int getConnectionCountViaGet( final Import importRoot ) { try { EntityManager emMgmtApp = setup.getEmf() .getEntityManager(setup.getEmf().getManagementAppId() ); Results entities = emMgmtApp.getTargetEntities( importRoot, "includes", null, Query.Level.ALL_PROPERTIES); PagingResultsIterator itr = new PagingResultsIterator( entities ); int count = 0; while ( itr.hasNext() ) { itr.next(); count++; } return count; } catch ( Exception e ) { logger.error( "application doesn't exist within the current context" ); throw new RuntimeException( e ); } }
try { final Results connResults = managementEm.getTargetEntities( oldAppEntity, connType, null, Query.Level.ALL_PROPERTIES ); connResults.getEntities().forEach( entity -> { try {
List<ConnectionRef> connections; for (int i = 0; i < 2; i++) { r = em.getTargetEntities(entities.get(i), "related", null, Level.IDS); connections = r.getConnections(); assertNotNull(connections);
@Test public void testConnectionsIterable() throws Exception { EntityManager em = app.getEntityManager(); assertNotNull( em ); User first = new User(); first.setUsername( "first" ); first.setEmail( "first@usergrid.com" ); Entity firstUserEntity = em.create( first ); assertNotNull( firstUserEntity ); final int connectionCount = 100; final Map<UUID, Entity> things = new HashMap<>(); for(int i = 0; i < connectionCount; i ++){ Map<String, Object> data = new HashMap<String, Object>(); data.put( "ordinal", i ); Entity entity = em.create( "thing", data ); em.createConnection( firstUserEntity, "likes", entity ); things.put( entity.getUuid(), entity ); } app.waitForQueueDrainAndRefreshIndex(); Results r = em.getTargetEntities(firstUserEntity, "likes", null, Level.ALL_PROPERTIES) ; PagingResultsIterator itr = new PagingResultsIterator( r ); int checkedIndex = 0; for(; checkedIndex < connectionCount && itr.hasNext(); checkedIndex ++){ final Entity returned = ( Entity ) itr.next(); final Entity expected = things.get( returned.getUuid() ); assertEquals("Entity expected", expected, returned); } assertEquals("Checked all entities", connectionCount, checkedIndex ); }
List<ConnectionRef> connections; for (int i = 0; i < 2; i++) { r = em.getTargetEntities(entities.get(i), "related", null, Level.IDS); connections = r.getConnections(); assertNotNull(connections);
@Override public ServiceResults getCollection( ServiceContext context ) throws Exception { checkPermissionsForCollection( context ); Results r = null; if ( connecting() ) { r = em.getSourceEntities( new SimpleEntityRef(context.getOwner().getType(), context.getOwner().getUuid()), context.getCollectionName(), null, Level.ALL_PROPERTIES); } else { r = em.getTargetEntities( new SimpleEntityRef(context.getOwner().getType(), context.getOwner().getUuid()), context.getCollectionName(), null, Level.ALL_PROPERTIES); } importEntities( context, r ); return new ServiceResults( this, context, Type.CONNECTION, r, null, null ); }
@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() ) ); }
@Test public void testEntityConnectionsSimple() throws Exception { EntityManager em = app.getEntityManager(); assertNotNull( em ); User first = new User(); first.setUsername( "first" ); first.setEmail( "first@usergrid.com" ); Entity firstUserEntity = em.create( first ); assertNotNull( firstUserEntity ); User second = new User(); second.setUsername( "second" ); second.setEmail( "second@usergrid.com" ); Entity secondUserEntity = em.create( second ); assertNotNull( secondUserEntity ); em.createConnection( firstUserEntity, "likes", secondUserEntity ); Set<String> connectionTypes = em.getConnectionTypes(firstUserEntity); assertEquals( 1, connectionTypes.size()); assertEquals("likes", connectionTypes.iterator().next()); app.waitForQueueDrainAndRefreshIndex(); Results r = em.getTargetEntities(firstUserEntity, "likes", null, Level.IDS); List<ConnectionRef> connections = r.getConnections(); assertNotNull( connections ); assertEquals(1, connections.size()); assertEquals( secondUserEntity.getUuid(), connections.get( 0 ).getTargetRefs().getUuid() ); assertEquals( firstUserEntity.getUuid(), connections.get( 0 ).getSourceRefs().getUuid() ); }
Results r = emApp2.getTargetEntities(e, "related", null, Level.IDS); List<ConnectionRef> connections = r.getConnections(); conCount += connections.size();
Results r = em.getTargetEntities(firstUserEntity, "likes", "restaurant", Level.IDS); r = em.getTargetEntities(secondUserEntity, "likes", "restaurant", Level.IDS);