/** * 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 ); } }
/** * 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 Entity toTypedEntity() { Entity entity = EntityFactory.newEntity( getUuid(), getType() ); entity.setProperties( getProperties() ); return entity; }
/** * * @param cpEntity * @return */ private org.apache.usergrid.persistence.Entity mapEntity( final Entity cpEntity ) { final Id entityId = cpEntity.getId(); org.apache.usergrid.persistence.Entity entity = EntityFactory.newEntity( entityId.getUuid(), entityId.getType() ); entity.setProperties( cpEntity ); return entity; }
@Override public <A extends Entity> A get( EntityRef entityRef, Class<A> entityClass ) throws Exception { if ( entityRef == null ) { return null; } Entity entity = get(entityRef); if ( entity == null ) { logger.warn( "Entity {}/{} not found ", entityRef.getUuid(), entityRef.getType() ); return null; } A ret = EntityFactory.newEntity(entityRef.getUuid(), entityRef.getType(), entityClass); ret.setProperties( entity.getProperties() ); return ret; }
@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 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 ); }
/** * 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 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; }
@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; }
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; }
A entity = EntityFactory.newEntity( itemId, eType, entityClass ); entity.addProperties( properties );
/** * 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 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" ) ); }