Use separate progress bars next to each file, in order to better track the progress...
[pithos] / src / gr / ebs / gss / client / rest / MultipleDeleteCommand.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.InsufficientPermissionsException;
23
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import com.google.gwt.core.client.GWT;
30 import com.google.gwt.http.client.Request;
31 import com.google.gwt.http.client.RequestCallback;
32 import com.google.gwt.http.client.Response;
33
34 /**
35  * @author kman
36  */
37 public abstract class MultipleDeleteCommand extends RestCommand {
38
39
40         Map<String, Throwable> errors = new HashMap<String, Throwable>();
41
42         List<String> successPaths = new ArrayList<String>();
43
44         String[] paths;
45
46         public MultipleDeleteCommand(String[] pathToDelete){
47                 this(pathToDelete, true);
48         }
49
50         public MultipleDeleteCommand(String[] pathToDelete, boolean showLoading){
51                 setShowLoadingIndicator(showLoading);
52                 if(isShowLoadingIndicator())
53                         GSS.get().showLoadingIndicator();
54                 paths = pathToDelete;
55                 for (final String pathg : pathToDelete) {
56                         GWT.log("[DEL]"+pathg, null);
57                         RestRequestBuilder builder = new RestRequestBuilder("DELETE", pathg);
58
59                         try {
60                                 handleHeaders(builder, pathg);
61                                 builder.sendRequest("", new RequestCallback() {
62
63                                         public void onError(Request arg0, Throwable arg1) {
64                                                 errors.put(pathg, arg1);
65                                         }
66
67                                         public void onResponseReceived(Request arg0, Response arg1) {
68                                                 if (arg1.getStatusCode() == 204)
69                                                         successPaths.add(pathg);
70                                                 else if(arg1.getStatusCode() == 403)
71                                                         sessionExpired();
72                                                 else if (arg1.getStatusCode() == 405)
73                                                         errors.put(pathg, new InsufficientPermissionsException("You don't have permissions to delete this resource"));
74                                                 else
75                                                         errors.put(pathg, new RestException(pathg, arg1.getStatusCode(), arg1.getStatusText(), arg1.getText()));
76                                         }
77
78                                 });
79                         } catch (Exception ex) {
80                                 errors.put(pathg, ex);
81                         }
82                 }
83         }
84
85         public boolean isComplete() {
86                 return errors.size() + successPaths.size() == paths.length;
87         }
88
89         public boolean execute() {
90                 boolean com = isComplete();
91                 if (com) {
92                         if(hasErrors())
93                                 for(String p : errors.keySet())
94                                         onError(p, errors.get(p));
95                         onComplete();
96                         if(isShowLoadingIndicator())
97                                 GSS.get().hideLoadingIndicator();
98                         return false;
99                 }
100                 return true;
101         }
102
103         public boolean hasErrors(){
104                 return errors.size() >0;
105         }
106
107
108         /**
109          * Retrieve the errors.
110          *
111          * @return the errors
112          */
113         public Map<String, Throwable> getErrors() {
114                 return errors;
115         }
116
117         public void debug(){
118                 GWT.log("-ERRORS-->"+getErrors().size(), null);
119                 for(String p : getErrors().keySet())
120                         GWT.log("error:"+p, getErrors().get(p));
121         }
122
123         public abstract void onError(String path, Throwable throwable);
124
125 }