diff --git a/src/main/java/MongoToMinioPortingNotShared.java b/src/main/java/MongoToMinioPortingNotShared.java index 933f25e..790ad84 100644 --- a/src/main/java/MongoToMinioPortingNotShared.java +++ b/src/main/java/MongoToMinioPortingNotShared.java @@ -3,10 +3,10 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.List; import javax.jcr.Node; +import javax.jcr.NodeIterator; import javax.jcr.Session; import javax.jcr.lock.LockManager; import javax.jcr.version.Version; @@ -17,6 +17,9 @@ 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.exceptions.StorageIdNotFoundException; import org.gcube.common.storagehub.model.items.AbstractFileItem; import org.gcube.common.storagehub.model.items.FolderItem; import org.gcube.common.storagehub.model.items.Item; @@ -28,12 +31,10 @@ import org.gcube.common.storagehub.model.storages.StorageBackendFactory; 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 MongoToMinioPortingNotShared implements AbstractScript{ - private static Logger log = LoggerFactory.getLogger(MongoToMinioPortingNotShared.class); + //private static Logger log = LoggerFactory.getLogger(MongoToMinioPortingNotShared.class); @Override public String run(JackrabbitSession session, ScriptParameter prameters, ScriptUtil scriptUtil) { @@ -42,13 +43,13 @@ public class MongoToMinioPortingNotShared implements AbstractScript{ long startall = System.currentTimeMillis(); for (Node home: nodeToelaborate(session)) { - + stringBuilder.append("\n").append("elaborated user home of ").append(home.getPath()); long start = System.currentTimeMillis(); Item item = scriptUtil.getItem(home, null); - stringBuilder.append(elaborateItem(item, session, scriptUtil)); + elaborateItem(item, session, scriptUtil, stringBuilder); - stringBuilder.append("\n").append("elaborated user home of ").append(home.getName()).append(" in ").append(System.currentTimeMillis()-start).append(" millis"); + stringBuilder.append("\n").append("elaborated user home of ").append(home.getPath()).append(" in ").append(System.currentTimeMillis()-start).append(" millis"); } @@ -63,18 +64,17 @@ public class MongoToMinioPortingNotShared implements AbstractScript{ } - private String elaborateItem(Item item, JackrabbitSession session, ScriptUtil scriptUtil ) { - StringBuilder stringBuilder = new StringBuilder(); + private void elaborateItem(Item item, JackrabbitSession session, ScriptUtil scriptUtil, StringBuilder stringBuilder ) { if (item instanceof FolderItem) { if (item.isShared()) { stringBuilder.append("\n").append("shared folder ").append(item.getId()).append(" skipped"); - return stringBuilder.toString(); + return; } FolderItem folder = (FolderItem) item; try { List items = scriptUtil.getChildren(null, (Node) folder.getRelatedNode(), null, true, null); stringBuilder.append("\n").append("elaborating folder ").append(folder.getId()).append(" - ").append(folder.getTitle()); - items.stream().forEach(i -> stringBuilder.append(elaborateItem(i, session, scriptUtil))); + items.stream().forEach(i -> elaborateItem(i, session, scriptUtil, stringBuilder)); }catch (Exception e) { stringBuilder.append("\n").append("error on folder ").append(folder.getId()).append(" - ").append(folder.getTitle()).append(e.getMessage()); @@ -104,8 +104,12 @@ public class MongoToMinioPortingNotShared implements AbstractScript{ String mongoId = file.getContent().getStorageId(); stringBuilder.append("\n").append("searching for content id ").append(mongoId); MetaInfo info; - try (InputStream is = mongo.download(mongoId)){ + + try (InputStream is = mongo.download(mongoId);){ info = minio.upload(is, file.getContent().getRemotePath() , item.getName(), mongoId, file.getContent().getSize() ); + }catch (StorageIdNotFoundException e) { + stringBuilder.append("\n").append("not found content id ").append(mongoId); + throw e; } Content content = ((AbstractFileItem) item).getContent(); @@ -172,26 +176,35 @@ public class MongoToMinioPortingNotShared implements AbstractScript{ stringBuilder.append("\n").append("error TOTAL on file ").append(item.getTitle()).append(e.getMessage()); } } - return stringBuilder.toString(); } private List nodeToelaborate(JackrabbitSession session) throws Exception{ - Node node = session.getNode("/Home/leonardo.candela/Workspace"); + Node home = session.getNode("/Home"); - //Path workspacePath = Paths.append(Paths.getPath(home.getPath()),"Workspace"); - - //if (!session.nodeExists(workspacePath.toPath())) continue; - - //NodeIterator it = node.getNodes(); - return Collections.singletonList(node); + NodeIterator it = home.getNodes(); + + List homes = new ArrayList<>(); + while (it.hasNext()) { + + Node userHome = it.nextNode(); + Path workspacePath = Paths.append(Paths.getPath(userHome.getPath()),"Workspace"); + + if (!session.nodeExists(workspacePath.toPath())) + continue; + + + homes.add(session.getNode(workspacePath.toPath())); + } + + return homes; } - private void checkinContentNode(Node node) throws Exception{ +/* 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();