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 @ 8805
History | View | Annotate | Download (7.15 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.Reader; |
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.BooleanUtils; |
37 |
import org.apache.commons.lang3.StringUtils; |
38 |
import org.apache.commons.lang3.mutable.MutableObject; |
39 |
import org.cresques.cts.IProjection; |
40 |
import org.gvsig.fmap.crs.CRSFactory; |
41 |
import org.gvsig.fmap.geom.Geometry; |
42 |
import org.gvsig.fmap.geom.GeometryException; |
43 |
import org.gvsig.fmap.geom.GeometryLocator; |
44 |
import org.gvsig.json.Json; |
45 |
import org.gvsig.vcsgis.lib.repository.VCSGisRepository; |
46 |
import org.gvsig.vcsgis.lib.repository.VCSGisRepositoryData; |
47 |
import org.gvsig.vcsgis.lib.repository.VCSGisRepositoryLocaldb; |
48 |
import org.gvsig.vcsgis.lib.repository.localdb.VCSGisRepositoryLocaldbImpl; |
49 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisCheckoutRequest; |
50 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisRequest; |
51 |
import static org.gvsig.vcsgis.lib.server.handlers.AbstractVCSGisServertHandler.LOGGER; |
52 |
|
53 |
/**
|
54 |
*
|
55 |
* @author gvSIG Team
|
56 |
*/
|
57 |
@SuppressWarnings("UseSpecificCatch") |
58 |
public class CheckoutHandler extends AbstractVCSGisServertHandler { |
59 |
|
60 |
public CheckoutHandler(VCSGisRepository repository) {
|
61 |
super(repository, "CheckoutHandler"); |
62 |
} |
63 |
|
64 |
|
65 |
@Override
|
66 |
protected void requestProducer(MutableObject<VCSGisRequest>req, Map<String,String> params, Reader request_contents) throws IOException { |
67 |
JsonObject jsonParameters = null;
|
68 |
try {
|
69 |
JsonReader reader = Json.createReader(request_contents); |
70 |
JsonObject jsonRequest = reader.readObject(); |
71 |
jsonParameters = jsonRequest.getJsonObject("Parameters");
|
72 |
} catch (JsonException e) {
|
73 |
//Do nothing
|
74 |
} |
75 |
|
76 |
String entityName = params.get("EntityName"); |
77 |
if( jsonParameters != null ) { |
78 |
entityName = jsonParameters.getString("EntityName", entityName);
|
79 |
} |
80 |
|
81 |
VCSGisCheckoutRequest request = this.getRepository().createCheckoutRequest(entityName);
|
82 |
request.setAuthenticationTokenAndUser(params); |
83 |
if( jsonParameters == null ) { |
84 |
request.setEfectiveDate(params.get("EfectiveDate"));
|
85 |
request.setRevisionCode(params.get("RevisionCode"));
|
86 |
} else {
|
87 |
request.setEfectiveDate(jsonParameters.getString("EfectiveDate", params.get("EfectiveDate"))); |
88 |
request.setRevisionCode(jsonParameters.getString("RevisionCode", params.get("RevisionCode"))); |
89 |
} |
90 |
String roi_hexwkb = params.get("ROI"); |
91 |
if( jsonParameters != null ) { |
92 |
roi_hexwkb = jsonParameters.getString("ROI", null); |
93 |
} |
94 |
String roi_proj = params.get("ROIProjection"); |
95 |
if( jsonParameters != null ) { |
96 |
roi_proj = jsonParameters.getString("ROIProjection", null); |
97 |
} |
98 |
if( StringUtils.isNotBlank(roi_hexwkb) ) {
|
99 |
try {
|
100 |
Geometry roi = GeometryLocator.getGeometryManager().createFrom(roi_hexwkb); |
101 |
if( StringUtils.isNotBlank(roi_proj)) {
|
102 |
IProjection proj = CRSFactory.getCRS(roi_proj); |
103 |
roi.setProjection(proj); |
104 |
} |
105 |
request.setROI(roi.getEnvelope()); |
106 |
} catch (GeometryException ex) {
|
107 |
throw new RuntimeException("Can't parse roi ("+roi_hexwkb+").",ex); |
108 |
} |
109 |
} |
110 |
req.setValue(request); |
111 |
notifyRequestConsumers(); |
112 |
} |
113 |
|
114 |
@Override
|
115 |
protected void responseProducer(VCSGisRequest req, Writer response_contents) throws IOException { |
116 |
final VCSGisCheckoutRequest request = (VCSGisCheckoutRequest) req;
|
117 |
JsonGenerator gen = null;
|
118 |
try {
|
119 |
boolean use_revision_number = false; |
120 |
try {
|
121 |
if( this.getRepository() instanceof VCSGisRepositoryLocaldb ) { |
122 |
VCSGisRepositoryLocaldb repo = (VCSGisRepositoryLocaldb) this.getRepository();
|
123 |
String use_revision_number_s = repo.getConfigValue(VCSGisRepositoryLocaldbImpl.CONFIG_CHECKOUT_WITH_REVNUMBER);
|
124 |
use_revision_number = BooleanUtils.toBoolean(use_revision_number_s); |
125 |
} |
126 |
} catch(Exception ex) { |
127 |
LOGGER.debug("Can't retrieve use_revision_number value",ex);
|
128 |
} |
129 |
JsonGeneratorFactory genFactory = Json.createGeneratorFactory( |
130 |
Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true) |
131 |
); |
132 |
gen = genFactory.createGenerator(response_contents); |
133 |
gen.writeStartObject(); |
134 |
|
135 |
gen.writeStartObject("Parameters");
|
136 |
gen.write("StatusCode", request.getLastErrorCode());
|
137 |
|
138 |
notifyResponseConsumers(); |
139 |
gen.flush(); |
140 |
|
141 |
if( StringUtils.isBlank(request.getLastErrorMessage()) ) {
|
142 |
gen.writeNull("StatusMessage");
|
143 |
} else {
|
144 |
gen.write("StatusMessage", request.getLastErrorMessage());
|
145 |
} |
146 |
|
147 |
gen.write("Entity", request.getEntity().toJson());
|
148 |
gen.write("UsershashCode", request.getUsersHashCode());
|
149 |
gen.write("TopologyPlansHashCode", request.getTopologyPlansHashCode());
|
150 |
gen.writeEnd(); // Paramaters
|
151 |
|
152 |
gen.writeStartArray("Data");
|
153 |
for (VCSGisRepositoryData data : request.getData()) {
|
154 |
gen.writeStartObject(); |
155 |
gen.write("DataCode", data.getDataCode());
|
156 |
gen.write("Data", data.getData());
|
157 |
if( use_revision_number ) {
|
158 |
gen.write("RevisionNumber", data.getRevisionNumber());
|
159 |
} |
160 |
gen.writeEnd(); |
161 |
gen.flush(); |
162 |
} |
163 |
gen.writeEnd(); // Data
|
164 |
gen.writeEnd(); |
165 |
|
166 |
|
167 |
gen.flush(); |
168 |
response_contents.flush(); |
169 |
|
170 |
|
171 |
} catch (Exception ex) { |
172 |
LOGGER.warn("Can't produce Json data for "+this.getName()+" response.",ex); |
173 |
|
174 |
} finally {
|
175 |
IOUtils.closeQuietly(gen); |
176 |
gen.close(); |
177 |
// IOUtils.closeQuietly(response_contents);
|
178 |
} |
179 |
} |
180 |
|
181 |
} |