Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / rest / MultipleDeleteCommand.java @ bddcde7b

History | View | Annotate | Download (3.3 kB)

1 a52ea5e4 pastith
/*
2 a52ea5e4 pastith
 * Copyright 2009 Electronic Business Systems Ltd.
3 a52ea5e4 pastith
 *
4 a52ea5e4 pastith
 * This file is part of GSS.
5 a52ea5e4 pastith
 *
6 a52ea5e4 pastith
 * GSS is free software: you can redistribute it and/or modify
7 a52ea5e4 pastith
 * it under the terms of the GNU General Public License as published by
8 a52ea5e4 pastith
 * the Free Software Foundation, either version 3 of the License, or
9 a52ea5e4 pastith
 * (at your option) any later version.
10 a52ea5e4 pastith
 *
11 a52ea5e4 pastith
 * GSS is distributed in the hope that it will be useful,
12 a52ea5e4 pastith
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 a52ea5e4 pastith
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 a52ea5e4 pastith
 * GNU General Public License for more details.
15 a52ea5e4 pastith
 *
16 a52ea5e4 pastith
 * You should have received a copy of the GNU General Public License
17 a52ea5e4 pastith
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18 a52ea5e4 pastith
 */
19 a52ea5e4 pastith
package gr.ebs.gss.client.rest;
20 a52ea5e4 pastith
21 a52ea5e4 pastith
import gr.ebs.gss.client.GSS;
22 a52ea5e4 pastith
import gr.ebs.gss.client.exceptions.InsufficientPermissionsException;
23 a52ea5e4 pastith
24 a52ea5e4 pastith
import java.util.ArrayList;
25 a52ea5e4 pastith
import java.util.HashMap;
26 a52ea5e4 pastith
import java.util.List;
27 a52ea5e4 pastith
import java.util.Map;
28 a52ea5e4 pastith
29 a52ea5e4 pastith
import com.google.gwt.core.client.GWT;
30 a52ea5e4 pastith
import com.google.gwt.http.client.Request;
31 a52ea5e4 pastith
import com.google.gwt.http.client.RequestCallback;
32 a52ea5e4 pastith
import com.google.gwt.http.client.Response;
33 a52ea5e4 pastith
34 a52ea5e4 pastith
/**
35 a52ea5e4 pastith
 * @author kman
36 a52ea5e4 pastith
 */
