added the method createGroup

This commit is contained in:
Francesco Mangiacrapa 2020-08-27 17:25:56 +02:00
parent 2e1a047648
commit ed7a5f64e2
5 changed files with 145 additions and 14 deletions

View File

@ -7,6 +7,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import eu.trentorise.opendata.jackan.model.CkanDataset;
import eu.trentorise.opendata.jackan.model.CkanGroup;
import eu.trentorise.opendata.jackan.model.CkanResource;
// TODO: Auto-generated Javadoc
@ -31,6 +32,22 @@ public class MarshUnmarshCkanObject {
return ExtendCkanClient.getObjectMapper().readValue(jsonDataset, CkanDataset.class);
}
/**
* To ckan group.
*
* @param jsonGroup the json group
* @return the ckan group
* @throws JsonParseException the json parse exception
* @throws JsonMappingException the json mapping exception
* @throws IOException Signals that an I/O exception has occurred.
*/
public static CkanGroup toCkanGroup(String jsonGroup) throws JsonParseException, JsonMappingException, IOException {
return ExtendCkanClient.getObjectMapper().readValue(jsonGroup, CkanGroup.class);
}
/**
* To ckan resource.
@ -68,5 +85,17 @@ public class MarshUnmarshCkanObject {
public static String toJsonValueDataset(CkanDataset dataset) throws JsonProcessingException {
return ExtendCkanClient.getObjectMapper().writeValueAsString(dataset);
}
/**
* To json value group.
*
* @param group the group
* @return the string
* @throws JsonProcessingException the json processing exception
*/
public static String toJsonValueGroup(CkanGroup group) throws JsonProcessingException {
return ExtendCkanClient.getObjectMapper().writeValueAsString(group);
}
}

View File

@ -1,14 +1,16 @@
/*
*
*/
package org.gcube.datacatalogue.ckanutillibrary.gcat;
import java.io.IOException;
import java.net.MalformedURLException;
import org.gcube.gcat.client.Group;
import org.gcube.gcat.client.Item;
import org.gcube.gcat.client.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// TODO: Auto-generated Javadoc
/**
* The Class GCatCaller.
*
@ -36,8 +38,7 @@ public class GCatCaller {
*
* @param datasetIdOrName the dataset id or name
* @return the jsonValue
* @throws MalformedURLException
* @throws IOException Signals that an I/O exception has occurred.
* @throws MalformedURLException the malformed URL exception
*/
public String getDatasetForName(String datasetIdOrName) throws MalformedURLException {
LOG.debug("Get dataset for name "+datasetIdOrName+ "called");
@ -59,14 +60,40 @@ public class GCatCaller {
}
/**
* Adds the resource to dataset.
*
* @param datasetID the dataset ID
* @param jsonValueResource the json value resource
* @return the string
* @throws MalformedURLException the malformed URL exception
*/
public String addResourceToDataset(String datasetID, String jsonValueResource) throws MalformedURLException {
LOG.debug("Create resource called");
return new Resource().create(datasetID, jsonValueResource);
}
/**
* Gets the catalogue URL.
*
* @return the catalogue URL
*/
public String getCatalogueURL() {
return catalogueURL;
}
/**
* Creates the group.
*
* @param jsonGroup the json group
* @return the string
* @throws MalformedURLException
*/
public String createGroup(String jsonGroup) throws MalformedURLException {
LOG.debug("Create group called");
return new Group().create(jsonGroup);
}
}

View File

@ -275,8 +275,9 @@ public interface DataCatalogue {
*
* @param resourceId the resource id
* @return true on success, false otherwise.
* @throws Exception
*/
boolean deleteResourceFromDataset(String resourceId);
boolean deleteResourceFromDataset(String resourceId) throws Exception;
/**
* Returns the main landing pages for this catalogue (i.e. type, orgs, groups and items pages)
@ -284,6 +285,16 @@ public interface DataCatalogue {
* @throws Exception
*/
LandingPages getLandingPages() throws Exception;
/**
* 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
* @return the created CkanGroup on success, null otherwise
* @throws Exception
*/
CkanGroup createGroup(String nameOrId, String title, String description) throws Exception;
}

