Added method to add a dataset to a group and its parents (if any)

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@148652 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2017-05-15 09:46:51 +00:00
parent 8f05166de4
commit 174be6eaa3
3 changed files with 81 additions and 9 deletions

View File

@ -3,6 +3,7 @@
date="2017-05-01">
<Change>Pom fixes</Change>
<Change>Moved to java 8</Change>
<Change>Added method to add a dataset to a group and its parents (if any)</Change>
</Changeset>
<Changeset component="org.gcube.data-catalogue.ckan-util-library.2-2-1"
date="2017-04-01">

View File

@ -360,6 +360,17 @@ public interface DataCatalogue {
*/
boolean assignDatasetToGroup(String groupNameOrId, String datasetNameOrId,
String apiKey);
/**
* Assign a dataset to a group and the group's parents (if any)
* @param groupNameOrId the id or the name of the destination group.
* @param datasetNameOrId the id or the name of the dataset
* @param apiKey (the apiKey should belong to someone that has the role of editor/admin of the organization in which
* the dataset is placed, plus the admin role into the destination group.
* @return true on success, false otherwise
*/
boolean assignDatasetToGroup(String groupNameOrId, String datasetNameOrId,
String apiKey, boolean addOnParents);
/**
* Remove a dataset from a group.
@ -554,4 +565,5 @@ public interface DataCatalogue {
* @return a boolean value
*/
boolean isNotificationToUsersEnabled();
}

View File

@ -13,10 +13,12 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
@ -1338,6 +1340,56 @@ public class DataCatalogueImpl implements DataCatalogue{
@Override
public boolean assignDatasetToGroup(String groupNameOrId, String datasetNameOrId, String apiKey) {
return assignDatasetToGroupBody(groupNameOrId, datasetNameOrId, apiKey, false);
}
@Override
public boolean assignDatasetToGroup(String groupNameOrId, String datasetNameOrId, String apiKey, boolean addOnParents) {
return assignDatasetToGroupBody(groupNameOrId, datasetNameOrId, apiKey, addOnParents);
}
/**
* Find the hierarchy of trees
* @param uniqueGroups
* @param catalogue
* @param user's api key
*/
private void findHierarchyGroups(
List<String> groupsTitles,
String apiKey) {
ListIterator<String> iterator = groupsTitles.listIterator();
while (iterator.hasNext()) {
String group = (String) iterator.next();
List<CkanGroup> parents = getParentGroups(group, apiKey);
if(parents == null || parents.isEmpty())
return;
for (CkanGroup ckanGroup : parents) {
List<String> parentsList = new ArrayList<String>(Arrays.asList(ckanGroup.getName()));
findHierarchyGroups(parentsList, apiKey);
for (String parent : parentsList) {
iterator.add(parent);
}
}
}
}
/**
* 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) {
// checks
checkNotNull(groupNameOrId);
checkArgument(!groupNameOrId.isEmpty());
@ -1355,12 +1407,18 @@ public class DataCatalogueImpl implements DataCatalogue{
// 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);
List<String> fetchedGroups = new ArrayList<String>();
logger.debug("Response is " + response.getStatusLine().getStatusCode() + " and message is " + response.getStatusLine().getReasonPhrase());
@ -1379,17 +1437,16 @@ public class DataCatalogueImpl implements DataCatalogue{
JSONObject object = (JSONObject) it.next();
try{
if(object.containsKey("name"))
fetchedGroups.add((String)object.get("name"));
groupNames.add((String)object.get("name"));
}catch(Exception e){
logger.error("Error while building CkanRelationship bean from object " + object, e);
logger.error("Error", e);
}
}
// add the new one
if(!fetchedGroups.contains(group.getName()))
fetchedGroups.add(group.getName());
// remove duplicates
Set<String> groupNamesSet = new HashSet<String>(groupNames);
logger.debug("Groups to be added are " + fetchedGroups);
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";
@ -1398,9 +1455,11 @@ public class DataCatalogueImpl implements DataCatalogue{
req.put("id", datasetNameOrId);
JSONArray groups = new JSONArray();
for (int i = 0; i < fetchedGroups.size(); i++) {
Iterator<String> iteratorNameSet = groupNamesSet.iterator();
while (iteratorNameSet.hasNext()) {
String groupName = (String) iteratorNameSet.next();
JSONObject groupJSON = new JSONObject();
groupJSON.put("name", fetchedGroups.get(i));
groupJSON.put("name", groupName);
groups.add(groupJSON);
}
req.put("groups", groups);