added methods to manage groups

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@131818 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-09-26 16:34:48 +00:00
parent 9ed1027b67
commit ec1dac7750
2 changed files with 207 additions and 8 deletions

View File

@ -9,6 +9,7 @@ import org.gcube.datacatalogue.ckanutillibrary.models.DatasetRelationships;
import org.gcube.datacatalogue.ckanutillibrary.models.ResourceBean;
import org.gcube.datacatalogue.ckanutillibrary.models.RolesIntoOrganization;
import eu.trentorise.opendata.jackan.model.CkanGroup;
import eu.trentorise.opendata.jackan.model.CkanLicense;
import eu.trentorise.opendata.jackan.model.CkanOrganization;
@ -235,8 +236,37 @@ public interface CKanUtils {
* Checks if a product with such name already exists.
* Please remember that the name is unique.
* @param nameOrId the name or the id of the dataset to check
* @param apiKey to authorize the method
* @return
*/
boolean existProductWithNameOrId(String nameOrId);
boolean existProductWithNameOrId(String nameOrId, String apiKey);
/**
* Create a CkanGroup.
* @param nameOrId a unique id for the group
* @param title a title for the group
* @param description a description for the group
* @param an apikey to authorize the method execution
* @return the created CkanGroup on success, null otherwise
*/
CkanGroup createGroup(String nameOrId, String title, String description, String apiKey);
/**
* Assign a dataset to a certain group.
* @return true on success, false otherwise
*/
boolean assignDatasetToGroup(String groupNameOrId, String datasetNameOrId, String apiKey);
/**
* Returns a Map with key 'capacity' and as value a list of users with that capacity into the organization organizationName.
* @return
*/
Map<String, List<String>> getRolesAndUsersOrganization(String organizationName, String apiKey);
/**
* Returns a Map with key 'capacity' and as value a list of users with that capacity into the group groupName.
* @return
*/
Map<String, List<String>> getRolesAndUsersGroup(String groupName, String apiKey);
}

View File

