The parent folder name is never send encoded.
[pithos] / src / gr / ebs / gss / client / rest / MultiplePostCommand.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 /**
36  * @author kman
37  *
38  */
39 public abstract class MultiplePostCommand extends RestCommand {
40
41
42         Map<String, Throwable> errors = new HashMap<String, Throwable>();
43
44         List<String> successPaths = new ArrayList<String>();
45
46         String[] paths;
47
48         public MultiplePostCommand(String[] pathToDelete, final int okStatusCode){
49                 this(pathToDelete, okStatusCode, true);
50         }
51
52         public MultiplePostCommand(String[] pathToDelete, String data, final int okStatusCode){
53                 this(pathToDelete, data, okStatusCode, true);
54         }
55
56         public MultiplePostCommand(String[] pathToDelete, final int okStatusCode, boolean showLoading){
57                 this(pathToDelete, "", okStatusCode, showLoading);
58         }
59
60         public MultiplePostCommand(String[] pathToDelete, String data, final int okStatusCode, boolean showLoading){
61                 setShowLoadingIndicator(showLoading);
62                 if(isShowLoadingIndicator())
63                         GSS.get().showLoadingIndicator();
64                 paths = pathToDelete;
65                 for (final String pathg : pathToDelete) {
66                         GWT.log("[DEL]"+pathg, null);
67                         RestRequestBuilder builder = new RestRequestBuilder("POST", pathg);
68
69                         try {
70                                 handleHeaders(builder, pathg);
71                                 builder.sendRequest(data, new RequestCallback() {
72
73                                         public void onError(Request arg0, Throwable arg1) {
74                                                 errors.put(pathg, arg1);
75                                         }
76
77                                         public void onResponseReceived(Request arg0, Response arg1) {
78                                                 if (arg1.getStatusCode() == okStatusCode)
79                                                         successPaths.add(pathg);
80                                                 else if(arg1.getStatusCode() == 403)
81                                                         sessionExpired();
82                                                 else if (arg1.getStatusCode() == 405)
83                                                         errors.put(pathg, new InsufficientPermissionsException("You don't have permissions to delete this resource"));
84                                                 else
85                                                         errors.put(pathg, new RestException(pathg, arg1.getStatusCode(), arg1.getStatusText(), arg1.getText()));
86                                         }
87
88                                 });
89                         } catch (Exception ex) {
90                                 errors.put(pathg, ex);
91                         }
92                 }
93         }
94
95         public boolean isComplete() {
96                 return errors.size() + successPaths.size() == paths.length;
97         }
98
99         public boolean execute() {
100                 boolean com = isComplete();
101                 if (com) {
102                         if(hasErrors())
103                                 for(String p : errors.keySet())
104                                         onError(p, errors.get(p));
105                         onComplete();
106                         if(isShowLoadingIndicator())
107                                 GSS.get().hideLoadingIndicator();
108                         return false;
109                 }
110                 return true;
111         }
112
113
114         /**
115          * @param p
116          * @param throwable
117          */
118         public abstract void onError(String p, Throwable throwable);
119
120         public boolean hasErrors(){
121                 return errors.size() >0;
122         }
123
124
125         /**
126          * Retrieve the errors.
127          *
128          * @return the errors
129          */
130         public Map<String, Throwable> getErrors() {
131                 return errors;
132         }
133
134         public void debug(){
135                 GWT.log("-ERRORS-->"+getErrors().size(), null);
136                 for(String p : getErrors().keySet())
137                         GWT.log("error:"+p, getErrors().get(p));
138         }
139
140         @Override
141         public void onError(Throwable t) {
142
143
144         }
145
146 }