Statistics
| Revision:

gvsig-projects-pool / org.gvsig.vcsgis / trunk / org.gvsig.vcsgis / org.gvsig.vcsgis.lib / org.gvsig.vcsgis.lib.impl / src / main / java / org / gvsig / vcsgis / lib / repository / remoteclient / requests / UpdateRequestClient.java @ 2823

History | View | Annotate | Download (6.95 KB)

1 2273 jjdelcerro
/*
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2020 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
18
 *
19
 * For any additional information, do not hesitate to contact us
20
 * at info AT gvsig.com, or visit our website www.gvsig.com.
21
 */
22
23 2699 jjdelcerro
package org.gvsig.vcsgis.lib.repository.remoteclient.requests;
24 2273 jjdelcerro
25 2715 jjdelcerro
import java.io.BufferedReader;
26 2708 jjdelcerro
import java.io.OutputStream;
27 2724 jjdelcerro
import javax.json.JsonObject;
28
import javax.json.stream.JsonParser;
29
import org.apache.commons.io.IOUtils;
30
import org.gvsig.json.Json;
31
import org.gvsig.json.JsonObjectBuilder;
32 2708 jjdelcerro
import org.gvsig.tools.dispose.DisposableIterable;
33 2724 jjdelcerro
import org.gvsig.vcsgis.lib.EntityEditableImpl;
34
import org.gvsig.vcsgis.lib.PipedIterator;
35
import org.gvsig.vcsgis.lib.SAJParserImpl;
36
import org.gvsig.vcsgis.lib.SAJParserImpl.SAJParserContext;
37 2708 jjdelcerro
import org.gvsig.vcsgis.lib.VCSGisEntity;
38 2724 jjdelcerro
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_CHECKOUT;
39
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_OK;
40
import org.gvsig.vcsgis.lib.VCSGisRepositoryDataImpl;
41 2708 jjdelcerro
import org.gvsig.vcsgis.lib.repository.VCSGisRepositoryData;
42 2706 fdiaz
import org.gvsig.vcsgis.lib.repository.remoteclient.VCSGisRepositoryClient;
43 2708 jjdelcerro
import org.gvsig.vcsgis.lib.repository.requests.VCSGisUpdateRequest;
44
import org.gvsig.vcsgis.lib.requests.UpdateRequestHelper;
45 2273 jjdelcerro
46 2697 jjdelcerro
47 2273 jjdelcerro
/**
48
 *
49
 * @author gvSIG Team
50
 */
