added new methods, checked other ones
This commit is contained in:
parent
ba56fba7d3
commit
ec3e8dc4ab
2
pom.xml
2
pom.xml
|
@ -70,7 +70,7 @@
|
|||
<dependency>
|
||||
<groupId>org.gcube.data-publishing</groupId>
|
||||
<artifactId>gcat-client</artifactId>
|
||||
<version>[1.0.0, 2.0.0-SNAPSHOT)</version>
|
||||
<version>[1.0.0, 2.0.0)</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
|
|
|
@ -1,140 +0,0 @@
|
|||
//package org.gcube.datacatalogue.ckanutillibrary.ckan;
|
||||
//
|
||||
//import static com.google.common.base.Preconditions.checkArgument;
|
||||
//import static com.google.common.base.Preconditions.checkNotNull;
|
||||
//
|
||||
//import org.json.simple.JSONArray;
|
||||
//import org.json.simple.JSONObject;
|
||||
//import org.slf4j.Logger;
|
||||
//import org.slf4j.LoggerFactory;
|
||||
//
|
||||
//import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpResponse;
|
||||
//import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpStatus;
|
||||
//import eu.trentorise.opendata.jackan.internal.org.apache.http.client.methods.HttpPost;
|
||||
//import eu.trentorise.opendata.jackan.internal.org.apache.http.entity.StringEntity;
|
||||
//import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.CloseableHttpClient;
|
||||
//import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.HttpClientBuilder;
|
||||
//
|
||||
///**
|
||||
// * The Class DirectCkanCaller.
|
||||
// *
|
||||
// * @author Francesco Mangiacrapa at ISTI-CNR Pisa (Italy)
|
||||
// * Jun 3, 2020
|
||||
// */
|
||||
//public class DirectCkanCaller {
|
||||
//
|
||||
// private static final String PATH_PACKAGE_PATCH = "/api/3/action/package_patch";
|
||||
// public final static String PATH_SET_PRIVATE_DATASET = "/api/3/action/bulk_update_private";
|
||||
// public final static String PATH_SET_PUBLIC_DATASET = "/api/3/action/bulk_update_public";
|
||||
// private String catalogueURL;
|
||||
//
|
||||
// private static final Logger LOG = LoggerFactory.getLogger(DirectCkanCaller.class);
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * Instantiates a new direct ckan caller.
|
||||
// *
|
||||
// * @param catalogueURL the catalogue URL
|
||||
// */
|
||||
// public DirectCkanCaller(String catalogueURL){
|
||||
// this.catalogueURL = catalogueURL;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Set dataset private/public.
|
||||
// *
|
||||
// * @param priv the priv
|
||||
// * @param organizationId (NOTE: The ID, not the name!)
|
||||
// * @param datasetId (NOTE: The ID, not the name!)
|
||||
// * @param apiKey the user's api key
|
||||
// * @return true on success, false otherwise
|
||||
// */
|
||||
// public boolean setDatasetPrivate(boolean priv, String organizationId,
|
||||
// String datasetId, String apiKey) {
|
||||
//
|
||||
// // checks
|
||||
// checkNotNull(organizationId);
|
||||
// checkNotNull(apiKey);
|
||||
// checkNotNull(datasetId);
|
||||
// checkArgument(!apiKey.isEmpty());
|
||||
// checkArgument(!datasetId.isEmpty());
|
||||
// checkArgument(!organizationId.isEmpty());
|
||||
//
|
||||
// JSONObject obj = new JSONObject();
|
||||
// obj.put("org_id", organizationId);
|
||||
//
|
||||
// JSONArray array = new JSONArray();
|
||||
// array.add(datasetId);
|
||||
// obj.put("datasets", array);
|
||||
//
|
||||
// try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();) {
|
||||
// HttpPost request;
|
||||
//
|
||||
// if(priv)
|
||||
// request = new HttpPost(catalogueURL + PATH_SET_PRIVATE_DATASET);
|
||||
// else
|
||||
// request = new HttpPost(catalogueURL + PATH_SET_PUBLIC_DATASET);
|
||||
//
|
||||
// LOG.info("Excuting request for making dataset with id " + datasetId + " " + (priv? "private" : "public"));
|
||||
//
|
||||
// request.addHeader("Authorization", apiKey);
|
||||
// StringEntity params = new StringEntity(obj.toJSONString());
|
||||
// request.setEntity(params);
|
||||
// HttpResponse response = httpClient.execute(request);
|
||||
//
|
||||
// LOG.info("Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
|
||||
//
|
||||
// if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
|
||||
// return true;
|
||||
//
|
||||
// }catch (Exception ex) {
|
||||
// LOG.error("Error while trying to set private the dataset ", ex);
|
||||
// }
|
||||
//
|
||||
//
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * MOVED INTO gCatCaller
|
||||
// *
|
||||
// * Sets the searchable field.
|
||||
// *
|
||||
// * @param datasetId the dataset id
|
||||
// * @param searchable the searchable
|
||||
// * @return true, if successful
|
||||
// *
|
||||
// public boolean setSearchableField(String datasetId, boolean searchable, String apiKey) {
|
||||
//
|
||||
// // checks
|
||||
// checkNotNull(datasetId);
|
||||
// checkArgument(!datasetId.isEmpty());
|
||||
// String searchableAsString = searchable ? "True" : "False";
|
||||
//
|
||||
// // Patch package path
|
||||
// String patchPackage = catalogueURL + PATH_PACKAGE_PATCH;
|
||||
//
|
||||
// JSONObject obj = new JSONObject();
|
||||
// obj.put("id", datasetId);
|
||||
// obj.put("searchable", searchableAsString);
|
||||
//
|
||||
// try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();) {
|
||||
// HttpPost request = new HttpPost(patchPackage);
|
||||
// request.addHeader("Authorization", apiKey);
|
||||
// StringEntity params = new StringEntity(obj.toJSONString());
|
||||
// request.setEntity(params);
|
||||
// HttpResponse response = httpClient.execute(request);
|
||||
// LOG.debug("Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
|
||||
//
|
||||
// if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
|
||||
// return true;
|
||||
//
|
||||
// }catch (Exception ex) {
|
||||
// LOG.error("Error while trying to set searchable the dataset ", ex);
|
||||
// }
|
||||
//
|
||||
// return false;
|
||||
// }*/
|
||||
//
|
||||
//
|
||||
//}
|
|
@ -27,13 +27,13 @@ import eu.trentorise.opendata.jackan.model.CkanGroup;
|
|||
import eu.trentorise.opendata.jackan.model.CkanOrganization;
|
||||
import eu.trentorise.opendata.jackan.model.CkanResponse;
|
||||
|
||||
|
||||
/**
|
||||
* The Class SimpleExtendCkanClient.
|
||||
* The Class ExtendCkanClient.
|
||||
*
|
||||
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
|
||||
*
|
||||
* Jun 7, 2019
|
||||
*
|
||||
* Feb 9, 2021
|
||||
*/
|
||||
public class ExtendCkanClient extends CkanClient implements PatchedCkan{
|
||||
|
||||
|
@ -46,6 +46,13 @@ public class ExtendCkanClient extends CkanClient implements PatchedCkan{
|
|||
|
||||
private static final Map<String, ObjectMapper> OBJECT_MAPPERS_FOR_POSTING = new HashMap();
|
||||
|
||||
/**
|
||||
* The Enum HTTP_METHOD.
|
||||
*
|
||||
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
|
||||
*
|
||||
* Feb 9, 2021
|
||||
*/
|
||||
private static enum HTTP_METHOD {
|
||||
GET, POST
|
||||
}
|
||||
|
@ -123,6 +130,22 @@ public class ExtendCkanClient extends CkanClient implements PatchedCkan{
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the organization.
|
||||
*
|
||||
* @param idOrName the id or name
|
||||
* @param includeUsers the include users
|
||||
* @return the organization
|
||||
*/
|
||||
public synchronized CkanOrganization getOrganization(String idOrName, boolean includeUsers) {
|
||||
checkNotNull(idOrName, "Need a valid id or name!");
|
||||
logger.debug("Patched read organization for id/name: {}", idOrName);
|
||||
return getHttp(OrganizationResponse.class, "/api/3/action/organization_show", "id", idOrName,
|
||||
"include_datasets", "false", "include_users", ""+includeUsers+"").result;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the group.
|
||||
*
|
||||
|
@ -140,6 +163,21 @@ public class ExtendCkanClient extends CkanClient implements PatchedCkan{
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the group.
|
||||
*
|
||||
* @param idOrName the id or name
|
||||
* @param includeUsers the include users
|
||||
* @return the group
|
||||
*/
|
||||
public synchronized CkanGroup getGroup(String idOrName, boolean includeUsers) {
|
||||
checkNotNull(idOrName, "Need a valid id or name!");
|
||||
logger.debug("Patched read group for id/name: {}", idOrName);
|
||||
return getHttp(GroupResponse.class, "/api/3/action/group_show", "id", idOrName, "include_datasets",
|
||||
"false", "include_users", ""+includeUsers+"").result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the Jackson object mapper for reading operations. Internally,
|
||||
* Object mapper is initialized at first call.
|
||||
|
@ -158,9 +196,9 @@ public class ExtendCkanClient extends CkanClient implements PatchedCkan{
|
|||
* Retrieves the Jackson object mapper configured for creation/update
|
||||
* operations. Internally, Object mapper is initialized at first call.
|
||||
*
|
||||
* @param clazz
|
||||
* the class you want to post. For generic class, just put
|
||||
* @param clazz the class you want to post. For generic class, just put
|
||||
* Object.class
|
||||
* @return the object mapper for posting
|
||||
* @since 0.4.1
|
||||
*/
|
||||
static ObjectMapper getObjectMapperForPosting(Class clazz) {
|
||||
|
|
|
@ -15,22 +15,23 @@ import org.gcube.datacatalogue.ckanutillibrary.shared.RolesCkanGroupOrOrg;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
// TODO: Auto-generated Javadoc
|
||||
|
||||
/**
|
||||
* The Class DBCaller.
|
||||
*
|
||||
* @author Francesco Mangiacrapa at ISTI-CNR Pisa (Italy)
|
||||
* Jun 1, 2020
|
||||
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
|
||||
*
|
||||
* Feb 9, 2021
|
||||
*/
|
||||
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;
|
||||
private String ckanDBURL;
|
||||
private Integer ckanDBPort;
|
||||
private String ckanDBName;
|
||||
private String ckanDBUser;
|
||||
private String ckanDBPwd;
|
||||
|
||||
/**
|
||||
* Instantiates a new DB caller.
|
||||
|
@ -38,22 +39,23 @@ public class DBCaller {
|
|||
public DBCaller() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Instantiates a new DB caller.
|
||||
*
|
||||
* @param cKAN_DB_URL the c KA N D B URL
|
||||
* @param cKAN_DB_PORT the c KA N D B PORT
|
||||
* @param cKAN_DB_NAME the c KA N D B NAME
|
||||
* @param cKAN_DB_USER the c KA N D B USER
|
||||
* @param cKAN_DB_PASSWORD the c KA N D B PASSWORD
|
||||
* @param ckanDBURL the ckan DBURL
|
||||
* @param ckanDBPort the ckan DB port
|
||||
* @param ckanDBName the ckan DB name
|
||||
* @param ckanDBUser the ckan DB user
|
||||
* @param ckanDBPwd the ckan DB pwd
|
||||
*/
|
||||
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;
|
||||
public DBCaller(String ckanDBURL, Integer ckanDBPort, String ckanDBName, String ckanDBUser,
|
||||
String ckanDBPwd) {
|
||||
this.ckanDBURL = ckanDBURL;
|
||||
this.ckanDBPort = ckanDBPort;
|
||||
this.ckanDBName = ckanDBName;
|
||||
this.ckanDBUser = ckanDBUser;
|
||||
this.ckanDBPwd = ckanDBPwd;
|
||||
}
|
||||
|
||||
|
||||
|
@ -70,17 +72,16 @@ public class DBCaller {
|
|||
// create db connection
|
||||
Class.forName("org.postgresql.Driver");
|
||||
|
||||
String dbBaseConnectionURL = String.format("jdbc:postgresql://%s", CKAN_DB_URL);
|
||||
String dbBaseConnectionURL = String.format("jdbc:postgresql://%s", ckanDBURL);
|
||||
|
||||
if(CKAN_DB_PORT!=null)
|
||||
dbBaseConnectionURL+=":" + CKAN_DB_PORT;
|
||||
if(ckanDBPort!=null)
|
||||
dbBaseConnectionURL+=":" + ckanDBPort;
|
||||
|
||||
dbBaseConnectionURL+="/" + CKAN_DB_NAME;
|
||||
dbBaseConnectionURL+="/" + ckanDBName;
|
||||
|
||||
LOG.debug("DB CONNECTION URL: "+dbBaseConnectionURL);
|
||||
LOG.debug("CKAN_DB_USER: "+CKAN_DB_USER);
|
||||
LOG.debug("CKAN_DB_PASSWORD: "+CKAN_DB_PASSWORD);
|
||||
Connection connection = DriverManager.getConnection(dbBaseConnectionURL, CKAN_DB_USER, CKAN_DB_PASSWORD);
|
||||
LOG.debug("CKAN_DB_USER: "+ckanDBUser);
|
||||
Connection connection = DriverManager.getConnection(dbBaseConnectionURL, ckanDBUser, ckanDBPwd);
|
||||
|
||||
LOG.trace("Returnig db connection");
|
||||
return connection;
|
||||
|
|
|
@ -12,7 +12,6 @@ import eu.trentorise.opendata.jackan.model.CkanGroup;
|
|||
import eu.trentorise.opendata.jackan.model.CkanLicense;
|
||||
import eu.trentorise.opendata.jackan.model.CkanOrganization;
|
||||
|
||||
// TODO: Auto-generated Javadoc
|
||||
/**
|
||||
* The Interface DataCatalogue.
|
||||
*
|
||||
|
@ -189,6 +188,32 @@ public interface DataCatalogue {
|
|||
*/
|
||||
boolean checkValidUser(String username);
|
||||
|
||||
/**
|
||||
* Check if this role is present for this user in the organization. If he/she is not present we need to add it with the given role.
|
||||
* @param username
|
||||
* @param organizationName
|
||||
* @param correspondentRoleToCheck
|
||||
* @return true if the role can be set, false if it cannot
|
||||
*/
|
||||
boolean checkRoleIntoOrganization(String username, String organizationName, RolesCkanGroupOrOrg correspondentRoleToCheck);
|
||||
|
||||
/**
|
||||
* Check if this role is present for this user in the group. If he/she is not present we need to add it with the given role.
|
||||
* @param username
|
||||
* @param organizationName
|
||||
* @param correspondentRoleToCheck
|
||||
* @return true if the role can be set, false if it cannot
|
||||
*/
|
||||
boolean checkRoleIntoGroup(String username, String groupName, RolesCkanGroupOrOrg correspondentRoleToCheck);
|
||||
|
||||
/**
|
||||
* Returns the main landing pages for this catalogue (i.e. type, orgs, groups and items pages)
|
||||
*
|
||||
* @return the landing pages
|
||||
* @throws Exception the exception
|
||||
*/
|
||||
LandingPages getLandingPages() throws Exception;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
@ -269,13 +294,6 @@ public interface DataCatalogue {
|
|||
*/
|
||||
boolean deleteResourceFromDataset(String resourceId) throws Exception;
|
||||
|
||||
/**
|
||||
* Returns the main landing pages for this catalogue (i.e. type, orgs, groups and items pages)
|
||||
*
|
||||
* @return the landing pages
|
||||
* @throws Exception the exception
|
||||
*/
|
||||
LandingPages getLandingPages() throws Exception;
|
||||
|
||||
/**
|
||||
* Create a CkanGroup.
|
||||
|
|
|
@ -32,6 +32,7 @@ import org.slf4j.LoggerFactory;
|
|||
|
||||
import eu.trentorise.opendata.jackan.exceptions.JackanException;
|
||||
import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpResponse;
|
||||
import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpStatus;
|
||||
import eu.trentorise.opendata.jackan.internal.org.apache.http.client.methods.HttpPost;
|
||||
import eu.trentorise.opendata.jackan.internal.org.apache.http.entity.ContentType;
|
||||
import eu.trentorise.opendata.jackan.internal.org.apache.http.entity.StringEntity;
|
||||
|
@ -439,11 +440,21 @@ public class DataCatalogueImpl implements DataCatalogue {
|
|||
|
||||
try{
|
||||
|
||||
|
||||
Map<String, RolesCkanGroupOrOrg> partialResult = dbCaller.getOrganizationsByUserFromDB(ckanUsername);
|
||||
|
||||
for (String orgID : partialResult.keySet()) {
|
||||
|
||||
CkanOrganization org = ckanCaller.getOrganization(orgID,false);
|
||||
LOG.debug("User " + ckanUsername + " is into " + org.getName());
|
||||
toReturn.add(org);
|
||||
}
|
||||
|
||||
// get the list of all organizations
|
||||
List<CkanOrganization> organizations = ckanCaller.getOrganizationList();
|
||||
//List<CkanOrganization> organizations = ckanCaller.getOrganizationList();
|
||||
|
||||
// iterate over them
|
||||
for (CkanOrganization ckanOrganization : organizations) {
|
||||
/*for (CkanOrganization ckanOrganization : organizations) {
|
||||
// get the list of users in it (if you try ckanOrganization.getUsers() it returns null.. maybe a bug TODO)
|
||||
List<CkanUser> users = ckanCaller.getOrganization(ckanOrganization.getName()).getUsers();
|
||||
// check if the current user is among them
|
||||
|
@ -457,7 +468,7 @@ public class DataCatalogueImpl implements DataCatalogue {
|
|||
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}catch(Exception e){
|
||||
LOG.error("Unable to get user's organizations", e);
|
||||
}
|
||||
|
@ -477,6 +488,16 @@ public class DataCatalogueImpl implements DataCatalogue {
|
|||
|
||||
try{
|
||||
|
||||
Map<String, RolesCkanGroupOrOrg> partialResult = dbCaller.getGroupsByUserFromDB(ckanUsername);
|
||||
|
||||
for (String groupID : partialResult.keySet()) {
|
||||
|
||||
CkanGroup group = ckanCaller.getGroup(groupID,false);
|
||||
LOG.debug("User " + ckanUsername + " is into " + group.getName());
|
||||
toReturn.add(group);
|
||||
}
|
||||
|
||||
/*
|
||||
// get the list of all organizations
|
||||
List<CkanGroup> groups = ckanCaller.getGroupList();
|
||||
|
||||
|
@ -496,7 +517,7 @@ public class DataCatalogueImpl implements DataCatalogue {
|
|||
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}catch(Exception e){
|
||||
LOG.error("Unable to get user's groups", e);
|
||||
}
|
||||
|
@ -665,6 +686,149 @@ public class DataCatalogueImpl implements DataCatalogue {
|
|||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean checkRoleIntoOrganization(String username, String organizationName,
|
||||
RolesCkanGroupOrOrg correspondentRoleToCheck) {
|
||||
|
||||
LOG.debug("Request for checking if " + username + " into organization " + organizationName + " has role " + correspondentRoleToCheck);
|
||||
|
||||
// checks
|
||||
checkNotNull(username);
|
||||
checkNotNull(organizationName);
|
||||
checkNotNull(correspondentRoleToCheck);
|
||||
checkArgument(!username.isEmpty());
|
||||
checkArgument(!organizationName.isEmpty());
|
||||
|
||||
// convert ckan username
|
||||
String ckanUsername = CatalogueUtilMethods.fromUsernameToCKanUsername(username);
|
||||
|
||||
// check if this role is already present in ckan for this user within the organization
|
||||
String organizationNameToCheck = organizationName.toLowerCase();
|
||||
|
||||
try{
|
||||
boolean alreadyPresent = isRoleAlreadySet(ckanUsername, organizationNameToCheck, correspondentRoleToCheck, false);
|
||||
|
||||
if(alreadyPresent)
|
||||
return true; // just return
|
||||
else{
|
||||
|
||||
//TODO PASS by GCAT?
|
||||
// we need to use the APIs to make it
|
||||
String path = "/api/3/action/organization_member_create";
|
||||
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("id", organizationNameToCheck);
|
||||
obj.put("username", ckanUsername);
|
||||
obj.put("role", RolesCkanGroupOrOrg.convertToCkanCapacity(correspondentRoleToCheck));
|
||||
|
||||
LOG.debug("API request for organization membership is going to be " + obj.toJSONString());
|
||||
|
||||
|
||||
try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();) {
|
||||
HttpPost request = new HttpPost(CKAN_CATALOGUE_URL + path);
|
||||
request.addHeader("Authorization", CKAN_TOKEN_SYS); // sys token
|
||||
StringEntity params = new StringEntity(obj.toJSONString());
|
||||
request.setEntity(params);
|
||||
HttpResponse response = httpClient.execute(request);
|
||||
LOG.debug("Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
|
||||
|
||||
return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
|
||||
|
||||
}catch (Exception ex) {
|
||||
LOG.error("Error while trying to change the role for this user ", ex);
|
||||
}
|
||||
}
|
||||
}catch (Exception ex) {
|
||||
LOG.error("Unable to check if this role was already set, please check your parameters! ", ex);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkRoleIntoGroup(String username, String groupName, RolesCkanGroupOrOrg correspondentRoleToCheck) {
|
||||
LOG.debug("Request for checking if " + username + " into group " + groupName + " has role " + correspondentRoleToCheck);
|
||||
|
||||
// checks
|
||||
checkNotNull(username);
|
||||
checkNotNull(groupName);
|
||||
checkNotNull(correspondentRoleToCheck);
|
||||
checkArgument(!username.isEmpty());
|
||||
checkArgument(!groupName.isEmpty());
|
||||
|
||||
// convert ckan username
|
||||
String ckanUsername = CatalogueUtilMethods.fromUsernameToCKanUsername(username);
|
||||
|
||||
// check if this role is already present in ckan for this user within the group
|
||||
String groupNameToCheck = CatalogueUtilMethods.fromGroupTitleToName(groupName);
|
||||
|
||||
try{
|
||||
boolean alreadyPresent = isRoleAlreadySet(ckanUsername, groupNameToCheck, correspondentRoleToCheck, true);
|
||||
|
||||
if(alreadyPresent)
|
||||
return true; // just return
|
||||
else{
|
||||
|
||||
// we need to use the apis to make it
|
||||
String path = "/api/3/action/group_member_create";
|
||||
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("id", groupNameToCheck);
|
||||
obj.put("username", ckanUsername);
|
||||
obj.put("role", RolesCkanGroupOrOrg.convertToCkanCapacity(correspondentRoleToCheck));
|
||||
|
||||
LOG.debug("API request for organization membership is going to be " + obj.toJSONString());
|
||||
|
||||
try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();) {
|
||||
HttpPost request = new HttpPost(CKAN_CATALOGUE_URL + path);
|
||||
request.addHeader("Authorization", CKAN_TOKEN_SYS); // sys token
|
||||
StringEntity params = new StringEntity(obj.toJSONString());
|
||||
request.setEntity(params);
|
||||
HttpResponse response = httpClient.execute(request);
|
||||
LOG.debug("Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
|
||||
|
||||
return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
|
||||
|
||||
}catch (Exception ex) {
|
||||
LOG.error("Error while trying to change the role for this user ", ex);
|
||||
}
|
||||
}
|
||||
}catch (Exception ex) {
|
||||
LOG.error("Unable to check if this role was already set, please check your parameters! ", ex);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the user has this role into the organization/group with groupOrOrganization name
|
||||
* @param ckanUsername
|
||||
* @param organizationName
|
||||
* @param correspondentRoleToCheck
|
||||
* @return true if he has the role, false otherwise
|
||||
*/
|
||||
protected boolean isRoleAlreadySet(String ckanUsername, String groupOrOrganization, RolesCkanGroupOrOrg correspondentRoleToCheck, boolean group) throws Exception{
|
||||
|
||||
// get the users (if you try ckanOrganization.getUsers() it returns null.. maybe a bug TODO)
|
||||
List<CkanUser> users;
|
||||
|
||||
if(group)
|
||||
users = ckanCaller.getGroup(groupOrOrganization).getUsers();
|
||||
else
|
||||
users = ckanCaller.getOrganization(groupOrOrganization).getUsers();
|
||||
|
||||
for (CkanUser ckanUser : users) {
|
||||
if(ckanUser.getName().equals(ckanUsername))
|
||||
if(ckanUser.getCapacity().equals(RolesCkanGroupOrOrg.convertToCkanCapacity(correspondentRoleToCheck)))
|
||||
return true;
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
*
|
||||
|
@ -802,21 +966,6 @@ public class DataCatalogueImpl implements DataCatalogue {
|
|||
|
||||
if(CatalogueUtilMethods.resourceExists(resourceBean.getUrl())){
|
||||
|
||||
/*// in order to avoid errors, the username is always converted
|
||||
String ckanUsername = CatalogueUtilMethods.fromUsernameToCKanUsername(resourceBean.getOwner());
|
||||
|
||||
CkanResource resource = new CkanResource(CKAN_CATALOGUE_URL, resourceBean.getDatasetId());
|
||||
resource.setName(resourceBean.getName());
|
||||
|
||||
// escape description
|
||||
Source description = new Source(resourceBean.getDescription());
|
||||
Segment htmlSeg = new Segment(description, 0, description.length());
|
||||
Renderer htmlRend = new Renderer(htmlSeg);
|
||||
|
||||
resource.setDescription(htmlRend.toString());
|
||||
resource.setUrl(resourceBean.getUrl());
|
||||
resource.setOwner(ckanUsername);*/
|
||||
|
||||
CkanResource resource = CKANConveter.toCkanResource(CKAN_CATALOGUE_URL, resourceBean);
|
||||
|
||||
String jsonValueResource = MarshUnmarshCkanObject.toJsonValueResource(resource);
|
||||
|
@ -914,135 +1063,5 @@ public class DataCatalogueImpl implements DataCatalogue {
|
|||
return toReturn;
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public boolean patchProductCustomFields(String productId, Map<String, List<String>> customFieldsToChange,
|
||||
// boolean removeOld) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
|
||||
// @Override
|
||||
public boolean assignDatasetToGroup(String groupNameOrId, String datasetNameOrId) {
|
||||
|
||||
return assignDatasetToGroupBody(groupNameOrId, datasetNameOrId, "", false);
|
||||
|
||||
}
|
||||
|
||||
// @Override
|
||||
public boolean assignDatasetToGroup(String groupNameOrId, String datasetNameOrId, boolean addOnParents) {
|
||||
|
||||
return assignDatasetToGroupBody(groupNameOrId, datasetNameOrId, "", addOnParents);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* The real body of the assignDatasetToGroup
|
||||
* @param groupNameOrId
|
||||
* @param datasetNameOrId
|
||||
* @param apiKey
|
||||
* @param addOnParents
|
||||
* @return
|
||||
*/
|
||||
private boolean assignDatasetToGroupBody(String groupNameOrId, String datasetNameOrId, String apiKey, boolean addOnParents) {
|
||||
|
||||
return false;
|
||||
// // checks
|
||||
// checkNotNull(groupNameOrId);
|
||||
// checkArgument(!groupNameOrId.isEmpty());
|
||||
// checkNotNull(datasetNameOrId);
|
||||
// checkArgument(!datasetNameOrId.isEmpty());
|
||||
// checkNotNull(apiKey);
|
||||
// checkArgument(!apiKey.isEmpty());
|
||||
//
|
||||
// String groupNameToCheck = CatalogueUtilMethods.fromGroupTitleToName(groupNameOrId);
|
||||
//
|
||||
// try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();){
|
||||
//
|
||||
// ExtendCkanClient client = new ExtendCkanClient(CKAN_CATALOGUE_URL, CKAN_TOKEN_SYS);
|
||||
//
|
||||
// // check the group exists
|
||||
// CkanGroup group = client.getGroup(groupNameToCheck);
|
||||
//
|
||||
// // move to a list
|
||||
// List<String> groupNames = new ArrayList<String>();
|
||||
// groupNames.add(group.getName());
|
||||
// if(group != null && addOnParents){
|
||||
// findHierarchyGroups(groupNames, CKAN_TOKEN_SYS);
|
||||
// }
|
||||
//
|
||||
// // we need to use the apis to make it
|
||||
// String pathPackageShow = CKAN_CATALOGUE_URL + "/api/3/action/package_show?id=" + datasetNameOrId;
|
||||
// HttpGet getRequest = new HttpGet(pathPackageShow);
|
||||
// getRequest.addHeader("Authorization", CKAN_TOKEN_SYS);
|
||||
// HttpResponse response = httpClient.execute(getRequest);
|
||||
//
|
||||
// logger.debug("Response is " + response.getStatusLine().getStatusCode() + " and message is " + response.getStatusLine().getReasonPhrase());
|
||||
//
|
||||
// // read the json dataset and fetch the groups and fetch the groups' names, if any
|
||||
// if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
|
||||
//
|
||||
// // parse the json and convert to java beans
|
||||
// String jsonAsString = EntityUtils.toString(response.getEntity());
|
||||
// JSONParser parser = new JSONParser();
|
||||
// JSONObject json = (JSONObject) parser.parse(jsonAsString);
|
||||
// JSONObject resultJson = (JSONObject) json.get("result");
|
||||
// JSONArray groupsJson = (JSONArray)resultJson.get("groups");
|
||||
// Iterator<JSONObject> it = groupsJson.iterator();
|
||||
//
|
||||
// while (it.hasNext()) {
|
||||
// JSONObject object = it.next();
|
||||
// try{
|
||||
// if(object.containsKey("name"))
|
||||
// groupNames.add((String)object.get("name"));
|
||||
// }catch(Exception e){
|
||||
// logger.error("Error", e);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // remove duplicates
|
||||
// Set<String> groupNamesSet = new HashSet<String>(groupNames);
|
||||
//
|
||||
// logger.debug("Groups to be added are " + groupNamesSet);
|
||||
//
|
||||
// // now we patch the dataset with the new group
|
||||
// String pathUpdatePatch = CKAN_CATALOGUE_URL + "/api/3/action/package_patch";
|
||||
//
|
||||
// JSONObject req = new JSONObject();
|
||||
// req.put("id", datasetNameOrId);
|
||||
//
|
||||
// JSONArray groups = new JSONArray();
|
||||
// Iterator<String> iteratorNameSet = groupNamesSet.iterator();
|
||||
// while (iteratorNameSet.hasNext()) {
|
||||
// String groupName = iteratorNameSet.next();
|
||||
// JSONObject groupJSON = new JSONObject();
|
||||
// groupJSON.put("name", groupName);
|
||||
// groups.add(groupJSON);
|
||||
// }
|
||||
// req.put("groups", groups);
|
||||
//
|
||||
// logger.debug("Request for patch is going to be " + req.toJSONString());
|
||||
//
|
||||
// HttpPost request = new HttpPost(pathUpdatePatch);
|
||||
// request.addHeader("Authorization", CKAN_TOKEN_SYS);
|
||||
// StringEntity params = new StringEntity(req.toJSONString());
|
||||
// request.setEntity(params);
|
||||
// HttpResponse responsePatch = httpClient.execute(request);
|
||||
// logger.debug("Response code is " + responsePatch.getStatusLine().getStatusCode() + " and response message is " + responsePatch.getStatusLine().getReasonPhrase());
|
||||
//
|
||||
// if(responsePatch.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
|
||||
// logger.info("Dataset Added to the group!!");
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// }catch(Exception e){
|
||||
// logger.error("Unable to make this association", e);
|
||||
// }
|
||||
//
|
||||
// return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -76,6 +76,7 @@ public class CKANConveter {
|
|||
|
||||
LOG.debug("Name of the dataset is going to be " + nameToUse + ". Title is going to be " + title);
|
||||
|
||||
//TODO the name is required by gCat?
|
||||
dataset.setName(nameToUse);
|
||||
dataset.setTitle(title);
|
||||
|
||||
|
|
|
@ -20,6 +20,8 @@ public class Statistics implements Serializable{
|
|||
private long numGroups;
|
||||
private long numItems;
|
||||
|
||||
public Statistics() {}
|
||||
|
||||
public long getNumTypes() {
|
||||
return numTypes;
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ import eu.trentorise.opendata.jackan.model.CkanUser;
|
|||
*/
|
||||
public class TestDataCatalogueLib {
|
||||
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestDataCatalogueLib.class);
|
||||
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(TestDataCatalogueLib.class);
|
||||
|
||||
private DataCatalogueFactory factory;
|
||||
private String scope = "/gcube/devsec/devVRE";
|
||||
|
@ -89,12 +89,12 @@ public class TestDataCatalogueLib {
|
|||
ScopeProvider.instance.set(scope);
|
||||
String url = "https://dev4.d4science.org/group/devvre/ckan";
|
||||
String scopeToUse = ApplicationProfileScopePerUrlReader.getScopePerUrl(url);
|
||||
logger.debug("Retrieved scope is " + scopeToUse);
|
||||
LOG.debug("Retrieved scope is " + scopeToUse);
|
||||
|
||||
ScopeProvider.instance.reset(); // the following sysout should print null
|
||||
String url2 = "https://dev4.d4science.org/group/devvre/ckan";
|
||||
String scopeToUse2 = ApplicationProfileScopePerUrlReader.getScopePerUrl(url2);
|
||||
logger.debug("Retrieved scope is " + scopeToUse2);
|
||||
LOG.debug("Retrieved scope is " + scopeToUse2);
|
||||
}
|
||||
|
||||
|
||||
|
@ -111,7 +111,7 @@ public class TestDataCatalogueLib {
|
|||
long init = System.currentTimeMillis();
|
||||
instance.getUserRoleByGroup(username);
|
||||
long end = System.currentTimeMillis();
|
||||
logger.debug("Time taken " + (end - init));
|
||||
LOG.debug("Time taken " + (end - init));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -127,7 +127,7 @@ public class TestDataCatalogueLib {
|
|||
long init = System.currentTimeMillis();
|
||||
instance.getUserRoleByOrganization(username);
|
||||
long end = System.currentTimeMillis();
|
||||
logger.debug("Time taken " + (end - init));
|
||||
LOG.debug("Time taken " + (end - init));
|
||||
}
|
||||
|
||||
//@Test
|
||||
|
@ -137,7 +137,7 @@ public class TestDataCatalogueLib {
|
|||
DataCatalogueImpl utils = factory.getUtilsPerScope(scope);
|
||||
CkanOrganization org = utils.getOrganizationByIdOrName(orgName);
|
||||
String role = utils.getRoleOfUserInOrganization(testUser, org.getName());
|
||||
logger.debug("The user "+testUser+" in the org "+org.getName() + " has the role "+role);
|
||||
LOG.debug("The user "+testUser+" in the org "+org.getName() + " has the role "+role);
|
||||
}
|
||||
|
||||
//@Test
|
||||
|
@ -149,21 +149,51 @@ public class TestDataCatalogueLib {
|
|||
|
||||
CkanOrganization org = utils.getOrganizationByIdOrName(orgName);
|
||||
|
||||
logger.debug("The "+CkanOrganization.class.getSimpleName()+" is: "+org.getName());
|
||||
logger.debug("LandingPages of "+CkanOrganization.class.getSimpleName()+" for name " + utils.getLandingPages());
|
||||
LOG.debug("The "+CkanOrganization.class.getSimpleName()+" is: "+org.getName());
|
||||
LOG.debug("LandingPages of "+CkanOrganization.class.getSimpleName()+" for name " + utils.getLandingPages());
|
||||
if(org.getUsers()!=null) {
|
||||
|
||||
for (CkanUser user : org.getUsers()) {
|
||||
logger.debug("User: "+user.getName());
|
||||
LOG.debug("User: "+user.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//@Test
|
||||
public void getOrganizationsNamesByUser() throws Exception{
|
||||
|
||||
DataCatalogueImpl utils = factory.getUtilsPerScope(scope);
|
||||
|
||||
List<CkanOrganization> listOrgs = utils.getOrganizationsByUser(testUser);
|
||||
|
||||
LOG.debug("User :"+testUser+" found in the Organization/s:");
|
||||
|
||||
for (CkanOrganization ckanOrganization : listOrgs) {
|
||||
LOG.debug("Org: "+ckanOrganization.getName());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//@Test
|
||||
public void getGroupsNamesByUser() throws Exception{
|
||||
|
||||
DataCatalogueImpl utils = factory.getUtilsPerScope(scope);
|
||||
|
||||
List<CkanGroup> listGroups = utils.getGroupsByUser(testUser);
|
||||
|
||||
LOG.debug("User :"+testUser+" found in the Group/s:");
|
||||
|
||||
for (CkanGroup ckanGroup : listGroups) {
|
||||
LOG.debug("Group: "+ckanGroup.getName());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//@Test
|
||||
public void getLandingPages() throws Exception{
|
||||
|
||||
DataCatalogueImpl utils = factory.getUtilsPerScope(scope);
|
||||
logger.debug("Landing pages " + utils.getLandingPages());
|
||||
LOG.debug("Landing pages " + utils.getLandingPages());
|
||||
|
||||
}
|
||||
|
||||
|
@ -185,7 +215,7 @@ public class TestDataCatalogueLib {
|
|||
|
||||
}
|
||||
|
||||
@Test
|
||||
//@Test
|
||||
public void createDataset() throws Exception{
|
||||
|
||||
try {
|
||||
|
@ -214,7 +244,7 @@ public class TestDataCatalogueLib {
|
|||
|
||||
String orgName = scope.split("/")[3].toLowerCase();
|
||||
//System.out.println("Org name by VRE: "+orgName);
|
||||
logger.debug("Org name by VRE: "+orgName);
|
||||
LOG.debug("Org name by VRE: "+orgName);
|
||||
|
||||
int random = new Random().nextInt();
|
||||
String datasetTitle = "a dataset created by catalogue-util-library "+random;
|
||||
|
@ -238,7 +268,7 @@ public class TestDataCatalogueLib {
|
|||
setSearchable,
|
||||
true);
|
||||
|
||||
logger.info(createdDataset);
|
||||
LOG.info(createdDataset);
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -254,7 +284,7 @@ public class TestDataCatalogueLib {
|
|||
String groupTitle = "a grop created by catalogue-util-library "+random;
|
||||
String groupName = groupTitle.replace(" ", "");
|
||||
CkanGroup ckanGroup = instance.createGroup(groupName, groupTitle, "description");
|
||||
logger.info("Created the group: "+ckanGroup);
|
||||
LOG.info("Created the group: "+ckanGroup);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue