Ejemplo n.º 1
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);
  }
Ejemplo n.º 2
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);
 }
Ejemplo n.º 3
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);
 }
Ejemplo n.º 4
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);
 }
Ejemplo n.º 5
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);
 }