51 2724 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
52 2708 jjdelcerro
public class UpdateRequestClient  extends AbstractRequestClient implements VCSGisUpdateRequest {
53 2273 jjdelcerro
54 2708 jjdelcerro
    public UpdateRequestClient(VCSGisRepositoryClient repository, String entityName) {
55
        super(
56
                new UpdateRequestHelper(repository, entityName),
57 2724 jjdelcerro
                repository.getUrl("update")
58 2708 jjdelcerro
        );
59 2273 jjdelcerro
    }
60
61 2274 jjdelcerro
    @Override
62 2708 jjdelcerro
    public UpdateRequestHelper helper() {
63
        return (UpdateRequestHelper) super.helper();
64 2626 jjdelcerro
    }
65 2274 jjdelcerro
66
    @Override
67 2708 jjdelcerro
    public String getEntityName() {
68
        return helper().getEntityName();
69 2274 jjdelcerro
    }
70 2708 jjdelcerro
71
    @Override
72 2724 jjdelcerro
    public String getLocalRevisionCode() {
73
        return helper().getLocalRevisionCode();
74 2708 jjdelcerro
    }
75
76
    @Override
77 2724 jjdelcerro
    public void setLocalRevisionCode(String revisionCode) {
78
        helper().setLocalRevisionCode(revisionCode);
79 2708 jjdelcerro
    }
80
81
    @Override
82 2724 jjdelcerro
    public VCSGisEntity getEntity() {
83
        return helper().getEntity();
84 2708 jjdelcerro
    }
85
86
    @Override
87 2724 jjdelcerro
    public DisposableIterable<VCSGisRepositoryData> getData() {
88
        return helper().getData();
89 2708 jjdelcerro
    }
90
91
    @Override
92 2724 jjdelcerro
    public void requestProducer(OutputStream out) {
93
        try {
94 2823 jjdelcerro
            LOGGER.debug("===: ["+this.getRequestName()+"] requestProducer 1");
95
96
            JsonObjectBuilder parametersBuilder = Json.createObjectBuilder();
97
98
            parametersBuilder.add("EntityName", this.getEntityName());
99
            parametersBuilder.add("LocalRevisionCode", this.getLocalRevisionCode());
100
101 2724 jjdelcerro
            JsonObjectBuilder builder = Json.createObjectBuilder();
102 2823 jjdelcerro
            builder.add("Parameters", parametersBuilder);
103
104 2724 jjdelcerro
            IOUtils.write(builder.toString(), out);
105
106
            error(ERR_OK);
107
        } catch (Exception ex) {
108
            LOGGER.warn("Can't produce Json data for "+this.getRequestName()+" request.",ex);
109
            error(ERR_CANT_CHECKOUT, "Can't produce Json data for "+this.getRequestName()+" request. "+ ex.getMessage());
110
111
        } finally {
112
            IOUtils.closeQuietly(out);
113
        }
114 2708 jjdelcerro
    }
115
116
    @Override
117 2724 jjdelcerro
    public void responseConsumer(BufferedReader request_contents) {
118
        LOGGER.debug("===: ["+this.getRequestName()+"] responseConsumer 1");
119
        final PipedIterator<VCSGisRepositoryData> dataIterator = new PipedIterator<>();
120
        helper().data = dataIterator;
121
122
        LOGGER.debug("===: ["+this.getRequestName()+"] responseConsumer 2");
123
124
        SAJParserImpl parser = new SAJParserImpl(
125
                request_contents,
126
                (SAJParserContext context, JsonParser.Event e, Object value) -> {
127
                    if( e==null ) {
128
                        return;
129
                    }
130
                    switch (e) {
131
                        case END_OBJECT:
132
                            switch(context.getPathName()) {
133
                                case "/Parameters":
134
                                    JsonObject jsonParams = ((javax.json.JsonObjectBuilder)value).build();
135
                                    helper().error(
136
                                            jsonParams.getInt("StatusCode", ERR_OK),
137
                                            jsonParams.getString("StatusMessage", null)
138
                                    );
139
                                    if( jsonParams.isNull("Entity") ) {
140
                                        helper().entity = null;
141
                                    } else {
142
                                        helper().entity = new EntityEditableImpl(
143
                                                jsonParams.getJsonObject("Entity")
144
                                        );
145
                                    }
146
                                    notifyResponseConsumers();
147
                                    break;
148
                                case "/Data/?":
149
                                    // Hemos terminado de generar un elemento del array
150 2728 jjdelcerro
                                    // de datos, construimos un VCSGisRepositoryData y lo adicionamos a la
151 2724 jjdelcerro
                                    // cola de cambios.
152
                                    JsonObject jsonData = ((javax.json.JsonObjectBuilder)value).build();
153
                                    VCSGisRepositoryData data = new VCSGisRepositoryDataImpl(jsonData);
154
                                    dataIterator.put(data);
155
                                    break;
156
                            }
157
                            break;
158
                        case END_ARRAY:
159
                            if( "/Data".equals(context.getPathName()) ) {
160
                                dataIterator.close();
161
                            }
162
                            break;
163
                        case START_ARRAY:
164
                            switch(context.getPathName()) {
165
                                case "/Data":
166
                                    // Forzamos que no se cree el array en memoria.
167
                                    context.setArrayBuilder(null);
168
                                    break;
169
                            }
170
                            break;
171
                    }
172
        });
173
        parser.parse();
174 2708 jjdelcerro
    }
175
176 2273 jjdelcerro
}