/*******************************************************************
 * 
 * Copyright (C) 2013 Kevin Reverchon
 * This file/program is part of gZFS free software
 * See COPYING file for details
 * 
 *******************************************************************/
package gZFS;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.Logger;


public class zserver {
	private Logger log = Logger.getLogger(getClass().getPackage().getName()+" "+getClass().getCanonicalName());
	public Hashtable<String,zProperties> zProperties = new Hashtable<String, zProperties>();
	zserver(){
	List<Object> permitValueServerIP = new ArrayList<Object>();
	zProperties zPropertyServerIP = new zProperties();
	permitValueServerIP.clear();
	zPropertyServerIP.setNameProperty("serverIP");
	zPropertyServerIP.setLabelProperty("Address IP or Server Name");
	zPropertyServerIP.setType("freeentry");
	zPropertyServerIP.setValue("");
	zPropertyServerIP.setPermitValue(null);
	zProperties.put("serverIP", zPropertyServerIP);
	
	List<Object> permitValueUserLogin = new ArrayList<Object>();
	zProperties zPropertyUserLogin = new zProperties();
	permitValueServerIP.clear();
	zPropertyUserLogin.setNameProperty("userLogin");
	zPropertyUserLogin.setLabelProperty("User Login");
	zPropertyUserLogin.setType("freeentry");
	zPropertyUserLogin.setValue("");
	zPropertyUserLogin.setPermitValue(null);
	zProperties.put("userLogin", zPropertyUserLogin);
	
	List<Object> permitValueUserPasswd = new ArrayList<Object>();
	zProperties zPropertyUserPasswd = new zProperties();
	permitValueUserPasswd.clear();
	zPropertyUserPasswd.setNameProperty("userPasswd");
	zPropertyUserPasswd.setLabelProperty("User Password");
	zPropertyUserPasswd.setType("freeentry");
	zPropertyUserPasswd.setValue("");
	zPropertyUserPasswd.setPermitValue(null);
	zProperties.put("userPasswd", zPropertyUserPasswd);
	
	List<Object> permitValueZfsBinStatus = new ArrayList<Object>();
	zProperties zPropertyZfsBinStatus  = new zProperties();
	permitValueZfsBinStatus.clear();
	zPropertyZfsBinStatus.setNameProperty("binzfs");
	zPropertyZfsBinStatus.setLabelProperty("Binary : ZFS");
	zPropertyZfsBinStatus.setType("noupdateable");
	zPropertyZfsBinStatus.setValue("");
	zPropertyZfsBinStatus.setPermitValue(null);
	zProperties.put("binzfs", zPropertyZfsBinStatus);
	
	List<Object> permitValueBinZpool = new ArrayList<Object>();
	zProperties zPropertyBinZpool  = new zProperties();
	permitValueBinZpool.clear();
	zPropertyBinZpool.setNameProperty("binzpool");
	zPropertyBinZpool.setLabelProperty("Binary : Zpool");
	zPropertyBinZpool.setType("noupdateable");
	zPropertyBinZpool.setValue("");
	zPropertyBinZpool.setPermitValue(null);
	zProperties.put("binzpool", zPropertyBinZpool);
	
	List<Object> permitValueIscsiGetConfBinStatus = new ArrayList<Object>();
	zProperties zPropertyIscsiGetConfBinStatus  = new zProperties();
	permitValueIscsiGetConfBinStatus.clear();
	zPropertyIscsiGetConfBinStatus.setNameProperty("biniscsigetconf");
	zPropertyIscsiGetConfBinStatus.setLabelProperty("Tool : get ISCSI Configuration");
	zPropertyIscsiGetConfBinStatus.setType("noupdateable");
	zPropertyIscsiGetConfBinStatus.setValue("");
	zPropertyIscsiGetConfBinStatus.setPermitValue(null);
	zProperties.put("biniscsigetconf", zPropertyIscsiGetConfBinStatus);
	
	List<Object> permitValueBinGetProvisionedSpace = new ArrayList<Object>();
	zProperties zPropertyBinGetProvisionedSpace  = new zProperties();
	permitValueBinGetProvisionedSpace.clear();
	zPropertyBinGetProvisionedSpace.setNameProperty("bingetprovisionedspace");
	zPropertyBinGetProvisionedSpace.setLabelProperty("Tool : Provisionned Space");
	zPropertyBinGetProvisionedSpace.setType("noupdateable");
	zPropertyBinGetProvisionedSpace.setValue("");
	zPropertyBinGetProvisionedSpace.setPermitValue(null);
	zProperties.put("bingetprovisionedspace", zPropertyBinGetProvisionedSpace);
	
	List<Object> permitValueGetConfSnapshot = new ArrayList<Object>();
	zProperties zPropertyGetConfSnapshot  = new zProperties();
	permitValueGetConfSnapshot.clear();
	zPropertyGetConfSnapshot.setNameProperty("bingetconfsnapshot");
	zPropertyGetConfSnapshot.setLabelProperty("Tool : get Snapshot Configuration");
	zPropertyGetConfSnapshot.setType("noupdateable");
	zPropertyGetConfSnapshot.setValue("");
	zPropertyGetConfSnapshot.setPermitValue(null);
	zProperties.put("bingetconfsnapshot", zPropertyGetConfSnapshot);
	
	
	List<Object> permitValueBinZfsSnapshots = new ArrayList<Object>();
	zProperties zPropertyBinZfsSnapshots  = new zProperties();
	permitValueBinZfsSnapshots.clear();
	zPropertyBinZfsSnapshots.setNameProperty("binzfssnapshots");
	zPropertyBinZfsSnapshots.setLabelProperty("Tool : ZFS Snapshots");
	zPropertyBinZfsSnapshots.setType("noupdateable");
	zPropertyBinZfsSnapshots.setValue("");
	zPropertyBinZfsSnapshots.setPermitValue(null);
	zProperties.put("binzfssnapshots", zPropertyBinZfsSnapshots);
	
	List<Object> permitValueBinZfsEnableSnapshots = new ArrayList<Object>();
	zProperties zPropertyBinZfsEnableSnapshots  = new zProperties();
	permitValueBinZfsEnableSnapshots.clear();
	zPropertyBinZfsEnableSnapshots.setNameProperty("binzfsenablesnapshots");
	zPropertyBinZfsEnableSnapshots.setLabelProperty("Tool : Enable ZFS Snapshots");
	zPropertyBinZfsEnableSnapshots.setType("noupdateable");
	zPropertyBinZfsEnableSnapshots.setValue("");
	zPropertyBinZfsEnableSnapshots.setPermitValue(null);
	zProperties.put("binzfsenablesnapshots", zPropertyBinZfsEnableSnapshots);
	
	List<Object> permitValueBinZfsDisableSnapshots = new ArrayList<Object>();
	zProperties zPropertyBinZfsDisableSnapshots  = new zProperties();
	permitValueBinZfsDisableSnapshots.clear();
	zPropertyBinZfsDisableSnapshots.setNameProperty("binzfsdisablesnapshots");
	zPropertyBinZfsDisableSnapshots.setLabelProperty("Tool : Disable ZFS Snapshots");
	zPropertyBinZfsDisableSnapshots.setType("noupdateable");
	zPropertyBinZfsDisableSnapshots.setValue("");
	zPropertyBinZfsDisableSnapshots.setPermitValue(null);
	zProperties.put("binzfsdisablesnapshots", zPropertyBinZfsDisableSnapshots);
	
	
	List<Object> permitValuePathZfsconfSnapshots = new ArrayList<Object>();
	zProperties zPropertyPathZfsconfSnapshots  = new zProperties();
	permitValuePathZfsconfSnapshots.clear();
	zPropertyPathZfsconfSnapshots.setNameProperty("pathzfsconfsnapshots");
	zPropertyPathZfsconfSnapshots.setLabelProperty("Path : Snapshot Configuration File");
	zPropertyPathZfsconfSnapshots.setType("noupdateable");
	zPropertyPathZfsconfSnapshots.setValue("");
	zPropertyPathZfsconfSnapshots.setPermitValue(null);
	zProperties.put("pathzfsconfsnapshots", zPropertyPathZfsconfSnapshots);
	
	List<Object> permitValuePathZfsConfTools = new ArrayList<Object>();
	zProperties zPropertyPathZfsConfTools  = new zProperties();
	permitValuePathZfsConfTools.clear();
	zPropertyPathZfsConfTools.setNameProperty("pathzfsconftools");
	zPropertyPathZfsConfTools.setLabelProperty("Path : ZFS Tools");
	zPropertyPathZfsConfTools.setType("noupdateable");
	zPropertyPathZfsConfTools.setValue("");
	zPropertyPathZfsConfTools.setPermitValue(null);
	zProperties.put("pathzfsconftools", zPropertyPathZfsConfTools);
	
	List<Object> permitValuePathZfsConf = new ArrayList<Object>();
	zProperties zPropertyPathZfsConf  = new zProperties();
	permitValuePathZfsConf.clear();
	zPropertyPathZfsConf.setNameProperty("pathzfsconf");
	zPropertyPathZfsConf.setLabelProperty("Path : gZFS");
	zPropertyPathZfsConf.setType("noupdateable");
	zPropertyPathZfsConf.setValue("");
	zPropertyPathZfsConf.setPermitValue(null);
	zProperties.put("pathzfsconf", zPropertyPathZfsConf);
	
	List<Object> permitValuePathZfsVolDevice = new ArrayList<Object>();
	zProperties zPropertyPathZfsVolDevice  = new zProperties();
	permitValuePathZfsVolDevice.clear();
	zPropertyPathZfsVolDevice.setNameProperty("pathzfsvoldevice");
	zPropertyPathZfsVolDevice.setLabelProperty("Path : ZFS Volume Device");
	zPropertyPathZfsVolDevice.setType("noupdateable");
	zPropertyPathZfsVolDevice.setValue("");
	zPropertyPathZfsVolDevice.setPermitValue(null);
	zProperties.put("pathzfsvoldevice", zPropertyPathZfsVolDevice);
	
	List<Object> permitValueBinZfsAddISCSI = new ArrayList<Object>();
	zProperties zPropertyBinZfsAddISCSI  = new zProperties();
	permitValueBinZfsAddISCSI.clear();
	zPropertyBinZfsAddISCSI.setNameProperty("binzfsaddiscsi");
	zPropertyBinZfsAddISCSI.setLabelProperty("Tool : Create ISCSI Volume");
	zPropertyBinZfsAddISCSI.setType("noupdateable");
	zPropertyBinZfsAddISCSI.setValue("");
	zPropertyBinZfsAddISCSI.setPermitValue(null);
	zProperties.put("binzfsaddiscsi", zPropertyBinZfsAddISCSI);
	
	List<Object> permitValueBinZfsDelISCSI = new ArrayList<Object>();
	zProperties zPropertyBinZfsDelISCSI  = new zProperties();
	permitValueBinZfsDelISCSI.clear();
	zPropertyBinZfsDelISCSI.setNameProperty("binzfsadelscsi");
	zPropertyBinZfsDelISCSI.setLabelProperty("Tool : Delete ISCSI Volume");
	zPropertyBinZfsDelISCSI.setType("noupdateable");
	zPropertyBinZfsDelISCSI.setValue("");
	zPropertyBinZfsDelISCSI.setPermitValue(null);
	zProperties.put("binzfsdeliscsi", zPropertyBinZfsDelISCSI);
	
	List<Object> permitValueBinZfsCreateVol = new ArrayList<Object>();
	zProperties zPropertyBinZfsCreateVol  = new zProperties();
	permitValueBinZfsCreateVol.clear();
	zPropertyBinZfsCreateVol.setNameProperty("binzfscreatevolume");
	zPropertyBinZfsCreateVol.setLabelProperty("Tool : Create ZFS Volume");
	zPropertyBinZfsCreateVol.setType("noupdateable");
	zPropertyBinZfsCreateVol.setValue("");
	zPropertyBinZfsCreateVol.setPermitValue(null);
	zProperties.put("binzfscreatevolume", zPropertyBinZfsCreateVol);
	
	List<Object> permitValueBinZfsCreateFS = new ArrayList<Object>();
	zProperties zPropertyBinZfsCreateFS  = new zProperties();
	permitValueBinZfsCreateFS.clear();
	zPropertyBinZfsCreateFS.setNameProperty("binzfscreatefilesystem");
	zPropertyBinZfsCreateFS.setLabelProperty("Tool : Create ZFS FileSystem");
	zPropertyBinZfsCreateFS.setType("noupdateable");
	zPropertyBinZfsCreateFS.setValue("");
	zPropertyBinZfsCreateFS.setPermitValue(null);
	zProperties.put("binzfscreatefilesystem", zPropertyBinZfsCreateFS);
	
	List<Object> permitValueBinZfsDelFS = new ArrayList<Object>();
	zProperties zPropertyBinZfsDelFS  = new zProperties();
	permitValueBinZfsDelFS.clear();
	zPropertyBinZfsDelFS.setNameProperty("binzfsdelfilesystem");
	zPropertyBinZfsDelFS.setLabelProperty("Tool : Delete ZFS FileSystem");
	zPropertyBinZfsDelFS.setType("noupdateable");
	zPropertyBinZfsDelFS.setValue("");
	zPropertyBinZfsDelFS.setPermitValue(null);
	zProperties.put("binzfsdelfilesystem", zPropertyBinZfsDelFS);
	
	List<Object> permitValueBinZfsDelVolume = new ArrayList<Object>();
	zProperties zPropertyBinZfsDelVolume  = new zProperties();
	permitValueBinZfsDelVolume.clear();
	zPropertyBinZfsDelVolume.setNameProperty("binzfsdelvolume");
	zPropertyBinZfsDelVolume.setLabelProperty("Tool : Delete ZFS Volume");
	zPropertyBinZfsDelVolume.setType("noupdateable");
	zPropertyBinZfsDelVolume.setValue("");
	zPropertyBinZfsDelVolume.setPermitValue(null);
	zProperties.put("binzfsdelvolume", zPropertyBinZfsDelVolume);
	
	List<Object> permitValueBinGetDiskInfo = new ArrayList<Object>();
	zProperties zPropertyBinGetDiskInfo  = new zProperties();
	permitValueBinGetDiskInfo.clear();
	zPropertyBinGetDiskInfo.setNameProperty("bingetdiskinfo");
	zPropertyBinGetDiskInfo.setLabelProperty("Tool : Get Disk Informations");
	zPropertyBinGetDiskInfo.setType("noupdateable");
	zPropertyBinGetDiskInfo.setValue("");
	zPropertyBinGetDiskInfo.setPermitValue(null);
	zProperties.put("bingetdiskinfo", zPropertyBinGetDiskInfo);
	
	List<Object> permitValueBinZFSClone = new ArrayList<Object>();
	zProperties zPropertyBinZFSClone  = new zProperties();
	permitValueBinZFSClone.clear();
	zPropertyBinZFSClone.setNameProperty("binzfsclone");
	zPropertyBinZFSClone.setLabelProperty("Tool : ZFS Clone");
	zPropertyBinZFSClone.setType("noupdateable");
	zPropertyBinZFSClone.setValue("");
	zPropertyBinZFSClone.setPermitValue(null);
	zProperties.put("binzfsclone", zPropertyBinZFSClone);
	
	List<Object> permitValueBinZpoolGetState = new ArrayList<Object>();
	zProperties zPropertyBinZpoolGetState  = new zProperties();
	permitValueBinZpoolGetState.clear();
	zPropertyBinZpoolGetState.setNameProperty("binzpoolgetstate");
	zPropertyBinZpoolGetState.setLabelProperty("Tool : ZFS Clone");
	zPropertyBinZpoolGetState.setType("noupdateable");
	zPropertyBinZpoolGetState.setValue("");
	zPropertyBinZpoolGetState.setPermitValue(null);
	zProperties.put("binzpoolgetstate", zPropertyBinZpoolGetState);
	
	List<Object> permitValueBinGetConfReplica = new ArrayList<Object>();
	zProperties zPropertyBinGetConfReplica  = new zProperties();
	permitValueBinGetConfReplica.clear();
	zPropertyBinGetConfReplica.setNameProperty("bingetconfreplica");
	zPropertyBinGetConfReplica.setLabelProperty("Tool : ZFS Replication");
	zPropertyBinGetConfReplica.setType("noupdateable");
	zPropertyBinGetConfReplica.setValue("");
	zPropertyBinGetConfReplica.setPermitValue(null);
	zProperties.put("bingetconfreplica", zPropertyBinGetConfReplica);
	
	List<Object> permitValueBinEnableReplica = new ArrayList<Object>();
	zProperties zPropertyBinEnableReplica  = new zProperties();
	permitValueBinEnableReplica.clear();
	zPropertyBinEnableReplica.setNameProperty("binzfsenablereplicas");
	zPropertyBinEnableReplica.setLabelProperty("Tool :Enable ZFS Replication");
	zPropertyBinEnableReplica.setType("noupdateable");
	zPropertyBinEnableReplica.setValue("");
	zPropertyBinEnableReplica.setPermitValue(null);
	zProperties.put("binzfsenablereplicas", zPropertyBinEnableReplica);
	
	List<Object> permitValueBinDisableReplica = new ArrayList<Object>();
	zProperties zPropertyBinDisableReplica  = new zProperties();
	permitValueBinDisableReplica.clear();
	zPropertyBinDisableReplica.setNameProperty("binzfsdisablereplicas");
	zPropertyBinDisableReplica.setLabelProperty("Tool :Disable ZFS Replication");
	zPropertyBinDisableReplica.setType("noupdateable");
	zPropertyBinDisableReplica.setValue("");
	zPropertyBinDisableReplica.setPermitValue(null);
	zProperties.put("binzfsdisablereplicas", zPropertyBinDisableReplica);
	
	List<Object> permitValueBinReplica = new ArrayList<Object>();
	zProperties zPropertyBinReplica  = new zProperties();
	permitValueBinReplica.clear();
	zPropertyBinReplica.setNameProperty("binzfsreplica");
	zPropertyBinReplica.setLabelProperty("Tool : Binary ZFS Replication");
	zPropertyBinReplica.setType("noupdateable");
	zPropertyBinReplica.setValue("");
	zPropertyBinReplica.setPermitValue(null);
	zProperties.put("binzfsreplica", zPropertyBinReplica);
	
	/*List<Object> permitValueBinDisableReplica = new ArrayList<Object>();
	zProperties zPropertyBinDisableReplica  = new zProperties();
	permitValueBinDisableReplica.clear();
	zPropertyBinDisableReplica.setNameProperty("binzfsdisablereplicas");
	zPropertyBinDisableReplica.setLabelProperty("Tool :Disable ZFS Replication");
	zPropertyBinDisableReplica.setType("noupdateable");
	zPropertyBinDisableReplica.setValue("");
	zPropertyBinDisableReplica.setPermitValue(null);
	zProperties.put("binzfsdisablereplicas", zPropertyBinDisableReplica);*/
	
	}
	
	
	public boolean add(String fileName){
		
		try {
			Properties prop = new Properties();
			String[] keysProp = (String[])this.zProperties.keySet().toArray(new String[0]);
			Arrays.sort(keysProp);
			for (int i = 0; i < keysProp.length; i++) {
				String keyProp = keysProp[i];
				log.debug("keyProp :"+keyProp);
				prop.setProperty(keyProp, this.zProperties.get(keyProp).getValue());	
			}
			prop.store(new FileOutputStream(fileName), null);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public boolean load(String fileName){
		try {
			Properties prop = new Properties();
    		prop.load(new FileInputStream(fileName));
			String[] keysProp = (String[])this.zProperties.keySet().toArray(new String[0]);
			Arrays.sort(keysProp);
			for (int i = 0; i < keysProp.length; i++) {
				String keyProp = keysProp[i];	
				if ( prop.containsKey(keyProp)){
					this.zProperties.get(keyProp).setValue(prop.getProperty(keyProp));	
				}else{
					this.zProperties.get(keyProp).setValue("");
				}
					
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public boolean remove(String fileName){
		File f = new File(fileName);
		if (f.exists()){
			if (f.delete()){
				return true;
			}
		}
		return false;
	}
}
