root / src / org / gss_project / gss / web / client / rest / MultipleDeleteCommand.java @ 1206:292dec4eae08
History | View | Annotate | Download (3.5 kB)
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 org.gss_project.gss.web.client.rest; |
20 |
|
21 |
import org.gss_project.gss.web.client.GSS; |
22 |
import org.gss_project.gss.common.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 |
* @author kman
|
37 |
*/
|
38 |
public abstract class MultipleDeleteCommand extends RestCommand { |
39 |
|
40 |
|
41 |
Map<String, Throwable> errors = new HashMap<String, Throwable>(); |
42 |
|
43 |
List<String> successPaths = new ArrayList<String>(); |
44 |
|
45 |
String[] paths; |
46 |
|
47 |
public MultipleDeleteCommand(String[] pathToDelete){ |
48 |
this(pathToDelete, true); |
49 |
} |
50 |
|
51 |
public MultipleDeleteCommand(String[] pathToDelete, boolean showLoading){ |
52 |
setShowLoadingIndicator(showLoading); |
53 |
if(isShowLoadingIndicator())
|
54 |
GSS.get().showLoadingIndicator("Deleting "+pathToDelete.length+" items",null); |
55 |
paths = pathToDelete; |
56 |
for (final String pathg : pathToDelete) { |
57 |
GWT.log("[DEL]"+pathg, null); |
58 |
RequestBuilder builder = new RequestBuilder(RequestBuilder.DELETE, pathg);
|
59 |
|
60 |
try {
|
61 |
handleHeaders(builder, pathg); |
62 |
builder.sendRequest("", new RequestCallback() { |
63 |
|
64 |
@Override
|
65 |
public void onError(Request arg0, Throwable arg1) { |
66 |
errors.put(pathg, arg1); |
67 |
} |
68 |
|
69 |
@Override
|
70 |
public void onResponseReceived(Request arg0, Response arg1) { |
71 |
if (arg1.getStatusCode() == 204) |
72 |
successPaths.add(pathg); |
73 |
else if(arg1.getStatusCode() == 403) |
74 |
sessionExpired(); |
75 |
else if (arg1.getStatusCode() == 405) |
76 |
errors.put(pathg, new InsufficientPermissionsException("You don't have permissions to delete this resource")); |
77 |
else
|
78 |
errors.put(pathg, new RestException(pathg, arg1.getStatusCode(), arg1.getStatusText(), arg1.getText()));
|
79 |
} |
80 |
|
81 |
}); |
82 |
} catch (Exception ex) { |
83 |
errors.put(pathg, ex); |
84 |
} |
85 |
} |
86 |
} |
87 |
|
88 |
public boolean isComplete() { |
89 |
return errors.size() + successPaths.size() == paths.length;
|
90 |
} |
91 |
|
92 |
@Override
|
93 |
public boolean execute() { |
94 |
boolean com = isComplete();
|
95 |
if (com) {
|
96 |
if(hasErrors())
|
97 |
for(String p : errors.keySet()) |
98 |
onError(p, errors.get(p)); |
99 |
onComplete(); |
100 |
if(isShowLoadingIndicator())
|
101 |
GSS.get().hideLoadingIndicator(); |
102 |
return false; |
103 |
} |
104 |
return true; |
105 |
} |
106 |
|
107 |
public boolean hasErrors(){ |
108 |
return errors.size() >0; |
109 |
} |
110 |
|
111 |
|
112 |
/**
|
113 |
* Retrieve the errors.
|
114 |
*
|
115 |
* @return the errors
|
116 |
*/
|
117 |
public Map<String, Throwable> getErrors() { |
118 |
return errors;
|
119 |
} |
120 |
|
121 |
public void debug(){ |
122 |
GWT.log("-ERRORS-->"+getErrors().size(), null); |
123 |
for(String p : getErrors().keySet()) |
124 |
GWT.log("error:"+p, getErrors().get(p));
|
125 |
} |
126 |
|
127 |
public abstract void onError(String path, Throwable throwable); |
128 |
|
129 |
} |