/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package club.data;
import club.beans.AdministratorBean;
import club.beans.ConfigBean;
import club.ui.ConnectDBDialog;
import java.io.*;
/**
*
* @author Quang
*/
public class DataConfig {
private String DBServer;
private String DBName;
private String DBUser;
private String DBPass;
private File configFile;
public String getDBServer() {
return DBServer;
}
public String getDBName() {
return DBName;
}
public String getDBUser() {
return DBUser;
}
public String getDBPass() {
return DBPass;
}
public boolean isConfigFileExist(){
configFile = new File ("config.cgi");
return configFile.exists();
}
public boolean writeConfigFile(ConfigBean configBean){
try{
configFile = new File ("config.cgi");
//empty the file before start writting
new FileOutputStream(configFile).close();
FileOutputStream fout = new FileOutputStream(configFile);
try (ObjectOutputStream oos = new ObjectOutputStream(fout)) {
oos.writeObject(configBean);
}
return true;
}catch(Exception ex){
ex.printStackTrace();
return false;
}
}
public int getCurrentAdminID(){
ConfigBean config = readConfigFile();
return config.getAdminID();
}
public boolean isRightConfig(){
try{
ConfigBean config = readConfigFile();
if (IsValidDBInfo(config)){
return true;
} else{
(new ConnectDBDialog()).setVisible(true);
return false;
}
} catch(Exception e){
(new ConnectDBDialog()).setVisible(true);
return false;
}
}
public static boolean IsValidDBInfo(ConfigBean configBean){
String server = configBean.getDBServer();
String name = configBean.getDBName();
String port = configBean.getDBPort();
String user = configBean.getDBUser();
String pass = configBean.getDBPass();
if (ConnectDB.testConnect(server, port, user, pass, name)){
return true;
} else{
return false;
}
}
public boolean IsValidAdminInfo(ConfigBean configBean){
String admin = configBean.getAdminAccount();
int adminID = configBean.getAdminID();
return DataAdmin.isExistID(admin, adminID);
}
public boolean isFullControlAdmin(){
try{
ConfigBean config = readConfigFile();
AdministratorBean admin = DataAdmin.getAdminByID(config.getAdminID());
return (admin.getStatus()== 2) ? true: false;
} catch (Exception e){
e.printStackTrace();
return false;
}
}
public ConfigBean readConfigFile(){
ConfigBean config;
if (!isConfigFileExist())
return null;
try{
FileInputStream fin = new FileInputStream(configFile);
ObjectInputStream ois = new ObjectInputStream(fin);
config = (ConfigBean) ois.readObject();
ois.close();
return config;
}catch(Exception ex){
ex.printStackTrace();
//(new ConnectDBDialog()).setVisible(true);
return null;
}
}
}