package com.cipres.mrBayesPlugin; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Properties; import org.ngbw.directclient.CiApplication; import org.ngbw.directclient.CiClient; import com.biomatters.geneious.publicapi.components.Dialogs; import com.cipres.mrBayesPlugin.models.UserModel; import com.cipres.mrBayesPlugin.utilities.CipresUtilities; import com.cipres.mrBayesPlugin.utilities.ClientCheckResult; import com.cipres.mrBayesPlugin.utilities.DataHandlingUtilities; public class CipresMrBayesInitializer { public static final CipresMrBayesInitializer Instance = new CipresMrBayesInitializer(); private CiClient myClient = null; private boolean userLoggedIn = false; private UserModel user = null; private CipresMrBayesInitializer() {} public synchronized boolean initialize(String username, String password) { //String username = null; //String password = null; String appName = null; String appKey = null; if (!userLoggedIn) { if (CipresUtilities.isNewUser()) { Properties prop = new Properties(); OutputStream output = null; try { output = new FileOutputStream(CipresUtilities.getConfigFilePath()); // set the properties value prop.setProperty("URL", CipresUtilities.SERVICEURL); prop.setProperty("APPNAME", CipresUtilities.APPNAME); prop.setProperty("APPID", CipresUtilities.APPID); prop.setProperty("USERNAME", username == null? "" : username); prop.setProperty("PASSWORD", password == null? "" : password); prop.setProperty("REPORTEMAIL", CipresUtilities.REPORTEMAIL); // save properties to project root folder prop.store(output, null); } catch (IOException io) { io.printStackTrace(); } finally { if (output != null) { try { output.close(); } catch (IOException e) { e.printStackTrace(); } } } //Dialogs.showMessageDialog("Configuration file: " + CipresUtilities.getConfigFilePath() + " has been created for you. \n" // + "USERNAME and PASSWORD fields need to be defined in " + CipresUtilities.getConfigFilePath() + ".\n" // + "Please create the account at 'https://www.phylo.org/restusers' and update the above mentioned file and relaunch the application."); //return false; } Properties prop = new Properties(); InputStream input = null; try { input = new FileInputStream(CipresUtilities.getConfigFilePath()); // load a properties file prop.load(input); username = prop.getProperty("USERNAME"); password = prop.getProperty("PASSWORD"); appKey = prop.getProperty("APPID"); appName = prop.getProperty("APPNAME"); } catch (IOException ex) { ex.printStackTrace(); } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } if(!(appName.equals(CipresUtilities.APPNAME) && appKey.startsWith(CipresUtilities.APPNAME))){ Dialogs.showMessageDialog("APPNAME is not defined correctly in " + CipresUtilities.getConfigFilePath() + ".\t" + appName + "\t" + CipresUtilities.APPNAME); return false; } if(CipresUtilities.checkEmptyString(username) || CipresUtilities.checkEmptyString(password) || CipresUtilities.checkEmptyString(appKey)){ Dialogs.showMessageDialog("USERNAME, and PASSWORD fields need to be defined in " + CipresUtilities.getConfigFilePath() + ".\n" + "Please create the account at 'https://www.phylo.org/restusers' and update the above mentioned file and relaunch the application."); return false; } System.setProperty("PYCIPRES_FILE", CipresUtilities.CONFIGFILE); CiApplication app = CiApplication.getInstance(); DataHandlingUtilities handler = DataHandlingUtilities.getInstance(); String url = app.getRestUrl(); user = new UserModel(username, password, url, appKey, appName); handler.addUser(user); myClient = handler.getClient(); ClientCheckResult result = CipresUtilities.clientCheck(myClient); if (result == ClientCheckResult.UNAUTHORIZED) { Dialogs.showMessageDialog("Login to CIPRES REST failed. Your login credentials(USERNAME/PASSWORD/APPID) for CIPRES REST seem to be incorrect. " + "Please update USERNAME/PASSWORD in " + CipresUtilities.getConfigFilePath() + "."); return false; } else if (result == ClientCheckResult.FALSE){ Dialogs.showMessageDialog("Please make sure you have correct URL in " + CipresUtilities.getConfigFilePath() + "."); return false; } userLoggedIn = true; } return userLoggedIn; } public synchronized CiClient getMyClient() { return myClient; } public synchronized boolean isUserLoggedIn() { return userLoggedIn; } public synchronized UserModel getUserModel() { return user; } }