public ConnectionRef createConnection( EntityRef connectingEntity, String connectionType, EntityRef connectedEntityRef ) throws Exception { return em.createConnection( connectingEntity, connectionType, connectedEntityRef ); }
@Override public ConnectionRef createConnection( EntityRef connectingEntity, String connectionType, EntityRef connectedEntityRef ) throws Exception { copyActivityFeed( connectingEntity, connectedEntityRef ); return em.createConnection( connectingEntity, connectionType, connectedEntityRef ); }
/** * @return UUID of the application itself (NOT the application_info entity). */ @Override public UUID addApplicationToOrganization(UUID organizationId, Entity appInfo) throws Exception { UUID applicationId = appInfo.getUuid(); if ( ( organizationId == null ) || ( applicationId == null ) ) { return null; } EntityManager em = emf.getEntityManager(smf.getManagementAppId()); em.createConnection( new SimpleEntityRef( Group.ENTITY_TYPE, organizationId ), ORG_APP_RELATIONSHIP, appInfo ); return applicationId; }
@Override public Entity writeEntity( Map<String, Object> entity ) throws Exception { // write to the collection Entity created = super.writeEntity( entity ); app.getEntityManager().createConnection( rootEntity, CONNECTION, created ); return created; }
/** * Invoked when a connection cannot be written */ public void connectionFailed( final String message ) { connectionsFailed.incrementAndGet(); FailedImportConnection failedImportConnection = new FailedImportConnection(); failedImportConnection.setErrorMessage( message ); try { EntityManager entityManager = emf.getEntityManager(emf.getManagementAppId()); failedImportConnection = entityManager.create( failedImportConnection ); entityManager.createConnection( fileImport, ERRORS_CONNECTION_NAME, failedImportConnection ); } catch ( Exception e ) { throw new PersistenceException( "Unable to save failed entity import message", e ); } maybeFlush(); }
/** * Invoke when an entity fails to write correctly */ public void entityFailed( final String message ) { entitiesFailed.incrementAndGet(); FailedImportEntity failedImportEntity = new FailedImportEntity(); failedImportEntity.setErrorMessage( message ); try { EntityManager entityManager = emf.getEntityManager(emf.getManagementAppId()); failedImportEntity = entityManager.create( failedImportEntity ); entityManager.createConnection( fileImport, ERRORS_CONNECTION_NAME, failedImportEntity ); } catch ( Exception e ) { throw new PersistenceException( "Unable to save failed entity import message", e ); } maybeFlush(); }
@Override public void doWrite(EntityManager em, FileImport fileImport, FileImportTracker tracker) { try { // TODO: do we need to ensure that all Entity events happen first? // TODO: what happens if ConnectionEvents happen before all entities are saved? // Connections are specified as UUIDs with no type if (entityRef.getType() == null) { entityRef = em.get(ownerEntityRef.getUuid()); } if (logger.isTraceEnabled()) { logger.trace("Creating connection from {}:{} to {}:{}", ownerEntityRef.getType(), ownerEntityRef.getUuid(), entityRef.getType(), entityRef.getUuid()); } em.createConnection(ownerEntityRef, connectionType, entityRef); tracker.connectionWritten(); } catch (Exception e) { logger.error("Error writing connection. From file: {}", fileImport.getFileName(), e); tracker.connectionFailed(e.getMessage() + " From file: " + fileImport.getFileName()); } } }
public void createConnection( Entity subject, String verb, Entity noun ) throws Exception { sm.getEntityManager().createConnection( subject, verb, noun ); setup.getEntityIndex().refresh(id); }
rootEM.createConnection(appInfo, APP_IMPORT_CONNECTION, importEntity);
void createAndSetup2ndApplication() throws Exception { UUID appId = setup.getMgmtSvc().createApplication( organization.getUuid(), "test-app-2" ).getId(); EntityManager emTest = setup.getEmf().getEntityManager(appId); Map<String, Object> userProperties = null; Entity entityTest[] = new Entity[5]; //creates entities and set permissions for ( int i = 0; i < 5; i++ ) { userProperties = new LinkedHashMap<String, Object>(); userProperties.put( "name", "user" + i ); userProperties.put( "email", "user" + i + "@test.com" ); entityTest[i] = emTest.create( "testobject", userProperties ); } //create connection emTest.createConnection( new SimpleEntityRef( "testobject", entityTest[0].getUuid()), "related", new SimpleEntityRef( "testobject", entityTest[1].getUuid())); emTest.createConnection( new SimpleEntityRef( "testobject", entityTest[1].getUuid()), "related", new SimpleEntityRef( "testobject", entityTest[0].getUuid())); } }
/** * Create test entities of a specified type. * First two entities are connected. */ private void createTestEntities(final EntityManager em, Map<UUID, Entity> thingsMap, List<Entity> things, final String type) throws Exception { logger.debug("\n\nCreating new {} collection in application {}\n", type, em.getApplication().getName()); setup.getEntityIndex().refresh(em.getApplicationId()); List<Entity> created = new ArrayList<>(); for (int i = 0; i < 10; i++) { final int count = i; Entity e = em.create(type, new HashMap<String, Object>() {{ put("name", em.getApplication().getName() + "-" + type + "-" + count); put("originalAppId", em.getApplication().getUuid()); put("originalAppName", em.getApplication().getName()); }}); thingsMap.put(e.getUuid(), e); things.add(e); created.add(e); } // first two things are related to each other em.createConnection(new SimpleEntityRef(type, created.get(0).getUuid()), "related", new SimpleEntityRef(type, created.get(1).getUuid())); em.createConnection(new SimpleEntityRef(type, created.get(1).getUuid()), "related", new SimpleEntityRef(type, created.get(0).getUuid())); setup.getEntityIndex().refresh(em.getApplicationId()); }
em.createConnection( SimpleEntityRef.fromId( source ), "likes", SimpleEntityRef.fromId( target ) ); connections.add( target );
FileImport fileImport = new FileImport("dummyFileName" + i, applicationId); fileImport = emMgmtApp.create(fileImport); emMgmtApp.createConnection(importEntity, "includes", fileImport);
@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 ); }
@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() ); }
Entity applicationEntity = em.create( applicationId, CpNamingUtils.APPLICATION_INFO, properties ); em.createConnection( new SimpleEntityRef( "group", organizationEntity.getUuid() ), "owns", new SimpleEntityRef( CpNamingUtils.APPLICATION_INFO, applicationId ) );
em.createConnection( foo, "testconnection", entity );
assertNotNull( JohnEntity ); em.createConnection( fredEntity, "likes", wilmaEntity ); em.createConnection( fredEntity, "likes", JohnEntity );
assertNotNull( wilmaEntity ); em.createConnection( fredEntity, "likes", wilmaEntity );