All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.buschmais.xo.impl.RelationPropertyManager Maven / Gradle / Ivy

The newest version!
package com.buschmais.xo.impl;

import com.buschmais.xo.api.metadata.method.EntityReferencePropertyMethodMetadata;
import com.buschmais.xo.api.metadata.type.DatastoreRelationMetadata;
import com.buschmais.xo.api.metadata.type.RelationTypeMetadata;
import com.buschmais.xo.spi.datastore.DatastorePropertyManager;
import com.buschmais.xo.spi.datastore.DatastoreRelationManager;

public class RelationPropertyManager extends AbstractPropertyManager {

    private final SessionContext sessionContext;

    /**
     * Constructor.
     *
     * @param sessionContext
     *     The {@link SessionContext}.
     */
    public RelationPropertyManager(SessionContext sessionContext) {
        this.sessionContext = sessionContext;
    }

    @Override
    protected DatastorePropertyManager getDatastorePropertyManager() {
        return sessionContext.getDatastoreSession()
            .getDatastoreRelationManager();
    }

    @Override
    protected AbstractInstanceManager getInstanceManager() {
        return sessionContext.getRelationInstanceManager();
    }

    public Entity getEntityReference(Relation relation, EntityReferencePropertyMethodMetadata metadata) {
        return sessionContext.getEntityInstanceManager()
            .readInstance(getReferencedEntity(relation, metadata.getDirection()));
    }

    private Entity getReferencedEntity(Relation relation, RelationTypeMetadata.Direction direction) {
        DatastoreRelationManager, ?, ?> relationManager = sessionContext.getDatastoreSession()
            .getDatastoreRelationManager();
        switch (direction) {
        case TO:
            return relationManager.getTo(relation);
        case FROM:
            return relationManager.getFrom(relation);
        default:
            throw direction.createNotSupportedException();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy