Esempio n. 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;
 }
Esempio n. 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);
  }
Esempio n. 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);
 }
Esempio n. 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.");
   }
 }
Esempio n. 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;
 }
Esempio n. 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;
 }
Esempio n. 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;
 }
Esempio n. 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.");
 }
Esempio n. 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);
 }
Esempio n. 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);
 }
Esempio n. 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;
 }
Esempio n. 12
0
 @Override
 public <I> void registerInstanceListener(I instanceListener) {
   sessionContext.getInstanceListenerService().registerInstanceListener(instanceListener);
 }
Esempio n. 13
0
 @Override
 public void flush() {
   sessionContext.getCacheSynchronizationService().flush();
 }
Esempio n. 14
0
 @Override
 public void close() {
   sessionContext.getEntityInstanceManager().close();
   sessionContext.getRelationInstanceManager().close();
   sessionContext.getDatastoreSession().close();
 }
Esempio n. 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);
 }
Esempio n. 16
0
 @Override
 public XOTransaction currentTransaction() {
   return sessionContext.getXOTransaction();
 }
Esempio n. 17
0
 @Override
 public Set<ConstraintViolation<Object>> validate() {
   return sessionContext.getInstanceValidationService().validate();
 }
Esempio n. 18
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);
 }
Esempio n. 19
0
 public EntityProxyMethodService(
     SessionContext<?, Entity, ?, ?, ?, Relation, ?, ?, ?> sessionContext) {
   for (TypeMetadata typeMetadata : sessionContext.getMetadataProvider().getRegisteredMetadata()) {
     for (MethodMetadata methodMetadata : typeMetadata.getProperties()) {
       AnnotatedMethod typeMethod = methodMetadata.getAnnotatedMethod();
       addUnsupportedOperationMethod(methodMetadata, typeMethod);
       addImplementedByMethod(methodMetadata, typeMethod);
       if (methodMetadata instanceof ResultOfMethodMetadata) {
         ResultOfMethodMetadata resultOfMethodMetadata = (ResultOfMethodMetadata) methodMetadata;
         addProxyMethod(
             new ResultOfMethod(sessionContext, resultOfMethodMetadata),
             typeMethod.getAnnotatedElement());
       }
       if (methodMetadata instanceof AbstractPropertyMethodMetadata) {
         PropertyMethod propertyMethod = (PropertyMethod) typeMethod;
         Method method = propertyMethod.getAnnotatedElement();
         EntityPropertyManager<Entity, Relation, ?> propertyManager =
             sessionContext.getEntityPropertyManager();
         if (methodMetadata instanceof PrimitivePropertyMethodMetadata) {
           if (propertyMethod instanceof GetPropertyMethod) {
             addProxyMethod(
                 new PrimitivePropertyGetMethod(
                     propertyManager, (PrimitivePropertyMethodMetadata) methodMetadata),
                 method);
           } else if (propertyMethod instanceof SetPropertyMethod) {
             addProxyMethod(
                 new PrimitivePropertySetMethod(
                     propertyManager, (PrimitivePropertyMethodMetadata) methodMetadata),
                 method);
           }
         } else if (methodMetadata instanceof TransientPropertyMethodMetadata) {
           if (propertyMethod instanceof GetPropertyMethod) {
             addProxyMethod(
                 new TransientPropertyGetMethod(
                     propertyManager, (TransientPropertyMethodMetadata) methodMetadata),
                 method);
           } else if (propertyMethod instanceof SetPropertyMethod) {
             addProxyMethod(
                 new TransientPropertySetMethod(
                     propertyManager, (TransientPropertyMethodMetadata) methodMetadata),
                 method);
           }
         } else if (methodMetadata instanceof EntityReferencePropertyMethodMetadata) {
           if (propertyMethod instanceof GetPropertyMethod) {
             addProxyMethod(
                 new EntityReferencePropertyGetMethod(
                     propertyManager, (EntityReferencePropertyMethodMetadata) methodMetadata),
                 method);
           } else if (propertyMethod instanceof SetPropertyMethod) {
             addProxyMethod(
                 new EntityReferencePropertySetMethod(
                     propertyManager, (EntityReferencePropertyMethodMetadata) methodMetadata),
                 method);
           }
         } else if (methodMetadata instanceof RelationReferencePropertyMethodMetadata) {
           if (propertyMethod instanceof GetPropertyMethod) {
             addProxyMethod(
                 new RelationReferencePropertyGetMethod(
                     propertyManager, (RelationReferencePropertyMethodMetadata) methodMetadata),
                 method);
           }
         } else if (methodMetadata instanceof EntityCollectionPropertyMethodMetadata) {
           if (propertyMethod instanceof GetPropertyMethod) {
             EntityCollectionPropertyGetMethod<Entity, ?> proxyMethod =
                 new EntityCollectionPropertyGetMethod<>(
                     sessionContext, (EntityCollectionPropertyMethodMetadata<?>) methodMetadata);
             addProxyMethod(proxyMethod, method);
           } else if (propertyMethod instanceof SetPropertyMethod) {
             addProxyMethod(
                 new EntityCollectionPropertySetMethod(
                     propertyManager, (EntityCollectionPropertyMethodMetadata) methodMetadata),
                 method);
           }
         } else if (methodMetadata instanceof RelationCollectionPropertyMethodMetadata) {
           if (propertyMethod instanceof GetPropertyMethod) {
             RelationCollectionPropertyGetMethod<Entity, ?> proxyMethod =
                 new RelationCollectionPropertyGetMethod<>(
                     sessionContext, (RelationCollectionPropertyMethodMetadata<?>) methodMetadata);
             addProxyMethod(proxyMethod, method);
           }
         }
       }
     }
   }
   addMethod(new AsMethod<>(sessionContext), CompositeObject.class, "as", Class.class);
   addMethod(new GetIdMethod<>(sessionContext), CompositeObject.class, "getId");
   addMethod(new GetDelegateMethod<Entity>(), CompositeObject.class, "getDelegate");
   addMethod(new HashCodeMethod<>(sessionContext), Object.class, "hashCode");
   addMethod(new EqualsMethod<>(sessionContext), Object.class, "equals", Object.class);
   addMethod(new ToStringMethod<>(sessionContext), Object.class, "toString");
 }
Esempio n. 20
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);
 }