script modfied
This commit is contained in:
parent
f7d2b47936
commit
fd45cb4583
|
@ -7,16 +7,23 @@ import java.util.List;
|
||||||
import javax.jcr.Node;
|
import javax.jcr.Node;
|
||||||
import javax.jcr.NodeIterator;
|
import javax.jcr.NodeIterator;
|
||||||
import javax.jcr.PathNotFoundException;
|
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.apache.jackrabbit.api.JackrabbitSession;
|
||||||
import org.gcube.common.storagehub.model.Constants;
|
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.Path;
|
||||||
import org.gcube.common.storagehub.model.Paths;
|
import org.gcube.common.storagehub.model.Paths;
|
||||||
import org.gcube.common.storagehub.model.items.AbstractFileItem;
|
import org.gcube.common.storagehub.model.items.AbstractFileItem;
|
||||||
import org.gcube.common.storagehub.model.items.FolderItem;
|
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.Item;
|
||||||
import org.gcube.common.storagehub.model.items.TrashItem;
|
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.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.StorageBackend;
|
||||||
import org.gcube.common.storagehub.model.storages.StorageBackendFactory;
|
import org.gcube.common.storagehub.model.storages.StorageBackendFactory;
|
||||||
import org.gcube.data.access.storagehub.scripting.AbstractScript;
|
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) {
|
public String run(JackrabbitSession session, ScriptParameter prameters, ScriptUtil scriptUtil) {
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
try {
|
try {
|
||||||
|
|
||||||
Node node = session.getNode("/Home");
|
|
||||||
|
|
||||||
NodeIterator it = node.getNodes();
|
|
||||||
|
|
||||||
long startall = System.currentTimeMillis();
|
long startall = System.currentTimeMillis();
|
||||||
|
|
||||||
while (it.hasNext()) {
|
for (Node home: nodeToelaborate(session)) {
|
||||||
Node home = it.nextNode();
|
|
||||||
|
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
Path workspacePath = Paths.append(Paths.getPath(home.getPath()),"Workspace");
|
Path workspacePath = Paths.append(Paths.getPath(home.getPath()),"Workspace");
|
||||||
|
|
||||||
if (!session.nodeExists(workspacePath.toPath())) continue;
|
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)));
|
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 ) {
|
private String elaborateItem(Item item, JackrabbitSession session, ScriptUtil scriptUtil ) {
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
if (item instanceof FolderItem) {
|
/*if (item instanceof FolderItem) {
|
||||||
FolderItem folder = (FolderItem) item;
|
FolderItem folder = (FolderItem) item;
|
||||||
try {
|
try {
|
||||||
List<Item> items = scriptUtil.getChildren(null, (Node) folder.getRelatedNode(), null, true, null);
|
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());
|
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;
|
AbstractFileItem file = (AbstractFileItem) item;
|
||||||
if (file.getContent().getPayloadBackend().getStorageName()==Constants.MONGO_STORAGE) {
|
if (file.getContent().getPayloadBackend().getStorageName()==Constants.MONGO_STORAGE) {
|
||||||
stringBuilder.append("\n").append("elaborating file ").append(file.getId()).append(" - ").append(file.getTitle());
|
stringBuilder.append("\n").append("elaborating file ").append(file.getId()).append(" - ").append(file.getTitle());
|
||||||
try {
|
try {
|
||||||
Collection<StorageBackendFactory> factories = scriptUtil.getStorageBackendHandler();
|
Collection<StorageBackendFactory> factories = scriptUtil.getStorageBackendHandler();
|
||||||
StorageBackend mongo;
|
StorageBackend mongo = null;
|
||||||
StorageBackend minio;
|
StorageBackend minio= null;
|
||||||
for (StorageBackendFactory factory : factories) {
|
for (StorageBackendFactory factory : factories) {
|
||||||
if (factory.getName() == Constants.MONGO_STORAGE)
|
if (factory.getName() == Constants.MONGO_STORAGE)
|
||||||
mongo = factory.create(file.getContent().getPayloadBackend());
|
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));
|
minio = factory.create(new PayloadBackend(Constants.DEFAULT_MINIO_STORAGE, null));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
String mongoId = file.getContent().getStorageId();
|
||||||
//TODO: make real copy
|
MetaInfo info = minio.upload(mongo.download(mongoId), item.getParentPath() , item.getName() );
|
||||||
//TODO: change content
|
Content content = ((AbstractFileItem) item).getContent();
|
||||||
stringBuilder.append("\n").append("file copied ").append(file.getId()).append(" - ").append(file.getTitle());
|
|
||||||
}catch (Exception e) {
|
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());
|
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();
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue