package webapp.storage;
import webapp.IStorage;
import webapp.WebAppException;
import webapp.model.Resume;
import java.util.Collection;
import java.util.logging.Logger;
/**
* 25.04.2014.
*/
abstract public class AbstractStorage<C> implements IStorage {
public static final Logger LOGGER = Logger.getLogger("Storage");
public void clearAll() {
LOGGER.info("Delete all resumes.");
doClearAll();
}
public String save(Resume resume) {
LOGGER.info("Save resume with uuid=" + resume.getUuid());
C ctx = createCtx(resume.getUuid());
if (!exist(ctx)) {
LOGGER.info("Save resume with uuid=" + resume.getUuid());
doSave(ctx, resume);
return resume.getUuid();
}
throw new WebAppException("Resume " + resume + " already present", resume);
}
public Resume load(String uuid) {
LOGGER.info("Load resume with uuid=" + uuid);
C ctx = createCtx(uuid);
if (exist(ctx)) {
return doLoad(ctx);
}
throw new WebAppException("Resume not found", uuid);
}
public void delete(String uuid) {
LOGGER.info("Delete resume with uuid=" + uuid);
C ctx = createCtx(uuid);
if (exist(ctx)) {
doDelete(ctx);
return;
}
throw new WebAppException("Resume not found", uuid);
}
public void update(Resume resume) {
LOGGER.info("Update resume with uuid=" + resume.getUuid());
C ctx = createCtx(resume.getUuid());
if (exist(ctx)) {
doUpdate(ctx, resume);
return;
}
throw new WebAppException("Resume not found", resume);
}
public Collection<Resume> getCollection() {
LOGGER.info("Get all collections");
Collection<Resume> c = doGetCollection();
LOGGER.info("Collections size is " + c.size());
return c;
}
public abstract int size();
//////////////////// child
protected abstract C createCtx(String uuid);
protected abstract boolean exist(C ctx);
abstract void doClearAll();
abstract void doSave(C ctx, Resume resume);
abstract Resume doLoad(C ctx);
abstract void doDelete(C ctx);
abstract void doUpdate(C ctx, Resume resume);
abstract Collection<Resume> doGetCollection();
}