Beispiel #1
0
 /**
  * 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;
 }
Beispiel #2
0
  /**
   * Find entities according to the given example entity.
   *
   * @param type The entity type.
   * @param entity The example entity.
   * @param <T> The entity type.
   * @return A {@link com.buschmais.xo.api.ResultIterable}.
   */
  private <T> ResultIterable<T> findByExample(
      Class<?> type, Map<PrimitivePropertyMethodMetadata<PropertyMetadata>, Object> entity) {
    EntityTypeMetadata<EntityMetadata> entityTypeMetadata =
        sessionContext.getMetadataProvider().getEntityMetadata(type);
    EntityDiscriminator entityDiscriminator =
        entityTypeMetadata.getDatastoreMetadata().getDiscriminator();
    if (entityDiscriminator == null) {
      throw new XOException(
          "Type "
              + type.getName()
              + " has no discriminator (i.e. cannot be identified in datastore).");
    }
    ResultIterator<Entity> iterator =
        sessionContext
            .getDatastoreSession()
            .getDatastoreEntityManager()
            .findEntity(entityTypeMetadata, entityDiscriminator, entity);
    ResultIterator<T> resultIterator =
        new ResultIterator<T>() {

          @Override
          public boolean hasNext() {
            return iterator.hasNext();
          }

          @Override
          public T next() {
            Entity entity = iterator.next();
            AbstractInstanceManager<EntityId, Entity> entityInstanceManager =
                sessionContext.getEntityInstanceManager();
            return entityInstanceManager.readInstance(entity);
          }

          @Override
          public void remove() {
            throw new UnsupportedOperationException("Cannot remove instance.");
          }

          @Override
          public void close() {
            iterator.close();
          }
        };
    XOTransaction xoTransaction = sessionContext.getXOTransaction();
    final ResultIterator<T> transactionalIterator =
        xoTransaction != null
            ? new TransactionalResultIterator<>(resultIterator, xoTransaction)
            : resultIterator;
    return sessionContext
        .getInterceptorFactory()
        .addInterceptor(
            new AbstractResultIterable<T>() {
              @Override
              public ResultIterator<T> iterator() {
                return transactionalIterator;
              }
            },
            ResultIterable.class);
  }
Beispiel #3
0
 @Override
 public <T> T getRepository(Class<T> repositoryType) {
   RepositoryTypeMetadata repositoryMetadata =
       sessionContext.getMetadataProvider().getRepositoryMetadata(repositoryType);
   RepositoryProxyMethodService<Entity, Relation> proxyMethodService =
       new RepositoryProxyMethodService<>(sessionContext);
   RepositoryInvocationHandler invocationHandler =
       new RepositoryInvocationHandler(proxyMethodService, this);
   T instance =
       sessionContext
           .getProxyFactory()
           .createInstance(invocationHandler, new Class<?>[0], repositoryType);
   return sessionContext.getInterceptorFactory().addInterceptor(instance, repositoryType);
 }
Beispiel #4
0
 @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.");
   }
 }
Beispiel #5
0
 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;
 }
Beispiel #6
0
 @Override
 public <T, M> CompositeObject migrate(
     T instance,
     MigrationStrategy<T, M> migrationStrategy,
     Class<M> targetType,
     Class<?>... targetTypes) {
   AbstractInstanceManager<EntityId, Entity> entityInstanceManager =
       sessionContext.getEntityInstanceManager();
   Entity entity = entityInstanceManager.getDatastoreType(instance);
   DatastoreSession<
           EntityId,
           Entity,
           EntityMetadata,
           EntityDiscriminator,
           RelationId,
           Relation,
           RelationMetadata,
           RelationDiscriminator,
           PropertyMetadata>
       datastoreSession = sessionContext.getDatastoreSession();
   Set<EntityDiscriminator> entityDiscriminators =
       datastoreSession.getDatastoreEntityManager().getEntityDiscriminators(entity);
   MetadataProvider<EntityMetadata, EntityDiscriminator, RelationMetadata, RelationDiscriminator>
       metadataProvider = sessionContext.getMetadataProvider();
   TypeMetadataSet<EntityTypeMetadata<EntityMetadata>> types =
       metadataProvider.getTypes(entityDiscriminators);
   TypeMetadataSet<EntityTypeMetadata<EntityMetadata>> effectiveTargetTypes =
       getEffectiveTypes(targetType, targetTypes);
   Set<EntityDiscriminator> targetEntityDiscriminators =
       metadataProvider.getEntityDiscriminators(effectiveTargetTypes);
   datastoreSession
       .getDatastoreEntityManager()
       .migrateEntity(
           entity, types, entityDiscriminators, effectiveTargetTypes, targetEntityDiscriminators);
   entityInstanceManager.removeInstance(instance);
   CompositeObject migratedInstance = entityInstanceManager.updateInstance(entity);
   if (migrationStrategy != null) {
     migrationStrategy.migrate(instance, migratedInstance.as(targetType));
   }
   entityInstanceManager.closeInstance(instance);
   return migratedInstance;
 }
Beispiel #7
0
 private TypeMetadataSet<EntityTypeMetadata<EntityMetadata>> getEffectiveTypes(
     Class<?> type, Class<?>... types) {
   MetadataProvider<EntityMetadata, EntityDiscriminator, RelationMetadata, RelationDiscriminator>
       metadataProvider = sessionContext.getMetadataProvider();
   TypeMetadataSet<EntityTypeMetadata<EntityMetadata>> effectiveTypes = new TypeMetadataSet<>();
   effectiveTypes.add(metadataProvider.getEntityMetadata(type));
   for (Class<?> otherType : types) {
     effectiveTypes.add(metadataProvider.getEntityMetadata(otherType));
   }
   return effectiveTypes;
 }
