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

poussecafe.spring.mongo.storage.MongoDataAccess Maven / Gradle / Ivy

There is a newer version: 0.18.0
Show newest version
package poussecafe.spring.mongo.storage;

import java.io.Serializable;
import java.util.List;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.data.mongodb.repository.MongoRepository;
import poussecafe.domain.EntityAttributes;
import poussecafe.domain.EntityDataAccess;
import poussecafe.runtime.OptimisticLockingException;

public abstract class MongoDataAccess, M extends Serializable> implements EntityDataAccess {

    @Override
    public D findData(K id) {
        return mongoRepository().findById(convertId(id)).orElse(null);
    }

    protected abstract M convertId(K id);

    protected abstract MongoRepository mongoRepository();

    @Override
    public void addData(D data) {
        try {
            mongoRepository().insert(data);
        } catch (OptimisticLockingFailureException e) {
            throw translateOptimisticLockingFailure(e);
        }
    }

    private OptimisticLockingException translateOptimisticLockingFailure(OptimisticLockingFailureException e) {
        return new OptimisticLockingException(e);
    }

    @Override
    public void updateData(D data) {
        try {
            mongoRepository().save(data);
        } catch (OptimisticLockingFailureException e) {
            throw translateOptimisticLockingFailure(e);
        }
    }

    @Override
    public void deleteData(K id) {
        try {
            mongoRepository().deleteById(convertId(id));
        } catch (OptimisticLockingFailureException e) {
            throw translateOptimisticLockingFailure(e);
        }
    }

    @Override
    public void deleteAll() {
        mongoRepository().deleteAll();
    }

    @Override
    public List findAll() {
        return mongoRepository().findAll();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy