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 / repository / remoteclient / requests / CheckoutRequestClient.java @ 3446

History | View | Annotate | Download (8.19 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
package org.gvsig.vcsgis.lib.repository.remoteclient.requests;
23

    
24
import java.io.BufferedReader;
25
import java.io.OutputStream;
26
import java.sql.Timestamp;
27
import java.util.Objects;
28
import javax.json.JsonObject;
29
import javax.json.stream.JsonParser;
30
import org.apache.commons.io.IOUtils;
31
import org.gvsig.json.Json;
32
import org.gvsig.json.JsonObjectBuilder;
33
import org.gvsig.tools.dispose.DisposableIterable;
34
import org.gvsig.vcsgis.lib.EntityEditableImpl;
35
import org.gvsig.vcsgis.lib.PipedIterator;
36
import org.gvsig.vcsgis.lib.SAJParserImpl;
37
import org.gvsig.vcsgis.lib.SAJParserImpl.SAJParserContext;
38
import org.gvsig.vcsgis.lib.VCSGisEntity;
39
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_CANT_CHECKOUT;
40
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_OK;
41
import org.gvsig.vcsgis.lib.VCSGisRepositoryDataImpl;
42
import org.gvsig.vcsgis.lib.repository.VCSGisRepositoryData;
43
import org.gvsig.vcsgis.lib.repository.remoteclient.VCSGisRepositoryClient;
44
import org.gvsig.vcsgis.lib.repository.requests.VCSGisCheckoutRequest;
45
import org.gvsig.vcsgis.lib.requests.CheckoutRequestHelper;
46

    
47
/**
48
 *
49
 * @author gvSIG Team
50
 */
51
@SuppressWarnings("UseSpecificCatch")
52
public class CheckoutRequestClient extends AbstractRequestClient implements VCSGisCheckoutRequest {
53

    
54
    public CheckoutRequestClient(VCSGisRepositoryClient repository, String entityName) {
55
        super(
56
                new CheckoutRequestHelper(repository, entityName),
57
                "checkout"
58
        );
59
    }
60

    
61
    @Override
62
    public CheckoutRequestHelper helper() {
63
        return (CheckoutRequestHelper) super.helper(); 
64
    }
65

    
66
    @Override
67
    public String getEntityName() {
68
        return this.helper().getEntityName();
69
    }
70

    
71
    @Override
72
    public Timestamp getEfectiveDate() {
73
        return this.helper().getEfectiveDate();
74
    }
75

    
76
    @Override
77
    public void setEfectiveDate(Timestamp efectiveDate) {
78
        this.helper().setEfectiveDate(efectiveDate);
79
    }
80

    
81
    @Override
82
    public void setEfectiveDate(String efectiveDate) {
83
        this.helper().setEfectiveDate(efectiveDate);
84
    }
85

    
86
    @Override
87
    public String getRevisionCode() {
88
        return this.helper().getRevisionCode();
89
    }
90

    
91
    @Override
92
    public void setRevisionCode(String revisionCode) {
93
        this.helper().setRevisionCode(revisionCode);
94
    }
95

    
96
    @Override
97
    public VCSGisEntity getEntity() {
98
        return this.helper().getEntity();
99
    }
100

    
101
    @Override
102
    public DisposableIterable<VCSGisRepositoryData> getData() {
103
        return this.helper().getData();
104
    }
105

    
106
    @Override
107
    public String getUsersHashCode() {
108
        return this.helper().usersHashCode;
109
    }
110

    
111
    @Override
112
    public String getTopologyPlansHashCode() {
113
        return this.helper().topologyPlansHashCode;
114
    }
115
    
116
    @Override
117
    public void requestProducer(OutputStream out) {
118
        try {
119
            LOGGER.debug("===: ["+this.getRequestName()+"] requestProducer 1");
120
            
121
            JsonObjectBuilder parametersBuilder = Json.createObjectBuilder();
122
            
123
            parametersBuilder.add("EntityName", this.getEntityName());
124
            parametersBuilder.add("EfectiveDate", Objects.toString(this.getEfectiveDate(),null));
125
            parametersBuilder.add("RevisionCode", this.getRevisionCode());
126
            
127
            JsonObjectBuilder builder = Json.createObjectBuilder();
128
            builder.add("Parameters", parametersBuilder);
129
            
130
            IOUtils.write(builder.toString(), out);
131
            
132
            error(ERR_OK);
133
        } catch (Exception ex) {
134
            LOGGER.warn("Can't produce Json data for "+this.getRequestName()+".",ex);
135
            error(ERR_CANT_CHECKOUT, "Can't produce Json data for "+this.getRequestName()+". "+ ex.getMessage());
136
            
137
        } finally {
138
            IOUtils.closeQuietly(out);
139
        }
140
    }
141

    
142
    @Override
143
    public void responseConsumer(BufferedReader request_contents) {
144
        LOGGER.debug("===: ["+this.getRequestName()+"] responseConsumer 1");
145
        final PipedIterator<VCSGisRepositoryData> dataIterator = new PipedIterator<>();                
146
        helper().data = dataIterator;
147

    
148
        LOGGER.debug("===: ["+this.getRequestName()+"] responseConsumer 2");
149
//        final Object syncParseResponse = new Object();
150
        
151
        SAJParserImpl parser = new SAJParserImpl(
152
                request_contents, 
153
                (SAJParserContext context, JsonParser.Event e, Object value) -> {
154
                    if( e==null ) {
155
                        return;
156
                    }
157
                    switch (e) {
158
                        case END_OBJECT:
159
                            switch(context.getPathName()) {
160
                                case "/Parameters":
161
                                    JsonObject jsonParams = ((javax.json.JsonObjectBuilder)value).build();
162
                                    helper().error(
163
                                            jsonParams.getInt("StatusCode", ERR_OK),
164
                                            jsonParams.getString("StatusMessage", null)
165
                                    );
166
                                    if( jsonParams.isNull("Entity") ) {
167
                                        helper().entity = null;
168
                                    } else {
169
                                        helper().entity = new EntityEditableImpl(
170
                                                jsonParams.getJsonObject("Entity")
171
                                        );
172
                                    }
173
                                    helper().usersHashCode = jsonParams.getString("UsershashCode",null);
174
                                    helper().topologyPlansHashCode = jsonParams.getString("TopologyPlansHashCode",null);
175
                                    notifyResponseConsumers();
176
                                    break;
177
                                case "/Data/?":
178
                                    // Hemos terminado de generar un elemento del array
179
                                    // de datos, construimos un VCSGisRepositoryData y lo adicionamos a la
180
                                    // cola de cambios.
181
                                    JsonObject jsonData = ((javax.json.JsonObjectBuilder)value).build();
182
                                    VCSGisRepositoryData data = new VCSGisRepositoryDataImpl(jsonData);
183
                                    LOGGER.debug("===: ["+this.getRequestName()+"] responseConsumer data = "+jsonData.toString());
184
                                    dataIterator.put(data);
185
                                    break;
186
                            }
187
                            break;
188
                        case END_ARRAY:
189
                            if( "/Data".equals(context.getPathName()) ) {
190
                                dataIterator.close();
191
                            }
192
                            break;
193
                        case START_ARRAY:
194
                            switch(context.getPathName()) {
195
                                case "/Data":
196
                                    // Forzamos que no se cree el array en memoria.
197
                                    context.setArrayBuilder(null);
198
                                    break;
199
                            }
200
                            break;
201
                    }
202
        });
203
        parser.parse();
204
        LOGGER.debug("===: ["+this.getRequestName()+"] responseConsumer 3 (parse)");
205
        IOUtils.closeQuietly(request_contents);
206
   }
207
}