@JsonSerialize(include = Inclusion.NON_NULL) public EntityRef getRef() { if ( ref != null ) { return ref; } ref = getEntity(); if ( ref != null ) { return ref; } UUID u = getId(); if ( u != null ) { String type= null; if(refs!=null && refs.size()>0){ type = refs.get(0).getType(); } return ref( type,u ); } return null; }
@Override public Map<String, String> getServiceProperties() { Map<String, String> props = new HashMap<String,String>(); EntityManager em = getEntityManager(getManagementAppId()); Query q = Query.fromQL("select *"); Results results = null; try { results = em.searchCollection( em.getApplicationRef(), "propertymaps", q); } catch (Exception ex) { logger.error("Error getting system properties", ex); } if ( results == null || results.isEmpty() ) { return props; } org.apache.usergrid.persistence.Entity e = results.getEntity(); for ( String key : e.getProperties().keySet() ) { props.put( key, props.get(key).toString() ); } return props; }
protected Entity getOrganizationEntityForApplication( UUID applicationInfoId ) throws Exception { if ( applicationInfoId == null ) { return null; } final EntityManager em = emf.getEntityManager(smf.getManagementAppId()); Results r = em.getSourceEntities( new SimpleEntityRef(CpNamingUtils.APPLICATION_INFO, applicationInfoId), ORG_APP_RELATIONSHIP, Group.ENTITY_TYPE, Level.ALL_PROPERTIES); return r.getEntity(); }
@Override public boolean updateServiceProperties(Map<String, String> properties) { EntityManager em = getEntityManager(getManagementAppId()); Query q = Query.fromQL("select *"); Results results = null; try { results = em.searchCollection( em.getApplicationRef(), "propertymaps", q); } catch (Exception ex) { logger.error("Error getting system properties", ex); return false; } org.apache.usergrid.persistence.Entity propsEntity = null; if ( !results.isEmpty() ) { propsEntity = results.getEntity(); } else { propsEntity = EntityFactory.newEntity( UUIDUtils.newTimeUUID(), "propertymap"); } // intentionally going only one-level deep into fields and treating all // values as strings because that is all we need for service properties for ( String key : properties.keySet() ) { propsEntity.setProperty(key, properties.get(key).toString()); } try { em.update( propsEntity ); } catch (Exception ex) { logger.error("Error updating service properties", ex); return false; } return true; }
@Override public JobStat getStatsForJob( String jobName, UUID jobId ) throws Exception { EntityManager em = emf.getEntityManager( emf.getManagementAppId() ); Query query = Query.fromQL( "select * where " + JOB_NAME + " = '" + jobName + "' AND " + JOB_ID + " = " + jobId ); Results r = em.searchCollection( em.getApplicationRef(), "job_stats", query ); if ( r.size() == 1 ) { return ( JobStat ) r.getEntity(); } return null; }
user = ( User ) r.getEntity().toTypedEntity(); Map<String, Object> properties = new LinkedHashMap<String, Object>();
@Override public boolean deleteServiceProperty(String name) { EntityManager em = getEntityManager(getManagementAppId()); Query q = Query.fromQL( "select *"); Results results = null; try { results = em.searchCollection( em.getApplicationRef(), "propertymaps", q); } catch (Exception ex) { logger.error("Error getting service property for delete of property: {}", name, ex); return false; } org.apache.usergrid.persistence.Entity propsEntity = null; if ( !results.isEmpty() ) { propsEntity = results.getEntity(); } else { propsEntity = EntityFactory.newEntity( UUIDUtils.newTimeUUID(), "propertymap"); } try { ((AbstractEntity)propsEntity).clearDataset( name ); em.update( propsEntity ); } catch (Exception ex) { logger.error("Error deleting service property orgAppName: {}", name, ex); return false; } return true; }
public OrganizationConfig load( UUID applicationInfoId ) { try { if (applicationInfoId != null && applicationInfoId != CpNamingUtils.MANAGEMENT_APPLICATION_ID) { final EntityManager em = emf.getEntityManager(smf.getManagementAppId()); Results r = em.getSourceEntities( new SimpleEntityRef(CpNamingUtils.APPLICATION_INFO, applicationInfoId), ORG_APP_RELATIONSHIP, Group.ENTITY_TYPE, Level.ALL_PROPERTIES); Group org = (Group) r.getEntity(); if (org != null) { Map<Object, Object> entityProperties = em.getDictionaryAsMap(org, ORGANIZATION_CONFIG_DICTIONARY); return new OrganizationConfig(orgConfigProperties, org.getUuid(), org.getPath(), entityProperties, false); } } return new OrganizationConfig(orgConfigProperties); } catch (Exception e) { return new OrganizationConfig(orgConfigProperties); } }} );
assertEquals( user.getUuid(), r.getEntity().getUuid() );
@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() ); }
@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() ); }
Results listResults = em.searchCollection(em.getApplicationRef(), "users", query); assertEquals(1, listResults.size()); Entity entity = listResults.getEntity(); assertTrue(entity.getMetadata("distance")!=null); assertTrue(Double.parseDouble( entity.getMetadata("distance").toString())>0);
final Entity entityResults = r.getEntity(); assertEquals( "ed@anuff.com", entityResults.getProperty( "username" ) ); assertEquals( "ed@anuff.com", entityResults.getProperty( "email" ) );
assertNotNull( results ); assertEquals( 1, results.size() ); user = results.getEntity(); assertNotNull( user ); assertEquals( "user.username not expected value", "edanuff", user.getProperty( "username" ) ); assertNotNull( results ); assertEquals( 1, results.size() ); user = results.getEntity(); assertNotNull( user ); assertEquals( "user.username not expected value", "edanuff", user.getProperty( "username" ) );
@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() ); }