script modfied

This commit is contained in:
Lucio Lelii 2022-11-22 17:21:01 +01:00
parent f7d2b47936
commit fd45cb4583
1 changed files with 65 additions and 17 deletions

View File

@ -7,16 +7,23 @@ import java.util.List;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException;
import javax.jcr.Session;
import javax.jcr.lock.LockManager;
import javax.jcr.version.VersionManager;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.gcube.common.storagehub.model.Constants;
import org.gcube.common.storagehub.model.NodeConstants;
import org.gcube.common.storagehub.model.Path;
import org.gcube.common.storagehub.model.Paths;
import org.gcube.common.storagehub.model.items.AbstractFileItem;
import org.gcube.common.storagehub.model.items.FolderItem;
import org.gcube.common.storagehub.model.items.ImageFile;
import org.gcube.common.storagehub.model.items.Item;
import org.gcube.common.storagehub.model.items.TrashItem;
import org.gcube.common.storagehub.model.items.nodes.Content;
import org.gcube.common.storagehub.model.items.nodes.PayloadBackend;
import org.gcube.common.storagehub.model.storages.MetaInfo;
import org.gcube.common.storagehub.model.storages.StorageBackend;
import org.gcube.common.storagehub.model.storages.StorageBackendFactory;
import org.gcube.data.access.storagehub.scripting.AbstractScript;
@ -33,22 +40,16 @@ public class MongoToMinioPorting implements AbstractScript{
public String run(JackrabbitSession session, ScriptParameter prameters, ScriptUtil scriptUtil) {
StringBuilder stringBuilder = new StringBuilder();
try {
Node node = session.getNode("/Home");
NodeIterator it = node.getNodes();
long startall = System.currentTimeMillis();
while (it.hasNext()) {
Node home = it.nextNode();
for (Node home: nodeToelaborate(session)) {
long start = System.currentTimeMillis();
Path workspacePath = Paths.append(Paths.getPath(home.getPath()),"Workspace");
if (!session.nodeExists(workspacePath.toPath())) continue;
List<Item> items = scriptUtil.getChildren(null, session.getNode(workspacePath.toPath()), null, true,null);
List<Item> items = scriptUtil.getChildren(null, session.getNode(workspacePath.toPath()), null, true, null);
items.parallelStream().forEach(i -> stringBuilder.append(elaborateItem(i, session, scriptUtil)));
@ -69,7 +70,7 @@ public class MongoToMinioPorting implements AbstractScript{
private String elaborateItem(Item item, JackrabbitSession session, ScriptUtil scriptUtil ) {
StringBuilder stringBuilder = new StringBuilder();
if (item instanceof FolderItem) {
/*if (item instanceof FolderItem) {
FolderItem folder = (FolderItem) item;
try {
List<Item> items = scriptUtil.getChildren(null, (Node) folder.getRelatedNode(), null, true, null);
@ -80,14 +81,15 @@ public class MongoToMinioPorting implements AbstractScript{
stringBuilder.append("\n").append("error on folder ").append(folder.getId()).append(" - ").append(folder.getTitle()).append(e.getMessage());
}
} else if (item instanceof AbstractFileItem) {
} else*/
if (item instanceof AbstractFileItem) {
AbstractFileItem file = (AbstractFileItem) item;
if (file.getContent().getPayloadBackend().getStorageName()==Constants.MONGO_STORAGE) {
stringBuilder.append("\n").append("elaborating file ").append(file.getId()).append(" - ").append(file.getTitle());
try {
Collection<StorageBackendFactory> factories = scriptUtil.getStorageBackendHandler();
StorageBackend mongo;
StorageBackend minio;
StorageBackend mongo = null;
StorageBackend minio= null;
for (StorageBackendFactory factory : factories) {
if (factory.getName() == Constants.MONGO_STORAGE)
mongo = factory.create(file.getContent().getPayloadBackend());
@ -95,11 +97,38 @@ public class MongoToMinioPorting implements AbstractScript{
minio = factory.create(new PayloadBackend(Constants.DEFAULT_MINIO_STORAGE, null));
}
}
//TODO: make real copy
//TODO: change content
stringBuilder.append("\n").append("file copied ").append(file.getId()).append(" - ").append(file.getTitle());
}catch (Exception e) {
String mongoId = file.getContent().getStorageId();
MetaInfo info = minio.upload(mongo.download(mongoId), item.getParentPath() , item.getName() );
Content content = ((AbstractFileItem) item).getContent();
content.setPayloadBackend(info.getPayloadBackend());
Node contentNode = ((Node) item.getRelatedNode()).getNode(NodeConstants.CONTENT_NAME);
boolean canBeDeleted = true;
LockManager lockMan = session.getWorkspace().getLockManager();
lockMan.lock(((Node) item.getRelatedNode()).getPath(), true, true, -1, "");
try {
checkoutContentNode(contentNode);
scriptUtil.updateContentNode(content, contentNode );
stringBuilder.append("\n").append("file copied ").append(file.getId()).append(" - ").append(file.getPath());
session.save();
} catch (Throwable e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
stringBuilder.append("\n").append("ERROR copying file ").append(file.getId()).append(" - ").append(file.getPath()).append(" || ").append(sw.toString());
canBeDeleted = false;
} finally {
lockMan.unlock(((Node) item.getRelatedNode()).getPath());
}
if (canBeDeleted)
mongo.delete(mongoId);
else minio.delete(info.getStorageId());
}catch (Throwable e) {
stringBuilder.append("\n").append("error on file ").append(file.getId()).append(" - ").append(file.getTitle()).append(e.getMessage());
}
}
@ -107,4 +136,23 @@ public class MongoToMinioPorting implements AbstractScript{
return stringBuilder.toString();
}
private List<Node> nodeToelaborate(JackrabbitSession session) throws Exception{
Node node = session.getNode("/Home/andrea.rossi");
//NodeIterator it = node.getNodes();
return Collections.singletonList(node);
}
private void checkinContentNode(Node node) throws Exception{
Session session = node.getSession();
VersionManager versionManager = session.getWorkspace().getVersionManager();
versionManager.checkin(node.getPath());
}
private void checkoutContentNode(Node node) throws Exception{
Session session = node.getSession();
VersionManager versionManager = session.getWorkspace().getVersionManager();
versionManager.checkout(node.getPath());
}
}