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 / CheckoutHandler.java @ 3315

History | View | Annotate | Download (5.12 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.util.Collections;
29
import java.util.Map;
30
import javax.json.JsonException;
31
import javax.json.JsonObject;
32
import javax.json.JsonReader;
33
import javax.json.stream.JsonGenerator;
34
import javax.json.stream.JsonGeneratorFactory;
35
import org.apache.commons.io.IOUtils;
36
import org.apache.commons.lang3.StringUtils;
37
import org.apache.commons.lang3.mutable.MutableObject;
38
import org.gvsig.json.Json;
39
import org.gvsig.vcsgis.lib.repository.VCSGisRepository;
40
import org.gvsig.vcsgis.lib.repository.VCSGisRepositoryData;
41
import org.gvsig.vcsgis.lib.repository.requests.VCSGisCheckoutRequest;
42
import org.gvsig.vcsgis.lib.repository.requests.VCSGisRequest;
43
import static org.gvsig.vcsgis.lib.server.handlers.AbstractVCSGisServertHandler.LOGGER;
44

    
45
/**
46
 *
47
 * @author gvSIG Team
48
 */
49
@SuppressWarnings("UseSpecificCatch")
50
public class CheckoutHandler extends AbstractVCSGisServertHandler {
51

    
52
    public CheckoutHandler(VCSGisRepository repository) {
53
        super(repository, "CheckoutHandler");
54
    }
55
    
56

    
57
    @Override
58
    protected void requestProducer(MutableObject<VCSGisRequest>req, Map<String,String> params, InputStream request_contents) throws IOException {
59
        JsonObject jsonParameters = null;
60
        try {
61
        JsonReader reader = Json.createReader(request_contents);
62
        JsonObject jsonRequest = reader.readObject();
63
        jsonParameters = jsonRequest.getJsonObject("Parameters");
64
        } catch (JsonException e) {
65
            //Do nothing
66
        }
67
        
68
        String entityName = params.get("EntityName");;
69
        if( jsonParameters != null ) {
70
            entityName = jsonParameters.getString("EntityName", entityName);
71
        }
72
        
73
        VCSGisCheckoutRequest request = this.getRepository().createCheckoutRequest(entityName);        
74
        if( jsonParameters == null ) {
75
            request.setEfectiveDate(params.get("EfectiveDate"));
76
            request.setRevisionCode(params.get("RevisionCode"));
77
        } else {
78
            request.setEfectiveDate(jsonParameters.getString("EfectiveDate", params.get("EfectiveDate")));
79
            request.setRevisionCode(jsonParameters.getString("RevisionCode", params.get("RevisionCode")));
80
        }
81
        req.setValue(request);
82
        notifyRequestConsumers();
83
    }
84
    
85
    @Override
86
    protected void responseProducer(VCSGisRequest req, Writer response_contents) throws IOException {
87
        final VCSGisCheckoutRequest request = (VCSGisCheckoutRequest) req;
88
        JsonGenerator gen = null;
89
        try {
90
            JsonGeneratorFactory genFactory = Json.createGeneratorFactory(
91
                Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true)
92
            );
93
            gen = genFactory.createGenerator(response_contents);
94
            gen.writeStartObject();
95

    
96
            gen.writeStartObject("Parameters");
97
            gen.write("StatusCode", request.getLastErrorCode());
98

    
99
            notifyResponseConsumers();
100
            gen.flush();
101

    
102
            if( StringUtils.isBlank(request.getLastErrorMessage()) ) {
103
                gen.writeNull("StatusMessage");
104
            } else {
105
                gen.write("StatusMessage", request.getLastErrorMessage());
106
            }
107

    
108
            gen.write("Entity", request.getEntity().toJson());
109
            gen.write("UsershashCode", request.getUsersHashCode());
110
            gen.write("TopologyPlansHashCode", request.getTopologyPlansHashCode());
111
            gen.writeEnd(); // Paramaters
112

    
113
            gen.writeStartArray("Data");
114
            for (VCSGisRepositoryData data : request.getData()) {
115
                gen.writeStartObject();
116
                gen.write("DataCode", data.getDataCode());
117
                gen.write("Data", data.getData());
118
                gen.writeEnd(); 
119
                gen.flush();
120
            }
121
            gen.writeEnd(); // Data
122
            gen.writeEnd(); 
123
            
124

    
125
            gen.flush();
126
            response_contents.flush();
127
            
128
                        
129
        } catch (Exception ex) {
130
            LOGGER.warn("Can't produce Json data for "+this.getName()+" response.",ex);
131

    
132
        } finally {
133
            IOUtils.closeQuietly(gen);
134
            gen.close();
135
//            IOUtils.closeQuietly(response_contents);
136
        }
137
    }
138

    
139
}