Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / rest / MultipleHeadCommand.java @ a853017c

History | View | Annotate | Download (5 kB)

1
/*
2
 *  Copyright (c) 2011 Greek Research and Technology Network
3
 */
4
package gr.grnet.pithos.web.client.rest;
5

    
6
import gr.grnet.pithos.web.client.GSS;
7
import gr.grnet.pithos.web.client.rest.MultipleGetCommand.Cached;
8
import gr.grnet.pithos.web.client.rest.resource.FileResource;
9
import gr.grnet.pithos.web.client.rest.resource.FolderResource;
10
import gr.grnet.pithos.web.client.rest.resource.GroupResource;
11
import gr.grnet.pithos.web.client.rest.resource.GroupUserResource;
12
import gr.grnet.pithos.web.client.rest.resource.GroupsResource;
13
import gr.grnet.pithos.web.client.rest.resource.RestResource;
14
import gr.grnet.pithos.web.client.rest.resource.SharedResource;
15
import gr.grnet.pithos.web.client.rest.resource.TrashResource;
16
import gr.grnet.pithos.web.client.rest.resource.UserResource;
17

    
18
import java.util.ArrayList;
19
import java.util.HashMap;
20
import java.util.List;
21
import java.util.Map;
22

    
23
import com.google.gwt.core.client.GWT;
24
import com.google.gwt.http.client.Response;
25
import com.google.gwt.user.client.DeferredCommand;
26

    
27

    
28
public abstract class MultipleHeadCommand <T extends RestResource> extends RestCommand {
29
        String[] paths;
30
        Class<T> aclass;
31
        List<T> result = new ArrayList<T>();
32
        Map<String, Throwable> errors = new HashMap<String, Throwable>();
33
        private boolean requestSent=false;
34
        Cached[] cached;
35

    
36
        public MultipleHeadCommand(Class<T> theClass, String[] pathToGet, Cached[] theCached) {
37
                this(theClass, pathToGet, true, theCached);
38
        }
39

    
40
        public MultipleHeadCommand(Class<T> theClass, String[] pathToGet, boolean showLoading, Cached[] theCached) {
41
                setShowLoadingIndicator(showLoading);
42
                if(isShowLoadingIndicator())
43
                        GSS.get().showLoadingIndicator("Getting "+pathToGet.length+" items", null);
44
                paths = pathToGet;
45
                this.aclass = theClass;
46
                this.cached = theCached;
47
                //sendRequest();
48
        }
49

    
50
        private void sendRequest() {
51
                if(requestSent)
52
                        return;
53
                requestSent=true;
54
                if(cached!=null)
55
                        for (final Cached c : cached){
56
                                final String path;
57
                                if(aclass.equals(FileResource.class)){
58
                                        if(c.uri.indexOf("?") == -1)
59
                                                path=c.uri+"?"+Math.random();
60
                                        else
61
                                                path=c.uri;
62
                                }
63
                                else
64
                                        path = fixPath(c.uri);
65
                                DeferredCommand.addCommand(new HeadCommand<T>(aclass,path,false, (T)c.cache) {
66

    
67
                                        @Override
68
                                        public void onComplete() {
69
                                                MultipleHeadCommand.this.result.add(getResult());
70
                                        }
71

    
72
                                        @Override
73
                                        public void onError(Throwable t) {
74
                                                errors.put(path, t);
75
                                        }
76

    
77
                                });
78
                        }
79
                else
80
                        for (String pathg : paths) {
81
                                final String path;
82
                                if(aclass.equals(FileResource.class))
83
                                        path = pathg;
84
                                else
85
                                        path = fixPath(pathg);
86
                                DeferredCommand.addCommand(new HeadCommand<T>(aclass,path,false, null) {
87
                                        @Override
88
                                        public void onComplete() {
89
                                                MultipleHeadCommand.this.result.add(getResult());
90
                                        }
91

    
92
                                        @Override
93
                                        public void onError(Throwable t) {
94
                                                errors.put(path, t);
95
                                        }
96
                                });
97
                        }
98
        }
99
        public boolean isComplete() {
100
                return result.size()+errors.size() == paths.length;
101
        }
102

    
103
        public List<T> getResult() {
104
                return result;
105
        }
106

    
107
        @Override
108
        public boolean execute() {
109
                if(!requestSent)
110
                        sendRequest();
111
                boolean com = isComplete();
112
                if (com) {
113
                        if(isShowLoadingIndicator())
114
                                GSS.get().hideLoadingIndicator();
115
                        if(hasErrors())
116
                                for(String p : errors.keySet())
117
                                        onError(p, errors.get(p));
118
                        onComplete();
119
                        return false;
120
                }
121
                return true;
122
        }
123

    
124
        /**
125
         * @param p
126
         * @param throwable
127
         */
128
        public abstract void onError(String p, Throwable throwable);
129

    
130
        public Object deserializeResponse(String path, Response response) {
131
                RestResource result1 = null;
132
                if (aclass.equals(FolderResource.class)) {
133
                        result1 = new FolderResource(path);
134
                        result1.createFromJSON(response.getText());
135
                } else if (aclass.equals(FileResource.class)) {
136
                        result1 = new FileResource(path);
137
                        result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
138
                } else if (aclass.equals(GroupsResource.class)) {
139
                        result1 = new GroupsResource(path);
140
                        result1.createFromJSON(response.getText());
141
                } else if (aclass.equals(TrashResource.class)) {
142
                        result1 = new TrashResource(path);
143
                        result1.createFromJSON(response.getText());
144
                } else if (aclass.equals(SharedResource.class)) {
145
                        result1 = new SharedResource(path);
146
                        result1.createFromJSON(response.getText());
147
                } else if (aclass.equals(GroupResource.class)) {
148
                        result1 = new GroupResource(path);
149
                        result1.createFromJSON(response.getText());
150
                } else if (aclass.equals(GroupUserResource.class)) {
151
                        result1 = new GroupUserResource(path);
152
                        result1.createFromJSON(response.getText());
153
                } else if (aclass.equals(UserResource.class)) {
154
                        result1 = new UserResource(path);
155
                        result1.createFromJSON(response.getText());
156
                }
157
                return result1;
158
        }
159

    
160
        public boolean hasErrors(){
161
                return errors.size() >0;
162
        }
163

    
164
        /**
165
         * Retrieve the errors.
166
         *
167
         * @return the errors
168
         */
169
        public Map<String, Throwable> getErrors() {
170
                return errors;
171
        }
172

    
173
        public void debug(){
174
                GWT.log("--->"+result.size(), null);
175
                GWT.log("-ERRORS-->"+getErrors().size(), null);
176
                for(String p : getErrors().keySet())
177
                        GWT.log("error:"+p, getErrors().get(p));
178
        }
179
}