Example #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;
 }
Example #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);
  }
Example #3
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;
 }
Example #4
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);
 }
Example #5
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);
 }
Example #6
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;
 }
Example #7
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;
 }