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