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
/*
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
package org.gvsig.vcsgis.lib.repository.remoteclient.requests;
24

    
25
import java.io.BufferedReader;
26
import java.io.OutputStream;
27
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
import org.gvsig.tools.dispose.DisposableIterable;
33
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
import org.gvsig.vcsgis.lib.VCSGisEntity;
38
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
import org.gvsig.vcsgis.lib.repository.VCSGisRepositoryData;
42
import org.gvsig.vcsgis.lib.repository.remoteclient.VCSGisRepositoryClient;
43
import org.gvsig.vcsgis.lib.repository.requests.VCSGisUpdateRequest;
44
import org.gvsig.vcsgis.lib.requests.UpdateRequestHelper;
45

    
46

    
47
/**
48
 *
49
 * @author gvSIG Team
50
 */
51
@SuppressWarnings("UseSpecificCatch")
52
public class UpdateRequestClient  extends AbstractRequestClient implements VCSGisUpdateRequest {
53

    
54
    public UpdateRequestClient(VCSGisRepositoryClient repository, String entityName) {
55
        super(
56
                new UpdateRequestHelper(repository, entityName),
57
                repository.getUrl("update")
58
        );
59
    }
60

    
61
    @Override
62
    public UpdateRequestHelper helper() {
63
        return (UpdateRequestHelper) super.helper(); 
64
    }
65

    
66
    @Override
67
    public String getEntityName() {
68
        return helper().getEntityName();
69
    }
70

    
71
    @Override
72
    public String getLocalRevisionCode() {
73
        return helper().getLocalRevisionCode();
74
    }
75

    
76
    @Override
77
    public void setLocalRevisionCode(String revisionCode) {
78
        helper().setLocalRevisionCode(revisionCode);
79
    }
80

    
81
    @Override
82
    public VCSGisEntity getEntity() {
83
        return helper().getEntity();
84
    }
85

    
86
    @Override
87
    public DisposableIterable<VCSGisRepositoryData> getData() {
88
        return helper().getData();
89
    }
90

    
91
    @Override
92
    public void requestProducer(OutputStream out) {
93
        try {
94
            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
            JsonObjectBuilder builder = Json.createObjectBuilder();
102
            builder.add("Parameters", parametersBuilder);
103
            
104
            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
    }
115

    
116
    @Override
117
    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
                                    // de datos, construimos un VCSGisRepositoryData y lo adicionamos a la
151
                                    // 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
    }
175

    
176
}