public Object convertToPropertyType( Class<? extends Entity> entityClass, String property, Object value ) { Class<?> cls = getPropertyType( getEntityType( entityClass ), property ); if ( cls != null ) { return mapper.convertValue( value, cls ); } return value; }
@Override @EntityProperty(required = true, mutable = false, basic = true, indexed = false) public String getType() { return Schema.getDefaultSchema().getEntityType( this.getClass() ); }
/** * New entity. * * @param <A> the generic type * @param id the id * @param entityClass the entity class * * @return new entity */ public static <A extends Entity> A newEntity( UUID id, Class<A> entityClass ) { if ( entityClass == DynamicEntity.class ) { return null; } String type = Schema.getDefaultSchema().getEntityType( entityClass ); return newEntity( id, type, entityClass ); }
@Override public Results get( Collection<UUID> entityIds, Class<? extends Entity> entityClass, Level resultsLevel ) throws Exception { String type = Schema.getDefaultSchema().getEntityType( entityClass ); ArrayList<Entity> entities = new ArrayList<Entity>(); for ( UUID uuid : entityIds ) { EntityRef ref = new SimpleEntityRef( type, uuid ); Entity entity = get( ref, entityClass ); if ( entity != null ) { entities.add( entity ); } } return Results.fromEntities( entities ); }
@Override public void deleteJob( UUID jobId ) { /** * just delete our target job data. This is easier than attempting to delete * from the queue. The runner should catch this and treat the queued message * as discarded */ try { if (logger.isDebugEnabled()) { logger.debug("deleteJob {}", jobId); } getEm().delete( new SimpleEntityRef( Schema.getDefaultSchema().getEntityType(JobData.class), jobId ) ); } catch ( Exception e ) { throw new JobRuntimeException( e ); } }
@Override public Results getImports(final UUID applicationId, @Nullable final String ql, @Nullable final String cursor) { Preconditions.checkNotNull(applicationId, "applicationId must be specified"); try { final EntityManager rootEm = emf.getEntityManager(emf.getManagementAppId()); final Entity appInfo = getApplicationInfoEntity(rootEm, applicationId); Query query = Query.fromQLNullSafe(ql); query.setCursor(cursor); //set our entity type query.setEntityType(Schema.getDefaultSchema().getEntityType(Import.class)); return rootEm.searchCollection(appInfo, APP_IMPORT_CONNECTION, query); } catch (Exception e) { throw new RuntimeException("Unable to get import entity", e); } }
@Override public Results getFailedImportEntities(final UUID applicationId, final UUID importId, final UUID fileImportId, @Nullable final String ql, @Nullable final String cursor) { Preconditions.checkNotNull(applicationId, "applicationId must be specified"); Preconditions.checkNotNull(importId, "importId must be specified"); Preconditions.checkNotNull(fileImportId, "fileImportId must be specified"); try { final EntityManager rootEm = emf.getEntityManager(emf.getManagementAppId()); final FileImport importEntity = getFileImport(applicationId, importId, fileImportId); Query query = Query.fromQLNullSafe(ql); query.setCursor(cursor); query.setConnectionType(FileImportTracker.ERRORS_CONNECTION_NAME); query.setResultsLevel(Level.ALL_PROPERTIES); //set our entity type query.setEntityType(Schema.getDefaultSchema().getEntityType(FailedImportEntity.class)); return rootEm.searchTargetEntities(importEntity, query); } catch (Exception e) { throw new RuntimeException("Unable to get import entity", e); } }
@Override public Results getFileImports(final UUID applicationId, final UUID importId, @Nullable final String ql, @Nullable final String cursor) { Preconditions.checkNotNull(applicationId, "applicationId must be specified"); Preconditions.checkNotNull(importId, "importId must be specified"); try { final EntityManager rootEm = emf.getEntityManager(emf.getManagementAppId()); final Import importEntity = getImport(applicationId, importId); Query query = Query.fromQLNullSafe(ql); query.setCursor(cursor); query.setConnectionType(IMPORT_FILE_INCLUDES_CONNECTION); query.setResultsLevel(Level.ALL_PROPERTIES); //set our entity type query.setEntityType(Schema.getDefaultSchema().getEntityType(FileImport.class)); return rootEm.searchTargetEntities(importEntity, query); } catch (Exception e) { throw new RuntimeException("Unable to get import entity", e); } }
EntityInfo entity = new EntityInfo(); String type = getEntityType( entityClass );
/** * Gets the specified entity. * * @param entityId the entity id * @param entityClass the entity class * * @return entity * * @throws Exception the exception */ public <A extends Entity> A getEntity( UUID entityId, Class<A> entityClass ) throws Exception { String type = Schema.getDefaultSchema().getEntityType( entityClass ); Id id = new SimpleId( entityId, type ); // if ( !UUIDUtils.isTimeBased( id.getUuid() ) ) { // throw new IllegalArgumentException( // "Entity Id " + id.getType() + ":"+ id.getUuid() +" uuid not time based"); // } org.apache.usergrid.persistence.model.entity.Entity cpEntity = load( id ); if ( cpEntity == null ) { if ( logger.isDebugEnabled() ) { logger.debug( "FAILED to load entity {}:{} from app {}\n", id.getType(), id.getUuid(), applicationId ); } return null; } A entity = EntityFactory.newEntity( entityId, type, entityClass ); entity.setProperties( cpEntity ); return entity; }
@Override public Results queryJobData( Query query ) throws Exception { if ( query == null ) { query = new Query(); } String jobDataType = Schema.getDefaultSchema().getEntityType(JobData.class); return getEm().searchCollection( getEm().getApplicationRef(), Schema.defaultCollectionName(jobDataType), query ); }
@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() ); }
query.setEntityType(Schema.getDefaultSchema().getEntityType(FileImport.class)); query.setConnectionType(IMPORT_FILE_INCLUDES_CONNECTION); query.setLimit(MAX_FILE_IMPORTS);
@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" ) ); }