2016-11-24 17:53:50 +01:00
|
|
|
package org.gcube.data_catalogue.grsf_publish_ws.utils.threads;
|
2016-10-08 23:10:25 +02:00
|
|
|
|
2016-12-01 15:37:26 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashSet;
|
2016-10-08 23:10:25 +02:00
|
|
|
import java.util.List;
|
2016-12-01 15:37:26 +01:00
|
|
|
import java.util.ListIterator;
|
|
|
|
import java.util.Set;
|
2016-10-08 23:10:25 +02:00
|
|
|
|
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogue;
|
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.models.RolesCkanGroupOrOrg;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2016-12-01 15:37:26 +01:00
|
|
|
import eu.trentorise.opendata.jackan.model.CkanGroup;
|
|
|
|
|
2016-10-08 23:10:25 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
2016-10-10 18:50:11 +02:00
|
|
|
private String organizationId;
|
2016-10-08 23:10:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param groupTitle
|
|
|
|
* @param datasetId
|
|
|
|
* @param username
|
2016-10-10 18:50:11 +02:00
|
|
|
* @param organizationId
|
2016-10-08 23:10:25 +02:00
|
|
|
* @param catalogue
|
|
|
|
*/
|
2016-10-10 18:50:11 +02:00
|
|
|
public AssociationToGroupThread(List<String> groupsTitles, String datasetId, String organizationId,
|
2016-10-09 13:04:03 +02:00
|
|
|
String username, DataCatalogue catalogue) {
|
2016-10-08 23:10:25 +02:00
|
|
|
this.groupsTitles = groupsTitles;
|
|
|
|
this.datasetId = datasetId;
|
|
|
|
this.username = username;
|
|
|
|
this.catalogue = catalogue;
|
2016-10-10 18:50:11 +02:00
|
|
|
this.organizationId = organizationId;
|
2016-10-08 23:10:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
|
2016-12-01 15:37:26 +01:00
|
|
|
try{
|
2016-10-10 18:50:11 +02:00
|
|
|
|
2016-12-01 15:37:26 +01:00
|
|
|
logger.info("Association thread started to put the dataset with id="+ datasetId + " into group with title(s) " + groupsTitles + " for user " + username);
|
2016-10-10 18:50:11 +02:00
|
|
|
|
2016-12-01 15:37:26 +01:00
|
|
|
// find parents' groups
|
|
|
|
String userApiKey = catalogue.getApiKeyFromUsername(username);
|
|
|
|
findHierarchy(groupsTitles, catalogue, userApiKey);
|
|
|
|
|
|
|
|
Set<String> uniqueGroups = new HashSet<String>(groupsTitles);
|
|
|
|
|
2016-12-04 15:40:58 +01:00
|
|
|
logger.info("Full set of groups is " + uniqueGroups);
|
2016-12-01 15:37:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
// 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, userApiKey).toUpperCase());
|
2016-10-14 18:44:34 +02:00
|
|
|
|
2016-12-01 15:37:26 +01:00
|
|
|
if(!role.equals(RolesCkanGroupOrOrg.ADMIN))
|
|
|
|
role = RolesCkanGroupOrOrg.MEMBER; // decrease the role to member if it is not an admin
|
2016-10-10 18:50:11 +02:00
|
|
|
|
2016-12-01 15:37:26 +01:00
|
|
|
for (String groupTitle : uniqueGroups) {
|
2016-10-10 18:50:11 +02:00
|
|
|
|
2016-12-01 15:37:26 +01:00
|
|
|
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);
|
|
|
|
}
|
2016-10-09 13:04:03 +02:00
|
|
|
}
|
2016-12-01 15:37:26 +01:00
|
|
|
logger.info("The Association Group thread ended correctly");
|
|
|
|
}catch(Exception e){
|
|
|
|
logger.error("Exception follows ", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the hierarchy of trees
|
|
|
|
* @param uniqueGroups
|
|
|
|
* @param catalogue
|
|
|
|
* @param user's api key
|
|
|
|
*/
|
|
|
|
public static void findHierarchy(
|
|
|
|
List<String> groupsTitles,
|
|
|
|
DataCatalogue catalogue,
|
|
|
|
String apiKey) {
|
|
|
|
|
|
|
|
ListIterator<String> iterator = groupsTitles.listIterator();
|
|
|
|
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
String group = (String) iterator.next();
|
|
|
|
|
|
|
|
List<CkanGroup> parents = catalogue.getParentGroups(group, apiKey);
|
|
|
|
|
|
|
|
if(parents == null || parents.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (CkanGroup ckanGroup : parents) {
|
|
|
|
List<String> parentsList = new ArrayList<String>(Arrays.asList(ckanGroup.getName()));
|
|
|
|
findHierarchy(parentsList, catalogue, apiKey);
|
|
|
|
|
|
|
|
for (String parent : parentsList) {
|
|
|
|
iterator.add(parent);
|
|
|
|
}
|
2016-10-10 18:50:11 +02:00
|
|
|
}
|
2016-10-08 23:10:25 +02:00
|
|
|
}
|
2016-12-01 15:37:26 +01:00
|
|
|
|
2016-10-08 23:10:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|