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 @ 3375

History | View | Annotate | Download (10 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 java.io.IOException;
26
import java.io.InputStream;
27
import java.io.Writer;
28
import java.sql.Timestamp;
29
import java.util.Map;
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.PipedIterator;
45
import org.gvsig.vcsgis.lib.SAJParserImpl;
46
import org.gvsig.vcsgis.lib.VCSGisChange;
47
import org.gvsig.vcsgis.lib.VCSGisEntityEditable;
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
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceEntity;
54
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceEntityImpl;
55

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

    
63
    public CommitHandler(VCSGisRepository repository) {
64
        super(repository, "CommitHandler");
65
    }
66
    
67

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

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

    
185
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 2 notifyResponseConsumers");
186
            notifyResponseConsumers();
187

    
188
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 3 write json to response");
189
            IOUtils.write(builder.toString(), response_contents);
190
            
191
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 4 flush");
192
            response_contents.flush();
193

    
194
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 5 return");
195
            
196
                        
197
        } catch (Exception ex) {
198
            LOGGER.warn("Can't produce Json data for "+this.getName()+" response.",ex);
199

    
200
        } finally {
201
            LOGGER.debug("===: ["+this.getName()+"] responseProducer  6 close response_contents");
202
            IOUtils.closeQuietly(response_contents);
203
            LOGGER.debug("===: ["+this.getName()+"] responseProducer  7");
204
        }
205
    }
206

    
207
}