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