Registered File deletion routine

This commit is contained in:
Fabio Sinibaldi 2022-01-31 13:09:54 +01:00
parent 42c986d3f2
commit 405129f37f
11 changed files with 68 additions and 16 deletions

View File

@ -51,6 +51,7 @@
<groupId>com.vdurmont</groupId>
<artifactId>semver4j</artifactId>
<version>3.1.0</version>
<scope>compile</scope>
</dependency>
<!-- TEST -->

View File

@ -0,0 +1,12 @@
package org.gcube.application.geoportal.common.model.document;
import lombok.*;
import org.bson.Document;
public class Materialization extends Document {
public static final String TYPE ="_type";
public String getType(){return super.getString(TYPE);}
}

View File

@ -4,7 +4,6 @@ import com.mongodb.client.model.geojson.GeoJsonObjectType;
import com.vdurmont.semver4j.Semver;
import lombok.*;
import org.bson.Document;
import org.bson.types.ObjectId;
@NoArgsConstructor

View File

@ -22,4 +22,5 @@ public class RegisteredFileSet {
private String folderID;
private List<RegisteredFile> payloads;
private List<Materialization> materializations;
}

View File

@ -1,11 +1,19 @@
package org.gcube.application.geoportal.common.model.profile;
import lombok.*;
import org.bson.Document;
import org.bson.types.MaxKey;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@XmlRootElement
@AllArgsConstructor
@Getter
@Setter
@ToString(callSuper = true)
public class Field extends Document {
public static final String TYPE="_type";

View File

@ -126,7 +126,11 @@ public class WorkspaceManager {
public void deleteFromWS(WorkspaceContent toDelete) throws StorageHubException {
sgClient.open(toDelete.getStorageID()).asFile().forceDelete();
}
public void deleteItem(String itemId)throws StorageHubException{
sgClient.open(itemId).asItem().forceDelete();
}
// STATIC SYNCH METHODS
@Synchronized

View File

@ -39,6 +39,6 @@ public interface MongoManagerI<T> {
public T performStep(String id, String step, Document options) throws IOException, StepException;
public T registerFileSet(String id, RegisterFileSetRequest request) throws ConfigurationException, StorageHubException, StorageException, StepException, JsonProcessingException;
public T registerFileSet(String id, RegisterFileSetRequest request) throws ConfigurationException, StorageHubException, StorageException, StepException, JsonProcessingException, DeletionException;
public T deleteFileSet(String id, String destination, Boolean force) throws ConfigurationException, StorageHubException, StorageException, StepException, JsonProcessingException;
}

View File

@ -254,7 +254,7 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI<
*
*/
@Override
public ProfiledDocument registerFileSet(String id,RegisterFileSetRequest request) throws ConfigurationException, StorageHubException, StorageException, StepException, JsonProcessingException {
public ProfiledDocument registerFileSet(String id,RegisterFileSetRequest request) throws ConfigurationException, StorageHubException, StorageException, StepException, JsonProcessingException, DeletionException {
List<TempFile> files=request.getStreams();
Document attributes =request.getAttributes();
@ -273,14 +273,14 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI<
throw new WebApplicationException("No Field found in schema "+profile.getId()+" at "+request.getFieldPath(), Response.Status.BAD_REQUEST);
if(fieldDefinitions.size()>1)
throw new WebApplicationException("Multiple field definitions ("+fieldDefinitions.size()+") found in "+profile.getId()+" for "+request.getFieldPath(),Response.Status.BAD_REQUEST);
Field fieldDefinition=fieldDefinitions.get(0);
Field fieldDefinition=Serialization.convert(fieldDefinitions.get(0),Field.class);
log.debug("Field definition is {}",fieldDefinition);
JSONPathWrapper docWrapper=new JSONPathWrapper(doc.getTheDocument().toJson());
List<RegisteredFileSet> found=docWrapper.getByPath(request.getDestinationPath(),RegisteredFileSet.class);
if(fieldDefinition.getMaxCardinality()==1 && (!found.isEmpty())){
throw new WebApplicationException("Cannot add registered fileset at "+request.getFieldPath()+" : field is not collection.",Response.Status.BAD_REQUEST);
}
// if(fieldDefinition.getMaxCardinality()==1 && (!found.isEmpty())){
// throw new WebApplicationException("Cannot add registered fileset at "+request.getFieldPath()+" : field is not collection.",Response.Status.BAD_REQUEST);
// }
Object toSet=null;
@ -296,14 +296,14 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI<
case REPLACE_EXISTING: {
if(found.size()>1)
throw new WebApplicationException("Cannot replace multiple items at "+request.getDestinationPath()+".",Response.Status.BAD_REQUEST);
deleteFileSet(doc.get_id(),request.getDestinationPath(),false);
deleteFileSetRoutine(doc,request.getDestinationPath(),false,ws);
toSet = prepareRegisteredFileSet(doc,profile,request.getDestinationPath(),attributes,files,storage,ws);
break;
}case MERGE_EXISTING: {
if(found.size()>1)
throw new WebApplicationException("Cannot merge multiple items at "+request.getDestinationPath()+".",Response.Status.BAD_REQUEST);
attributes.putAll(Serialization.asDocument(found.get(0)));
deleteFileSet(doc.get_id(),request.getDestinationPath(),false);
deleteFileSetRoutine(doc,request.getDestinationPath(),false,ws);
toSet = prepareRegisteredFileSet(doc,profile,request.getDestinationPath(),attributes,files,storage,ws);
break;
}case APPEND: {
@ -343,11 +343,10 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI<
throw new RuntimeException("Implement this");
}
// @Override
// public ProfiledDocument deleteRegisteredFileSet(String id, String destination, List<TempFile> files) {
// throw new RuntimeException("TO IMPLEMENT");
// }
private ExecutionReport step(ProfiledDocument theDocument,String step,Document callParameters) throws StepException {
log.info("[Profile {} ] Invoking Step {} on {}" ,profile.getId(),step,getManager().getDescriptor());
@ -402,4 +401,22 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI<
attributes.putIfAbsent(RegisteredFileSet.PAYLOADS,registeredFiles);
return Serialization.convert(attributes,RegisteredFileSet.class);
}
private static ProfiledDocument deleteFileSetRoutine(ProfiledDocument doc,String fileSetPath, Boolean force, WorkspaceManager ws) throws DeletionException, StorageHubException {
log.info("Document ID {} : deleting fileset at {} [force : {}]",doc.get_id(),fileSetPath,force);
JSONPathWrapper wrapper =new JSONPathWrapper(doc.getTheDocument().toJson());
RegisteredFileSet toDelete = Serialization.convert(wrapper.getByPath(fileSetPath).get(0),RegisteredFileSet.class);
if(toDelete.getMaterializations()!=null && !toDelete.getMaterializations().isEmpty()){
if(!force) throw new DeletionException("Fileset (Document ID "+doc.get_id()+", path "+fileSetPath+") already materialized. Use force = true");
else throw new RuntimeException("Implement this");
// TODO manager force deletion
// NB handlers for materialization types
}
log.debug("Document ID {} : deleting ws folder {}",doc.get_id(),toDelete.getFolderID());
if(toDelete.getPayloads()!=null)
ws.deleteItem(toDelete.getFolderID());
doc.setTheDocument(Document.parse(wrapper.set(fileSetPath,null).getCtx().jsonString()));
return doc;
}
}

View File

@ -32,6 +32,7 @@ public abstract class GuardedMethod<T> {
result=run();
return this;
}catch(WebApplicationException e) {
log.error("Throwing Web Application Exception ",e);
throw e;
}catch(Throwable t) {
log.error("Unexpected error ",t);

View File

@ -9,6 +9,7 @@ import org.gcube.application.geoportal.common.model.document.ProfiledDocument;
import org.gcube.application.geoportal.common.model.legacy.Concessione;
import org.gcube.application.geoportal.common.model.rest.Configuration;
import org.gcube.application.geoportal.common.model.rest.QueryRequest;
import org.gcube.application.geoportal.common.model.rest.RegisterFileSetRequest;
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.common.utils.FileSets;
import org.gcube.application.geoportal.common.utils.Files;
@ -112,6 +113,7 @@ public class ProfiledDocumentsTests extends BasicServiceTestUnit{
"relazioneScavo",
"relazioneScavo",
Document.parse("{\"titolo\" : \"mio titolo\"}"),
RegisterFileSetRequest.ClashOptions.MERGE_EXISTING,
"relazione.pdf");
System.out.println(Serialization.write(doc));
}
@ -126,9 +128,17 @@ public class ProfiledDocumentsTests extends BasicServiceTestUnit{
post(Entity.entity(document, MediaType.APPLICATION_JSON)),ProfiledDocument.class);
}
private ProfiledDocument upload(StorageUtils storage, String id, String path, String fieldPath, Document attributes, String ...files) throws Exception {
private ProfiledDocument upload(StorageUtils storage,
String id,
String path,
String fieldDefinitionPath,
Document attributes,
RegisterFileSetRequest.ClashOptions clashPolicy,
String ...files) throws Exception {
FileSets.RequestBuilder builder = FileSets.build(path);
builder.setFieldDescriptionPath(fieldDefinitionPath).setClashPolicy(clashPolicy).setAttributes(attributes);
for(String file:files)
builder.add(storage.putOntoStorage(new File(TestConcessioniModel.getBaseFolder(),file),file));