catalogue-util-library/src/main/java/org/gcube/datacatalogue/ckanutillibrary/db/DBCaller.java

210 lines
6.0 KiB
Java

package org.gcube.datacatalogue.ckanutillibrary.db;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.gcube.datacatalogue.ckanutillibrary.shared.RolesCkanGroupOrOrg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DBCaller {
private static final Logger LOG = LoggerFactory.getLogger(DBCaller.class);
private String CKAN_DB_URL;
private Integer CKAN_DB_PORT;
private String CKAN_DB_NAME;
private String CKAN_DB_USER;
private String CKAN_DB_PASSWORD;
public DBCaller() {
}
public DBCaller(String cKAN_DB_URL, Integer cKAN_DB_PORT, String cKAN_DB_NAME, String cKAN_DB_USER,
String cKAN_DB_PASSWORD) {
CKAN_DB_URL = cKAN_DB_URL;
CKAN_DB_PORT = cKAN_DB_PORT;
CKAN_DB_NAME = cKAN_DB_NAME;
CKAN_DB_USER = cKAN_DB_USER;
CKAN_DB_PASSWORD = cKAN_DB_PASSWORD;
}
/**
* Retrieve connection from the pool
* @return a connection available within the pool
* @throws SQLException
* @throws ClassNotFoundException
*/
private Connection getConnection() throws SQLException, ClassNotFoundException{
LOG.debug("CONNECTION REQUEST");
// create db connection
Class.forName("org.postgresql.Driver");
String dbBaseConnectionURL = String.format("jdbc:postgresql://%s", CKAN_DB_URL);
if(CKAN_DB_PORT!=null)
dbBaseConnectionURL+=":" + CKAN_DB_PORT;
dbBaseConnectionURL+="/" + CKAN_DB_NAME;
Connection connection = DriverManager.getConnection(dbBaseConnectionURL, CKAN_DB_USER, CKAN_DB_PASSWORD);
// Connection connection = DriverManager.getConnection(
// "jdbc:postgresql://" + CKAN_DB_URL + ":" + CKAN_DB_PORT + "/" + CKAN_DB_NAME, CKAN_DB_USER, CKAN_DB_PASSWORD);
return connection;
}
/**
* Retrieve the map (groups id, capacity) with the groups to which the user belongs by querying directly the database.
* @param username
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public Map<String, RolesCkanGroupOrOrg> getGroupsByUserFromDB(String userId){
checkNotNull(userId);
//couples (groups id, capacity) of the user in the group
Map<String, RolesCkanGroupOrOrg> toReturn = new HashMap<String, RolesCkanGroupOrOrg>();
Connection connection = null;
try{
connection = getConnection();
ResultSet rs;
String joinQuery = "SELECT \"group_id\",\"capacity\" FROM \"public\".\"member\" "
+ "JOIN \"public\".\"group\" ON \"member\".\"group_id\" = \"group\".\"id\" where \"table_id\"=?"
+ " and \"table_name\"='user' and \"member\".\"state\"='active' and \"group\".\"state\"='active' and \"group\".\"is_organization\"=?;";
PreparedStatement preparedStatement = connection.prepareStatement(joinQuery);
preparedStatement.setString(1, userId);
preparedStatement.setBoolean(2, false);
rs = preparedStatement.executeQuery();
while (rs.next()) {
toReturn.put(rs.getString("group_id"), RolesCkanGroupOrOrg.convertFromCapacity(rs.getString("capacity")));
}
}catch(Exception e){
LOG.error("Failed to retrieve the groups to whom the user belongs. Error is " + e.getMessage());
return null;
}finally{
closeConnection(connection);
}
return toReturn;
}
/**
* Retrieve the map (organisation id, capacity) with the organisations to which the user belongs by querying directly the database.
* @param username
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public Map<String, RolesCkanGroupOrOrg> getOrganizationsByUserFromDB(String userId){
checkNotNull(userId);
Map<String, RolesCkanGroupOrOrg> toReturn = new HashMap<String, RolesCkanGroupOrOrg>();
Connection connection = null;
try{
connection = getConnection();
ResultSet rs;
String joinQuery = "SELECT \"group_id\",\"capacity\" FROM \"public\".\"member\" "
+ "JOIN \"public\".\"group\" ON \"member\".\"group_id\" = \"group\".\"id\" where \"table_id\"=?"
+ " and \"table_name\"='user' and \"member\".\"state\"='active' and \"group\".\"state\"='active' and \"group\".\"is_organization\"=?;";
PreparedStatement preparedStatement = connection.prepareStatement(joinQuery);
preparedStatement.setString(1, userId);
preparedStatement.setBoolean(2, true);
rs = preparedStatement.executeQuery();
while (rs.next()) {
toReturn.put(rs.getString("group_id"), RolesCkanGroupOrOrg.convertFromCapacity(rs.getString("capacity")));
}
}catch(Exception e){
LOG.error("Failed to retrieve the groups to whom the user belongs. Error is " + e.getMessage());
return null;
}finally{
closeConnection(connection);
}
return toReturn;
}
public String getApiKeyFromUsername(String username, String state) {
LOG.debug("Request api key for user = " + username);
// checks
checkNotNull(username);
checkArgument(!username.isEmpty());
// the connection
Connection connection = null;
String apiToReturn = null;
try{
connection = getConnection();
String query = "SELECT \"apikey\" FROM \"user\" WHERE \"name\"=? and \"state\"=?;";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, username);
preparedStatement.setString(2, state);
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
apiToReturn = rs.getString("apikey");
LOG.debug("Api key "+apiToReturn.substring(0,10)+" MASKED-TOKEN retrieved for user " + username);
break;
}
return apiToReturn;
}catch(Exception e){
LOG.error("Unable to retrieve key for user " + username, e);
}finally{
closeConnection(connection);
}
return null;
}
/**
* Tries to close a connection
* @param connection
*/
private void closeConnection(Connection connection){
if(connection != null){
try{
connection.close();
}catch(Exception e){
LOG.error("Unable to close this connection ", e);
}
}
}
}