Package webapp.storage

Source Code of webapp.storage.FileStorage

package webapp.storage;

import webapp.WebAppException;
import webapp.model.Resume;

import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
* User: gkislin
* Date: 16.04.2014
*/
abstract public class FileStorage extends AbstractStorage<File> {
    private File dir;

    @Override
    protected File createCtx(String uuid) {
        return getFile(uuid);
    }

    @Override
    protected boolean exist(File file) {
        return file.exists();
    }

    public FileStorage(String path) {
        this.dir = new File(path);
        if (!dir.isDirectory() || !dir.canWrite()) {
            throw new IllegalArgumentException("'" + path + "' is not directory or is not writable");
        }
    }

    public void doClearAll() {
        File[] files = dir.listFiles();
        if (files == null) return;
        for (File file : files) {
            file.delete();
        }
    }

    public void doSave(File file, Resume resume) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            throw new WebAppException("Couldn't write file " + file.getAbsolutePath(), resume, e);
        }
        write(file, resume);
    }

    private File getFile(String uuid) {
        return new File(dir, uuid);
    }

    protected void write(File file, Resume resume) {
        try {
            doWrite(new FileOutputStream(file), resume);
        } catch (IOException e) {
            throw new WebAppException("Couldn't write file " + file.getAbsolutePath(), resume, e);
        }
    }

    abstract protected void doWrite(OutputStream os, Resume resume) throws IOException;

    public Resume doLoad(File file) {
        return read(file);
    }

    protected Resume read(File file) {
        try {
            Resume r = doRead(new FileInputStream(file));
            r.setUuid(file.getName());
            return r;
        } catch (IOException e) {
            throw new WebAppException("Couldn't read file " + file.getAbsolutePath(), e);
        }
    }

    abstract protected Resume doRead(InputStream is) throws IOException;

    @Override
    public void doDelete(File file) {
        file.delete();
    }

    public void doUpdate(File file, Resume resume) {
        write(file, resume);
    }

    public Collection<Resume> doGetCollection() {
        File[] files = dir.listFiles();
        if (files == null) return Collections.emptyList();

        List<Resume> list = new ArrayList<>(files.length);
        for (File uuid : files) {
            list.add(read(uuid));
        }
        return list;
    }

    public int size() {
        File[] files = dir.listFiles();
        return (files == null) ? 0 : files.length;
    }
}
TOP

Related Classes of webapp.storage.FileStorage

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.