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 / server / handlers / CommitHandler.java @ 2823

History | View | Annotate | Download (9.51 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.server.handlers;
24

    
25
import org.gvsig.vcsgis.lib.PipedIterator;
26
import java.io.IOException;
27
import java.io.InputStream;
28
import java.io.OutputStream;
29
import java.sql.Timestamp;
30
import javax.json.JsonObject;
31
import javax.json.JsonString;
32
import javax.json.JsonValue;
33
import javax.json.stream.JsonParser;
34
import static javax.json.stream.JsonParser.Event.END_ARRAY;
35
import static javax.json.stream.JsonParser.Event.END_OBJECT;
36
import static javax.json.stream.JsonParser.Event.START_ARRAY;
37
import org.apache.commons.io.IOUtils;
38
import org.apache.commons.lang3.StringUtils;
39
import org.apache.commons.lang3.mutable.MutableObject;
40
import org.gvsig.json.Json;
41
import org.gvsig.json.JsonArrayBuilder;
42
import org.gvsig.json.JsonObjectBuilder;
43
import org.gvsig.vcsgis.lib.ChangeImpl;
44
import org.gvsig.vcsgis.lib.EntityEditableImpl;
45
import org.gvsig.vcsgis.lib.SAJParserImpl;
46
import org.gvsig.vcsgis.lib.VCSGisEntityEditable;
47
import org.gvsig.vcsgis.lib.VCSGisChange;
48
import org.gvsig.vcsgis.lib.VCSGisUtils;
49
import org.gvsig.vcsgis.lib.repository.VCSGisRepository;
50
import org.gvsig.vcsgis.lib.repository.requests.VCSGisCommitRequest;
51
import org.gvsig.vcsgis.lib.repository.requests.VCSGisRequest;
52
import static org.gvsig.vcsgis.lib.server.handlers.AbstractVCSGisServertHandler.LOGGER;
53

    
54
/**
55
 *
56
 * @author gvSIG Team
57
 */
58
@SuppressWarnings("UseSpecificCatch")
59
public class CommitHandler extends AbstractVCSGisServertHandler {
60

    
61
    public CommitHandler(VCSGisRepository repository) {
62
        super(repository, "CommitHandler");
63
    }
64
    
65

    
66
    @Override
67
    protected void requestProducer(MutableObject<VCSGisRequest>req, InputStream request_contents) throws IOException {
68
        LOGGER.debug("===: ["+this.getName()+"] requestProducer 1");
69
        final PipedIterator<VCSGisChange> changesIterator = new PipedIterator<>();                
70
        final VCSGisCommitRequest request = this.getRepository().createCommitRequest();
71
        request.add(changesIterator);
72
        req.setValue(request);
73
        LOGGER.debug("===: ["+this.getName()+"] requestProducer 2");
74
        
75
        SAJParserImpl parser = new SAJParserImpl(
76
                request_contents, 
77
                new SAJParserImpl.SAJParserHandler() {
78
                    @Override
79
                    public void handle(SAJParserImpl.SAJParserContext context, JsonParser.Event e, Object value) {
80
                        if( e==null ) {
81
                            return;
82
                        }
83
                        switch (e) {
84
                            case END_OBJECT:
85
                                switch(context.getPathName()) {
86
                                    case "/Parameters":
87
                                        JsonObject jsonParams = ((javax.json.JsonObjectBuilder)value).build();
88
                                        if( jsonParams.isNull("EfectiveDate") ) {
89
                                            request.setEfectiveDate(null);
90
                                        } else {
91
                                            request.setEfectiveDate(Timestamp.valueOf(jsonParams.getString("EfectiveDate")));
92
                                        }
93
                                        if( jsonParams.isNull("Comment") ) {
94
                                            request.setComment(null);
95
                                        } else {
96
                                            request.setComment(jsonParams.getString("Comment"));
97
                                        }
98
                                        if( !jsonParams.isNull("Entities") ) {
99
                                            for (JsonValue jsonvEntity : jsonParams.getJsonArray("Entities")) {
100
                                                VCSGisEntityEditable entity = new EntityEditableImpl((JsonObject) jsonvEntity);
101
                                                request.add(entity);
102
                                            }
103
                                        }
104
                                        if( !jsonParams.isNull("NewEntityCodes") ) {
105
                                            for (JsonValue jsonvEntityCode : jsonParams.getJsonArray("NewEntityCodes")) {
106
                                                request.markAsNew(((JsonString)jsonvEntityCode).getString());
107
                                            }
108
                                        }
109
                                        notifyRequestConsumers();
110
                                        break;
111
                                    case "/Data/?":
112
                                        // Hemos terminado de generar un elemento del array
113
                                        // de datos, construimos un Change y lo adicionamos a la 
114
                                        // cola de cambios.
115
                                        JsonObject jsonChange = ((javax.json.JsonObjectBuilder)value).build();
116
                                        VCSGisChange change = new ChangeImpl(jsonChange);
117
                                        changesIterator.put(change);
118
                                        break;
119

    
120
                                }
121
                                break;
122
                            case END_ARRAY:
123
                                if( "/Data".equals(context.getPathName()) ) {
124
                                    // Marcamos como que ya hemos llegado al final del
125
                                    // array de datos.
126
                                    changesIterator.close();
127
                                }
128
                                break;
129
                            case START_ARRAY:
130
                                switch(context.getPathName()) {
131
                                    case "/Data":
132
                                        // Forzamos que no se cree el array en memoria.
133
                                        context.setArrayBuilder(null);
134
                                        break;
135
                                }
136
                                break;
137
                            
138
                            case START_OBJECT:
139
                                switch(context.getPathName()) {
140
                                    case "/Data/?":
141
                                        break;
142
                                }
143
                                break;
144
                            default:
145
                                break;
146
                        }
147
                    }
148
                }
149
        );
150
        parser.parse();
151
        LOGGER.debug("===: ["+this.getName()+"] requestProducer 3 notifyRequestConsumers");
152
        notifyRequestConsumers();
153
        LOGGER.debug("===: ["+this.getName()+"] requestProducer 4 return");
154
    }
155
    
156
    @Override
157
    protected void responseProducer(VCSGisRequest req, OutputStream response_contents) throws IOException {
158
        LOGGER.debug("===: ["+this.getName()+"] responseProducer 1");
159
        final VCSGisCommitRequest request = (VCSGisCommitRequest) req;
160
        try {
161
            JsonObjectBuilder builder = Json.createObjectBuilder();
162
            JsonObjectBuilder jsonParams = Json.createObjectBuilder();
163
            jsonParams.add("StatusCode", request.getLastErrorCode());
164
            jsonParams.add("StatusMessage", request.getLastErrorMessage());
165
            JsonArrayBuilder jsonEntities = Json.createArrayBuilder();
166
            for (VCSGisEntityEditable entity : request.getChangedLocalEntities()) {
167
//                jsonEntities.add(entity.toJsonBuilder());
168
                JsonObject jsonEntity = VCSGisUtils.toJsonBuilder(
169
                        entity, 
170
                        (String t) -> StringUtils.equalsIgnoreCase(t, VCSGisUtils.ENTITY_FEATURETYPEASJSON)
171
                    ).build();
172
                jsonEntities.add(jsonEntity);
173
            }
174
            jsonParams.add("Entities", jsonEntities);
175
            builder.add("Parameters", jsonParams);
176

    
177
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 2 notifyResponseConsumers");
178
            notifyResponseConsumers();
179

    
180
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 3 write json to response");
181
            IOUtils.write(builder.toString(), response_contents);
182
            
183
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 4 flush");
184
            response_contents.flush();
185

    
186
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 5 return");
187
            
188
                        
189
        } catch (Exception ex) {
190
            LOGGER.warn("Can't produce Json data for "+this.getName()+" response.",ex);
191

    
192
        } finally {
193
            LOGGER.debug("===: ["+this.getName()+"] responseProducer  6 close response_contents");
194
            IOUtils.closeQuietly(response_contents);
195
            LOGGER.debug("===: ["+this.getName()+"] responseProducer  7");
196
        }
197
    }
198

    
199
}