Improved rounding for human-readable percentOfFreeSpace().
[pithos] / src / gr / ebs / gss / client / rest / MultipleHeadCommand.java
1 /*
2  * Copyright 2009 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 package gr.ebs.gss.client.rest;
20
21 import gr.ebs.gss.client.GSS;
22 import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
23 import gr.ebs.gss.client.rest.resource.FileResource;
24 import gr.ebs.gss.client.rest.resource.FolderResource;
25 import gr.ebs.gss.client.rest.resource.GroupResource;
26 import gr.ebs.gss.client.rest.resource.GroupUserResource;
27 import gr.ebs.gss.client.rest.resource.GroupsResource;
28 import gr.ebs.gss.client.rest.resource.RestResource;
29 import gr.ebs.gss.client.rest.resource.SharedResource;
30 import gr.ebs.gss.client.rest.resource.TrashResource;
31 import gr.ebs.gss.client.rest.resource.UserResource;
32
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37
38 import com.google.gwt.core.client.GWT;
39 import com.google.gwt.http.client.Request;
40 import com.google.gwt.http.client.Response;
41
42
43 /**
44  * @author kman
45  *
46  */
47 public abstract class MultipleHeadCommand <T extends RestResource> extends RestCommand {
48         String[] paths;
49         Class<T> aclass;
50         List<T> result = new ArrayList<T>();
51         Map<String, Throwable> errors = new HashMap<String, Throwable>();
52
53         public MultipleHeadCommand(Class<T> aclass, String[] pathToGet){
54                 this(aclass, pathToGet, true);
55         }
56
57         public MultipleHeadCommand(Class<T> aclass, String[] pathToGet, boolean showLoading){
58                 setShowLoadingIndicator(showLoading);
59                 if(isShowLoadingIndicator())
60                         GSS.get().showLoadingIndicator();
61                 paths = pathToGet;
62                 this.aclass = aclass;
63                 for (String pathg : pathToGet) {
64                         final String path;
65                         if(aclass.equals(FileResource.class))
66                                 path = pathg;
67                         else
68                                 path = fixPath(pathg);
69                         RestRequestBuilder builder = new RestRequestBuilder("HEAD", path);
70
71                         try {
72                                 handleHeaders(builder, path);
73                                 builder.sendRequest("", new RestCallback(path) {
74
75                                         public Object deserialize(Response response) {
76                                                 return deserializeResponse(path, response);
77                                         }
78
79                                         public void handleError(Request request, Throwable exception) {
80                                                 errors.put(path, exception);
81                                         }
82
83                                         public void handleSuccess(Object object) {
84                                                 if(object!= null)
85                                                         result.add((T)object);
86                                                 else
87                                                         errors.put(path, new ObjectNotFoundException("resource not found"));
88
89
90                                         }
91
92                                 });
93                         } catch (Exception ex) {
94                                 errors.put(path, ex);
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         public boolean execute() {
108                 boolean com = isComplete();
109                 if (com) {
110                         if(isShowLoadingIndicator())
111                                 GSS.get().hideLoadingIndicator();
112                         if(hasErrors())
113                                 for(String p : errors.keySet())
114                                         onError(p, errors.get(p));
115                         onComplete();
116                         return false;
117                 }
118                 return true;
119         }
120
121         /**
122          * @param p
123          * @param throwable
124          */
125         public abstract void onError(String p, Throwable throwable);
126
127         public Object deserializeResponse(String path, Response response) {
128                 RestResource result1 = null;
129                 if (aclass.equals(FolderResource.class)) {
130                         result1 = new FolderResource(path);
131                         result1.createFromJSON(response.getText());
132
133                 } else if (aclass.equals(FileResource.class)) {
134                         result1 = new FileResource(path);
135                         result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
136                 } else if (aclass.equals(GroupsResource.class)) {
137                         result1 = new GroupsResource(path);
138                         result1.createFromJSON(response.getText());
139                 } else if (aclass.equals(TrashResource.class)) {
140                         result1 = new TrashResource(path);
141                         result1.createFromJSON(response.getText());
142
143                 } else if (aclass.equals(SharedResource.class)) {
144                         result1 = new SharedResource(path);
145                         result1.createFromJSON(response.getText());
146
147                 } else if (aclass.equals(GroupResource.class)) {
148                         result1 = new GroupResource(path);
149                         result1.createFromJSON(response.getText());
150
151                 } else if (aclass.equals(GroupUserResource.class)) {
152                         result1 = new GroupUserResource(path);
153                         result1.createFromJSON(response.getText());
154
155                 } else if (aclass.equals(UserResource.class)) {
156                         result1 = new UserResource(path);
157                         result1.createFromJSON(response.getText());
158
159                 }
160                 return result1;
161
162         }
163
164         public boolean hasErrors(){
165                 return errors.size() >0;
166         }
167
168
169         /**
170          * Retrieve the errors.
171          *
172          * @return the errors
173          */
174         public Map<String, Throwable> getErrors() {
175                 return errors;
176         }
177
178         public void debug(){
179                 GWT.log("--->"+result.size(), null);
180                 GWT.log("-ERRORS-->"+getErrors().size(), null);
181                 for(String p : getErrors().keySet())
182                         GWT.log("error:"+p, getErrors().get(p));
183         }
184 }