@ -36,6 +36,7 @@ import org.slf4j.LoggerFactory;
import eu.trentorise.opendata.jackan.CheckedCkanClient;
import eu.trentorise.opendata.jackan.CkanClient;
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;
@ -43,6 +44,7 @@ import eu.trentorise.opendata.jackan.internal.org.apache.http.entity.StringEntit
import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.CloseableHttpClient;
import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.HttpClientBuilder;
import eu.trentorise.opendata.jackan.model.CkanDataset;
import eu.trentorise.opendata.jackan.model.CkanGroup;
import eu.trentorise.opendata.jackan.model.CkanLicense;
import eu.trentorise.opendata.jackan.model.CkanOrganization;
import eu.trentorise.opendata.jackan.model.CkanPair;
@ -56,11 +58,6 @@ import eu.trentorise.opendata.jackan.model.CkanUser;
*/
public class CKanUtilsImpl implements CKanUtils{
// public final static String PRODUCTION_SCOPE_ROOT = "/d4science.research-infrastructures.eu";
// public final static String PRODUCTION_CKAN_ORGNAME_ROOT = "d4science";
// public final static String PRODUCTION_LIFERAY_ORGNAME_ROOT = "d4science.research-infrastructures.eu";
private static final Logger logger = LoggerFactory.getLogger(CKanUtilsImpl.class);
private String CKAN_CATALOGUE_URL;
@ -102,6 +99,17 @@ public class CKanUtilsImpl implements CKanUtils{
}
/**
* Check if the key belongs to someone or not
* @param apiKey
*/
private boolean validateApiKey(String apiKey) {
checkNotNull(apiKey);
return getUserFromApiKey(apiKey) != null;
}
/**
* Retrieve connection from the pool
* @return a connection available within the pool
@ -187,7 +195,7 @@ public class CKanUtilsImpl implements CKanUtils{
checkNotNull(apiKey);
checkArgument(!apiKey.isEmpty());
CKanUserWrapper user = new CKanUserWrapper();
CKanUserWrapper user = null;
// the connection
Connection connection = null;
@ -203,6 +211,7 @@ public class CKanUtilsImpl implements CKanUtils{
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
user = new CKanUserWrapper();
user.setId(rs.getString("id"));
user.setName(rs.getString("name"));
user.setApiKey(apiKey);
@ -1056,12 +1065,15 @@ public class CKanUtilsImpl implements CKanUtils{
}
@Override
public boolean existProductWithNameOrId(String nameOrId) {
public boolean existProductWithNameOrId(String nameOrId, String apiKey) {
// checks
checkNotNull(nameOrId);
checkArgument(!nameOrId.isEmpty());
if(!validateApiKey(apiKey))
throw new IllegalArgumentException("This apikey is not valid");
try{
CheckedCkanClient client = new CheckedCkanClient(CKAN_CATALOGUE_URL, CKAN_TOKEN_SYS);
CkanDataset product = client.getDataset(nameOrId);
@ -1071,4 +1083,161 @@ public class CKanUtilsImpl implements CKanUtils{
return false;
}
}
@Override
public CkanGroup createGroup(String nameOrId, String title, String description, String apiKey) {
// checks
checkNotNull(nameOrId);
checkArgument(!nameOrId.isEmpty());
checkNotNull(apiKey);
checkArgument(!apiKey.isEmpty());
// validate api key
validateApiKey(apiKey);
// check if it exists
CkanGroup toCreate = null;
CheckedCkanClient client = new CheckedCkanClient(CKAN_CATALOGUE_URL, CKAN_TOKEN_SYS);
if((toCreate = groupExists(nameOrId, client))!= null)
return toCreate;
else{
try{
CkanGroup group = new CkanGroup(nameOrId);
group.setTitle(title);
group.setDisplayName(title);
group.setDescription(description);
toCreate = client.createGroup(group);
}catch(JackanException je){
logger.error("This group doesn't exist", je);
}
}
return toCreate;
}
/**
* Just check if the group exists
* @param nameOrId
* @param client
* @return
*/
private CkanGroup groupExists(String nameOrId, CheckedCkanClient client){
CkanGroup toReturn = null;
try{
toReturn = client.getGroup(nameOrId);
}catch(JackanException je){
logger.error("This group doesn't exist", je);
}
return toReturn;
}
@Override
public boolean assignDatasetToGroup(String groupNameOrId,
String datasetNameOrId, String apiKey) {
// checks
checkNotNull(groupNameOrId);
checkArgument(!groupNameOrId.isEmpty());
checkNotNull(datasetNameOrId);
checkArgument(!datasetNameOrId.isEmpty());
checkNotNull(apiKey);
checkArgument(!apiKey.isEmpty());
try{
CheckedCkanClient client = new CheckedCkanClient(CKAN_CATALOGUE_URL, apiKey);
CkanDataset dataset = client.getDataset(datasetNameOrId);
logger.debug("Dataset is " + dataset.getMaintainer());
List<CkanGroup> originalGroups = dataset.getGroups();
CkanGroup group = client.getGroup(groupNameOrId);
logger.debug("Group is " + group.getUsers());
originalGroups.add(group);
dataset.setGroups(originalGroups);
client.patchUpdateDataset(dataset);
return true;
}catch(JackanException je){
logger.error("Unable to make this association", je);
}
return false;
}
@Override
public Map<String, List<String>> getRolesAndUsersOrganization(
String organizationName, String apiKey) {
// checks
checkNotNull(organizationName);
checkArgument(!organizationName.isEmpty());
checkNotNull(apiKey);
checkArgument(!apiKey.isEmpty());
if(!validateApiKey(apiKey))
throw new IllegalArgumentException("This apikey is not valid");
Map<String, List<String>> capacityAndUsers = new HashMap<String, List<String>>();
CkanOrganization org = client.getOrganization(organizationName);
List<CkanUser> users = org.getUsers();
for (CkanUser ckanUser : users) {
logger.debug(ckanUser.getName());
List<String> listUsers;
if(capacityAndUsers.containsKey(ckanUser.getCapacity())){
listUsers = capacityAndUsers.get(ckanUser.getCapacity());
}else
listUsers = new ArrayList<String>();
listUsers.add(ckanUser.getName());
capacityAndUsers.put(ckanUser.getCapacity(), listUsers);
}
return capacityAndUsers;
}
@Override
public Map<String, List<String>> getRolesAndUsersGroup(
String groupName, String apiKey) {
// checks
checkNotNull(groupName);
checkArgument(!groupName.isEmpty());
checkNotNull(apiKey);
checkArgument(!apiKey.isEmpty());
if(!validateApiKey(apiKey))
throw new IllegalArgumentException("This apikey is not valid");
Map<String, List<String>> capacityAndUsers = new HashMap<String, List<String>>();
CkanGroup org = client.getGroup(groupName);
List<CkanUser> users = org.getUsers();
for (CkanUser ckanUser : users) {
logger.debug(ckanUser.getName());
List<String> listUsers;
if(capacityAndUsers.containsKey(ckanUser.getCapacity())){
listUsers = capacityAndUsers.get(ckanUser.getCapacity());
}else
listUsers = new ArrayList<String>();
listUsers.add(ckanUser.getName());
capacityAndUsers.put(ckanUser.getCapacity(), listUsers);
}
return capacityAndUsers;
}
}