package smserve_plugins;


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Formatter;
import java.util.Vector;

/**
 * This Class my tends do dump data in a partly
 * inconsistent.
 * @SMdump
 */

import structures.Message;
import structures.Messagequeue;

/**
 * This class handles the backups
 * of our inRam-Database.
 * @author Zeri
 * @version 1.0
 * @category life-and-death
 */
public class SMdump {
	private String dataPath;
	private  Vector<String> users;
	private Vector<Messagequeue> mails;
	
	@SuppressWarnings("unchecked")
	public SMdump(String pPath) {
		this.dataPath=pPath;
		this.users=(Vector<String>)smserve_main.SMserve.users.clone();
		this.mails=(Vector<Messagequeue>)smserve_main.SMserve.mails.clone();
	}

	/**
	 * The Standard-Constructor.
	 * is not important at all,
	 * since it isn't used anyway.
	 * @category death
	 */
	public SMdump() {
		this("tmp/");
	}
	/**
	 * This is a very important Function
	 * since it does the backup stuff for us.
	 * @category live-and-death
	 * @param none
	 * @throws Exception
	 */
	public void dumpMails() throws Exception {
		BufferedWriter fout=new BufferedWriter(new FileWriter(this.dataPath));
		Formatter 		format=new Formatter();
		try {
			for (int i = 0 ;i < this.users.size() ; i++ ) {
				if ((i%2)==0) {
					fout.write(format.format("REG %s %s\n",this.users.get(i),this.users.get(i+1)).toString());
					fout.flush();
					format=new Formatter();
				}
			}
			for (Messagequeue mh : mails) {
				for (Message msg : mh.messages){
					fout.write(format.format("HELO %s\n",msg.fromUser).toString());
					fout.flush();
					format=new Formatter();
					fout.write(format.format("MSG MSGTO:%s;%s\n",mh.user,msg.message).toString());
					fout.flush();
					format=new Formatter();
				}
			}
		}catch(Exception e) {
			System.out.println(e);
		}
	}
	
}
