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 / RowCreateHandler.java @ 2823

History | View | Annotate | Download (4.77 KB)

1 2697 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.server.handlers;
24 2697 jjdelcerro
25 2708 jjdelcerro
import java.io.IOException;
26 2697 jjdelcerro
import java.io.InputStream;
27
import java.io.OutputStream;
28
import javax.json.JsonObject;
29 2716 jjdelcerro
import javax.json.JsonReader;
30 2697 jjdelcerro
import org.apache.commons.io.IOUtils;
31 2708 jjdelcerro
import org.apache.commons.lang3.mutable.MutableObject;
32 2716 jjdelcerro
import org.gvsig.json.Json;
33
import org.gvsig.json.JsonObjectBuilder;
34 2724 jjdelcerro
import static org.gvsig.vcsgis.lib.VCSGisUtils.ENTITY_CODE;
35
import static org.gvsig.vcsgis.lib.VCSGisUtils.ENTITY_NAME;
36
import static org.gvsig.vcsgis.lib.VCSGisUtils.ENTITY_REVISIONCODE;
37 2697 jjdelcerro
import org.gvsig.vcsgis.lib.repository.VCSGisRepository;
38
import org.gvsig.vcsgis.lib.repository.requests.VCSGisRequest;
39 2716 jjdelcerro
import org.gvsig.vcsgis.lib.repository.requests.VCSGisRowCreateRequest;
40 2708 jjdelcerro
import static org.gvsig.vcsgis.lib.server.handlers.AbstractVCSGisServertHandler.LOGGER;
41 2697 jjdelcerro
42
/**
43
 *
44
 * @author gvSIG Team
45
 */
46
@SuppressWarnings("UseSpecificCatch")
47 2716 jjdelcerro
public class RowCreateHandler extends AbstractVCSGisServertHandler {
48 2697 jjdelcerro
49 2716 jjdelcerro
    private static final String REQUEST_NAME = "RowCreate";
50 2711 jjdelcerro
51 2716 jjdelcerro
    public RowCreateHandler(VCSGisRepository repository) {
52
        super(repository, REQUEST_NAME);
53
    }
54
55 2697 jjdelcerro
    @Override
56 2708 jjdelcerro
    protected void requestProducer(MutableObject<VCSGisRequest>req, InputStream request_contents) throws IOException {
57 2716 jjdelcerro
        LOGGER.debug("===: ["+this.getName()+"] requestProducer 1");
58
        JsonReader reader = Json.createReader(request_contents);
59
        JsonObject jsonRequest = reader.readObject();
60
61
        String entityName = jsonRequest.getString("EntityName");
62
        String localRevisionCode = jsonRequest.getString("LocalRevisionCode");
63
64
        VCSGisRowCreateRequest request = this.getRepository().createRowCreateRequest(entityName, localRevisionCode);
65
66
        request.setEfectiveDate(jsonRequest.getString("EfectiveDate", null));
67
        request.setComment(jsonRequest.getString("Comment", null));
68
        request.setData(jsonRequest.getString("Data", null));
69
70 2708 jjdelcerro
        req.setValue(request);
71
72 2716 jjdelcerro
        LOGGER.debug("===: ["+this.getName()+"] requestProducer 2 notifyRequestConsumers");
73 2708 jjdelcerro
        notifyRequestConsumers();
74 2716 jjdelcerro
75
        LOGGER.debug("===: ["+this.getName()+"] requestProducer 3 return");
76 2697 jjdelcerro
    }
77 2708 jjdelcerro
78 2697 jjdelcerro
    @Override
79 2708 jjdelcerro
    protected void responseProducer(VCSGisRequest req, OutputStream response_contents) throws IOException {
80 2716 jjdelcerro
        LOGGER.debug("===: ["+this.getName()+"] responseProducer 1");
81
        final VCSGisRowCreateRequest request = (VCSGisRowCreateRequest) req;
82 2697 jjdelcerro
        try {
83 2716 jjdelcerro
            JsonObjectBuilder jsonbuilder = Json.createObjectBuilder();
84
            jsonbuilder.add("StatusCode", request.getLastErrorCode());
85
            jsonbuilder.add("StatusMessage", request.getLastErrorMessage());
86
87
            jsonbuilder.add("RelatedFeatureCode", request.getRelatedFeatureCode());
88 2697 jjdelcerro
89 2724 jjdelcerro
            jsonbuilder.add(ENTITY_NAME, request.getEntityName());
90
            jsonbuilder.add(ENTITY_CODE, request.getEntityCode());
91
            jsonbuilder.add(ENTITY_REVISIONCODE, request.getEntityRevisionCode());
92
93 2716 jjdelcerro
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 2 notifyResponseConsumers");
94 2711 jjdelcerro
            notifyResponseConsumers();
95
96 2716 jjdelcerro
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 3 write json to response");
97
            IOUtils.write(jsonbuilder.toString(), response_contents);
98
99
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 4 flush");
100 2708 jjdelcerro
            response_contents.flush();
101 2716 jjdelcerro
102
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 5 return");
103 2697 jjdelcerro
104 2708 jjdelcerro
105 2697 jjdelcerro
        } catch (Exception ex) {
106
            LOGGER.warn("Can't produce Json data for "+this.getName()+" response.",ex);
107
108
        } finally {
109 2716 jjdelcerro
            LOGGER.debug("===: ["+this.getName()+"] responseProducer  6 close response_contents");
110 2708 jjdelcerro
            IOUtils.closeQuietly(response_contents);
111 2716 jjdelcerro
            LOGGER.debug("===: ["+this.getName()+"] responseProducer  7");
112 2697 jjdelcerro
        }
113
    }
114
115
}