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() {
}
};
}
Aggregations