managing swith of scope in the deleteResourceFromDataset

This commit is contained in:
Francesco Mangiacrapa 2021-03-02 16:01:12 +01:00
parent 7fc5fab646
commit 96b4fc0732
4 changed files with 31 additions and 5 deletions

View File

@ -339,10 +339,11 @@ public interface DataCatalogue {
* Remove the resource with id resourceId from dataset in which it is.
*
* @param resourceId the resource id
* @param username the username
* @return true on success, false otherwise.
* @throws Exception the exception
*/
boolean deleteResourceFromDataset(String resourceId) throws Exception;
boolean deleteResourceFromDataset(String resourceId, String username) throws Exception;
/**
* Create a CkanGroup.

View File

@ -1205,21 +1205,32 @@ public class DataCatalogueImpl implements DataCatalogue {
* @see org.gcube.datacatalogue.utillibrary.server.DataCatalogue#deleteResourceFromDataset(java.lang.String)
*/
@Override
public boolean deleteResourceFromDataset(String resourceId) throws Exception {
public boolean deleteResourceFromDataset(String resourceId, String username) throws Exception {
LOG.info("Called deleteResourceFromDataset - resource with id " + resourceId);
// checks
checkNotNull(resourceId);
checkArgument(!resourceId.isEmpty());
GcubeContext gcubeContext = null;
try{
CkanResource theResource = ckanCaller.getResource(resourceId);
CkanDataset theDataset = getDataset(theResource.getPackageId(), username);
//Switch the context if needed
gcubeContext = GCubeUtils.detectTheOrgNameContexts(theDataset.getOrganization().getName(), username, true);
gCatCaller.deleteResource(theResource.getPackageId(), resourceId);
LOG.info("Resource with id: " + resourceId + " deleted correclty");
return true;
}catch(Exception e){
LOG.error("Unable to delete resource whose id is " + resourceId, e);
}finally {
//revert the context if needed
if(gcubeContext!=null)
GCubeUtils.revertToSourceContext(gcubeContext);
}
return false;

View File

@ -133,7 +133,7 @@ public class GCubeUtils {
String destinationTokenForUser = pContext.getCurrentUserToken(destinationScopeForOrg, username);
LOG.debug("Destination token detected: "
+ destinationTokenForUser.substring(0, destinationTokenForUser.length() - 12) + "XXXXX"
+ "for user: " + username);
+ " for user: " + username);
if (destinationScopeForOrg != null && destinationTokenForUser != null) {
gcubeContext.setTargetScope(destinationScopeForOrg);

View File

@ -35,13 +35,14 @@ public class TestDataCatalogueLib {
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(TestDataCatalogueLib.class);
private DataCatalogueFactory factory;
private String scope = "/gcube/devsec/devVRE";
//private String scope = "/gcube/devsec/devVRE";
private String scope = "/gcube";
//private String scope = "/pred4s/preprod/preVRE";
//private String scope = "/d4science.research-infrastructures.eu/SoBigData/Catalogue-TerritoriAperti";
//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";
private String authorizationToken = "";
String subjectId = "aa_father4";
String objectId = "bb_son4";
String productName = "adatasetcreatedbycatalogue-util-library-873805063";
@ -325,6 +326,19 @@ public class TestDataCatalogueLib {
}
}
//@Test
public void deleteResource() throws Exception {
ScopeProvider.instance.set(scope);
SecurityTokenProvider.instance.set(authorizationToken);
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
String resourceId = "ce7295cf-47db-4faf-901f-4fec2d3fae7a";
boolean deleted = instance.deleteResourceFromDataset(resourceId, testUser);
LOG.info("Deleted the resource? "+deleted);
}
//@Test
public void patchFieldsForDataset() throws Exception{