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.RequestBuilder;
32 import com.google.gwt.http.client.RequestCallback;
33 import com.google.gwt.http.client.Response;
40 public abstract class MultiplePostCommand extends RestCommand {
43 Map<String, Throwable> errors = new HashMap<String, Throwable>();
45 List<String> successPaths = new ArrayList<String>();
49 public MultiplePostCommand(String[] pathToDelete, final int okStatusCode){
50 this(pathToDelete, okStatusCode, true);
53 public MultiplePostCommand(String[] pathToDelete, String data, final int okStatusCode){
54 this(pathToDelete, data, okStatusCode, true);
57 public MultiplePostCommand(String[] pathToDelete, final int okStatusCode, boolean showLoading){
58 this(pathToDelete, "", okStatusCode, showLoading);
61 public MultiplePostCommand(String[] pathToDelete, String data, final int okStatusCode, boolean showLoading){
62 setShowLoadingIndicator(showLoading);
63 if(isShowLoadingIndicator())
64 GSS.get().showLoadingIndicator();
66 for (final String pathg : pathToDelete) {
67 GWT.log("[DEL]"+pathg, null);
68 RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, pathg);
71 handleHeaders(builder, pathg);
72 builder.sendRequest(data, new RequestCallback() {
75 public void onError(Request arg0, Throwable arg1) {
76 errors.put(pathg, arg1);
80 public void onResponseReceived(Request arg0, Response arg1) {
81 if (arg1.getStatusCode() == okStatusCode)
82 successPaths.add(pathg);
83 else if(arg1.getStatusCode() == 403)
85 else if (arg1.getStatusCode() == 405)
86 errors.put(pathg, new InsufficientPermissionsException("You don't have permissions to delete this resource"));
88 errors.put(pathg, new RestException(pathg, arg1.getStatusCode(), arg1.getStatusText(), arg1.getText()));
92 } catch (Exception ex) {
93 errors.put(pathg, ex);
98 public boolean isComplete() {
99 return errors.size() + successPaths.size() == paths.length;
103 public boolean execute() {
104 boolean com = isComplete();
107 for(String p : errors.keySet())
108 onError(p, errors.get(p));
110 if(isShowLoadingIndicator())
111 GSS.get().hideLoadingIndicator();
122 public abstract void onError(String p, Throwable throwable);
124 public boolean hasErrors(){
125 return errors.size() >0;
130 * Retrieve the errors.
134 public Map<String, Throwable> getErrors() {
139 GWT.log("-ERRORS-->"+getErrors().size(), null);
140 for(String p : getErrors().keySet())
141 GWT.log("error:"+p, getErrors().get(p));
145 public void onError(Throwable t) {