@Override public <T> void delete(T instance) { AbstractInstanceManager<EntityId, Entity> entityInstanceManager = sessionContext.getEntityInstanceManager(); AbstractInstanceManager<RelationId, Relation> relationInstanceManager = sessionContext.getRelationInstanceManager(); DatastoreSession< EntityId, Entity, EntityMetadata, EntityDiscriminator, RelationId, Relation, RelationMetadata, RelationDiscriminator, PropertyMetadata> datastoreSession = sessionContext.getDatastoreSession(); if (entityInstanceManager.isInstance(instance)) { Entity entity = entityInstanceManager.getDatastoreType(instance); sessionContext.getInstanceListenerService().preDelete(instance); datastoreSession.getDatastoreEntityManager().deleteEntity(entity); entityInstanceManager.removeInstance(instance); entityInstanceManager.closeInstance(instance); sessionContext.getInstanceListenerService().postDelete(instance); } else if (relationInstanceManager.isInstance(instance)) { Relation relation = relationInstanceManager.getDatastoreType(instance); sessionContext.getInstanceListenerService().preDelete(instance); datastoreSession.getDatastoreRelationManager().deleteRelation(relation); relationInstanceManager.removeInstance(instance); relationInstanceManager.closeInstance(instance); sessionContext.getInstanceListenerService().postDelete(instance); } else { throw new XOException(instance + " is not a managed XO instance."); } }
/** * Create a new {@link CompositeObject} instance using an example. * * @param type The interface the property type shall implement. * @param types Additional interfaces the entity type shall implement. * @param exampleEntity The example instance. * @return The {@link CompositeObject} instance. */ private CompositeObject createByExample( Class<?> type, Class<?>[] types, Map<PrimitivePropertyMethodMetadata<PropertyMetadata>, Object> exampleEntity) { TypeMetadataSet<EntityTypeMetadata<EntityMetadata>> effectiveTypes = getEffectiveTypes(type, types); Set<EntityDiscriminator> entityDiscriminators = sessionContext.getMetadataProvider().getEntityDiscriminators(effectiveTypes); DatastoreSession< EntityId, Entity, EntityMetadata, EntityDiscriminator, RelationId, Relation, RelationMetadata, RelationDiscriminator, PropertyMetadata> datastoreSession = sessionContext.getDatastoreSession(); Entity entity = datastoreSession .getDatastoreEntityManager() .createEntity(effectiveTypes, entityDiscriminators, exampleEntity); AbstractInstanceManager<EntityId, Entity> entityInstanceManager = sessionContext.getEntityInstanceManager(); CompositeObject instance = entityInstanceManager.createInstance(entity); sessionContext.getInstanceListenerService().postCreate(instance); return instance; }
private <S, R, T> R createByExample( S from, Class<R> relationType, T to, Map<PrimitivePropertyMethodMetadata<PropertyMetadata>, Object> example) { MetadataProvider<EntityMetadata, EntityDiscriminator, RelationMetadata, RelationDiscriminator> metadataProvider = sessionContext.getMetadataProvider(); AbstractRelationPropertyMethodMetadata<?> fromProperty = metadataProvider.getPropertyMetadata(from.getClass(), relationType, FROM); AbstractRelationPropertyMethodMetadata<?> toProperty = metadataProvider.getPropertyMetadata(to.getClass(), relationType, TO); Entity entity = sessionContext.getEntityInstanceManager().getDatastoreType(from); R instance = sessionContext .getEntityPropertyManager() .createRelationReference(entity, fromProperty, to, toProperty, example); sessionContext.getInstanceListenerService().postCreate(instance); return instance; }
@Override public <I> void registerInstanceListener(I instanceListener) { sessionContext.getInstanceListenerService().registerInstanceListener(instanceListener); }