select row on right click
[pithos] / src / gr / ebs / gss / client / rest / MultipleHeadCommand.java
index 17c6beb..ba02d39 100644 (file)
@@ -19,7 +19,7 @@
 package gr.ebs.gss.client.rest;
 
 import gr.ebs.gss.client.GSS;
-import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
+import gr.ebs.gss.client.rest.MultipleGetCommand.Cached;
 import gr.ebs.gss.client.rest.resource.FileResource;
 import gr.ebs.gss.client.rest.resource.FolderResource;
 import gr.ebs.gss.client.rest.resource.GroupResource;
@@ -36,8 +36,8 @@ import java.util.List;
 import java.util.Map;
 
 import com.google.gwt.core.client.GWT;
-import com.google.gwt.http.client.Request;
 import com.google.gwt.http.client.Response;
+import com.google.gwt.user.client.DeferredCommand;
 
 
 /**
@@ -49,53 +49,72 @@ public abstract class MultipleHeadCommand <T extends RestResource> extends RestC
        Class<T> aclass;
        List<T> result = new ArrayList<T>();
        Map<String, Throwable> errors = new HashMap<String, Throwable>();
+       private boolean requestSent=false;
+       Cached[] cached;
 
-       public MultipleHeadCommand(Class<T> aclass, String[] pathToGet){
-               this(aclass, pathToGet, true);
+       public MultipleHeadCommand(Class<T> theClass, String[] pathToGet, Cached[] theCached) {
+               this(theClass, pathToGet, true, theCached);
        }
 
-       public MultipleHeadCommand(Class<T> aclass, String[] pathToGet, boolean showLoading){
+       public MultipleHeadCommand(Class<T> theClass, String[] pathToGet, boolean showLoading, Cached[] theCached) {
                setShowLoadingIndicator(showLoading);
                if(isShowLoadingIndicator())
                        GSS.get().showLoadingIndicator();
                paths = pathToGet;
-               this.aclass = aclass;
-               for (String pathg : pathToGet) {
-                       final String path;
-                       if(aclass.equals(FileResource.class))
-                               path = pathg;
-                       else
-                               path = fixPath(pathg);
-                       RestRequestBuilder builder = new RestRequestBuilder("HEAD", path);
-
-                       try {
-                               handleHeaders(builder, path);
-                               builder.sendRequest("", new RestCallback(path) {
-
-                                       public Object deserialize(Response response) {
-                                               return deserializeResponse(path, response);
-                                       }
+               this.aclass = theClass;
+               this.cached = theCached;
+               //sendRequest();
+       }
 
-                                       public void handleError(Request request, Throwable exception) {
-                                               errors.put(path, exception);
+       private void sendRequest() {
+               if(requestSent)
+                       return;
+               requestSent=true;
+               if(cached!=null)
+                       for (final Cached c : cached){
+                               final String path;
+                               if(aclass.equals(FileResource.class)){
+                                       if(c.uri.indexOf("?") == -1)
+                                               path=c.uri+"?"+Math.random();
+                                       else
+                                               path=c.uri;
+                               }
+                               else
+                                       path = fixPath(c.uri);
+                               DeferredCommand.addCommand(new HeadCommand<T>(aclass,path,false, (T)c.cache) {
+
+                                       @Override
+                                       public void onComplete() {
+                                               MultipleHeadCommand.this.result.add(getResult());
                                        }
 
-                                       public void handleSuccess(Object object) {
-                                               if(object!= null)
-                                                       result.add((T)object);
-                                               else
-                                                       errors.put(path, new ObjectNotFoundException("resource not found"));
-
+                                       @Override
+                                       public void onError(Throwable t) {
+                                               errors.put(path, t);
+                                       }
 
+                               });
+                       }
+               else
+                       for (String pathg : paths) {
+                               final String path;
+                               if(aclass.equals(FileResource.class))
+                                       path = pathg;
+                               else
+                                       path = fixPath(pathg);
+                               DeferredCommand.addCommand(new HeadCommand<T>(aclass,path,false, null) {
+                                       @Override
+                                       public void onComplete() {
+                                               MultipleHeadCommand.this.result.add(getResult());
                                        }
 
+                                       @Override
+                                       public void onError(Throwable t) {
+                                               errors.put(path, t);
+                                       }
                                });
-                       } catch (Exception ex) {
-                               errors.put(path, ex);
                        }
-               }
        }
-
        public boolean isComplete() {
                return result.size()+errors.size() == paths.length;
        }
@@ -104,7 +123,10 @@ public abstract class MultipleHeadCommand <T extends RestResource> extends RestC
                return result;
        }
 
+       @Override
        public boolean execute() {
+               if(!requestSent)
+                       sendRequest();
                boolean com = isComplete();
                if (com) {
                        if(isShowLoadingIndicator())
@@ -129,7 +151,6 @@ public abstract class MultipleHeadCommand <T extends RestResource> extends RestC
                if (aclass.equals(FolderResource.class)) {
                        result1 = new FolderResource(path);
                        result1.createFromJSON(response.getText());
-
                } else if (aclass.equals(FileResource.class)) {
                        result1 = new FileResource(path);
                        result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
@@ -139,33 +160,26 @@ public abstract class MultipleHeadCommand <T extends RestResource> extends RestC
                } else if (aclass.equals(TrashResource.class)) {
                        result1 = new TrashResource(path);
                        result1.createFromJSON(response.getText());
-
                } else if (aclass.equals(SharedResource.class)) {
                        result1 = new SharedResource(path);
                        result1.createFromJSON(response.getText());
-
                } else if (aclass.equals(GroupResource.class)) {
                        result1 = new GroupResource(path);
                        result1.createFromJSON(response.getText());
-
                } else if (aclass.equals(GroupUserResource.class)) {
                        result1 = new GroupUserResource(path);
                        result1.createFromJSON(response.getText());
-
                } else if (aclass.equals(UserResource.class)) {
                        result1 = new UserResource(path);
                        result1.createFromJSON(response.getText());
-
                }
                return result1;
-
        }
 
        public boolean hasErrors(){
                return errors.size() >0;
        }
 
-
        /**
         * Retrieve the errors.
         *