Implemented tag assignment to files
authorChristos Stathis <chstath@ebs.gr>
Thu, 21 Jul 2011 15:24:43 +0000 (18:24 +0300)
committerChristos Stathis <chstath@ebs.gr>
Thu, 21 Jul 2011 15:24:43 +0000 (18:24 +0300)
web_client/src/gr/grnet/pithos/web/client/AbstractPropertiesDialog.java
web_client/src/gr/grnet/pithos/web/client/FileList.java
web_client/src/gr/grnet/pithos/web/client/FilePropertiesDialog.java
web_client/src/gr/grnet/pithos/web/client/Pithos.java
web_client/src/gr/grnet/pithos/web/client/foldertree/File.java
web_client/src/gr/grnet/pithos/web/client/foldertree/Folder.java
web_client/src/gr/grnet/pithos/web/client/tagtree/TagTreeViewModel.java

index 33671ef..b63350a 100644 (file)
@@ -90,8 +90,6 @@ public abstract class AbstractPropertiesDialog extends DialogBox {
        }\r
        /**\r
         * Retrieves all user tags from the server and updates the FlowPanel\r
-        *\r
-        * @param userId\r
         */\r
        protected void updateTags() {\r
                GetCommand<TagsResource> tc = new GetCommand<TagsResource>(TagsResource.class, Pithos.get().getCurrentUserResource().getTagsPath(),null) {\r
index fd2f876..a43e66e 100644 (file)
@@ -663,6 +663,7 @@ public class FileList extends Composite {
         Folder selectedItem = treeView.getSelection();
 
         provider.setList(files);
+        selectionModel.clear();
        }
 
        /**
index 86e0d26..380f19c 100644 (file)
 package gr.grnet.pithos.web.client;\r
 \r
 import com.google.gwt.core.client.Scheduler;\r
+import com.google.gwt.json.client.JSONArray;\r
+import com.google.gwt.json.client.JSONString;\r
+import com.google.gwt.user.client.ui.Anchor;\r
+import com.google.gwt.user.client.ui.Label;\r
 import gr.grnet.pithos.web.client.foldertree.File;\r
 import gr.grnet.pithos.web.client.foldertree.Resource;\r
 import gr.grnet.pithos.web.client.rest.PostCommand;\r
+import gr.grnet.pithos.web.client.rest.PostRequest;\r
 import gr.grnet.pithos.web.client.rest.PutRequest;\r
 import gr.grnet.pithos.web.client.rest.RestException;\r
 \r
@@ -62,6 +67,8 @@ import com.google.gwt.user.client.ui.HasHorizontalAlignment;
 import com.google.gwt.user.client.ui.HorizontalPanel;\r
 import com.google.gwt.user.client.ui.TextBox;\r
 import com.google.gwt.user.client.ui.VerticalPanel;\r
+import gr.grnet.pithos.web.client.tagtree.Tag;\r
+import java.util.Iterator;\r
 \r
 /**\r
  * The 'File properties' dialog box implementation.\r
@@ -170,15 +177,6 @@ public class FilePropertiesDialog extends AbstractPropertiesDialog {
 \r
         focusPanel.setFocus(true);\r
         setWidget(outer);\r
-\r
-               // Asynchronously retrieve the tags defined by this user.\r
-               DeferredCommand.addCommand(new Command() {\r
-\r
-                       @Override\r
-                       public void execute() {\r
-//                             updateTags();\r
-                       }\r
-               });\r
        }\r
 \r
     private VerticalPanel createGeneralPanel() {\r
@@ -188,7 +186,7 @@ public class FilePropertiesDialog extends AbstractPropertiesDialog {
         generalTable.setText(1, 0, "Folder");\r
         generalTable.setText(2, 0, "Owner");\r
         generalTable.setText(3, 0, "Last modified");\r
-//        generalTable.setText(4, 0, "Tags");\r
+        generalTable.setText(4, 0, "Tags");\r
 \r
         name.setWidth("100%");\r
         name.setText(file.getName());\r
@@ -202,31 +200,26 @@ public class FilePropertiesDialog extends AbstractPropertiesDialog {
         final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");\r
         generalTable.setText(3, 1, formatter.format(file.getLastModified()));\r
 \r
-               // Get the tags.\r
-//             StringBuffer tagsBuffer = new StringBuffer();\r
-//             Iterator i = file.getTags().iterator();\r
-//             while (i.hasNext()) {\r
-//                     String tag = (String) i.next();\r
-//                     tagsBuffer.append(tag).append(", ");\r
-//             }\r
-//             if (tagsBuffer.length() > 1)\r
-//                     tagsBuffer.delete(tagsBuffer.length() - 2, tagsBuffer.length() - 1);\r
-//             initialTagText = tagsBuffer.toString();\r
-//             tags.setWidth("100%");\r
-//             tags.getElement().setId("filePropertiesDialog.textBox.tags");\r
-//             tags.setText(initialTagText);\r
-//             generalTable.setWidget(4, 1, tags);\r
+               StringBuffer tagsBuffer = new StringBuffer();\r
+        for (String t : file.getTags())\r
+                       tagsBuffer.append(t).append(", ");\r
+               if (tagsBuffer.length() > 1)\r
+                       tagsBuffer.delete(tagsBuffer.length() - 2, tagsBuffer.length() - 1);\r
+               initialTagText = tagsBuffer.toString();\r
+               tags.setWidth("100%");\r
+               tags.setText(initialTagText);\r
+               generalTable.setWidget(4, 1, tags);\r
 \r
         generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");\r
         generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");\r
         generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");\r
         generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");\r
-//        generalTable.getFlexCellFormatter().setStyleName(4, 0, "props-labels");\r
+        generalTable.getFlexCellFormatter().setStyleName(4, 0, "props-labels");\r
         generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");\r
         generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");\r
         generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");\r
         generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");\r
-//        generalTable.getFlexCellFormatter().setStyleName(4, 1, "props-values");\r
+        generalTable.getFlexCellFormatter().setStyleName(4, 1, "props-values");\r
         generalTable.setCellSpacing(4);\r
 \r
         generalPanel.add(generalTable);\r
@@ -234,6 +227,28 @@ public class FilePropertiesDialog extends AbstractPropertiesDialog {
         DisclosurePanel allTags = new DisclosurePanel("All tags");\r
         allTagsContent = new FlowPanel();\r
         allTagsContent.setWidth("100%");\r
+        for (Tag t : app.getAllTags()) {\r
+            final Anchor tagAnchor = new Anchor(t.getName(), false);\r
+            tagAnchor.addStyleName("pithos-tag");\r
+            allTagsContent.add(tagAnchor);\r
+            Label separator = new Label(", ");\r
+            separator.addStyleName("pithos-tag");\r
+            allTagsContent.add(separator);\r
+            tagAnchor.addClickHandler(new ClickHandler() {\r
+\r
+                @Override\r
+                public void onClick(ClickEvent event) {\r
+                    String existing = tags.getText().trim();\r
+                    if (MULTIPLE_VALUES_TEXT.equals(existing))\r
+                        existing = "";\r
+                    String newTag = tagAnchor.getText().trim();\r
+                    // insert the new tag only if it is not in the list\r
+                    // already\r
+                    if (existing.indexOf(newTag) == -1)\r
+                        tags.setText(existing + (existing.length() > 0 ? ", " : "") + newTag);\r
+                }\r
+            });\r
+        }\r
         allTags.setContent(allTagsContent);\r
         generalPanel.add(allTags);\r
         generalPanel.setSpacing(4);\r
@@ -389,8 +404,8 @@ public class FilePropertiesDialog extends AbstractPropertiesDialog {
 //             permList.updatePermissionsAccordingToInput();\r
 //             Set<PermissionHolder> perms = permList.getPermissions();\r
 //             JSONObject json = new JSONObject();\r
-               if (!name.getText().equals(file.getName())) {\r
-                       newFilename = name.getText();\r
+               if (!name.getText().trim().equals(file.getName())) {\r
+                       newFilename = name.getText().trim();\r
 //                     json.put("name", new JSONString(newFilename));\r
                }\r
 //             if (versioned.getValue() != file.isVersioned())\r
@@ -418,28 +433,44 @@ public class FilePropertiesDialog extends AbstractPropertiesDialog {
 //                     }\r
 //                     json.put("permissions", perma);\r
 //             }\r
-//             JSONArray taga = new JSONArray();\r
-//             i = 0;\r
-//             if (!tags.getText().equals(initialTagText)) {\r
-//                     String[] tagset = tags.getText().split(",");\r
-//                     for (String t : tagset) {\r
-//                             JSONString to = new JSONString(t);\r
-//                             taga.set(i, to);\r
-//                             i++;\r
-//                     }\r
-//                     json.put("tags", taga);\r
-//             }\r
+        String[] tagset = null;\r
+               if (!tags.getText().equals(initialTagText))\r
+                       tagset = tags.getText().split(",");\r
 //             String jsonString = json.toString();\r
 //             if(jsonString.equals("{}")){\r
 //                     GWT.log("NO CHANGES", null);\r
 //                     return;\r
 //             }\r
-               final String newFilenameFinal = newFilename;\r
+        final String[] newTags = tagset;\r
+\r
+        if (newFilename != null) {\r
+            final String path = app.getApiPath() + app.getUsername() + file.getParent().getUri() + "/" + newFilename;\r
+            PutRequest updateFile = new PutRequest(path) {\r
+                @Override\r
+                public void onSuccess(Resource result) {\r
+                    if (newTags != null)\r
+                        updateMetaData(path + "?update=", newTags);\r
+                    else\r
+                        app.updateFolder(file.getParent());\r
+                }\r
+\r
+                @Override\r
+                public void onError(Throwable t) {\r
+                    GWT.log("", t);\r
+                    app.displayError("System error modifying file:" + t.getMessage());\r
+                }\r
+            };\r
+            updateFile.setHeader("X-Auth-Token", app.getToken());\r
+            updateFile.setHeader("X-Move-From", file.getUri());\r
+            updateFile.setHeader("Content-Type", file.getContentType());\r
+            Scheduler.get().scheduleDeferred(updateFile);\r
+        }\r
+        else if (newTags != null)\r
+            updateMetaData(app.getApiPath() + app.getUsername() + file.getUri() + "?update=", newTags);\r
+       }\r
 \r
-        if (newFilename == null)\r
-            return;\r
-        String path = app.getApiPath() + app.getUsername() + file.getParent().getUri() + "/" + newFilename;\r
-        PutRequest updateFile = new PutRequest(path) {\r
+    private void updateMetaData(String path, String[] newTags) {\r
+        PostRequest updateFile = new PostRequest(path) {\r
             @Override\r
             public void onSuccess(Resource result) {\r
                 app.updateFolder(file.getParent());\r
@@ -452,10 +483,10 @@ public class FilePropertiesDialog extends AbstractPropertiesDialog {
             }\r
         };\r
         updateFile.setHeader("X-Auth-Token", app.getToken());\r
-        updateFile.setHeader("X-Move-From", file.getUri());\r
-        updateFile.setHeader("Content-Type", file.getContentType());\r
+        for (String t : newTags)\r
+            updateFile.setHeader("X-Object-Meta-" + t.trim(), "true");\r
         Scheduler.get().scheduleDeferred(updateFile);\r
-       }\r
+    }\r
 \r
        private void removeAllOldVersions() {\r
                JSONObject json = new JSONObject();\r
index 9898ccc..7d93af1 100644 (file)
@@ -74,6 +74,7 @@ import gr.grnet.pithos.web.client.tagtree.TagTreeView;
 import gr.grnet.pithos.web.client.tagtree.TagTreeViewModel;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -141,7 +142,17 @@ public class Pithos implements EntryPoint, ResizeHandler {
     }
 
     public void updateTags() {
-        tagTreeViewModel.initialize(account);
+        tagTreeViewModel.initialize(getAllTags());
+    }
+
+    public List<Tag> getAllTags() {
+        List<Tag> tagList = new ArrayList<Tag>();
+        for (Folder f : account.getContainers()) {
+            for (String t : f.getTags()) {
+                tagList.add(new Tag(t));
+            }
+        }
+        return tagList;
     }
 
     /**
@@ -461,7 +472,7 @@ public class Pithos implements EntryPoint, ResizeHandler {
 
 //        Window.Location.assign(GWT.getModuleBaseURL() + conf.loginUrl() + "?next=" + Window.Location.getHref());
         Cookies.setCookie(conf.authCookie(), "test" + conf.cookieSeparator() + "0000");
-        Window.Location.assign(GWT.getModuleBaseURL() + "Pithos.html");
+        Window.Location.assign(GWT.getModuleBaseURL() + "pithos.html");
        }
 
     private void fetchAccount() {
index 9a17ba8..a3ce501 100644 (file)
@@ -35,6 +35,7 @@
 
 package gr.grnet.pithos.web.client.foldertree;
 
+import com.google.gwt.http.client.Header;
 import com.google.gwt.http.client.Response;
 import com.google.gwt.i18n.client.DateTimeFormat;
 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
@@ -42,7 +43,11 @@ import com.google.gwt.i18n.client.NumberFormat;
 import com.google.gwt.json.client.JSONObject;
 import com.google.gwt.json.client.JSONParser;
 import com.google.gwt.json.client.JSONValue;
+import java.io.StringWriter;
+import java.security.Key;
 import java.util.Date;
+import java.util.HashSet;
+import java.util.Set;
 
 public class File extends Resource {
     private String name;
@@ -71,6 +76,8 @@ public class File extends Resource {
 
     private Folder parent;
 
+    private Set<String> tags = new HashSet<String>();
+
     public String getContentType() {
         return contentType;
     }
@@ -149,6 +156,10 @@ public class File extends Resource {
         modifiedBy = unmarshallString(o, "modified_by");
         versionTimestamp = unmarshallDate(o, "version_timestamp");
         this.container = container;
+
+        for (String key : o.keySet())
+            if (key.startsWith("x_object_meta_") && !key.equals("x_object_meta_trash"))
+                tags.add(key.substring("x_object_meta_".length()).trim().toLowerCase());
     }
 
     public boolean equals(Object other) {
@@ -173,6 +184,12 @@ public class File extends Resource {
     }
 
     private void populate(Response response) {
+        for (Header h : response.getHeaders()) {
+            String header = h.getName();
+            if (header.startsWith("X-Object-Meta-") && !header.equals("X-Object-Meta-Trash"))
+                tags.add(header.substring("X-Object-Meta-".length()).trim().toLowerCase());
+
+        }
         String header = response.getHeader("X-Object-Meta-Trash");
         if (header != null)
             inTrash = Boolean.valueOf(header);
@@ -186,4 +203,8 @@ public class File extends Resource {
     public Folder getParent() {
         return parent;
     }
+
+    public Set<String> getTags() {
+        return tags;
+    }
 }
index 2998336..7c89ab6 100644 (file)
@@ -142,14 +142,10 @@ public class Folder extends Resource {
             inTrash = true;
 
         header = response.getHeader("X-Container-Object-Meta");
-        if (header != null && header.length() > 2) {
-            String tagStr = header.substring(1, header.length() - 1);
-            while (tagStr.indexOf(",") > -1) {
-                String tag = tagStr.substring(2, tagStr.indexOf(",") - 1);
-                tags.add(tag);
-                tagStr = tagStr.substring(tagStr.indexOf(",") + 1).trim();
+        if (header != null && header.length() > 0) {
+            for (String t : header.split(",")) {
+                tags.add(t.toLowerCase().trim());
             }
-            tags.add(tagStr.substring(2, tagStr.length() - 1));
         }
 
         subfolders.clear(); //This is necessary in case we update a pre-existing Folder so that stale subfolders won't show up
index c74efbd..e0a6bcb 100644 (file)
@@ -203,12 +203,8 @@ public class TagTreeViewModel implements TreeViewModel {
         }
     }
 
-    public void initialize(AccountResource account) {
-        List<Tag> tagList = tagDataProvider.getList();
-        for (Folder f : account.getContainers()) {
-            for (String t : f.getTags())
-                tagList.add(new Tag(t));
-        }
+    public void initialize(List<Tag> allTags) {
+        tagDataProvider.getList().addAll(allTags);
         rootDataProvider.getList().add("Tags");
     }
 }