2 * Copyright 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
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.
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.
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/>.
19 package gr.ebs.gss.client.rest;
21 import gr.ebs.gss.client.GSS;
22 import gr.ebs.gss.client.exceptions.InsufficientPermissionsException;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
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;
39 public abstract class MultiplePostCommand extends RestCommand {
42 Map<String, Throwable> errors = new HashMap<String, Throwable>();
44 List<String> successPaths = new ArrayList<String>();
48 public MultiplePostCommand(String[] pathToDelete, final int okStatusCode){
49 this(pathToDelete, okStatusCode, true);
52 public MultiplePostCommand(String[] pathToDelete, final int okStatusCode, boolean showLoading){
53 setShowLoadingIndicator(showLoading);
54 if(isShowLoadingIndicator())
55 GSS.get().showLoadingIndicator();
57 for (final String pathg : pathToDelete) {
58 GWT.log("[DEL]"+pathg, null);
59 RestRequestBuilder builder = new RestRequestBuilder("POST", pathg);
62 handleHeaders(builder, pathg);
63 builder.sendRequest("", new RequestCallback() {
65 public void onError(Request arg0, Throwable arg1) {
66 errors.put(pathg, arg1);
69 public void onResponseReceived(Request arg0, Response arg1) {
70 if (arg1.getStatusCode() == okStatusCode)
71 successPaths.add(pathg);
72 else if(arg1.getStatusCode() == 403)
74 else if (arg1.getStatusCode() == 405)
75 errors.put(pathg, new InsufficientPermissionsException("You don't have permissions to delete this resource"));
77 errors.put(pathg, new RestException(pathg, arg1.getStatusCode(), arg1.getStatusText(), arg1.getText()));
81 } catch (Exception ex) {
82 errors.put(pathg, ex);
87 public boolean isComplete() {
88 return errors.size() + successPaths.size() == paths.length;
91 public boolean execute() {
92 boolean com = isComplete();
95 for(String p : errors.keySet())
96 onError(p, errors.get(p));
98 if(isShowLoadingIndicator())
99 GSS.get().hideLoadingIndicator();
110 public abstract void onError(String p, Throwable throwable);
112 public boolean hasErrors(){
113 return errors.size() >0;
118 * Retrieve the errors.
122 public Map<String, Throwable> getErrors() {
127 GWT.log("-ERRORS-->"+getErrors().size(), null);
128 for(String p : getErrors().keySet())
129 GWT.log("error:"+p, getErrors().get(p));
133 public void onError(Throwable t) {