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

History | View | Annotate | Download (4.21 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.List;
29
import java.util.Map;
30
import org.apache.commons.io.IOUtils;
31
import org.apache.commons.lang3.mutable.MutableObject;
32
import org.gvsig.json.Json;
33
import org.gvsig.json.JsonArrayBuilder;
34
import org.gvsig.json.JsonObjectBuilder;
35
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_OK;
36
import org.gvsig.vcsgis.lib.VCSGisRuntimeException;
37
import org.gvsig.vcsgis.lib.VCSGisTopologyPlan;
38
import org.gvsig.vcsgis.lib.repository.VCSGisRepository;
39
import org.gvsig.vcsgis.lib.repository.requests.VCSGisRequest;
40
import org.gvsig.vcsgis.lib.repository.requests.VCSGisTopologyPlansRequest;
41

    
42
/**
43
 *
44
 * @author gvSIG Team
45
 */
46
public class TopologyPlansHandler extends AbstractVCSGisServertHandler {
47

    
48
    public TopologyPlansHandler(VCSGisRepository repository) {
49
        super(repository, "TopologyPlansHandler");
50
    }
51

    
52
    @Override
53
    protected void requestProducer(MutableObject<VCSGisRequest> req,Map<String,String> params, InputStream request_contents) throws IOException {
54
        LOGGER.debug("===: ["+this.getName()+"] requestProducer 1");
55
        final VCSGisTopologyPlansRequest request = this.getRepository().createTopologyPlansRequest();
56
        req.setValue(request);
57
        LOGGER.debug("===: ["+this.getName()+"] requestProducer 2 notifyRequestConsumers");
58
        notifyRequestConsumers();
59
        LOGGER.debug("===: ["+this.getName()+"] requestProducer 3 return");
60
    }
61
    
62
    @Override
63
    protected void responseProducer(VCSGisRequest req, Writer response_contents) throws IOException {
64
        LOGGER.debug("===: ["+this.getName()+"] responseProducer  1");
65
        VCSGisTopologyPlansRequest request = (VCSGisTopologyPlansRequest) req;
66
        if( request.execute()!=ERR_OK ) {
67
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 2 error "+request.getLastErrorCode());
68
            throw new VCSGisRuntimeException(request.getLastErrorCode(), request.getLastErrorMessage());
69
        }
70
        
71
        LOGGER.debug("===: ["+this.getName()+"] responseProducer  3");
72
        List<VCSGisTopologyPlan> topologyPlans = request.getTopologyPlans();
73
        JsonArrayBuilder jsonTopologyPlans = Json.createArrayBuilder();
74
        for (VCSGisTopologyPlan topologyPlan : topologyPlans) {
75
            LOGGER.debug("===: ["+this.getName()+"] responseProducer  4 entity "+topologyPlan.getName());
76
            jsonTopologyPlans.add(topologyPlan.toJsonBuilder());
77
        }
78
        JsonObjectBuilder jsonResponse = Json.createObjectBuilder();
79
        jsonResponse.add("StatusCode", ERR_OK);
80
        jsonResponse.addNull("StatusMessage");
81
        jsonResponse.add("TopologyPlans", jsonTopologyPlans);
82
        
83
        LOGGER.debug("===: ["+this.getName()+"] responseProducer  5 notifyResponseConsumers");
84
        notifyResponseConsumers();
85
        
86
        LOGGER.debug("===: ["+this.getName()+"] responseProducer  6 write json");
87
        IOUtils.write(jsonResponse.toString(), response_contents);
88
        
89
        LOGGER.debug("===: ["+this.getName()+"] responseProducer  7 flush");
90
        response_contents.flush();
91
        
92
        LOGGER.debug("===: ["+this.getName()+"] responseProducer  8 close");
93
        IOUtils.closeQuietly(response_contents);
94
        
95
        LOGGER.debug("===: ["+this.getName()+"] responseProducer  9 return");
96
        
97
    }
98

    
99
}