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

com.buschmais.xo.impl.cache.CacheSynchronizationService Maven / Gradle / Ivy

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

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import com.buschmais.xo.api.ValidationMode;
import com.buschmais.xo.api.bootstrap.XOUnit;
import com.buschmais.xo.impl.AbstractInstanceManager;
import com.buschmais.xo.impl.SessionContext;
import com.buschmais.xo.impl.instancelistener.InstanceListenerService;
import com.buschmais.xo.spi.datastore.DatastorePropertyManager;
import com.buschmais.xo.spi.datastore.DatastoreSession;

public class CacheSynchronizationService {

    private final SessionContext sessionContext;
    private final XOUnit xoUnit;

    public CacheSynchronizationService(SessionContext sessionContext, XOUnit xoUnit) {
        this.sessionContext = sessionContext;
        this.xoUnit = xoUnit;
    }

    public void flush() {
        DatastoreSession datastoreSession = sessionContext.getDatastoreSession();
        InstanceListenerService instanceListenerService = sessionContext.getInstanceListenerService();
        flush(sessionContext.getRelationCache(), sessionContext.getRelationInstanceManager(), datastoreSession.getDatastoreRelationManager(),
            instanceListenerService);
        flush(sessionContext.getEntityCache(), sessionContext.getEntityInstanceManager(), datastoreSession.getDatastoreEntityManager(),
            instanceListenerService);
    }

    private  void flush(TransactionalCache cache, AbstractInstanceManager instanceManager, DatastorePropertyManager datastoreManager,
        InstanceListenerService instanceListenerService) {
        Collection writtenInstances = cache.writtenInstances();
        if (!writtenInstances.isEmpty()) {
            List entities = new ArrayList<>(writtenInstances.size());
            for (Object instance : writtenInstances) {
                T entity = instanceManager.getDatastoreType(instance);
                entities.add(entity);
                instanceListenerService.preUpdate(instance);
                validateInstance(instance);
                instanceListenerService.postUpdate(instance);
            }
            datastoreManager.flush(entities);
            cache.flush();
        }
    }

    public void clear() {
        DatastoreSession datastoreSession = sessionContext.getDatastoreSession();
        clear(sessionContext.getRelationCache(), sessionContext.getRelationInstanceManager(), datastoreSession.getDatastoreRelationManager());
        clear(sessionContext.getEntityCache(), sessionContext.getEntityInstanceManager(), datastoreSession.getDatastoreEntityManager());
    }

    private  void clear(TransactionalCache cache, AbstractInstanceManager instanceManager, DatastorePropertyManager datastoreManager) {
        Collection instances = cache.readInstances();
        for (Object instance : instances) {
            T datastoreType = instanceManager.getDatastoreType(instance);
            if (datastoreType != null) {
                datastoreManager.afterCompletion(datastoreType, xoUnit.isClearAfterCompletion());
            }
        }
        cache.clear();
    }

    private void validateInstance(Object instance) {
        if (!ValidationMode.NONE.equals(xoUnit.getValidationMode())) {
            Set> constraintViolations = sessionContext.getInstanceValidationService()
                .validate(instance);
            if (!constraintViolations.isEmpty()) {
                throw new ConstraintViolationException(constraintViolations);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy