From 9cf13543791863bac3cfbc97004681a9ca512c95 Mon Sep 17 00:00:00 2001 From: lucio Date: Wed, 14 Dec 2022 14:52:03 +0100 Subject: [PATCH] new scripts added --- pom.xml | 5 + src/main/java/BackupJackrabbit.java | 35 +++++++ src/main/java/GeneralScript.java | 17 +--- src/main/java/MoveSharedToMyShared.java | 126 ++++++++++++++++++++++++ 4 files changed, 167 insertions(+), 16 deletions(-) create mode 100644 src/main/java/BackupJackrabbit.java create mode 100644 src/main/java/MoveSharedToMyShared.java diff --git a/pom.xml b/pom.xml index c1920d3..b7e23aa 100644 --- a/pom.xml +++ b/pom.xml @@ -26,6 +26,11 @@ + + 1.8 + 1.8 + + org.gcube.data.access diff --git a/src/main/java/BackupJackrabbit.java b/src/main/java/BackupJackrabbit.java new file mode 100644 index 0000000..1829e4d --- /dev/null +++ b/src/main/java/BackupJackrabbit.java @@ -0,0 +1,35 @@ +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.io.StringWriter; + +import org.apache.jackrabbit.api.JackrabbitSession; +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 BackupJackrabbit implements AbstractScript{ + + private static Logger log = LoggerFactory.getLogger(BackupJackrabbit.class); + + @Override + public String run(JackrabbitSession session, ScriptParameter prameters, ScriptUtil scriptUtil) { + + StringBuilder stringBuilder = new StringBuilder(); + try { + try(OutputStream os = new ByteArrayOutputStream()){ + session.exportDocumentView("/", os, true, false); + stringBuilder.append(os.toString()); + } + }catch (Throwable e) { + log.error("error getting jcr backup",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/GeneralScript.java b/src/main/java/GeneralScript.java index aff1ec6..bcd9023 100644 --- a/src/main/java/GeneralScript.java +++ b/src/main/java/GeneralScript.java @@ -61,22 +61,7 @@ public class GeneralScript implements AbstractScript{ stringBuilder.append("\n").append(writer.toString()); } return stringBuilder.toString(); - - - /*StringBuilder stringBuilder = new StringBuilder(); - try { - try(OutputStream os = new ByteArrayOutputStream()){ - session.exportDocumentView("/", os, true, false); - stringBuilder.append(os.toString()); - } - }catch (Throwable e) { - log.error("error updating nodes",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/MoveSharedToMyShared.java b/src/main/java/MoveSharedToMyShared.java new file mode 100644 index 0000000..59a9d00 --- /dev/null +++ b/src/main/java/MoveSharedToMyShared.java @@ -0,0 +1,126 @@ +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.List; + +import javax.jcr.Node; +import javax.jcr.NodeIterator; + +import org.apache.jackrabbit.api.JackrabbitSession; +import org.gcube.common.storagehub.model.Excludes; +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.SharedFolder; +import org.gcube.data.access.storagehub.scripting.AbstractScript; +import org.gcube.data.access.storagehub.scripting.ScriptParameter; +import org.gcube.data.access.storagehub.scripting.ScriptUtil; + +public class MoveSharedToMyShared implements AbstractScript{ + + //private static Logger log = LoggerFactory.getLogger(MoveVREsAndTrash.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(); + + if (home.getProperty("hl:version").getLong()<1) { + stringBuilder.append(home.getPath()).append("cannot be update, first update to version 1\n"); + continue; + } + + if (!home.getPath().contains("lucio.lelii")) + continue; + + stringBuilder.append("\n").append("working on ").append(home.getPath()); + + long start = System.currentTimeMillis(); + Path workspacePath = Paths.append(Paths.getPath(home.getPath()),"Workspace"); + Path homePath = Paths.getPath(home.getPath()); + if (!session.nodeExists(workspacePath.toPath())) continue; + + if (home.getProperty("hl:version").getLong()<1) { + stringBuilder.append(home.getPath()).append("cannot be update, first update to version 1\n"); + continue; + } + + if (home.getProperty("hl:version").getLong()>1) { + stringBuilder.append(home.getPath()).append(" already updated\n"); + continue; + } + + String wsOwner = scriptUtil.getItem(session.getNode(workspacePath.toPath()), Excludes.ALL).getOwner(); + Node sharedWithMe; + if (home.hasNode("SharedWithMe")) + sharedWithMe = home.getNode("SharedWithMe"); + else sharedWithMe= scriptUtil.createInternalFolder(session, "SharedWithMe", "parent folder of folders shared with "+wsOwner, wsOwner, home.getIdentifier()); + + /*Node myShared; + if (home.hasNode("MyShared")) + myShared = home.getNode("MyShared"); + else myShared = scriptUtil.createInternalFolder(session, "MyShared", "parent folder of folders shared by "+wsOwner, wsOwner, home.getIdentifier()); + */ + + home.setProperty("hl:version", 2l); + + List sharedFolders = scriptUtil.getChildren(null, session.getNode(workspacePath.toPath()), Excludes.ALL, false, SharedFolder.class); + + + + for (Item item : sharedFolders) { + SharedFolder folder = (SharedFolder) item; + if (folder.isVreFolder()) continue; + if (!folder.getOwner().equals(wsOwner)) + /*{ + session.getWorkspace().clone(session.getWorkspace().getName(), folder.getPath(), Paths.append(Paths.getPath(myShared.getPath()), folder.getName()).toPath() , false); + stringBuilder.append("\n").append("moving ").append(folder.getTitle()).append(" to myshared"); + }else */{ + session.getWorkspace().clone(session.getWorkspace().getName(), folder.getPath(), Paths.append(Paths.getPath(sharedWithMe.getPath()), folder.getName()).toPath() , false); + stringBuilder.append("\n").append("moving ").append(folder.getTitle()).append(" to sharedWithMe"); + ((Node)folder.getRelatedNode()).removeShare(); + } + + } + + + + session.save(); + + stringBuilder.append("\n").append("sharedWithMe children: "); + for (Item item : scriptUtil.getChildren(null, sharedWithMe, Excludes.ALL, false,null)) { + stringBuilder.append(item.getTitle()).append(" - ").append(item.getId()).append(", "); + } +/* + stringBuilder.append("\n").append("MyShared children: "); + for (Item item : scriptUtil.getChildren(null, myShared, Excludes.ALL, false,null)) { + stringBuilder.append(item.getTitle()).append(" - ").append(item.getId()).append(", "); + } +*/ + /* + session.move("/Home/lucio.lelii/Workspace/MySpecialFolders", "/Home/lucio.lelii/VREs"); + + Node vres = session.getNode("/Home/lucio.lelii/VREs"); + + session.move("/Home/lucio.lelii/Workspace/Trash", "/Home/lucio.lelii/Trash"); + + Node trash = session.getNode("/Home/lucio.lelii/Trash"); + session.save();*/ + + + + } + }catch (Throwable e) { + StringWriter writer = new StringWriter(); + PrintWriter sw = new PrintWriter(writer, true); + e.printStackTrace(sw); + return writer.toString(); + } + return stringBuilder.toString(); + } +} \ No newline at end of file