diff --git a/distro/changelog.xml b/distro/changelog.xml
index 758d6ca..7e4f0a1 100644
--- a/distro/changelog.xml
+++ b/distro/changelog.xml
@@ -3,6 +3,7 @@
date="2017-05-01">
Pom fixes
Moved to java 8
+ Added method to add a dataset to a group and its parents (if any)
diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogue.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogue.java
index c2706c0..ba108f9 100644
--- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogue.java
+++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogue.java
@@ -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();
+
}
diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.java
index 775793b..67f9c7f 100644
--- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.java
+++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.java
@@ -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 groupsTitles,
+ String apiKey) {
+ ListIterator iterator = groupsTitles.listIterator();
+ while (iterator.hasNext()) {
+ String group = (String) iterator.next();
+
+ List parents = getParentGroups(group, apiKey);
+
+ if(parents == null || parents.isEmpty())
+ return;
+
+ for (CkanGroup ckanGroup : parents) {
+ List parentsList = new ArrayList(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 groupNames = new ArrayList();
+ 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 fetchedGroups = new ArrayList();
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 groupNamesSet = new HashSet(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 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);