public Entity toEntity( Map<String, Object> map ) { Class<? extends Entity> entityClass = DynamicEntity.class; String type = ( String ) map.get( PROPERTY_TYPE ); if ( type != null ) { entityClass = getEntityClass( type ); } if ( entityClass == null ) { entityClass = DynamicEntity.class; } return mapper.convertValue( map, entityClass ); }
/** * New entity. * * @param id the id * @param type the type * * @return new entity */ public static Entity newEntity( UUID id, String type ) { Class<? extends Entity> entityClass = Schema.getDefaultSchema().getEntityClass( type ); if ( entityClass == null ) { entityClass = DynamicEntity.class; } return newEntity( id, type, entityClass ); } }
throw new IllegalArgumentException( errorMsg ); Class<? extends Entity> expectedCls = Schema.getDefaultSchema().getEntityClass( type );
public JsonNode getEntityJsonSchema( String entityType ) { Class<?> cls = getEntityClass( entityType ); if ( cls == null ) { cls = DynamicEntity.class;
@Override public Results getEntities( List<UUID> ids, String type ) { List<Id> entityIds = new ArrayList<>(); for( UUID uuid : ids){ entityIds.add(new SimpleId( uuid, type )); } // leverage ecm.load so it's a batch fetch of all entities from Cassandra EntitySet entitySet = ecm.load( entityIds ).toBlocking().last(); List<Entity> entities = entitySet.getEntities().stream().map( mvccEntity -> { if( mvccEntity.getEntity().isPresent() ){ org.apache.usergrid.persistence.model.entity.Entity cpEntity = mvccEntity.getEntity().get(); Class clazz = Schema.getDefaultSchema().getEntityClass( mvccEntity.getId().getType() ); Entity entity = EntityFactory.newEntity( mvccEntity.getId().getUuid(), mvccEntity.getId().getType(), clazz ); entity.setProperties( cpEntity ); return entity; }else{ logger.warn("Tried fetching entity with id: {} and type: but was not found", mvccEntity.getId().getUuid(), mvccEntity.getId().getType() ); return null; } }).collect(Collectors.toList()); return Results.fromEntities( entities ); }
@Test public void testTypes() throws Exception { logger.info( "" + Schema.getDefaultSchema().getEntityClass( "sample_entity" ) ); logger.info( "" + Schema.getDefaultSchema().getEntityType( SampleEntity.class ) ); SampleEntity entity = new SampleEntity(); logger.info( entity.getType() ); }
@Override public Entity get( EntityRef entityRef ) throws Exception { if ( entityRef == null ) { return null; } Id id = new SimpleId( entityRef.getUuid(), entityRef.getType() ); org.apache.usergrid.persistence.model.entity.Entity cpEntity = load( id ); if ( cpEntity == null ) { if ( logger.isDebugEnabled() ) { logger.debug( "FAILED to load entity {}:{} from app {}", id.getType(), id.getUuid(), applicationId ); } return null; } Class clazz = Schema.getDefaultSchema().getEntityClass( entityRef.getType() ); Entity entity = EntityFactory.newEntity( entityRef.getUuid(), entityRef.getType(), clazz ); entity.setProperties( cpEntity ); return entity; }
entityClass = ( Class<A> ) Schema.getDefaultSchema().getEntityClass( entityType );
public Entity convertMvccEntityToEntity( org.apache.usergrid.persistence.model.entity.Entity entity){ if(entity == null) { return null; } Class clazz = Schema.getDefaultSchema().getEntityClass(entity.getId().getType()); Entity oldFormatEntity = EntityFactory.newEntity(entity.getId().getUuid(), entity.getId().getType(), clazz); oldFormatEntity.setProperties(CpEntityMapUtils.toMap(entity)); return oldFormatEntity; }
/** * TODO: Use Graph to get application_info for an specified Application. */ private org.apache.usergrid.persistence.Entity getApplicationInfo( final UUID appId ) throws Exception { final ApplicationScope managementAppScope = getApplicationScope( CpNamingUtils.MANAGEMENT_APPLICATION_ID ); final EntityCollectionManager managementCollectionManager = entityCollectionManagerFactory.createCollectionManager( managementAppScope ); Observable<MarkedEdge> edgesObservable = getApplicationInfoEdges( appId ); //get the graph for all app infos Observable<org.apache.usergrid.persistence.model.entity.Entity> entityObs = edgesObservable.flatMap( edge -> { final Id appInfoId = edge.getTargetNode(); return managementCollectionManager.load( appInfoId ).filter( entity -> { //check for app id return entity != null ? entity.getId().getUuid().equals( appId ) : false; } ); } ); // don't expect many applications, so we block org.apache.usergrid.persistence.model.entity.Entity applicationInfo = entityObs.toBlocking().lastOrDefault( null ); if ( applicationInfo == null ) { return null; } Class clazz = Schema.getDefaultSchema().getEntityClass( applicationInfo.getId().getType() ); org.apache.usergrid.persistence.Entity entity = EntityFactory.newEntity( applicationInfo.getId().getUuid(), applicationInfo.getId().getType(), clazz ); entity.setProperties( CpEntityMapUtils.toMap( applicationInfo ) ); return entity; }
@Test public void testThirdPartyEntityTypes() throws Exception { String thirdPartyPackage = "org.usergrid"; Schema schema = Schema.getDefaultSchema(); schema.addEntitiesPackage( thirdPartyPackage ); schema.scanEntities(); List<String> entitiesPackage = schema.getEntitiesPackage(); for ( String entityPackage : entitiesPackage ) { logger.info( entityPackage ); } Assert.assertEquals( schema.getEntityClass( "simple" ), Simple.class ); Assert.assertEquals( schema.getEntityType( Simple.class ), "simple" ); Simple entity = new Simple(); logger.info( entity.getType() ); }
@Test public void testEntityClasses() throws Exception { logger.info( "testEntityClasses" ); Schema mapper = Schema.getDefaultSchema(); assertEquals( "group", mapper.getEntityType( Group.class ) ); assertEquals( User.class, mapper.getEntityClass( "user" ) ); Entity entity = EntityFactory.newEntity( null, "user" ); assertEquals( User.class, entity.getClass() ); User user = ( User ) entity; user.setUsername( "testuser" ); assertEquals( user.getUsername(), user.getProperty( "username" ) ); user.setProperty( "username", "blahblah" ); assertEquals( "blahblah", user.getUsername() ); entity = EntityFactory.newEntity( null, "foobar" ); assertEquals( DynamicEntity.class, entity.getClass() ); Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put( Schema.PROPERTY_UUID, new UUID( 1, 2 ) ); properties.put( "foo", "bar" ); entity.setProperties( properties ); assertEquals( new UUID( 1, 2 ), entity.getUuid() ); assertEquals( new UUID( 1, 2 ), entity.getProperty( Schema.PROPERTY_UUID ) ); assertEquals( "bar", entity.getProperty( "foo" ) ); }