new scripts added

This commit is contained in:
Lucio Lelii 2022-12-14 14:52:03 +01:00
parent 14e4f554a3
commit 9cf1354379
4 changed files with 167 additions and 16 deletions

View File

@ -26,6 +26,11 @@
<relativePath />
</parent>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.gcube.data.access</groupId>

View File

@ -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();
}
}

View File

@ -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();*/
}

View File

@ -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<Item> 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();
}
}