View File

@ -13,7 +13,6 @@ import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.common.scope.impl.ScopeBean.Type;
import org.gcube.datacatalogue.ckanutillibrary.ckan.DirectCkanCaller;
@ -32,6 +31,7 @@ import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.client.methods.HttpPost;
import eu.trentorise.opendata.jackan.internal.org.apache.http.entity.ContentType;
@ -812,8 +812,6 @@ public class DataCatalogueImpl implements DataCatalogue {
// try to create
String jsonValueDataset = MarshUnmarshCkanObject.toJsonValueDataset(dataset);
LOG.info("Serialized dataset is: " + jsonValueDataset);
ScopeProvider.instance.set("/gcube/devsec/devVRE");
SecurityTokenProvider.instance.set("8e74a17c-92f1-405a-b591-3a6090066248-98187548");
jsonValueDataset = gCatCaller.createDataset(jsonValueDataset,true);
LOG.debug("Created dataset is: " + jsonValueDataset);
@ -846,10 +844,10 @@ public class DataCatalogueImpl implements DataCatalogue {
return null;
}
public boolean patchProductCustomFields(String productId, Map<String, List<String>> customFieldsToChange,
boolean removeOld) {
return false;
}
// public boolean patchProductCustomFields(String productId, Map<String, List<String>> customFieldsToChange,
// boolean removeOld) {
// return false;
// }
@Override
public String addResourceToDataset(ResourceBean resourceBean) throws Exception {
@ -893,9 +891,75 @@ public class DataCatalogueImpl implements DataCatalogue {
return null;
}
public boolean deleteResourceFromDataset(String resourceId) {
@Override
public boolean deleteResourceFromDataset(String resourceId) throws Exception {
throw new Exception("Method not implemented");
}
@Override
public CkanGroup createGroup(String nameOrId, String title, String description) throws Exception {
// checks
checkNotNull(nameOrId);
checkArgument(!nameOrId.trim().isEmpty());
// check if it exists
CkanGroup toCreate = null;
LOG.debug("Request for creating group with name " + nameOrId + " title " + title + " and description " + description);
String name = CatalogueUtilMethods.fromGroupTitleToName(nameOrId);
if((toCreate = groupExists(name))!= null)
return toCreate;
else{
try{
CkanGroup group = new CkanGroup(name);
group.setTitle(title);
group.setDisplayName(title);
group.setDescription(description);
String jsonValueGroup = MarshUnmarshCkanObject.toJsonValueGroup(group);
LOG.trace("Serialized group is: " + jsonValueGroup);
String theGroup = gCatCaller.createGroup(jsonValueGroup);
toCreate = MarshUnmarshCkanObject.toCkanGroup(theGroup);
LOG.info("Created the group with id: " + toCreate.getId() + ", name: "+toCreate.getName());
}catch(JackanException je){
LOG.error("Unable to create such group", je);
}
}
return toCreate;
}
/**
* Just check if the group exists
* @param nameOrId
* @param client
* @return
*/
private CkanGroup groupExists(String nameOrId){
CkanGroup toReturn = null;
try{
ExtendCkanClient client = new ExtendCkanClient(CKAN_CATALOGUE_URL, CKAN_TOKEN_SYS);
toReturn = client.getGroup(nameOrId);
}catch(JackanException je){
LOG.error("The group "+nameOrId+" doesn't exist");
}
return toReturn;
}
@Override
public boolean patchProductCustomFields(String productId, Map<String, List<String>> customFieldsToChange,
boolean removeOld) {
return false;
}
}

View File

@ -38,7 +38,7 @@ public class TestDataCatalogueLib {
//private String scope = "/d4science.research-infrastructures.eu/gCubeApps/BiodiversityLab";
//private String testUser = "costantino_perciante";
private String testUser = "francesco.mangiacrapa";
private String authorizationToken = "8e74a17c-92f1-405a-b591-3a6090066248-98187548"; //devVRE
private String authorizationToken = "";
String subjectId = "aa_father4";
String objectId = "bb_son4";