37 895035a2 pastith
public abstract class MultipleDeleteCommand extends RestCommand {
38 a52ea5e4 pastith
39 a52ea5e4 pastith
40 a52ea5e4 pastith
        Map<String, Throwable> errors = new HashMap<String, Throwable>();
41 a52ea5e4 pastith
42 a52ea5e4 pastith
        List<String> successPaths = new ArrayList<String>();
43 a52ea5e4 pastith
44 a52ea5e4 pastith
        String[] paths;
45 a52ea5e4 pastith
46 895035a2 pastith
        public MultipleDeleteCommand(String[] pathToDelete){
47 aa8608d3 koutsoub
                this(pathToDelete, true);
48 aa8608d3 koutsoub
        }
49 aa8608d3 koutsoub
50 895035a2 pastith
        public MultipleDeleteCommand(String[] pathToDelete, boolean showLoading){
51 aa8608d3 koutsoub
                setShowLoadingIndicator(showLoading);
52 aa8608d3 koutsoub
                if(isShowLoadingIndicator())
53 aa8608d3 koutsoub
                        GSS.get().showLoadingIndicator();
54 a52ea5e4 pastith
                paths = pathToDelete;
55 a52ea5e4 pastith
                for (final String pathg : pathToDelete) {
56 a52ea5e4 pastith
                        GWT.log("[DEL]"+pathg, null);
57 a52ea5e4 pastith
                        RestRequestBuilder builder = new RestRequestBuilder("DELETE", pathg);
58 a52ea5e4 pastith
59 a52ea5e4 pastith
                        try {
60 a52ea5e4 pastith
                                handleHeaders(builder, pathg);
61 a52ea5e4 pastith
                                builder.sendRequest("", new RequestCallback() {
62 a52ea5e4 pastith
63 a52ea5e4 pastith
                                        public void onError(Request arg0, Throwable arg1) {
64 a52ea5e4 pastith
                                                errors.put(pathg, arg1);
65 a52ea5e4 pastith
                                        }
66 a52ea5e4 pastith
67 a52ea5e4 pastith
                                        public void onResponseReceived(Request arg0, Response arg1) {
68 a52ea5e4 pastith
                                                if (arg1.getStatusCode() == 204)
69 a52ea5e4 pastith
                                                        successPaths.add(pathg);
70 05f65955 koutsoub
                                                else if(arg1.getStatusCode() == 403)
71 05f65955 koutsoub
                                                        sessionExpired();
72 a52ea5e4 pastith
                                                else if (arg1.getStatusCode() == 405)
73 a52ea5e4 pastith
                                                        errors.put(pathg, new InsufficientPermissionsException("You don't have permissions to delete this resource"));
74 a52ea5e4 pastith
                                                else
75 a52ea5e4 pastith
                                                        errors.put(pathg, new RestException(pathg, arg1.getStatusCode(), arg1.getStatusText(), arg1.getText()));
76 a52ea5e4 pastith
                                        }
77 a52ea5e4 pastith
78 a52ea5e4 pastith
                                });
79 a52ea5e4 pastith
                        } catch (Exception ex) {
80 a52ea5e4 pastith
                                errors.put(pathg, ex);
81 a52ea5e4 pastith
                        }
82 a52ea5e4 pastith
                }
83 a52ea5e4 pastith
        }
84 a52ea5e4 pastith
85 a52ea5e4 pastith
        public boolean isComplete() {
86 a52ea5e4 pastith
                return errors.size() + successPaths.size() == paths.length;
87 a52ea5e4 pastith
        }
88 a52ea5e4 pastith
89 a52ea5e4 pastith
        public boolean execute() {
90 a52ea5e4 pastith
                boolean com = isComplete();
91 a52ea5e4 pastith
                if (com) {
92 a52ea5e4 pastith
                        if(hasErrors())
93 a52ea5e4 pastith
                                for(String p : errors.keySet())
94 a52ea5e4 pastith
                                        onError(p, errors.get(p));
95 a52ea5e4 pastith
                        onComplete();
96 aa8608d3 koutsoub
                        if(isShowLoadingIndicator())
97 aa8608d3 koutsoub
                                GSS.get().hideLoadingIndicator();
98 a52ea5e4 pastith
                        return false;
99 a52ea5e4 pastith
                }
100 a52ea5e4 pastith
                return true;
101 a52ea5e4 pastith
        }
102 a52ea5e4 pastith
103 a52ea5e4 pastith
        public boolean hasErrors(){
104 a52ea5e4 pastith
                return errors.size() >0;
105 a52ea5e4 pastith
        }
106 a52ea5e4 pastith
107 a52ea5e4 pastith
108 a52ea5e4 pastith
        /**
109 a52ea5e4 pastith
         * Retrieve the errors.
110 a52ea5e4 pastith
         *
111 a52ea5e4 pastith
         * @return the errors
112 a52ea5e4 pastith
         */
113 a52ea5e4 pastith
        public Map<String, Throwable> getErrors() {
114 a52ea5e4 pastith
                return errors;
115 a52ea5e4 pastith
        }
116 a52ea5e4 pastith
117 a52ea5e4 pastith
        public void debug(){
118 a52ea5e4 pastith
                GWT.log("-ERRORS-->"+getErrors().size(), null);
119 a52ea5e4 pastith
                for(String p : getErrors().keySet())
120 a52ea5e4 pastith
                        GWT.log("error:"+p, getErrors().get(p));
121 a52ea5e4 pastith
        }
122 a52ea5e4 pastith
123 a52ea5e4 pastith
        public abstract void onError(String path, Throwable throwable);
124 a52ea5e4 pastith
125 a52ea5e4 pastith
}