package org.gcube.dataharvest.dao; import java.util.List; import org.gcube.common.encryption.StringEncrypter; import org.gcube.common.resources.gcore.ServiceEndpoint; import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.gcube.resources.discovery.icclient.ICFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class DatabaseDataExplorer { private static Logger logger = LoggerFactory.getLogger(DatabaseDataExplorer.class); private boolean testMode = false; private boolean productionMode = false; public DatabaseDataExplorer() { } /** * setter method for testMode member. this member is set to TRUE if you want * avoid to use production database during develop/test phase * * @param testMode */ public void setTestMode(boolean testMode) { this.testMode = testMode; } /** * getter method for testMode member. * * @return */ public boolean getTestMode() { return testMode; } public DatabaseConnectionData retrieveDatabaseInfo() throws DaoException { DatabaseConnectionData dcd = null; if ((testMode == false) && (productionMode == false)) { String jdbcUrl = "jdbc:postgresql://"; dcd = new DatabaseConnectionData(); try { SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class); query.addCondition("$resource/Profile/Category/text() eq 'Database'") .addCondition("$resource/Profile/Name/text() eq 'Analytics Board'"); DiscoveryClient client = ICFactory.client(); List list = client.submit(query); if (!list.isEmpty()) { String xml = list.iterator().next(); String server = getItemValueFromXmlString(xml, "Endpoint"); String db = getItemAttributeFromXmlString(xml, "Endpoint", "EntryName"); jdbcUrl += server + "/" + db; dcd.setUrl(jdbcUrl); String user = getItemValueFromXmlString(xml, "Username"); dcd.setUser(user); String password = StringEncrypter.getEncrypter() .decrypt(getItemValueFromXmlString(xml, "Password")); dcd.setPassword(password); logger.debug("Database data found"); } } catch (Exception ex) { logger.error(ex.getLocalizedMessage()); throw new DaoException(ex.getLocalizedMessage(), ex.getCause()); } } else if(testMode) { logger.debug("TEST mode is ON"); dcd = new DatabaseConnectionData("jdbc:postgresql://postgresql-srv-dev.d4science.org:5432/analytics_b_dev", "analytics_b_dev_u", "78cb625303be21b"); } else if(productionMode) { logger.warn("PRODUCTION mode is FORCED ON"); dcd = new DatabaseConnectionData("jdbc:postgresql://postgresql-srv.d4science.org:5432/analytics_board", "analytics_board_u", "b52b64ab07ea0b5"); } return dcd; } private String getItemValueFromXmlString(String xml, String item) { int start = xml.indexOf("<" + item); int begin = xml.indexOf(">", start); int end = xml.indexOf("<", begin); String value = xml.substring(begin + 1, end); return value; } private String getItemAttributeFromXmlString(String xml, String item, String attribute) { int start = xml.indexOf("<" + item); start = xml.indexOf(attribute, start); int begin = xml.indexOf("\"", start); int end = xml.indexOf("\"", begin + 1); String attributeValue = xml.substring(begin + 1, end); return attributeValue; } }