Purge upon deletion (optional)

This commit is contained in:
Fabio Sinibaldi 2021-08-02 17:43:30 +02:00
parent 815a972c82
commit b68257d150
6 changed files with 200 additions and 43 deletions

View File

@ -364,7 +364,11 @@ public class SDIManager {
}
//delete file
dtGeoServer.getWebClient().delete(toDelete.getGeoserverPath());
// TODO REMOVE HARDCODED PATCH
String path=toDelete.getGeoserverPath().replace("/srv/geoserver_data","geoserver");
// path=toDelete.getGeoserverPath();
dtGeoServer.getWebClient().delete(path);
}

View File

@ -69,6 +69,10 @@ public class WorkspaceManager {
return sgClient.open(id).asFolder();
}
public void removeFolderById(String id) throws StorageHubException {
sgClient.open(id).asFolder().delete();
}
public FolderContainer getSubFolder(FolderContainer parentFolder,String path) throws StorageHubException {
try{
return parentFolder.openByRelativePath(path).asFolder();

View File

@ -18,14 +18,13 @@ import org.gcube.application.geoportal.service.engine.WorkspaceManager;
import org.gcube.application.geoportal.service.engine.WorkspaceManager.FileOptions;
import org.gcube.application.geoportal.service.engine.WorkspaceManager.FolderOptions;
import org.gcube.application.geoportal.service.engine.postgis.PostgisIndex;
import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException;
import org.gcube.application.geoportal.service.model.internal.faults.InvalidStateException;
import org.gcube.application.geoportal.service.model.internal.faults.PublishException;
import org.gcube.application.geoportal.service.model.internal.faults.SDIInteractionException;
import org.gcube.application.geoportal.service.model.internal.faults.*;
import org.gcube.application.geoportal.service.utils.Serialization;
import org.gcube.common.storagehub.client.dsl.FolderContainer;
import org.gcube.data.transfer.library.faults.RemoteServiceException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
@ -114,12 +113,50 @@ public class ConcessioniMongoManager extends MongoManager{
return toReturn;
}
public Concessione getById(String id) throws JsonProcessingException, IOException {
public Concessione getById(String id)throws IOException {
log.debug("Loading by ID "+id);
return asConcessione(getById(asId(id),collectionName));
}
public void deleteById(String id) {
delete(asId(id), collectionName);
public void deleteById(String id,boolean force) throws DeletionException {
log.debug("Deleting by ID {}, force {}",id,force);
try{
Concessione concessione =unpublish(id);
try{
// UNPUBLISH
if (!concessione.getReport().getStatus().equals(ValidationStatus.PASSED)&&!force)
throw new DeletionException("Unable to unpublish "+concessione.getMongo_id());
//clean WS
concessione = removeContent(concessione);
if (!concessione.getReport().getStatus().equals(ValidationStatus.PASSED)&&!force)
throw new DeletionException("Unable to unpublish "+concessione.getMongo_id());
delete(asId(id), collectionName);
}catch(DeletionException e) {
//storing updated - partially deleted
replace(asDocument(concessione), collectionName);
throw e;
}
}catch(Throwable t){
throw new DeletionException("Unable to delete "+id,t);
}
}
public Concessione unpublish(String id) throws DeletionException {
try{
Concessione toReturn=asConcessione(getById(asId(id),collectionName));
removeFromIndex(toReturn);
log.debug("Removed from centroids "+toReturn.getMongo_id());
toReturn = unpublish(toReturn);
log.debug("Concessione after unpublishing is "+toReturn);
return asConcessione(replace(asDocument(toReturn),collectionName));
}catch(Throwable t){
throw new DeletionException("Unable to unpublish "+id,t);
}
}
public Concessione publish(String id) throws JsonProcessingException, IOException, InvalidStateException{
@ -139,6 +176,33 @@ public class ConcessioniMongoManager extends MongoManager{
}
public static Concessione removeContent(Concessione concessione) throws DeletionException {
if(concessione.getFolderId()==null) {
log.debug("No content for " + concessione.getMongo_id());
return concessione;
}
try {
log.debug("Removing content for " + concessione.getMongo_id());
WorkspaceManager manager = new WorkspaceManager();
manager.removeFolderById(concessione.getFolderId());
//Removing references from Object
concessione.setFolderId(null);
ArrayList<AssociatedContent> list = new ArrayList<>();
list.add(concessione.getPosizionamentoScavo());
list.addAll(concessione.getPianteFineScavo());
list.addAll(concessione.getImmaginiRappresentative());
list.addAll(concessione.getGenericContent());
for (AssociatedContent c : list) {
c.getActualContent().clear();
}
return concessione;
}catch(Throwable t){
throw new DeletionException("Unable to delete from WS ",t);
}
}
public Concessione persistContent(String id, String destinationPath, List<TempFile> files) throws Exception{
log.info("Persisting {} files for path {} in concessione ",files.size(),destinationPath,id);
try{
@ -182,7 +246,20 @@ public class ConcessioniMongoManager extends MongoManager{
return record;
}
private static Concessione removeFromIndex(Concessione record) {
log.info("Removing from index {} ",record.getId());
ValidationReport report= new ValidationReport("Remove From Index Report ");
PostgisIndex index;
try {
index = new PostgisIndex();
index.removeCentroid(record);
report.addMessage(ValidationStatus.PASSED, "Removed centroid");
} catch (SDIInteractionException | SQLException | ConfigurationException e) {
log.error("Unable to reove from index {} ",record,e);
report.addMessage(ValidationStatus.WARNING, "Internal error while indexing.");
}
return record;
}
@ -231,6 +308,43 @@ public class ConcessioniMongoManager extends MongoManager{
return conc;
}
private static final Concessione unpublish(Concessione concessione){
ValidationReport report=new ValidationReport("Unpublish report");
try{
SDIManager sdi=new SDIManager();
ArrayList<AssociatedContent> list=new ArrayList<AssociatedContent>();
list.add(concessione.getPosizionamentoScavo());
list.addAll(concessione.getPianteFineScavo());
for(AssociatedContent c:list) {
if(c instanceof LayerConcessione) {
//TODO actually delete
List<PersistedContent> contents=c.getActualContent();
List<PersistedContent> toRemove=new ArrayList<>();
for(PersistedContent p:contents){
if(p instanceof GeoServerContent){
try {
sdi.deleteContent((GeoServerContent) p);
toRemove.add(p);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteServiceException e) {
e.printStackTrace();
}
}
}
c.getActualContent().removeAll(toRemove);
}
}
}catch(SDIInteractionException e){
report.addMessage(ValidationStatus.WARNING, "Unable to unpublish layers "+e.getMessage());
}
concessione.setReport(report);
return concessione;
}
private static final void store(AssociatedContent content,List<TempFile> files, WorkspaceManager ws, FolderContainer base) throws Exception {
FolderContainer sectionParent=null;

View File

@ -0,0 +1,23 @@
package org.gcube.application.geoportal.service.model.internal.faults;
public class DeletionException extends Exception {
public DeletionException() {
}
public DeletionException(String message) {
super(message);
}
public DeletionException(String message, Throwable cause) {
super(message, cause);
}
public DeletionException(Throwable cause) {
super(cause);
}
public DeletionException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@ -89,12 +89,14 @@ public class ConcessioniOverMongo {
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Path("{"+InterfaceConstants.Parameters.PROJECT_ID+"}")
public void deleteById(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) {
public void deleteById(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id,
@QueryParam("force") Boolean forceOption) {
new GuardedMethod<Concessione> () {
@Override
protected Concessione run() throws Exception, WebApplicationException {
Boolean force=(forceOption!=null)?forceOption:false;
ConcessioniMongoManager manager=new ConcessioniMongoManager();
manager.deleteById(id);
manager.deleteById(id,force);
return null;
}
}.execute();

View File

@ -91,7 +91,14 @@ public class ConcessioniOverMongoTest extends BasicServiceTestUnit{
public void delete() throws Exception {
WebTarget target=target(PATH);
Concessione c = get(target);
check(target.path(c.getMongo_id()).request(MediaType.APPLICATION_JSON).delete(),null);
Concessione published=getFullPublished(target);
check(target.path(published.getMongo_id()).request(MediaType.APPLICATION_JSON).delete(),null);
}
@ -145,6 +152,22 @@ public class ConcessioniOverMongoTest extends BasicServiceTestUnit{
@Test
public void publish() throws Exception {
WebTarget target=target(PATH);
Concessione published=getFullPublished(target);
System.out.println("Published : "+published);
System.out.println("Report is : "+published.getReport());
assertNotNull(published.getReport());
assertEquals(ValidationStatus.PASSED,published.getReport().getStatus());
assertEquals(published.getImmaginiRappresentative().size(),2);
assertEquals(published.getPianteFineScavo().size(),1);
assertNotNull(published.getPosizionamentoScavo().getWmsLink());
for(LayerConcessione l : published.getPianteFineScavo())
assertNotNull(l.getWmsLink());
assertNotNull(published.getCentroidLat());
assertNotNull(published.getCentroidLong());
}
private Concessione getFullPublished(WebTarget target) throws Exception {
Concessione c=TestModel.prepareConcessione(1,2);
c.setNome("Concessione : publish test");
@ -167,21 +190,8 @@ public class ConcessioniOverMongoTest extends BasicServiceTestUnit{
// Immagini
Concessione published=publish(target, c);
System.out.println("Published : "+published);
System.out.println("Report is : "+published.getReport());
assertNotNull(published.getReport());
assertEquals(ValidationStatus.PASSED,published.getReport().getStatus());
assertEquals(published.getImmaginiRappresentative().size(),2);
assertEquals(published.getPianteFineScavo().size(),1);
assertNotNull(published.getPosizionamentoScavo().getWmsLink());
for(LayerConcessione l : published.getPianteFineScavo())
assertNotNull(l.getWmsLink());
assertNotNull(published.getCentroidLat());
assertNotNull(published.getCentroidLong());
return published;
}
}