e63f91fec1412103283c6cc282271026361dc584
[pithos] / src / gr / ebs / gss / web / client / rest / DeleteCommand.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.web.client.rest;
20
21 import gr.ebs.gss.web.client.GSS;
22 import gr.ebs.gss.common.exceptions.InsufficientPermissionsException;
23
24 import com.google.gwt.http.client.Request;
25 import com.google.gwt.http.client.RequestBuilder;
26 import com.google.gwt.http.client.RequestCallback;
27 import com.google.gwt.http.client.Response;
28
29
30 /**
31  * @author kman
32  *
33  */
34 public abstract class DeleteCommand extends RestCommand{
35
36         boolean complete = false;
37
38         public DeleteCommand(String pathToDelete){
39                 this(pathToDelete, true);
40         }
41
42
43         public DeleteCommand(String pathToDelete, boolean showLoading){
44                 setShowLoadingIndicator(showLoading);
45                 if(isShowLoadingIndicator())
46                         GSS.get().showLoadingIndicator();
47                 final String path;
48                 if(pathToDelete.endsWith("/"))
49                         path = pathToDelete;
50                 else
51                         path = pathToDelete+"/";
52                 RequestBuilder builder = new RequestBuilder(RequestBuilder.DELETE, path);
53
54                 try {
55                         handleHeaders(builder, path);
56                         builder.sendRequest("", new RequestCallback() {
57
58                                 @Override
59                                 public void onError(Request arg0, Throwable arg1) {
60                                         complete = true;
61                                         DeleteCommand.this.onError(arg1);
62                                 }
63
64                                 @Override
65                                 public void onResponseReceived(Request arg0, Response arg1) {
66                                         complete=true;
67                                         if(arg1.getStatusCode() == 204)
68                                                 onComplete();
69                                         else if(arg1.getStatusCode() == 403)
70                                                 sessionExpired();
71                                         else if(arg1.getStatusCode() == 405)
72                                                 DeleteCommand.this.onError(new InsufficientPermissionsException("You don't have permissions to delete this resource"));
73                                         else
74                                                 DeleteCommand.this.onError(new RestException(path, arg1.getStatusCode(), arg1.getStatusText(), arg1.getText()));
75                                 }
76
77                         });
78                 } catch (Exception ex) {
79                         complete=true;
80                         onError(ex);
81                 }
82         }
83
84         public boolean isComplete() {
85                 return complete;
86         }
87
88         @Override
89         public boolean execute() {
90                 boolean com = isComplete();
91                 if(com){
92                         if(isShowLoadingIndicator())
93                                 GSS.get().hideLoadingIndicator();
94                         return false;
95                 }
96                 return true;
97         }
98
99 }