package org.peerbox;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class PropertyHandler {
	
	PropertyHandler propHandler = new PropertyHandler();
	static Properties prop = new Properties();

	
	public static void checkFileExists(){
		
		boolean fileExists = false;
		
		File f = new File("config.properties");
		if(f.exists() && !f.isDirectory()) { 
			fileExists = true;
			System.out.println("Existing property file found.");
		} else {
			//create new property file if no existing is found
			createPropertyFile();
		}
	}
	
	public static void createPropertyFile(){
    	
		try {
    		//Dummy test set
    		prop.setProperty("peerAddress", "localhost");
    		prop.setProperty("username", "myuser");
    		prop.setProperty("password", "mypwd");
    		prop.setProperty("pin", "1234");
    		//save properties to project root folder
    		prop.store(new FileOutputStream("config.properties"), null);
    		System.out.println("New property file created.");
    	} catch (IOException ex) {
    		ex.printStackTrace();
        }
    }
	
	public static void setRootPath(String path){

    	try {
    		prop.setProperty("rootpath",path);
    		//save properties to project root folder
    		prop.store(new FileOutputStream("config.properties"),null);
    		System.out.println("Root path stored in property file.");
    	} catch (IOException ex) {
    		ex.printStackTrace();
        }
	}
}


@@ -39,16 +39,49 @@
 .File;%0D%0A
+import java.io.FileInputStream;%0D%0A
 import j
@@ -103,32 +103,32 @@
 oundException;%0D%0A
-
 import java.io.F
@@ -194,32 +194,61 @@
 o.IOException;%0D%0A
+import java.io.InputStream;%0D%0A
 import java.util
@@ -401,16 +401,78 @@
 ();%0D%0A%0D%0A%09
+//check if property file is already existing in project folder
 %0D%0A%09publi
@@ -513,43 +513,8 @@
 %09%09%0D%0A
-%09%09boolean fileExists = false;%0D%0A%09%09%0D%0A
 %09%09Fi
@@ -600,25 +600,54 @@
 %0A%09%09%09
-fileExists = true
+//read in property file%0D%0A%09%09%09loadPropertyFile()
 ;%0D%0A%09
@@ -803,24 +803,473 @@
 %09%09%7D%0D%0A%09%7D%0D%0A%09%0D%0A
+%09%0D%0A%09//load existing property file%0D%0A%09public static void loadPropertyFile()%7B%0D%0A%09%09%0D%0A%09%09InputStream input = null;%0D%0A%09%09try %7B%0D%0A%09%09%09input = new FileInputStream(%22config.properties%22);%0D%0A%09%09%7D catch (FileNotFoundException e) %7B%0D%0A%09%09%09e.printStackTrace();%0D%0A%09%09%7D%0D%0A%09%09// load a properties file%0D%0A%09%09try %7B%0D%0A%09%09%09prop.load(input);%0D%0A%09%09%7D catch (IOException e) %7B%0D%0A%09%09%09e.printStackTrace();%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%09%0D%0A%09//if no property file is found, a new one will be written to the project folder%0D%0A
 %09public stat
@@ -1781,16 +1781,83 @@
   %7D%0D%0A%09%0D%0A
+%09//write root path from SelectRootPathController to property file%0D%0A
 %09public 
@@ -1895,18 +1895,16 @@
 path)%7B%0D%0A
-%0D%0A
     %09try
@@ -2183,32 +2183,32 @@
 tStackTrace();%0D%0A
-
         %7D%0D%0A%09%7D%0D%0A%7D
@@ -2206,13 +2206,470 @@
  %7D%0D%0A%09%7D%0D%0A
+%09%0D%0A%09//check whether the property file already holds a rootpath property%0D%0A%09public static boolean rootPathExists()%7B%0D%0A%09%09%0D%0A%09%09boolean exists = false;%0D%0A%09%09%0D%0A%09%09if(prop.getProperty(%22rootpath%22) != null)%7B%0D%0A%09%09%09exists = true;%0D%0A%09%09%09System.out.println(%22using rootpath from property file.%22);%0D%0A%09%09%7D%0D%0A%09%09return exists;%0D%0A%09%7D%0D%0A%09%0D%0A%09//returns rootpath value from property file%0D%0A%09public static String getRootPath()%7B%0D%0A%09%09String path = prop.getProperty(%22rootpath%22);%0D%0A%09%09return path;%0D%0A%09%7D%0D%0A
 %7D%0D%0A%0D%0A

check whether rootpath has been set before