Beispiel #8
0
 @Override
 public <T, Id> Id getId(T instance) {
   AbstractInstanceManager<EntityId, Entity> entityInstanceManager =
       sessionContext.getEntityInstanceManager();
   AbstractInstanceManager<RelationId, Relation> relationInstanceManager =
       sessionContext.getRelationInstanceManager();
   if (entityInstanceManager.isInstance(instance)) {
     Entity entity = entityInstanceManager.getDatastoreType(instance);
     return (Id)
         sessionContext.getDatastoreSession().getDatastoreEntityManager().getEntityId(entity);
   } else if (relationInstanceManager.isInstance(instance)) {
     Relation relation = relationInstanceManager.getDatastoreType(instance);
     return (Id)
         sessionContext
             .getDatastoreSession()
             .getDatastoreRelationManager()
             .getRelationId(relation);
   }
   throw new XOException(instance + " is not a managed XO instance.");
 }
Beispiel #9
0
 @Override
 public <T> ResultIterable<T> find(final Class<T> type, final Object value) {
   EntityTypeMetadata<EntityMetadata> entityTypeMetadata =
       sessionContext.getMetadataProvider().getEntityMetadata(type);
   IndexedPropertyMethodMetadata indexedProperty = entityTypeMetadata.getIndexedProperty();
   Map<PrimitivePropertyMethodMetadata<PropertyMetadata>, Object> exampleEntity = new HashMap<>(1);
   if (indexedProperty != null) {
     exampleEntity.put(indexedProperty.getPropertyMethodMetadata(), value);
   } else {
     exampleEntity.put(null, value);
   }
   return findByExample(type, exampleEntity);
 }
Beispiel #10
0
 @Override
 public <DS> DS getDatastoreSession(Class<DS> sessionType) {
   DatastoreSession<
           EntityId,
           Entity,
           EntityMetadata,
           EntityDiscriminator,
           RelationId,
           Relation,
           RelationMetadata,
           RelationDiscriminator,
           PropertyMetadata>
       datastoreSession = sessionContext.getDatastoreSession();
   return sessionType.cast(datastoreSession);
 }
Beispiel #11
0
 /**
  * Setup an example entity.
  *
  * @param type The type.
  * @param example The provided example.
  * @param <T> The type.
  * @return The example.
  */
 private <T> Map<PrimitivePropertyMethodMetadata<PropertyMetadata>, Object> prepareExample(
     Example<T> example, Class<?> type, Class<?>... types) {
   Map<PrimitivePropertyMethodMetadata<PropertyMetadata>, Object> exampleEntity = new HashMap<>();
   InstanceInvocationHandler invocationHandler =
       new InstanceInvocationHandler(
           exampleEntity, new ExampleProxyMethodService(type, sessionContext));
   List<Class<?>> effectiveTypes = new ArrayList<>();
   effectiveTypes.add(type);
   effectiveTypes.addAll(Arrays.asList(types));
   T instance =
       sessionContext
           .getProxyFactory()
           .createInstance(
               invocationHandler,
               effectiveTypes.toArray(new Class<?>[effectiveTypes.size()]),
               CompositeObject.class);
   example.prepare(instance);
   return exampleEntity;
 }
Beispiel #12
0
 @Override
 public <T> Query<T> createQuery(Class<T> query) {
   XOQueryImpl<T, ?, Class<T>, Entity, Relation> xoQuery =
       new XOQueryImpl<>(sessionContext, query, query);
   return sessionContext.getInterceptorFactory().addInterceptor(xoQuery, Query.class);
 }
Beispiel #13
0
 @Override
 public XOTransaction currentTransaction() {
   return sessionContext.getXOTransaction();
 }
Beispiel #14
0
 @Override
 public Set<ConstraintViolation<Object>> validate() {
   return sessionContext.getInstanceValidationService().validate();
 }
Beispiel #15
0
 @Override
 public Query<CompositeRowObject> createQuery(String query) {
   XOQueryImpl<CompositeRowObject, ?, String, Entity, Relation> xoQuery =
       new XOQueryImpl<>(sessionContext, query);
   return sessionContext.getInterceptorFactory().addInterceptor(xoQuery, Query.class);
 }
Beispiel #16
0
 @Override
 public <I> void registerInstanceListener(I instanceListener) {
   sessionContext.getInstanceListenerService().registerInstanceListener(instanceListener);
 }
Beispiel #17
0
 @Override
 public void flush() {
   sessionContext.getCacheSynchronizationService().flush();
 }
Beispiel #18
0
 @Override
 public <Q> Query<CompositeRowObject> createQuery(Class<Q> query, Class<?>... types) {
   XOQueryImpl<CompositeRowObject, ?, Class<Q>, Entity, Relation> xoQuery =
       new XOQueryImpl<>(sessionContext, query, query, Arrays.asList(types));
   return sessionContext.getInterceptorFactory().addInterceptor(xoQuery, Query.class);
 }
Beispiel #19
0
 @Override
 public void close() {
   sessionContext.getEntityInstanceManager().close();
   sessionContext.getRelationInstanceManager().close();
   sessionContext.getDatastoreSession().close();
 }