diff --git a/src/main/java/org/gcube/common/storagehub/client/dsl/ItemContainer.java b/src/main/java/org/gcube/common/storagehub/client/dsl/ItemContainer.java index 35d9032..a22f193 100644 --- a/src/main/java/org/gcube/common/storagehub/client/dsl/ItemContainer.java +++ b/src/main/java/org/gcube/common/storagehub/client/dsl/ItemContainer.java @@ -2,6 +2,7 @@ package org.gcube.common.storagehub.client.dsl; import org.gcube.common.storagehub.client.StreamDescriptor; import org.gcube.common.storagehub.client.proxies.ItemManagerClient; +import org.gcube.common.storagehub.model.Metadata; import org.gcube.common.storagehub.model.items.FolderItem; import org.gcube.common.storagehub.model.items.Item; @@ -12,6 +13,8 @@ public abstract class ItemContainer { protected String itemId = null; protected I item; + boolean invalidated = false; + protected ItemContainer(ItemManagerClient itemclient, I item) { this.itemclient = itemclient; this.itemId = item.getId(); @@ -36,14 +39,18 @@ public abstract class ItemContainer { @SuppressWarnings("unchecked") public I get() { - if (item==null) return (I)itemclient.get(itemId); + if (item==null || invalidated) { + I toRet = (I)itemclient.get(itemId); + invalidated = false; + return toRet; + } else return item; } - private I get(boolean reset) { - this.item = (I)itemclient.get(itemId); - return this.item; + protected void invalidateItem() { + invalidated = true; } + public StreamDescriptor download(String ... nodeIdsToExclude) { return itemclient.download(this.itemId, nodeIdsToExclude); @@ -55,18 +62,22 @@ public abstract class ItemContainer { public void delete() { itemclient.delete(this.itemId); + invalidateItem(); } - public OpenResolver rename(String newName) { + public void rename(String newName) { itemclient.rename(this.getId(), newName); - return new OpenResolver(this.get(true), itemclient); + invalidateItem(); } - public OpenResolver move(FolderContainer folder) { + public void move(FolderContainer folder) { itemclient.move(this.getId(), folder.getId()); - return new OpenResolver(this.get(true), itemclient); - - + invalidateItem(); + } + + public void setMetadata(Metadata metadata) { + itemclient.setMetadata(this.getId(), metadata); + invalidateItem(); } public FolderContainer getRootSharedFolder() { diff --git a/src/main/java/org/gcube/common/storagehub/client/proxies/DefaultItemManager.java b/src/main/java/org/gcube/common/storagehub/client/proxies/DefaultItemManager.java index 6c555aa..4244088 100644 --- a/src/main/java/org/gcube/common/storagehub/client/proxies/DefaultItemManager.java +++ b/src/main/java/org/gcube/common/storagehub/client/proxies/DefaultItemManager.java @@ -18,6 +18,7 @@ import org.gcube.common.clients.delegates.ProxyDelegate; import org.gcube.common.gxrest.request.GXWebTargetAdapterRequest; import org.gcube.common.gxrest.response.inbound.GXInboundResponse; import org.gcube.common.storagehub.client.StreamDescriptor; +import org.gcube.common.storagehub.model.Metadata; import org.gcube.common.storagehub.model.acls.ACL; import org.gcube.common.storagehub.model.acls.AccessType; import org.gcube.common.storagehub.model.annotations.RootNode; @@ -736,5 +737,34 @@ public class DefaultItemManager implements ItemManagerClient { throw new RuntimeException(e); } } + + + @Override + public String setMetadata(String id, Metadata metadata) { + Call call = new Call() { + @Override + public String call(GXWebTargetAdapterRequest manager) throws Exception { + GXWebTargetAdapterRequest myManager = manager.path(id) + .path("metadata"); + + + GXInboundResponse response = myManager.put(Entity.json(metadata)); + + if (response.isErrorResponse()) { + if (response.hasException()) + throw response.getException(); + else + throw new BackendGenericError(); + } + + return response.getSource().readEntity(String.class); + } + }; + try { + return delegate.make(call); + }catch(Exception e) { + throw new RuntimeException(e); + } + } } diff --git a/src/main/java/org/gcube/common/storagehub/client/proxies/ItemManagerClient.java b/src/main/java/org/gcube/common/storagehub/client/proxies/ItemManagerClient.java index 3fc6cfc..de7b900 100644 --- a/src/main/java/org/gcube/common/storagehub/client/proxies/ItemManagerClient.java +++ b/src/main/java/org/gcube/common/storagehub/client/proxies/ItemManagerClient.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Set; import org.gcube.common.storagehub.client.StreamDescriptor; +import org.gcube.common.storagehub.model.Metadata; import org.gcube.common.storagehub.model.acls.ACL; import org.gcube.common.storagehub.model.acls.AccessType; import org.gcube.common.storagehub.model.items.Item; @@ -63,4 +64,6 @@ public interface ItemManagerClient { StreamDescriptor downloadSpecificVersion(String id, String version); + String setMetadata(String id,Metadata metadata); + } diff --git a/src/test/java/org/gcube/data/access/fs/Items.java b/src/test/java/org/gcube/data/access/fs/Items.java index ba05b62..00b020c 100644 --- a/src/test/java/org/gcube/data/access/fs/Items.java +++ b/src/test/java/org/gcube/data/access/fs/Items.java @@ -202,6 +202,9 @@ public class Items { } + + + /* static String baseUrl="http://workspace-repository1-d.d4science.org/storagehub"; diff --git a/src/test/java/org/gcube/data/access/fs/TestCall.java b/src/test/java/org/gcube/data/access/fs/TestCall.java index d28d41d..3907f23 100644 --- a/src/test/java/org/gcube/data/access/fs/TestCall.java +++ b/src/test/java/org/gcube/data/access/fs/TestCall.java @@ -7,6 +7,7 @@ import java.io.FileOutputStream; import java.net.URI; import java.nio.file.Files; import java.util.Arrays; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.regex.Matcher; @@ -24,6 +25,7 @@ import org.gcube.common.storagehub.client.StreamDescriptor; import org.gcube.common.storagehub.client.plugins.AbstractPlugin; import org.gcube.common.storagehub.client.proxies.ItemManagerClient; import org.gcube.common.storagehub.client.proxies.WorkspaceManagerClient; +import org.gcube.common.storagehub.model.Metadata; import org.gcube.common.storagehub.model.Paths; import org.gcube.common.storagehub.model.acls.ACL; import org.gcube.common.storagehub.model.acls.AccessType; @@ -131,6 +133,19 @@ public class TestCall { System.in.read(); } + @Test + public void setMetadata() { + final ItemManagerClient client = AbstractPlugin.item().build(); + Metadata meta = new Metadata(); + HashMap prop = new HashMap<>(); + prop.put("folderProp", "test2"); + prop.put("folderProp2", "test2"); + meta.setValues(prop); + client.setMetadata("8822478a-4fd3-41d5-87de-9ff161d0935e", meta); + + + } + @Test public void upload() throws Exception{