From b27a314f9bfe719430cbdf4456f6e1259a0f6250 Mon Sep 17 00:00:00 2001 From: lucio Date: Wed, 28 Sep 2022 19:21:02 +0200 Subject: [PATCH] added scritp for trash --- pom.xml | 13 +++++- src/main/java/CleanTrash.java | 63 ++++++++++++++++++++++++++ src/main/java/MongoToMinioPorting.java | 4 ++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 src/main/java/CleanTrash.java create mode 100644 src/main/java/MongoToMinioPorting.java diff --git a/pom.xml b/pom.xml index b6195f4..c1920d3 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ 1.0.0-SNAPSHOT Storagehub Scripts + + + + org.gcube.distribution + gcube-bom + 3.0.0-SNAPSHOT + pom + import + + + + maven-parent org.gcube.tools @@ -24,7 +36,6 @@ org.gcube.common storagehub-model - [1.0.0,2.0.0-SNAPSHOT) provided diff --git a/src/main/java/CleanTrash.java b/src/main/java/CleanTrash.java new file mode 100644 index 0000000..bb5c37b --- /dev/null +++ b/src/main/java/CleanTrash.java @@ -0,0 +1,63 @@ +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.Collections; +import java.util.List; + +import javax.jcr.Node; +import javax.jcr.NodeIterator; + +import org.apache.jackrabbit.api.JackrabbitSession; +import org.gcube.common.storagehub.model.Path; +import org.gcube.common.storagehub.model.Paths; +import org.gcube.common.storagehub.model.items.Item; +import org.gcube.common.storagehub.model.items.TrashItem; +import org.gcube.data.access.storagehub.scripting.AbstractScript; +import org.gcube.data.access.storagehub.scripting.ScriptParameter; +import org.gcube.data.access.storagehub.scripting.ScriptUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CleanTrash implements AbstractScript{ + + private static Logger log = LoggerFactory.getLogger(CleanTrash.class); + + @Override + public String run(JackrabbitSession session, ScriptParameter prameters, ScriptUtil scriptUtil) { + StringBuilder stringBuilder = new StringBuilder(); + try { + Node node = session.getNode("/Home"); + + NodeIterator it = node.getNodes(); + + while (it.hasNext()) { + Node home = it.nextNode(); + + long start = System.currentTimeMillis(); + Path workspacePath = Paths.append(Paths.getPath(home.getPath()),"Workspace"); + + if (!session.nodeExists(workspacePath.toPath())) continue; + + + Node trashNode = session.getNode(Paths.append(workspacePath, "Trash").toPath()); + + List items = scriptUtil.getChildren(null, trashNode, null, true, TrashItem.class); + + for (Item item: items) { + TrashItem trashItem = (TrashItem) item; + if (System.currentTimeMillis() - trashItem.getDeletedTime().getTimeInMillis() > 2629800000l ) { + scriptUtil.removeNodes(session, Collections.singletonList(trashItem)); + + } + } + + } + }catch (Exception e) { + StringWriter writer = new StringWriter(); + PrintWriter sw = new PrintWriter(writer, true); + e.printStackTrace(sw); + return writer.toString(); + } + return stringBuilder.toString(); + } + +} diff --git a/src/main/java/MongoToMinioPorting.java b/src/main/java/MongoToMinioPorting.java new file mode 100644 index 0000000..5384f14 --- /dev/null +++ b/src/main/java/MongoToMinioPorting.java @@ -0,0 +1,4 @@ + +public class MongoToMinioPorting { + +}