Package com.nexirius.theteam.persistence

Source Code of com.nexirius.theteam.persistence.FilePersistence

package com.nexirius.theteam.persistence;

import com.nexirius.framework.datamodel.DataModel;
import com.nexirius.framework.datamodel.DataModelVector;
import com.nexirius.util.SortedVector;
import com.nexirius.util.StringVector;
import com.nexirius.util.XFile;

import java.io.File;

/**
* A simple mechanism to store persistant information into a file system.
*
* @author Marcel Baumann
*/
class FilePersistence {
    private static FilePersistence instance;
    String rootDirectory;

    private FilePersistence(String root) {
        if (!root.endsWith(File.separator)) {
            root = root + File.separator;
        }

        this.rootDirectory = root;
    }

    /**
     * init the persistant data store (all data will be stored under this directory)
     *
     * @param persistenceRootDirectory
     */
    public static void init(String persistenceRootDirectory) {
        XFile dir = new XFile(persistenceRootDirectory);

        dir.mkdirs();
        instance = new FilePersistence(persistenceRootDirectory);
    }

    public static FilePersistence getInstance() {
        if (instance == null) {
            throw new RuntimeException("Call FilePersistence.init() first");
        }

        return instance;
    }

    public String getNewID() {
        try {
            XFile f = new XFile(rootDirectory, "ID.TXT");
            int id = 1;
            if (f.exists()) {
                String sID = new String(f.getBytes()); // reads the content of the file into a string
                id = Integer.parseInt(sID);
                ++id;
            }
            // write the latest id into file
            f.writeText(Integer.toString(id));

            return Integer.toString(id);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    private XFile getDirectory(Class cl) {
        XFile dir = new XFile(rootDirectory + cl.getName());
        if (!dir.isDirectory()) {
            dir.mkdirs();
        }
        return dir;
    }

    /**
     * create a persistent instance
     *
     * @param model
     */
    public void create(DataModel model) {
        try {
            String id = getNewID();
            XFile dir = getDirectory(model.getClass());
            XFile file = new XFile(dir.getPath(), id);
            file.writeText(model.dragData());
            model.setInstanceName(id);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * delete an instance from the persistence data store
     *
     * @param cl the class of the instance
     * @param id the is of the instance
     */
    public void delete(Class cl, String id) {
        try {
            XFile dir = getDirectory(cl);
            XFile file = new XFile(dir.getPath(), id);

            if (file.exists()) {
                file.delete();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * delete the persistent model
     *
     * @param model
     */
    public void delete(DataModel model) {
        delete(model.getClass(), model.getInstanceName());
    }

    /**
     * read an instance from the persistence data store
     *
     * @param cl the class of the instance
     * @param id the is of the instance
     * @return null if instance does not exist
     */
    public DataModel read(Class cl, String id) {
        try {
            XFile dir = getDirectory(cl);
            XFile file = new XFile(dir.getPath(), id);
            DataModel ret = (DataModel) cl.newInstance();
            ret.dropData(new String(file.getBytes()));
            ret.setInstanceName(id);
            return ret;
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }

    /**
     * read the persistent data directly into an existing model
     *
     * @param model
     */
    public void read(DataModel model) {
        try {
            XFile dir = getDirectory(model.getClass());
            XFile file = new XFile(dir.getPath(), model.getInstanceName());

            model.dropData(new String(file.getBytes()));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * access all items of a certain class
     *
     * @param cl the class of the accessed items
     * @return always returns a valid vector (which can be empty)
     */
    public SortedVector getAll(Class cl) {
        return getAll(cl, null, null);
    }

    /**
     * access all items of a certain class which share a given field value
     *
     * @param cl            the class of the items which shall be accessed
     * @param propertyName  the field name of the field which is used to reduce the hits (e.g. foreign key) can be null to access all items
     * @param propertyValue the value of the specified field (or null)
     * @return always returns a valid vector (which can be empty)
     */
    public SortedVector getAll(Class cl, String propertyName, String propertyValue) {
        SortedVector ret = new DataModelVector();
        try {
            XFile dir = getDirectory(cl);
            StringVector files = dir.getFiles(false); // get the list of all the files in this directory
            for (String id = files.firstItem(); id != null; id = files.nextItem()) {
                DataModel model = (DataModel) cl.newInstance(); //create a new instance of the given type
                XFile file = new XFile(dir.getPath(), id);
                model.dropData(new String(file.getBytes()));
                model.setInstanceName(id);
                if (propertyValue == null || propertyValue.equals(model.getChildText(propertyName))) {
                    ret.addElement(model);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return ret;
    }

    /**
     * update the instance
     *
     * @param model
     */
    public void update(DataModel model) {
        try {
            XFile dir = getDirectory(model.getClass());
            XFile file = new XFile(dir.getPath(), model.getInstanceName());
            file.writeText(model.dragData());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
TOP

Related Classes of com.nexirius.theteam.persistence.FilePersistence

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.