75 lines
2.4 KiB
Java
75 lines
2.4 KiB
Java
package org.gcube.data_catalogue.grsf_publish_ws.utils;
|
|
|
|
import java.util.List;
|
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogue;
|
|
import org.gcube.datacatalogue.ckanutillibrary.models.RolesCkanGroupOrOrg;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
/**
|
|
* Associate the dataset to a group.
|
|
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
|
*/
|
|
public class AssociationToGroupThread extends Thread {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(AssociationToGroupThread.class);
|
|
|
|
private List<String> groupsTitles;
|
|
private String datasetId;
|
|
private String username;
|
|
private DataCatalogue catalogue;
|
|
private String organizationId;
|
|
|
|
/**
|
|
* @param groupTitle
|
|
* @param datasetId
|
|
* @param username
|
|
* @param organizationId
|
|
* @param catalogue
|
|
*/
|
|
public AssociationToGroupThread(List<String> groupsTitles, String datasetId, String organizationId,
|
|
String username, DataCatalogue catalogue) {
|
|
this.groupsTitles = groupsTitles;
|
|
this.datasetId = datasetId;
|
|
this.username = username;
|
|
this.catalogue = catalogue;
|
|
this.organizationId = organizationId;
|
|
}
|
|
|
|
@Override
|
|
public void run() {
|
|
|
|
logger.info("Association thread started to put the dataset with id="+ datasetId + " into group with title(s) " + groupsTitles + " for user " + username);
|
|
|
|
String userApiKey = catalogue.getApiKeyFromUsername(username);
|
|
|
|
// retrieve the role to be assigned according the one the user has into the organization of the dataset
|
|
RolesCkanGroupOrOrg role = RolesCkanGroupOrOrg.valueOf(catalogue.getRoleOfUserInOrganization(username, organizationId, catalogue.getApiKeyFromUsername(username)).toUpperCase());
|
|
|
|
if(!role.equals(RolesCkanGroupOrOrg.ADMIN))
|
|
role = RolesCkanGroupOrOrg.MEMBER; // decrease the role to member if it is not an admin
|
|
|
|
for (String groupTitle : groupsTitles) {
|
|
|
|
logger.debug("Setting role " + role + " into group " + groupTitle + " to user " + username);
|
|
boolean assigned = catalogue.checkRoleIntoGroup(username, groupTitle, role);
|
|
|
|
if(!assigned){
|
|
|
|
logger.warn("The user " + username + " has not enough privileges to associate the dataset into group OR the group doesn't exist" + groupTitle);
|
|
continue;
|
|
|
|
}
|
|
else{
|
|
|
|
boolean putIntoGroup = catalogue.assignDatasetToGroup(groupTitle, datasetId, userApiKey);
|
|
logger.info("Was product put into group " + groupTitle + "? " + putIntoGroup);
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|