public static Results fromRef( EntityRef ref ) { if ( ref instanceof Entity ) { return fromEntity( ( Entity ) ref ); } Results r = new Results(); r.setRef(ref); return r; }
public ServiceResults getApplicationEntity( ServiceContext context ) throws Exception { checkPermissionsForPath( context, "/" ); Entity entity = em.get( em.getApplicationRef() ); Results r = Results.fromEntity( entity ); Map<String, Object> collections = em.getApplicationCollectionMetadata(); // Set<String> collections = em.getApplicationCollections(); if ( collections.size() > 0 ) { r.setMetadata( em.getApplicationRef().getUuid(), "collections", collections ); } return genericServiceResults( r ); }
return new ServiceResults( this, context, Type.COLLECTION, Results.fromEntity( item ), null, null );
@Override public ServiceResults getApplicationMetadata( UUID applicationId ) throws Exception { if ( applicationId == null ) { return ServiceResults.genericServiceResults(); } EntityManager em = emf.getEntityManager( applicationId ); Entity entity = em.get( em.getApplicationRef() ); Results r = Results.fromEntity( entity ); Map<String, Object> collections = em.getApplicationCollectionMetadata(); if ( collections.size() > 0 ) { r.setMetadata( em.getApplicationRef().getUuid(), "collections", collections ); } return genericServiceResults( r ); }
public ServiceResults updateApplicationEntity( ServiceContext context, ServicePayload payload ) throws Exception { checkPermissionsForPath( context, "/" ); Map<String, Object> properties = payload.getProperties(); Object m = properties.get( "metadata" ); if ( m instanceof Map ) { @SuppressWarnings("unchecked") Map<String, Object> metadata = ( Map<String, Object> ) m; Object c = metadata.get( "collections" ); if ( c instanceof Map ) { @SuppressWarnings("unchecked") Map<String, Object> collections = ( Map<String, Object> ) c; for ( String collection : collections.keySet() ) { if ( isReservedCollection( collection ) ) { continue; } em.createApplicationCollection( collection ); if (logger.isDebugEnabled()) { logger.debug("Created collection {} for application {}", collection, sm.getApplicationId()); } } } } Entity entity = em.get( em.getApplicationRef() ); em.updateProperties( entity, properties ); entity.addProperties( properties ); Results r = Results.fromEntity( entity ); Set<String> collections = em.getApplicationCollections(); if ( collections.size() > 0 ) { r.setMetadata( em.getApplicationRef().getUuid(), "collections", collections ); } return genericServiceResults( r ); }
@Override public ServiceResults putItemByName( ServiceContext context, String name ) throws Exception { if ( context.moreParameters() ) { return getItemByName( context, name ); } // use unique index repair here before any write logic if there are problems Entity entity = em.getUniqueEntityFromAlias( getEntityType(), name, true); if ( entity == null ) { // null entity ref means we tried to put a non-existing entity // before we create a new entity for it, we should check for permission checkPermissionsForCollection(context); Map<String, Object> properties = context.getPayload().getProperties(); if ( !properties.containsKey( "name" ) || !( ( String ) properties.get( "name" ) ).trim().equalsIgnoreCase( name ) ) { properties.put( "name", name ); } entity = em.create( getEntityType(), properties ); } else { entity = importEntity( context, entity ); checkPermissionsForEntity( context, entity ); if( context.getOwner().getType().equals(TYPE_APPLICATION)) { // this will repair any missing edges em.addToCollection(context.getOwner(), context.getCollectionName(), entity); } updateEntity( context, entity ); } return new ServiceResults( this, context, Type.COLLECTION, Results.fromEntity( entity ), null, null ); }
@Override public ServiceResults postItemById( ServiceContext context, UUID id ) throws Exception { checkPermissionsForEntity( context, id ); if ( context.moreParameters() ) { return getItemById( context, id ); } Entity entity = em.get( id ); if ( entity == null ) { throw new ServiceResourceNotFoundException( context ); } entity = importEntity( context, entity ); createConnection( context.getOwner(), context.getCollectionName(), entity ); return new ServiceResults( null, context, Type.CONNECTION, Results.fromEntity( entity ), null, null ); }
@Override public ServiceResults deleteItemById( ServiceContext context, UUID id ) throws Exception { checkPermissionsForEntity( context, id ); if ( context.moreParameters() ) { return getItemById( context, id ); } Entity entity = em.get( id ); if ( entity == null ) { throw new ServiceResourceNotFoundException( context ); } entity = importEntity( context, entity ); deleteConnection( em.connectionRef( context.getOwner(), context.getCollectionName(), entity ) ); return new ServiceResults( null, context, Type.CONNECTION, Results.fromEntity( entity ), null, null ); }
@Override public ServiceResults deleteItemByName( ServiceContext context, String name ) throws Exception { if ( context.moreParameters() ) { return getItemByName( context, name ); } // use unique index repair here before any write logic if there are problems Entity entity = em.getUniqueEntityFromAlias( getEntityType(), name, true); if ( entity == null ) { throw new ServiceResourceNotFoundException( context ); } entity = importEntity( context, entity ); checkPermissionsForEntity( context, entity ); prepareToDelete( context, entity ); em.removeFromCollection( context.getOwner(), context.getCollectionName(), entity ); return new ServiceResults( this, context, Type.COLLECTION, Results.fromEntity( entity ), null, null ); }
@Override public ServiceResults postItemById( ServiceContext context, UUID id ) throws Exception { if ( context.moreParameters() ) { return getItemById( context, id, true ); } checkPermissionsForEntity( context, id ); Entity entity = em.get( new SimpleEntityRef( this.getEntityType(), id) ); if ( entity == null ) { throw new ServiceResourceNotFoundException( context ); } validateEntityType( entity, id ); entity = importEntity( context, entity ); em.addToCollection( context.getOwner(), context.getCollectionName(), entity ); return new ServiceResults( null, context, Type.COLLECTION, Results.fromEntity( entity ), null, null ); }
@Override public ServiceResults deleteItemById( ServiceContext context, UUID id ) throws Exception { checkPermissionsForEntity( context, id ); if ( context.moreParameters() ) { return getItemById( context, id ); } Entity item = em.get( new SimpleEntityRef( this.getEntityType(), id) ); if ( item == null ) { throw new ServiceResourceNotFoundException( context ); } validateEntityType( item, id ); item = importEntity( context, item ); prepareToDelete( context, item ); em.removeFromCollection( context.getOwner(), context.getCollectionName(), item ); return new ServiceResults( this, context, Type.COLLECTION, Results.fromEntity( item ), null, null ); }
@Override public ServiceResults putItemById( ServiceContext context, UUID id ) throws Exception { if ( context.moreParameters() ) { return getItemById( context, id, true ); } checkPermissionsForEntity( context, id ); Entity item = em.get( id ); if ( item != null ) { validateEntityType( item, id ); if( context.getOwner().getType().equals(TYPE_APPLICATION)) { // this will repair any missing edges em.addToCollection(context.getOwner(), context.getCollectionName(), item); } updateEntity( context, item, context.getPayload() ); item = importEntity( context, item ); } else { String entityType = getEntityType(); item = em.create( id, entityType, context.getPayload().getProperties() ); } return new ServiceResults( this, context, Type.COLLECTION, Results.fromEntity( item ), null, null ); }
@Override public ServiceResults putItemById( ServiceContext context, UUID id ) throws Exception { if ( context.moreParameters() ) { return getItemById( context, id ); } checkPermissionsForEntity( context, id ); Entity item = em.get( id ); if ( item != null ) { updateEntity( context, item, context.getPayload() ); item = importEntity( context, item ); } else { String entityType = getEntityType(); item = em.create( id, entityType, context.getPayload().getProperties() ); } //create the connection createConnection( context.getOwner(), context.getCollectionName(), item ); return new ServiceResults( this, context, Type.CONNECTION, Results.fromEntity( item ), null, null ); }
return new ServiceResults( null, context, Type.CONNECTION, Results.fromEntity( entity ), null, null );
@Override public ServiceResults deleteItemsByQuery( ServiceContext context, Query query ) throws Exception { checkPermissionsForCollection( context ); if ( !query.hasQueryPredicates() && ( query.getEntityType() != null ) && query .containsNameOrEmailIdentifiersOnly() ) { String name = query.getSingleNameOrEmailIdentifier(); String nameProperty = Schema.getDefaultSchema().aliasProperty( query.getEntityType() ); if ( nameProperty == null ) { nameProperty = "name"; } // use unique index repair here before any write logic if there are problems Entity entity = em.getUniqueEntityFromAlias( query.getEntityType(), name, true); if ( entity == null ) { throw new ServiceResourceNotFoundException( context ); } entity = importEntity( context, entity ); deleteConnection( em.connectionRef( context.getOwner(), query.getConnectionType(), entity ) ); return new ServiceResults( null, context, Type.CONNECTION, Results.fromEntity( entity ), null, null ); } return getItemsByQuery( context, query ); }
@Override public ServiceResults postItemsByQuery( ServiceContext context, Query query ) throws Exception { checkPermissionsForCollection( context ); if ( !query.hasQueryPredicates() && ( query.getEntityType() != null ) ) { Entity entity; if ( query.containsSingleNameOrEmailIdentifier() ) { String name = query.getSingleNameOrEmailIdentifier(); // use unique index repair here before any write logic if there are problems entity = em.getUniqueEntityFromAlias( query.getEntityType(), name, true); if ( entity == null ) { throw new ServiceResourceNotFoundException( context ); } } else { entity = em.create( query.getEntityType(), context.getProperties() ); //if entity is null here it throws NPE. Fixing it to throw 404. if ( entity == null ) { throw new ServiceResourceNotFoundException( context ); } } entity = importEntity( context, entity ); createConnection( context.getOwner(), query.getConnectionType(), entity ); return new ServiceResults( null, context, Type.CONNECTION, Results.fromEntity( entity ), null, null ); } return getItemsByQuery( context, query ); }