added method to set the parent group of a group

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@135261 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-12-02 21:42:17 +00:00
parent 610946dcf5
commit aa2eabadff
3 changed files with 100 additions and 23 deletions

View File

@ -287,6 +287,14 @@ public interface DataCatalogue {
* @return the created CkanGroup on success, null otherwise
*/
CkanGroup createGroup(String nameOrId, String title, String description);
/**
* Associate a group with its parent.s
* @param parentName
* @param group
* @return
*/
boolean setGroupParent(String parentName, String groupName);
/**
* Returns a Map with key 'capacity' and as value a list of users with that capacity into the organization organizationName.

View File

@ -14,10 +14,12 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import net.htmlparser.jericho.Renderer;
@ -1490,7 +1492,7 @@ public class DataCatalogueImpl implements DataCatalogue{
// "name": "fishery-activity"
//
// }
// get the id of the group to be removed
String groupId = client.getGroup(groupNameOrId).getId();
@ -1528,7 +1530,7 @@ public class DataCatalogueImpl implements DataCatalogue{
}catch(Exception e){
logger.error("Failed to remove the group " + groupNameOrId + " from product " + datasetNameOrId, e);
}
return false;
}
@ -2084,15 +2086,74 @@ public class DataCatalogueImpl implements DataCatalogue{
// checks
checkNotNull(groupName);
checkNotNull(apiKey);
try{
CheckedCkanClient client = new CheckedCkanClient(CKAN_CATALOGUE_URL, apiKey);
return client.getGroup(groupName).getGroups();
}catch(Exception e){
logger.error("Something went wrong, returning null", e);
}
return null;
}
@Override
public boolean setGroupParent(String parentName, String groupName) {
// checks
checkNotNull(parentName);
checkNotNull(groupName);
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
String apiRequestUrl = CKAN_CATALOGUE_URL + "/api/3/action/group_patch";
HttpPost httpPostRequest = new HttpPost(apiRequestUrl);
httpPostRequest.setHeader("Authorization", CKAN_TOKEN_SYS);
CheckedCkanClient client = new CheckedCkanClient(CKAN_CATALOGUE_URL, CKAN_TOKEN_SYS);
List<CkanGroup> alreadyAvailableParents = client.getGroup(groupName).getGroups();
Set<String> parentsNames = new HashSet<String>();
parentsNames.add(parentName);
for (CkanGroup alreadyAvailableParent : alreadyAvailableParents) {
parentsNames.add(alreadyAvailableParent.getName());
}
logger.info("Setting as parents of group " + groupName + " :" + parentsNames);
// Request parameters to be replaced
JSONObject jsonRequest = new JSONObject();
JSONArray parentsAsJson = new JSONArray();
for(String parent : parentsNames){
JSONObject obj = new JSONObject();
obj.put("name", parent);
parentsAsJson.add(obj);
}
// perform the request
jsonRequest.put("id", groupName);
jsonRequest.put("groups", parentsAsJson);
logger.debug("Request param is going to be " + jsonRequest);
StringEntity params = new StringEntity(jsonRequest.toJSONString(), ContentType.APPLICATION_JSON);
httpPostRequest.setEntity(params);
HttpResponse response = httpClient.execute(httpPostRequest);
if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() >= 300) {
throw new RuntimeException("failed to patch the group. response status line from "
+ apiRequestUrl + " was: " + response.getStatusLine());
}
return true;
}catch(Exception e){
logger.error("Failed to set parents groups", e);
}
return false;
}
}

View File

@ -37,17 +37,17 @@ public class TestDataCatalogueLib {
String subjectId = "aa_father4";
String objectId = "bb_son4";
//@Before
// @Before
public void before(){
factory = DataCatalogueFactory.getFactory();
}
//@Test
public void testManageProduct() throws Exception{
DataCatalogueImpl catalogue = factory.getUtilsPerScope(scope);
logger.debug("Manage product is " + catalogue.isManageProductEnabled());
logger.debug("Manage product is " + catalogue.isManageProductEnabled());
}
//@Test
@ -389,26 +389,26 @@ public class TestDataCatalogueLib {
//@Test
public void testNameConversion(){
logger.debug(UtilMethods.fromCKanUsernameToUsername("costantino_perciante"));
}
//@Test
public void getGroupsRoles() throws Exception{
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
Map<RolesCkanGroupOrOrg, List<String>> res = instance.getRolesAndUsersGroup("abundance-level");
logger.debug(res.toString());
}
//@Test
public void testResourcePatch() throws Exception{
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
String id = "858f5e77-80c2-4cb2-bcfc-77529693dc9a";
instance.patchResource(id, "http://ftp.d4science.org/previews/69cc0769-de6f-45eb-a842-7be2807e8887.jpg", "new_name_for_testing_patch.csv", "description test", "", instance.getApiKeyFromUsername("costantino_perciante"));
}
//@Test
public void testPatchProduct() throws Exception{
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
@ -416,32 +416,40 @@ public class TestDataCatalogueLib {
customFieldsToChange.put("Status", Arrays.asList("Pending"));
instance.patchProductCustomFields("a-test-to-ignore", instance.getApiKeyFromUsername("costantino_perciante"), customFieldsToChange);
}
//@Test
public void addTag()throws Exception{
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
instance.addTag("test-after-tags-editing", instance.getApiKeyFromUsername("costantino_perciante"), "api add tag to test");
}
//@Test
public void removeTag()throws Exception{
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
instance.removeTag("test-after-tags-editing", instance.getApiKeyFromUsername("costantino_perciante"), "api add tag to test");
}
//@Test
public void removeGroup()throws Exception{
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
instance.removeDatasetFromGroup("pending", "test-after-tags-editing", instance.getApiKeyFromUsername("costantino_perciante"));
}
//@Test
public void getGroups() throws Exception{
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
instance.getParentGroups("abundance-level", instance.getApiKeyFromUsername("costantino_perciante"));
}
// @Test
public void createGroupsAndSetAsFather() throws Exception{
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
//CkanGroup childGroup = instance.createGroup("test-group-child", "A child group", null);
//CkanGroup parentGroup = instance.createGroup("test-group-parent", "A parent group", null);
instance.setGroupParent("test-group-parent", "test-group-child");
}
}