Package core

Source Code of core.RedoLogManager

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package core;

import java.util.ArrayList;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import business_layer.DataDictionary;


/**
*
* @author Tomek
*/
public class RedoLogManager {

    private DataDictionary dataDictionary = null;
    private RedoLog redoLog = null;

    public RedoLogManager() {
        //try to load the file
        redoLog = loadFile();
        dataDictionary = loadDataDictionaryFile();
        //if it wasn't loaded, create new RedoLog object
        if(redoLog==null)
            redoLog = new RedoLog();
        if(dataDictionary==null)
            dataDictionary = new DataDictionary();
    }

    public void setDictionary(DataDictionary dict) {
        this.dataDictionary = dict;
    }
    public DataDictionary getDictionary() {
        return this.dataDictionary;
    }

    public long getTimestamp() {
        return redoLog.getTimestamp();
    }
    public void setTimestamp(long ts) {
        redoLog.setTimestamp(ts);
    }

    public ArrayList<String> getOperationList() {
        return redoLog.getOperationList();
    }
    public void setOperationList(ArrayList<String> op) {
        redoLog.setOperationList(op);
    }

    public void addOperation(String op) {
        redoLog.addOperation(op);
    }
    public void addOperations(ArrayList<String> ops) {
        for(String elem : ops)
            redoLog.addOperation(elem);
    }

    public int getLogSize() {
        return redoLog.getLogSize();
    }
    public ArrayList<String> getLogFromIdx(int idx) {
        return redoLog.getLogFromIdx(idx);
    }

    public boolean save() {
        saveDataDictionaryFile(dataDictionary);
        return saveFile(redoLog);
    }

    //data saving methods
    private boolean saveFile(RedoLog log) {
        String filename = "redolog.xml";
        if (filename != null) {
            try {
                XStream xstream = new XStream();
                ObjectOutputStream out = xstream.createObjectOutputStream(new FileWriter(filename));
                out.writeObject(log);
                out.close();
            } catch (Exception e) {
                //debugging
                e.printStackTrace();
               
                return false;
            }
        } else return false;

        return true;
    }

    private RedoLog loadFile() {
        RedoLog log = null;
        String filename = "redolog.xml";
        if (filename != null) {
            try {
                XStream xstream = new XStream(new DomDriver());
                ObjectInputStream in = xstream.createObjectInputStream(new FileReader(filename));
                log = (RedoLog) in.readObject();
                in.close();
            } catch (Exception e) {
                //debugging
                //e.printStackTrace();
            }
        }

        return log;
    }

    //data saving methods
    private boolean saveDataDictionaryFile(DataDictionary dict) {
        String filename = "datadict.xml";
        if (filename != null) {
            try {
                XStream xstream = new XStream();
                ObjectOutputStream out = xstream.createObjectOutputStream(new FileWriter(filename));
                out.writeObject(dict);
                out.close();
            } catch (Exception e) {
                //debugging
                e.printStackTrace();

                return false;
            }
        } else return false;

        return true;
    }

    private DataDictionary loadDataDictionaryFile() {
        DataDictionary dict = null;
        String filename = "datadict.xml";
        if (filename != null) {
            try {
                XStream xstream = new XStream(new DomDriver());
                ObjectInputStream in = xstream.createObjectInputStream(new FileReader(filename));
                dict = (DataDictionary) in.readObject();
                in.close();
            } catch (Exception e) {
                //debugging
                //e.printStackTrace();
            }
        }

        return dict;
    }
}
TOP

Related Classes of core.RedoLogManager

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.