Exemplo 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);
  }
Exemplo n.º 2
0
 @Override
 public XOTransaction currentTransaction() {
   return sessionContext.getXOTransaction();
 }