Search in sources :

Example 1 with CacheOptions

use of org.qi4j.api.cache.CacheOptions in project qi4j-sdk by Qi4j.

the class JSONMapEntityStoreMixin method applyChanges.

@Override
public StateCommitter applyChanges(final EntityStoreUnitOfWork unitOfWork, final Iterable<EntityState> state) throws EntityStoreException {
    return new StateCommitter() {

        @Override
        public void commit() {
            try {
                mapEntityStore.applyChanges(new MapEntityStore.MapChanges() {

                    @Override
                    public void visitMap(MapEntityStore.MapChanger changer) throws IOException {
                        DefaultEntityStoreUnitOfWork uow = (DefaultEntityStoreUnitOfWork) unitOfWork;
                        CacheOptions options = uow.usecase().metaInfo(CacheOptions.class);
                        if (options == null) {
                            options = CacheOptions.ALWAYS;
                        }
                        for (EntityState entityState : state) {
                            JSONEntityState state = (JSONEntityState) entityState;
                            if (state.status().equals(EntityStatus.NEW)) {
                                Writer writer = changer.newEntity(state.identity(), state.entityDescriptor());
                                writeEntityState(state, writer, unitOfWork.identity(), unitOfWork.currentTime());
                                writer.close();
                                if (options.cacheOnNew()) {
                                    cache.put(state.identity().identity(), new CacheState(state.state()));
                                }
                            } else if (state.status().equals(EntityStatus.UPDATED)) {
                                Writer writer = changer.updateEntity(state.identity(), state.entityDescriptor());
                                writeEntityState(state, writer, unitOfWork.identity(), unitOfWork.currentTime());
                                writer.close();
                                if (options.cacheOnWrite()) {
                                    cache.put(state.identity().identity(), new CacheState(state.state()));
                                }
                            } else if (state.status().equals(EntityStatus.REMOVED)) {
                                changer.removeEntity(state.identity(), state.entityDescriptor());
                                cache.remove(state.identity().identity());
                            }
                        }
                    }
                });
            } catch (IOException e) {
                throw new EntityStoreException(e);
            }
        }

        @Override
        public void cancel() {
        }
    };
}
Also used : IOException(java.io.IOException) EntityState(org.qi4j.spi.entity.EntityState) CacheOptions(org.qi4j.api.cache.CacheOptions) DefaultEntityStoreUnitOfWork(org.qi4j.spi.entitystore.DefaultEntityStoreUnitOfWork) StateCommitter(org.qi4j.spi.entitystore.StateCommitter) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) Writer(java.io.Writer)

Aggregations

IOException (java.io.IOException)1 Writer (java.io.Writer)1 CacheOptions (org.qi4j.api.cache.CacheOptions)1 EntityState (org.qi4j.spi.entity.EntityState)1 DefaultEntityStoreUnitOfWork (org.qi4j.spi.entitystore.DefaultEntityStoreUnitOfWork)1 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)1 StateCommitter (org.qi4j.spi.entitystore.StateCommitter)1