Statistics
| Branch: | Tag: | Revision:

root / gss / src / gr / ebs / gss / client / rest / PostCommand.java @ 895035a2

History | View | Annotate | Download (2.4 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 gr.ebs.gss.client.rest;
20

    
21
import gr.ebs.gss.client.GSS;
22

    
23
import com.google.gwt.http.client.Request;
24
import com.google.gwt.http.client.RequestCallback;
25
import com.google.gwt.http.client.Response;
26

    
27

    
28
/**
29
 * @author kman
30
 *
31
 */
32
public abstract class PostCommand extends RestCommand{
33
        boolean complete = false;
34
        String postBody=null;
35

    
36
        public PostCommand(final String path, String data, final int okStatusCode){
37
                this(path, data, okStatusCode, true);
38
        }
39
        public PostCommand(final String path, String data, final int okStatusCode, boolean showLoading){
40
                setShowLoadingIndicator(showLoading);
41
                if(isShowLoadingIndicator())
42
                        GSS.get().showLoadingIndicator();
43

    
44
                RestRequestBuilder builder = new RestRequestBuilder("POST", path);
45

    
46
                try {
47
                        handleHeaders(builder, path);
48
                        builder.sendRequest(data, new RequestCallback() {
49

    
50

    
51
                                public void onError(Request arg0, Throwable arg1) {
52
                                        complete = true;
53
                                        PostCommand.this.onError(arg1);
54
                                }
55

    
56

    
57
                                public void onResponseReceived(Request arg0, Response arg1) {
58
                                        complete=true;
59
                                        if(arg1.getStatusCode() == okStatusCode){
60
                                                postBody = arg1.getText();
61
                                                onComplete();
62
                                        }
63
                                        else if(arg1.getStatusCode() == 403)
64
                                                sessionExpired();
65
                                        else
66
                                                PostCommand.this.onError(new RestException(path, arg1.getStatusCode(), arg1.getStatusText(), arg1.getText()));
67
                                }
68

    
69
                        });
70
                } catch (Exception ex) {
71
                        complete=true;
72
                        onError(ex);
73
                }
74
        }
75

    
76
        public boolean isComplete() {
77
                return complete;
78
        }
79

    
80
        public boolean execute() {
81
                boolean com = isComplete();
82
                if(com){
83
                        if(isShowLoadingIndicator())
84
                                GSS.get().hideLoadingIndicator();
85
                        return false;
86
                }
87
                return true;
88
        }
89

    
90

    
91
        /**
92
         * Retrieve the postBody.
93
         *
94
         * @return the postBody
95
         */
96
        public String getPostBody() {
97
                return postBody;
98
        }
99

    
100

    
101
}