From fd45cb4583ef888b70b7907abf1955690962588d Mon Sep 17 00:00:00 2001 From: lucio lelii Date: Tue, 22 Nov 2022 17:21:01 +0100 Subject: [PATCH] script modfied --- src/main/java/MongoToMinioPorting.java | 82 ++++++++++++++++++++------ 1 file changed, 65 insertions(+), 17 deletions(-) diff --git a/src/main/java/MongoToMinioPorting.java b/src/main/java/MongoToMinioPorting.java index 0cd1226..8c96be6 100644 --- a/src/main/java/MongoToMinioPorting.java +++ b/src/main/java/MongoToMinioPorting.java @@ -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 items = scriptUtil.getChildren(null, session.getNode(workspacePath.toPath()), null, true,null); + List 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 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 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 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()); + } }