2 * Copyright (c) 2011 Greek Research and Technology Network
4 package gr.grnet.pithos.web.client.rest;
6 import gr.grnet.pithos.web.client.GSS;
7 import gr.grnet.pithos.web.client.InsufficientPermissionsException;
9 import java.util.ArrayList;
10 import java.util.HashMap;
11 import java.util.List;
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;
20 public abstract class MultipleDeleteCommand extends RestCommand {
23 Map<String, Throwable> errors = new HashMap<String, Throwable>();
25 List<String> successPaths = new ArrayList<String>();
29 public MultipleDeleteCommand(String[] pathToDelete){
30 this(pathToDelete, true);
33 public MultipleDeleteCommand(String[] pathToDelete, boolean showLoading){
34 setShowLoadingIndicator(showLoading);
35 if(isShowLoadingIndicator())
36 GSS.get().showLoadingIndicator("Deleting "+pathToDelete.length+" items",null);
38 for (final String pathg : pathToDelete) {
39 GWT.log("[DEL]"+pathg, null);
40 RequestBuilder builder = new RequestBuilder(RequestBuilder.DELETE, pathg);
43 handleHeaders(builder, pathg);
44 builder.sendRequest("", new RequestCallback() {
47 public void onError(Request arg0, Throwable arg1) {
48 errors.put(pathg, arg1);
52 public void onResponseReceived(Request arg0, Response arg1) {
53 if (arg1.getStatusCode() == 204)
54 successPaths.add(pathg);
55 else if(arg1.getStatusCode() == 403)
57 else if (arg1.getStatusCode() == 405)
58 errors.put(pathg, new InsufficientPermissionsException("You don't have permissions to delete this resource"));
60 errors.put(pathg, new RestException(pathg, arg1.getStatusCode(), arg1.getStatusText(), arg1.getText()));
64 } catch (Exception ex) {
65 errors.put(pathg, ex);
70 public boolean isComplete() {
71 return errors.size() + successPaths.size() == paths.length;
75 public boolean execute() {
76 boolean com = isComplete();
79 for(String p : errors.keySet())
80 onError(p, errors.get(p));
82 if(isShowLoadingIndicator())
83 GSS.get().hideLoadingIndicator();
89 public boolean hasErrors(){
90 return errors.size() >0;
95 * Retrieve the errors.
99 public Map<String, Throwable> getErrors() {
104 GWT.log("-ERRORS-->"+getErrors().size(), null);
105 for(String p : getErrors().keySet())
106 GWT.log("error:"+p, getErrors().get(p));
109 public abstract void onError(String path, Throwable throwable);