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 / workspace / VCSGisWorkspaceImpl.java @ 2701
History | View | Annotate | Download (45.2 KB)
1 |
package org.gvsig.vcsgis.lib.workspace; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.sql.Timestamp; |
5 |
import java.time.LocalDateTime; |
6 |
import java.time.ZoneOffset; |
7 |
import java.util.ArrayList; |
8 |
import java.util.Collection; |
9 |
import java.util.HashMap; |
10 |
import java.util.HashSet; |
11 |
import java.util.Iterator; |
12 |
import java.util.List; |
13 |
import java.util.Map; |
14 |
import java.util.Set; |
15 |
import java.util.UUID; |
16 |
import org.apache.commons.lang3.StringUtils; |
17 |
import org.gvsig.fmap.dal.DALLocator; |
18 |
import org.gvsig.fmap.dal.DataManager; |
19 |
import org.gvsig.fmap.dal.DataStore; |
20 |
import org.gvsig.fmap.dal.DataStoreParameters; |
21 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
22 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
23 |
import org.gvsig.fmap.dal.feature.Feature; |
24 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
25 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
26 |
import org.gvsig.fmap.dal.feature.FeatureSet.DisposableFeatureSetIterable; |
27 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
28 |
import static org.gvsig.fmap.dal.feature.FeatureStore.MODE_PASS_THROUGH; |
29 |
import org.gvsig.fmap.dal.feature.FeatureType; |
30 |
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
31 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
32 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
33 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
34 |
import org.gvsig.json.Json; |
35 |
import org.gvsig.tools.dataTypes.DataTypes; |
36 |
import org.gvsig.tools.dispose.DisposableIterable; |
37 |
import org.gvsig.tools.dispose.DisposeUtils; |
38 |
import org.gvsig.tools.logger.FilteredLogger; |
39 |
import org.gvsig.tools.util.HasAFile; |
40 |
import org.gvsig.vcsgis.lib.VCSGisRuntimeException; |
41 |
import static org.gvsig.vcsgis.lib.VCSGisManagerImpl.INTERNAL_TABLES; |
42 |
import org.gvsig.vcsgis.lib.workspace.tables.EntitiesTable; |
43 |
import org.gvsig.vcsgis.lib.workspace.tables.EntitiesTable.EntityRow; |
44 |
import org.gvsig.vcsgis.lib.workspace.tables.WorkspaceChangesTable; |
45 |
import org.gvsig.vcsgis.lib.workspace.tables.WorkspaceChangesTable.WorkspaceChangeRow; |
46 |
import org.gvsig.vcsgis.lib.workspace.tables.LocalRevisionsTable; |
47 |
import org.gvsig.vcsgis.lib.workspace.tables.LocalRevisionsTable.LocalRevisionRow; |
48 |
import org.gvsig.vcsgis.lib.workspace.tables.RemoteChangesTable; |
49 |
import org.gvsig.vcsgis.lib.workspace.tables.RemoteChangesTable.RemoteChangeRow; |
50 |
import org.gvsig.vcsgis.lib.workspace.tables.VarsTable; |
51 |
import org.gvsig.vcsgis.lib.DisposableIterableAdapter; |
52 |
import org.gvsig.vcsgis.lib.VCSGisChange; |
53 |
import org.gvsig.vcsgis.lib.VCSGisUtils; |
54 |
import static org.gvsig.vcsgis.lib.VCSGisManager.DEFAULT_DATA_TABLE; |
55 |
import org.gvsig.vcsgis.lib.VCSGisEntity; |
56 |
import static org.gvsig.vcsgis.lib.VCSGisManager.FEATURECODE_FIELD_NAME; |
57 |
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_ADD_ENTITY; |
58 |
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_DELETE; |
59 |
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_INSERT; |
60 |
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_UPDATE; |
61 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace; |
62 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceEntity; |
63 |
import org.gvsig.vcsgis.lib.repository.VCSGisRepository; |
64 |
import org.gvsig.vcsgis.lib.repository.VCSGisRepositoryChange; |
65 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisCheckoutRequest; |
66 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisCommitRequest; |
67 |
import org.gvsig.vcsgis.lib.repository.VCSGisRepositoryData; |
68 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisEntitiesRequest; |
69 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisLogRequest; |
70 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisUpdateRequest; |
71 |
import org.slf4j.Logger; |
72 |
import org.slf4j.LoggerFactory; |
73 |
import org.gvsig.vcsgis.lib.repository.VCSGisRepositoryRevision; |
74 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceChange; |
75 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceChanges; |
76 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceRevision; |
77 |
|
78 |
/**
|
79 |
*
|
80 |
* @author gvSIG Team
|
81 |
*/
|
82 |
@SuppressWarnings({"UseSpecificCatch", "UnusedAssignment"}) |
83 |
public class VCSGisWorkspaceImpl implements VCSGisWorkspace { |
84 |
|
85 |
private static final Logger LOGGER = LoggerFactory.getLogger(VCSGisWorkspaceImpl.class); |
86 |
|
87 |
private final Map<String, FeatureStore> storesCache; |
88 |
private Map<String, EntityRow> entitiesByName; |
89 |
private Map<String, EntityRow> entitiesByCode; |
90 |
private Set<FeatureStore> storeIgnoreChanges; |
91 |
private JDBCServerExplorer wsexplorer;
|
92 |
private String code; |
93 |
private final VCSGisRepository repository; |
94 |
private final String label; |
95 |
private final FilteredLogger logger; |
96 |
|
97 |
public VCSGisWorkspaceImpl(JDBCServerExplorer wsexplorer, VCSGisRepository repository, String label) { |
98 |
this.logger = new FilteredLogger(LOGGER, "VCSGisWorkspace", 100); |
99 |
this.logger.setInterval(10000); |
100 |
this.storesCache = new HashMap<>(); |
101 |
this.code = createUniqueID();
|
102 |
this.wsexplorer = wsexplorer;
|
103 |
this.repository = repository;
|
104 |
this.label = label;
|
105 |
// LOGGER.debug("===: CREATE WORKSPACE "+ hexId(this)+ " (init label='"+this.label+"')");
|
106 |
|
107 |
} |
108 |
|
109 |
public VCSGisWorkspaceImpl(JDBCServerExplorer wsexplorer) {
|
110 |
this.logger = new FilteredLogger(LOGGER, "VCSGisWorkspace", 100); |
111 |
this.logger.setInterval(10000); |
112 |
this.storesCache = new HashMap<>(); |
113 |
this.wsexplorer = wsexplorer;
|
114 |
this.loadEntities();
|
115 |
|
116 |
VarsTable varsTable = new VarsTable();
|
117 |
this.code = varsTable.get(this, "WORKSPACE_CODE"); |
118 |
this.label = varsTable.get(this, "WORKSPACE_LABEL"); |
119 |
this.repository = (VCSGisRepository) Json.toObject(varsTable.get(this, "REPOSITORY")); |
120 |
if (this.repository == null) { |
121 |
throw new RuntimeException("Can't retrieve repository from workspace '" + this.getMessageLabel() + "'"); |
122 |
} |
123 |
// LOGGER.debug("===: CREATE WORKSPACE "+ hexId(this)+ " (open code='"+this.code+"', label='"+this.label+"')");
|
124 |
} |
125 |
|
126 |
public void initialize() throws Exception { |
127 |
DataManager dataManager = DALLocator.getDataManager(); |
128 |
|
129 |
List<VCSGisEntity> rentities = this.getRepositoryEntities(); |
130 |
if (!rentities.isEmpty()) {
|
131 |
JDBCStoreParameters storeParams = wsexplorer.get(EntitiesTable.TABLE_NAME); |
132 |
FeatureStore storeEntities = (FeatureStore) dataManager.openStore( |
133 |
DataStore.H2SPATIAL_PROVIDER_NAME, |
134 |
storeParams |
135 |
); |
136 |
storeEntities.edit(FeatureStore.MODE_APPEND); |
137 |
for (VCSGisEntity rentity : rentities) {
|
138 |
EntityRow entity = new EntityRow(this, storeEntities.createNewFeature()); |
139 |
entity.copyfrom(rentity); |
140 |
entity.setLocalRevisionCode(null);
|
141 |
entity.insert(storeEntities); |
142 |
} |
143 |
storeEntities.finishEditing(); |
144 |
} |
145 |
VarsTable varsTable = new VarsTable();
|
146 |
varsTable.set(this, "WORKSPACE_CODE", code); |
147 |
varsTable.set(this, "WORKSPACE_LABEL", label); |
148 |
varsTable.set(this, "REPOSITORY", repository.toJson().toString()); |
149 |
} |
150 |
|
151 |
private String getMessageLabel() { |
152 |
return this.wsexplorer.getParameters().getUrl(); |
153 |
} |
154 |
|
155 |
@Override
|
156 |
public String getCode() { |
157 |
return this.code; |
158 |
} |
159 |
|
160 |
@Override
|
161 |
public String getLabel() { |
162 |
return this.label; |
163 |
} |
164 |
|
165 |
@Override
|
166 |
public void dispose() { |
167 |
for (Map.Entry<String, FeatureStore> entry : storesCache.entrySet()) { |
168 |
FeatureStore store = entry.getValue(); |
169 |
if (store != null) { |
170 |
DisposeUtils.disposeQuietly(store); |
171 |
} |
172 |
} |
173 |
this.storesCache.clear();
|
174 |
DisposeUtils.dispose(this.wsexplorer);
|
175 |
this.wsexplorer= null; |
176 |
this.code = null; |
177 |
} |
178 |
|
179 |
@Override
|
180 |
public JDBCServerExplorerParameters getExplorerParameters() {
|
181 |
return this.wsexplorer.getParameters(); |
182 |
} |
183 |
|
184 |
private boolean isInStoreIgnoreChanges(FeatureStore store) { |
185 |
if (this.storeIgnoreChanges == null) { |
186 |
return false; |
187 |
} |
188 |
return this.storeIgnoreChanges.contains(store); |
189 |
} |
190 |
|
191 |
private void addStoreIgnoreChanges(FeatureStore store) { |
192 |
if (this.storeIgnoreChanges == null) { |
193 |
this.storeIgnoreChanges = new HashSet<>(); |
194 |
} |
195 |
this.storeIgnoreChanges.add(store);
|
196 |
} |
197 |
|
198 |
private void removeStoreIgnoreChanges(FeatureStore store) { |
199 |
if (this.storeIgnoreChanges == null) { |
200 |
this.storeIgnoreChanges = new HashSet<>(); |
201 |
} |
202 |
this.storeIgnoreChanges.remove(store);
|
203 |
} |
204 |
|
205 |
// private boolean contains(FeatureStore store) {
|
206 |
// return this.getEntity(store) != null;
|
207 |
// }
|
208 |
//
|
209 |
private EntityRow getEntity(FeatureStore store) {
|
210 |
String s = (String) store.getProperty("VCSGIS_WORKSPACE"); |
211 |
if (s != null) { |
212 |
if (StringUtils.equalsIgnoreCase(code, s)) {
|
213 |
return this.getWorkspaceEntityByName((String) store.getProperty("VCSGIS_ENTITY")); |
214 |
} |
215 |
return null; |
216 |
} |
217 |
|
218 |
DataStoreParameters params = store.getParameters(); |
219 |
if (!DataStore.H2SPATIAL_PROVIDER_NAME.equalsIgnoreCase(params.getProviderName())) {
|
220 |
store.setProperty("VCSGIS_WORKSPACE", "#########"); |
221 |
return null; |
222 |
} |
223 |
|
224 |
File f = ((HasAFile) params).getFile();
|
225 |
if (f == null) { |
226 |
store.setProperty("VCSGIS_WORKSPACE", "#########"); |
227 |
return null; |
228 |
} |
229 |
EntityRow entity = this.getWorkspaceEntityByName(((JDBCStoreParameters) params).getTable());
|
230 |
if (entity == null) { |
231 |
store.setProperty("VCSGIS_WORKSPACE", "#########"); |
232 |
return null; |
233 |
} |
234 |
store.setProperty("VCSGIS_WORKSPACE", this.code); |
235 |
store.setProperty("VCSGIS_ENTITY", entity.getEntityName());
|
236 |
|
237 |
return entity;
|
238 |
} |
239 |
|
240 |
private void loadEntities() { |
241 |
FeatureStore store = null;
|
242 |
try {
|
243 |
Map<String, EntityRow> theEntitiesByName = new HashMap<>(); |
244 |
Map<String, EntityRow> theEntitiesByCode = new HashMap<>(); |
245 |
store = getFeatureStore(EntitiesTable.TABLE_NAME); |
246 |
for (Feature feature : store.getFeatureSet().iterable()) {
|
247 |
EntityRow entity = new EntityRow(this, feature); |
248 |
theEntitiesByName.put(entity.getEntityName(), entity); |
249 |
theEntitiesByCode.put(entity.getCode(), entity); |
250 |
LOGGER.debug("===: loadEntities: "+entity.getEntityName()+" entity code "+entity.getEntityCode()+", repo rev. "+entity.getRepositoryRevisionCode()+", local rev. "+entity.getLocalRevisionCode()); |
251 |
} |
252 |
this.entitiesByName = theEntitiesByName;
|
253 |
this.entitiesByCode = theEntitiesByCode;
|
254 |
} catch (Exception ex) { |
255 |
LOGGER.warn("Can't load entity information.", ex);
|
256 |
} finally {
|
257 |
DisposeUtils.disposeQuietly(store); |
258 |
} |
259 |
} |
260 |
|
261 |
@Override
|
262 |
public List<VCSGisWorkspaceEntity> getWorkspaceEntities() { |
263 |
List<VCSGisWorkspaceEntity> entities = new ArrayList<>(); |
264 |
entities.addAll(this.getEntitiesAsEntityRow());
|
265 |
return entities;
|
266 |
} |
267 |
|
268 |
private Collection<EntityRow> getEntitiesAsEntityRow() { |
269 |
if (this.entitiesByName == null) { |
270 |
loadEntities(); |
271 |
} |
272 |
return this.entitiesByName.values(); |
273 |
} |
274 |
|
275 |
@Override
|
276 |
public EntityRow getWorkspaceEntityByName(String name) { |
277 |
if (this.entitiesByName == null) { |
278 |
this.loadEntities();
|
279 |
} |
280 |
return this.entitiesByName.get(name); |
281 |
} |
282 |
|
283 |
@Override
|
284 |
public EntityRow getWorkspaceEntityByCode(String code) { |
285 |
if (this.entitiesByCode == null) { |
286 |
this.loadEntities();
|
287 |
} |
288 |
return this.entitiesByCode.get(code); |
289 |
} |
290 |
|
291 |
public void reloadEntities() { |
292 |
this.entitiesByCode = null; |
293 |
this.entitiesByName = null; |
294 |
} |
295 |
|
296 |
@Override
|
297 |
public void create_table(String name) { |
298 |
EntityRow entity = this.getWorkspaceEntityByName(name);
|
299 |
if (entity == null) { |
300 |
throw new IllegalArgumentException("Can't locate informacion of table '" + name + "'."); |
301 |
} |
302 |
try {
|
303 |
DataManager dataManager = DALLocator.getDataManager(); |
304 |
JDBCNewStoreParameters addparams = (JDBCNewStoreParameters) this.wsexplorer.getAddParameters(name);
|
305 |
addparams.setDefaultFeatureType(entity.getFeatureType()); |
306 |
this.wsexplorer.add(DataStore.H2SPATIAL_PROVIDER_NAME, addparams, true); |
307 |
|
308 |
} catch (Exception ex) { |
309 |
throw new RuntimeException("Can't create table '" + name + "'.", ex); |
310 |
} |
311 |
|
312 |
} |
313 |
|
314 |
@Override
|
315 |
public FeatureStore getFeatureStore(String tableName) { |
316 |
EntityRow entity = null;
|
317 |
if( !INTERNAL_TABLES.contains(tableName) ) {
|
318 |
entity = this.getWorkspaceEntityByName(tableName);
|
319 |
if (entity == null) { |
320 |
return null; |
321 |
} |
322 |
} |
323 |
FeatureStore store = this.storesCache.get(tableName);
|
324 |
if (store != null) { |
325 |
DisposeUtils.bind(store); |
326 |
return store;
|
327 |
} |
328 |
DataManager dataManager = DALLocator.getDataManager(); |
329 |
try {
|
330 |
JDBCStoreParameters params = this.wsexplorer.get(tableName);
|
331 |
store = (FeatureStore) dataManager.openStore( |
332 |
DataStore.H2SPATIAL_PROVIDER_NAME, |
333 |
params |
334 |
); |
335 |
if( entity!=null ) { |
336 |
StoreProperties.set(store, this, entity.getEntityName());
|
337 |
} |
338 |
|
339 |
this.storesCache.put(tableName, store);
|
340 |
|
341 |
DisposeUtils.bind(store); |
342 |
return store;
|
343 |
} catch (Exception ex) { |
344 |
String msg = "can't open store from '" + this.getMessageLabel() + "'."; |
345 |
LOGGER.warn(msg, ex); |
346 |
throw new RuntimeException(msg, ex); |
347 |
} |
348 |
} |
349 |
|
350 |
@Override
|
351 |
public String createUniqueFeatureCode() { |
352 |
UUID x = UUID.randomUUID(); |
353 |
String s = x.toString().replace("-", ""); |
354 |
return s;
|
355 |
} |
356 |
|
357 |
private String createUniqueID() { |
358 |
UUID x = UUID.randomUUID(); |
359 |
String s = x.toString().replace("-", ""); |
360 |
return s;
|
361 |
} |
362 |
|
363 |
@Override
|
364 |
public String getErrorMessage(int errcode) { |
365 |
return VCSGisUtils.getErrorMessage(errcode);
|
366 |
} |
367 |
|
368 |
@Override
|
369 |
public int addChange(int operation, FeatureStore store, Feature feature) { |
370 |
String entityName = StoreProperties.getEntityName(store);
|
371 |
if (StringUtils.isBlank(entityName)) {
|
372 |
return ERR_STORE_NOT_IN_VERSION_CONTROL;
|
373 |
} |
374 |
if (this.isInStoreIgnoreChanges(store)) { |
375 |
// LOGGER.debug("===: ADD_CHANGE: Skip ("+store.getName()+")");
|
376 |
return ERR_NO_ERROR;
|
377 |
} |
378 |
EntityRow entity = this.getWorkspaceEntityByName(entityName);
|
379 |
WorkspaceChangeRow change = new WorkspaceChangeRow(this); |
380 |
change.setEntityCode(entity.getCode()); |
381 |
change.setFeatureCode(feature.getString(entity.getFeatureIdFieldName())); |
382 |
change.setOperation(operation); |
383 |
change.setLabel(feature.getString(entity.getFieldForLabel())); |
384 |
change.setSelected(true);
|
385 |
change.insert(); |
386 |
return ERR_NO_ERROR;
|
387 |
} |
388 |
|
389 |
@Override
|
390 |
public int addEntity(FeatureType featureType, String name, String description, String fieldForLabel) { |
391 |
FeatureStore store = null;
|
392 |
int err = ERR_NO_ERROR;
|
393 |
try {
|
394 |
// LOGGER.debug("===: ADD_ENTITY "+this.getCode()+", '"+this.getLabel()+"', "+name);
|
395 |
if (featureType.getAttributeDescriptor(FEATURECODE_FIELD_NAME) == null) { |
396 |
err = ERR_ENTITY_NOT_HAS_VCSGISCODE; |
397 |
EditableFeatureType ft = featureType.getCopy().getEditable(); |
398 |
ft.add(FEATURECODE_FIELD_NAME, DataTypes.STRING) |
399 |
.setSize(40)
|
400 |
.setIsIndexed(true)
|
401 |
.setAllowIndexDuplicateds(false)
|
402 |
.setLabel("VCSGIS Code");
|
403 |
ft.setHasOID(false);
|
404 |
featureType = ft.getNotEditableCopy(); |
405 |
} |
406 |
if(!featureType.hasPrimaryKey()){
|
407 |
EditableFeatureType ft = featureType.getCopy().getEditable(); |
408 |
ft.getEditableAttributeDescriptor(FEATURECODE_FIELD_NAME).setIsPrimaryKey(true);
|
409 |
featureType = ft.getNotEditableCopy(); |
410 |
} |
411 |
if (featureType.getAttributeDescriptor(fieldForLabel) == null) { |
412 |
FeatureAttributeDescriptor[] pk = featureType.getPrimaryKey();
|
413 |
if( pk == null || pk.length<1 ) { |
414 |
fieldForLabel = featureType.getAttributeName(0);
|
415 |
} else {
|
416 |
fieldForLabel = pk[0].getName();
|
417 |
} |
418 |
} |
419 |
|
420 |
err = ERR_CANT_OPEN_ENTITIES; |
421 |
EntityRow entity = new EntityRow(this); |
422 |
entity.setEntityName(name); |
423 |
entity.setDataTableName(DEFAULT_DATA_TABLE); |
424 |
entity.setFeatureIdFieldName(FEATURECODE_FIELD_NAME); |
425 |
entity.setGeometryFieldName(featureType.getDefaultGeometryAttributeName()); |
426 |
entity.setDescription(description); |
427 |
entity.setFieldForLabel(fieldForLabel); |
428 |
entity.setFeatureTypeAsJson(featureType.toJsonBuilder().toString()); |
429 |
entity.setLocalRevisionCode(null);
|
430 |
entity.setRepositoryRevisionCode(null);
|
431 |
entity.insert(); |
432 |
|
433 |
this.reloadEntities();
|
434 |
err = ERR_CANT_INSERT_CHANGE; |
435 |
|
436 |
entity = this.getWorkspaceEntityByName(name);
|
437 |
WorkspaceChangeRow change = new WorkspaceChangeRow(this); |
438 |
change.setEntityCode(entity.getCode()); |
439 |
change.setFeatureCode(null);
|
440 |
change.setOperation(OP_ADD_ENTITY); |
441 |
change.setSelected(true);
|
442 |
change.insert(); |
443 |
|
444 |
this.create_table(name);
|
445 |
|
446 |
} catch (VCSGisRuntimeException ex) {
|
447 |
LOGGER.warn("can't add entity '" + name + "'.", ex); |
448 |
if (store != null) { |
449 |
store.cancelEditingQuietly(); |
450 |
} |
451 |
return ex.getErrnum();
|
452 |
} catch (Exception ex) { |
453 |
LOGGER.warn("can't add entity '" + name + "'.", ex); |
454 |
if (store != null) { |
455 |
store.cancelEditingQuietly(); |
456 |
} |
457 |
return err;
|
458 |
} finally {
|
459 |
DisposeUtils.disposeQuietly(store); |
460 |
} |
461 |
return ERR_NO_ERROR;
|
462 |
} |
463 |
|
464 |
|
465 |
@Override
|
466 |
public int addChanges(FeatureStore store, Iterator<Feature> insertedsFeatures, Iterator<Feature> updatedsFeatures, Iterator<FeatureReference> deletedsFeatures) { |
467 |
String entityName = StoreProperties.getEntityName(store);
|
468 |
if (StringUtils.isBlank(entityName)) {
|
469 |
return ERR_STORE_NOT_IN_VERSION_CONTROL;
|
470 |
} |
471 |
if (this.isInStoreIgnoreChanges(store)) { |
472 |
// LOGGER.debug("===: ADD_CHANGE: Skip ("+store.getName()+")");
|
473 |
return ERR_NO_ERROR;
|
474 |
} |
475 |
EntityRow entity = this.getWorkspaceEntityByName(entityName);
|
476 |
if (entity == null) { |
477 |
return ERR_STORE_NOT_IN_VERSION_CONTROL;
|
478 |
} |
479 |
WorkspaceChangesTable changesTable = new WorkspaceChangesTable();
|
480 |
FeatureStore changes_store = null;
|
481 |
int err = ERR_CANT_OPEN_CHANGES;
|
482 |
try {
|
483 |
changes_store = this.getFeatureStore(WorkspaceChangesTable.TABLE_NAME);
|
484 |
err = ERR_CANT_INSERT_CHANGE; |
485 |
changes_store.edit(FeatureStore.MODE_PASS_THROUGH); |
486 |
if (insertedsFeatures != null) { |
487 |
while (insertedsFeatures.hasNext()) {
|
488 |
Feature f = insertedsFeatures.next(); |
489 |
if (f == null) { |
490 |
continue;
|
491 |
} |
492 |
WorkspaceChangeRow change = new WorkspaceChangeRow(this); |
493 |
change.setEntityCode(entity.getCode()); |
494 |
change.setFeatureCode(f.getString(entity.getFeatureIdFieldName())); |
495 |
change.setOperation(OP_INSERT); |
496 |
change.setLabel(f.getString(entity.getFieldForLabel())); |
497 |
change.setSelected(true);
|
498 |
change.insert(changes_store); |
499 |
} |
500 |
} |
501 |
if (updatedsFeatures != null) { |
502 |
while (updatedsFeatures.hasNext()) {
|
503 |
Feature f = updatedsFeatures.next(); |
504 |
if (f == null) { |
505 |
continue;
|
506 |
} |
507 |
WorkspaceChangeRow change = new WorkspaceChangeRow(this); |
508 |
String featureCode = f.getString(entity.getFeatureIdFieldName());
|
509 |
WorkspaceChangeRow previousChange = changesTable.find(this, changes_store, entity.getCode(), featureCode);
|
510 |
if(previousChange == null) { |
511 |
change.setEntityCode(entity.getCode()); |
512 |
change.setFeatureCode(f.getString(entity.getFeatureIdFieldName())); |
513 |
change.setOperation(OP_UPDATE); |
514 |
change.setLabel(f.getString(entity.getFieldForLabel())); |
515 |
change.setSelected(true);
|
516 |
change.insert(changes_store); |
517 |
} |
518 |
} |
519 |
} |
520 |
if (deletedsFeatures != null) { |
521 |
while (deletedsFeatures.hasNext()) {
|
522 |
FeatureReference fr = deletedsFeatures.next(); |
523 |
if (fr == null) { |
524 |
continue;
|
525 |
} |
526 |
Feature f = fr.getFeatureQuietly(); |
527 |
if (f == null) { |
528 |
continue;
|
529 |
} |
530 |
WorkspaceChangeRow change = new WorkspaceChangeRow(this); |
531 |
String featureCode = f.getString(entity.getFeatureIdFieldName());
|
532 |
WorkspaceChangeRow previousChange = changesTable.find(this, changes_store, entity.getCode(), featureCode);
|
533 |
if(previousChange == null) { |
534 |
change.setEntityCode(entity.getCode()); |
535 |
change.setFeatureCode(f.getString(entity.getFeatureIdFieldName())); |
536 |
change.setOperation(OP_DELETE); |
537 |
change.setLabel(f.getString(entity.getFieldForLabel())); |
538 |
change.setSelected(true);
|
539 |
change.insert(changes_store); |
540 |
} else {
|
541 |
switch(previousChange.getOperation()){
|
542 |
case OP_INSERT:
|
543 |
previousChange.delete(changes_store); |
544 |
break;
|
545 |
case OP_DELETE:
|
546 |
LOGGER.warn("Try delete feature already deleted (.'"+previousChange.getRelatedFeature().toJson().toString()+"'"); |
547 |
break;
|
548 |
case OP_UPDATE:
|
549 |
previousChange.setOperation(OP_DELETE); |
550 |
previousChange.setSelected(true);
|
551 |
previousChange.update(); |
552 |
break;
|
553 |
} |
554 |
} |
555 |
} |
556 |
} |
557 |
changes_store.finishEditing(); |
558 |
err = ERR_NO_ERROR; |
559 |
} catch (Exception ex) { |
560 |
this.logger.warn("Can't add changes.", ex); |
561 |
if (changes_store != null) { |
562 |
changes_store.cancelEditingQuietly(); |
563 |
} |
564 |
} finally {
|
565 |
DisposeUtils.disposeQuietly(changes_store); |
566 |
} |
567 |
return err;
|
568 |
} |
569 |
|
570 |
@Override
|
571 |
public int add(String name, FeatureStore source, String fieldForLabel) { |
572 |
FeatureStore target = null;
|
573 |
DisposableFeatureSetIterable features = null;
|
574 |
int errcode = ERR_NO_ERROR;
|
575 |
try {
|
576 |
// LOGGER.debug("===: ADD "+this.getCode()+", '"+this.getLabel()+"', "+name);
|
577 |
EntityRow entity = this.getWorkspaceEntityByName(name);
|
578 |
if (entity != null) { |
579 |
return ERR_ENTITY_ALREADY_EXISTS;
|
580 |
} |
581 |
FeatureType ft = source.getDefaultFeatureTypeQuietly(); |
582 |
errcode = this.addEntity(ft, name, null, fieldForLabel); |
583 |
if (errcode != ERR_NO_ERROR) {
|
584 |
return errcode;
|
585 |
} |
586 |
errcode = ERR_CANT_OPEN_STORE; |
587 |
target = this.getFeatureStore(name);
|
588 |
errcode = ERR_CANT_RETRIEVE_SOURCE_FEATURES; |
589 |
features = source.getFeatureSet().iterable(); |
590 |
errcode = ERR_CANT_INSERT_FEATURES; |
591 |
target.edit(FeatureStore.MODE_APPEND); |
592 |
for (Feature feature : features) {
|
593 |
EditableFeature f = target.createNewFeature(feature); |
594 |
target.insert(f); |
595 |
} |
596 |
target.finishEditing(); |
597 |
return ERR_NO_ERROR;
|
598 |
} catch (Exception ex) { |
599 |
LOGGER.warn("Can't add features to '" + name + "' in '" + this.getMessageLabel() + "'.", ex); |
600 |
return errcode;
|
601 |
} finally {
|
602 |
DisposeUtils.disposeQuietly(features); |
603 |
DisposeUtils.disposeQuietly(target); |
604 |
} |
605 |
} |
606 |
|
607 |
@Override
|
608 |
public VCSGisWorkspaceChanges<VCSGisWorkspaceChange> getLocalChanges() {
|
609 |
WorkspaceChangesTable changesTable = new WorkspaceChangesTable();
|
610 |
ChangesImpl changes = new ChangesImpl<WorkspaceChangeRow>(changesTable.getByEntityCode(this,null), WorkspaceChangesTable.SELECTED) { |
611 |
@Override
|
612 |
protected WorkspaceChangeRow createChange(Feature f) {
|
613 |
return new WorkspaceChangeRow(VCSGisWorkspaceImpl.this, f); |
614 |
} |
615 |
|
616 |
@Override
|
617 |
protected void updateChange(FeatureStore store, WorkspaceChangeRow change) { |
618 |
change.update(store); |
619 |
} |
620 |
|
621 |
}; |
622 |
return changes;
|
623 |
} |
624 |
|
625 |
@Override
|
626 |
public VCSGisWorkspaceChanges<VCSGisWorkspaceChange> getLocalChangesByEntityName(String entityName) { |
627 |
EntityRow entity = this.getWorkspaceEntityByName(entityName);
|
628 |
return getLocalChangesByEntityCode(entity.getEntityCode());
|
629 |
} |
630 |
|
631 |
@Override
|
632 |
public VCSGisWorkspaceChanges<VCSGisWorkspaceChange> getLocalChangesByEntityCode(String entityCode) { |
633 |
WorkspaceChangesTable changesTable = new WorkspaceChangesTable();
|
634 |
ChangesImpl changes = new ChangesImpl<WorkspaceChangeRow>(changesTable.getByEntityCode(this,entityCode),WorkspaceChangesTable.SELECTED) { |
635 |
@Override
|
636 |
protected WorkspaceChangeRow createChange(Feature f) {
|
637 |
return new WorkspaceChangeRow(VCSGisWorkspaceImpl.this, f); |
638 |
} |
639 |
|
640 |
@Override
|
641 |
protected void updateChange(FeatureStore store, WorkspaceChangeRow change) { |
642 |
change.update(store); |
643 |
} |
644 |
|
645 |
}; |
646 |
return changes;
|
647 |
} |
648 |
|
649 |
@Override
|
650 |
public VCSGisRepository getRepository() {
|
651 |
return repository;
|
652 |
} |
653 |
|
654 |
@Override
|
655 |
public List<VCSGisWorkspaceEntity> getEntitiesOfRemoteChanges() { |
656 |
ArrayList<VCSGisWorkspaceEntity> res = new ArrayList<>(); |
657 |
RemoteChangesTable changesTable = new RemoteChangesTable();
|
658 |
DisposableFeatureSetIterable features = changesTable.getGroupedByEntity(this);
|
659 |
EntitiesTable entitiesTable = new EntitiesTable();
|
660 |
for (Feature feature : features) {
|
661 |
EntityRow entity = entitiesTable.getByEntityCode(this, feature.getString(RemoteChangesTable.COD_ENTITY));
|
662 |
res.add(entity); |
663 |
} |
664 |
DisposeUtils.disposeQuietly(features); |
665 |
return res;
|
666 |
} |
667 |
|
668 |
@Override
|
669 |
public VCSGisWorkspaceChanges<VCSGisRepositoryChange> getRemoteChanges() {
|
670 |
RemoteChangesTable changesTable = new RemoteChangesTable();
|
671 |
ChangesImpl changes = new ChangesImpl<RemoteChangeRow>(changesTable.getByEntityCode(this,null),RemoteChangesTable.SELECTED) { |
672 |
@Override
|
673 |
protected RemoteChangeRow createChange(Feature f) {
|
674 |
return new RemoteChangeRow(VCSGisWorkspaceImpl.this, f); |
675 |
} |
676 |
|
677 |
@Override
|
678 |
protected void updateChange(FeatureStore store, RemoteChangeRow change) { |
679 |
change.update(store); |
680 |
} |
681 |
|
682 |
}; |
683 |
return changes;
|
684 |
} |
685 |
|
686 |
private Timestamp now() { |
687 |
return Timestamp.from(LocalDateTime.now().toInstant(ZoneOffset.UTC)); |
688 |
} |
689 |
|
690 |
@Override
|
691 |
public int commit() { |
692 |
return commit(now(),null); |
693 |
} |
694 |
|
695 |
@Override
|
696 |
@SuppressWarnings("Convert2Lambda") |
697 |
public int commit(Timestamp efectivedate, String comment) { |
698 |
VCSGisCommitRequest request = null;
|
699 |
try {
|
700 |
LOGGER.debug("===: COMMIT "+this.getCode()+", "+this.getLabel()); |
701 |
request = this.repository.createCommitRequest();
|
702 |
request.setEfectiveDate(efectivedate); |
703 |
request.setComment(comment); |
704 |
WorkspaceChangesTable changesTable = new WorkspaceChangesTable();
|
705 |
EntitiesTable entitiesTable = new EntitiesTable();
|
706 |
|
707 |
LOGGER.debug("===: COMMIT: Adding changed local entities");
|
708 |
// A?adimos las entidades que vamos a actualizar con las revisiones que tenemos.
|
709 |
DisposableFeatureSetIterable changesGroupedByEntity = changesTable.getGroupedByEntity(this);
|
710 |
for (Feature fchange : changesGroupedByEntity) {
|
711 |
WorkspaceChangeRow change = new WorkspaceChangeRow(this, fchange); |
712 |
EntityRow localEntity = change.getEntity(); |
713 |
// LOGGER.debug("===: COMMIT: add used entity = "+fchange.toJson().toString().replace('\n', ' '));
|
714 |
request.add(request.createEntity(localEntity)); |
715 |
} |
716 |
changesGroupedByEntity.dispose(); |
717 |
|
718 |
LOGGER.debug("===: COMMIT: Mark new local entities");
|
719 |
// Marcamos como nuevas las entidades que se han dado de alta.
|
720 |
DisposableFeatureSetIterable changesOfAddEntities = changesTable.getByOperation(this, OP_ADD_ENTITY);
|
721 |
for (Feature fchange : changesOfAddEntities) {
|
722 |
WorkspaceChangeRow change = new WorkspaceChangeRow(this, fchange); |
723 |
request.markAsNew(change.getEntity()); |
724 |
} |
725 |
changesOfAddEntities.dispose(); |
726 |
|
727 |
LOGGER.debug("===: COMMIT: Adding data");
|
728 |
// A?adimos los datos
|
729 |
boolean useSeleccion = true; |
730 |
DisposableFeatureSetIterable changes = changesTable.getSelectedsWithoutAddEntity(this);
|
731 |
if (changes == null) { |
732 |
changes = changesTable.getAll(this);
|
733 |
useSeleccion = false;
|
734 |
} |
735 |
request.add(new DisposableIterableAdapter(
|
736 |
changes, |
737 |
new DisposableIterableAdapter.ItemConverter<Feature, VCSGisChange>() {
|
738 |
@Override
|
739 |
public VCSGisChange convert(Feature f) {
|
740 |
return new WorkspaceChangeRow(VCSGisWorkspaceImpl.this, f); |
741 |
} |
742 |
} |
743 |
) |
744 |
); |
745 |
|
746 |
LOGGER.debug("===: COMMIT: Do execute request");
|
747 |
if( request.execute()!=ERR_NO_ERROR ) {
|
748 |
return request.getLastErrorCode();
|
749 |
} |
750 |
|
751 |
LOGGER.debug("===: COMMIT: update local entities table");
|
752 |
FeatureStore store = null;
|
753 |
try {
|
754 |
store = this.getFeatureStore(EntitiesTable.TABLE_NAME);
|
755 |
store.edit(FeatureStore.MODE_FULLEDIT); |
756 |
for (VCSGisEntity rentity : request.getChangedLocalEntities()) {
|
757 |
EntityRow entityRow = entitiesTable.getByEntityName(this, rentity.getEntityName());
|
758 |
if (entityRow != null) { |
759 |
entityRow.copyfrom(rentity); |
760 |
entityRow.setLocalRevisionCode(rentity.getRepositoryRevisionCode()); |
761 |
entityRow.update(store); |
762 |
} |
763 |
} |
764 |
store.finishEditing(); |
765 |
reloadEntities(); |
766 |
} finally {
|
767 |
DisposeUtils.disposeQuietly(store); |
768 |
store = null;
|
769 |
} |
770 |
|
771 |
LOGGER.debug("===: COMMIT: clean local changes");
|
772 |
if (useSeleccion) {
|
773 |
changesTable.deleteSelecteds(this);
|
774 |
} else {
|
775 |
changesTable.deleteAll(this);
|
776 |
} |
777 |
return ERR_NO_ERROR;
|
778 |
} catch (Exception ex) { |
779 |
LOGGER.warn("Can't commit changes.", ex);
|
780 |
return ERR_CANT_COMMIT;
|
781 |
} finally {
|
782 |
DisposeUtils.disposeQuietly(request); |
783 |
} |
784 |
} |
785 |
|
786 |
@Override
|
787 |
public int checkout(String tableName) { |
788 |
FeatureStore target = null;
|
789 |
int errcode = ERR_NO_ERROR;
|
790 |
VCSGisCheckoutRequest request = null;
|
791 |
try {
|
792 |
LOGGER.debug("===: CHECKOUT "+this.getCode()+", '"+this.getLabel()+"', "+tableName); |
793 |
request = this.repository.createCheckoutRequest(tableName);
|
794 |
request.setEfectiveDate(null);
|
795 |
request.setRepositoryRevisionCode(null);
|
796 |
if( request.execute()!= ERR_NO_ERROR ) {
|
797 |
return request.getLastErrorCode();
|
798 |
} |
799 |
VCSGisEntity rentity = request.getRepositoryEntity(); |
800 |
EntityRow lentity = this.getWorkspaceEntityByName(tableName);
|
801 |
if (lentity == null) { |
802 |
lentity = new EntityRow(this); |
803 |
rentity.copyto(lentity); |
804 |
lentity.insert(); |
805 |
} else {
|
806 |
rentity.copyto(lentity); |
807 |
lentity.update(); |
808 |
} |
809 |
LOGGER.debug("===: CHECKOUT "+this.getCode()+", '"+this.getLabel()+"', "+tableName+", rev."+rentity.getRepositoryRevisionCode()); |
810 |
|
811 |
this.reloadEntities();
|
812 |
|
813 |
this.create_table(lentity.getEntityName());
|
814 |
|
815 |
errcode = ERR_CANT_OPEN_STORE; |
816 |
target = this.getFeatureStore(tableName);
|
817 |
this.addStoreIgnoreChanges(target);
|
818 |
DisposableIterable<VCSGisRepositoryData> data = request.getRepositoryData(); |
819 |
errcode = ERR_CANT_INSERT_FEATURES; |
820 |
target.edit(FeatureStore.MODE_APPEND); |
821 |
for (VCSGisRepositoryData d : data) {
|
822 |
EditableFeature f = target.createNewFeature(d.getDataAsJson()); |
823 |
target.insert(f); |
824 |
} |
825 |
target.finishEditing(); |
826 |
request.dispose(); |
827 |
return ERR_NO_ERROR;
|
828 |
} catch (Exception ex) { |
829 |
LOGGER.warn("Can't commit changes.", ex);
|
830 |
return errcode;
|
831 |
} finally {
|
832 |
DisposeUtils.disposeQuietly(request); |
833 |
if(target != null) { |
834 |
this.removeStoreIgnoreChanges(target);
|
835 |
} |
836 |
DisposeUtils.disposeQuietly(target); |
837 |
} |
838 |
} |
839 |
|
840 |
@Override
|
841 |
public int updateEntities() { |
842 |
LOGGER.debug("===: UPDATE_ENTITIES "+this.getCode()+", '"+this.getLabel()+"'"); |
843 |
FeatureStore store = null;
|
844 |
EntitiesTable entitiesTable = new EntitiesTable();
|
845 |
try {
|
846 |
store = this.getFeatureStore(EntitiesTable.TABLE_NAME);
|
847 |
store.edit(FeatureStore.MODE_FULLEDIT); |
848 |
for (VCSGisEntity rentity : this.getRepositoryEntities()) { |
849 |
EntityRow entityRow = entitiesTable.getByEntityName(this, rentity.getEntityName());
|
850 |
if (entityRow != null) { |
851 |
rentity.copyto(entityRow); |
852 |
entityRow.update(store); |
853 |
} |
854 |
} |
855 |
reloadEntities(); |
856 |
store.finishEditingQuietly(); |
857 |
return ERR_NO_ERROR;
|
858 |
} catch (Exception ex) { |
859 |
LOGGER.warn("Can't update entities.", ex);
|
860 |
return ERR_CANT_UPDATE_ENTITIES;
|
861 |
} finally {
|
862 |
DisposeUtils.disposeQuietly(store); |
863 |
store = null;
|
864 |
} |
865 |
} |
866 |
|
867 |
@Override
|
868 |
public int updatePrepare(String tableName) { |
869 |
LOGGER.debug("===: UPDATE_PREPARE: "+tableName+" WS "+this.getCode()+", '"+this.getLabel()+"'"); |
870 |
|
871 |
FeatureStore target = null;
|
872 |
VCSGisUpdateRequest request = null;
|
873 |
DisposableIterable<VCSGisRepositoryData> updates = null;
|
874 |
RemoteChangesTable updatesTable = new RemoteChangesTable();
|
875 |
try {
|
876 |
EntityRow lentity = this.getWorkspaceEntityByName(tableName);
|
877 |
if(lentity == null){ |
878 |
updateEntities(); |
879 |
lentity = this.getWorkspaceEntityByName(tableName);
|
880 |
if(lentity == null){ |
881 |
return ERR_ENTITY_NOT_EXISTS;
|
882 |
} |
883 |
} |
884 |
request = this.repository.createUpdateRequest(lentity);
|
885 |
if( request.execute()!=ERR_OK ) {
|
886 |
return request.getLastErrorCode();
|
887 |
} |
888 |
|
889 |
LOGGER.debug("===: UPDATE_PREPARE: "+tableName+" request code "+request.getCode()); |
890 |
VCSGisEntity rentity = request.getRepositoryEntity(); |
891 |
lentity.copyfrom(rentity); |
892 |
lentity.update(); |
893 |
|
894 |
LOGGER.debug("===: UPDATE_PREPARE: "+tableName+" entity code "+lentity.getEntityCode()+", repo rev. "+lentity.getRepositoryRevisionCode()+", local rev. "+lentity.getLocalRevisionCode()); |
895 |
this.reloadEntities();
|
896 |
|
897 |
LOGGER.debug("===: UPDATE_PREPARE: Delete updates table");
|
898 |
updatesTable.delete(this, lentity.getCode());
|
899 |
|
900 |
WorkspaceChangesTable changesTable = new WorkspaceChangesTable();
|
901 |
String entityCode = lentity.getCode();
|
902 |
|
903 |
updates = request.getRepositoryData(); |
904 |
target = this.getFeatureStore(RemoteChangesTable.TABLE_NAME);
|
905 |
target.edit(MODE_PASS_THROUGH); |
906 |
LOGGER.debug("===: UPDATE_PREPARE: Inserting updates");
|
907 |
for (VCSGisRepositoryData update : updates) {
|
908 |
RemoteChangeRow updateRow = new RemoteChangeRow(this); |
909 |
LOGGER.debug("===: UPDATE_PREPARE: insert "+update.getCode()+", "+update.getOperation()+", "+update.getData()); |
910 |
updateRow.setCode(update.getCode()); |
911 |
updateRow.setData(update.getData()); |
912 |
updateRow.setEntityCode(update.getEntityCode()); |
913 |
updateRow.setDataCode(update.getDataCode()); |
914 |
updateRow.setOperation(update.getOperation()); |
915 |
updateRow.setRevisionCode(update.getRevisionCode()); |
916 |
updateRow.setRevisionNumber(update.getRevisionNumber()); |
917 |
|
918 |
LOGGER.debug("===: UPDATE_PREPARE: row code = "+updateRow.getDataCode());
|
919 |
LOGGER.debug("===: UPDATE_PREPARE: row state = "+lentity.getState()+", "+lentity.getStateLabel()); |
920 |
LOGGER.debug("===: UPDATE_PREPARE: row operation = "+updateRow.getOperation()+", "+updateRow.getOperationLabel()); |
921 |
switch (lentity.getState()) {
|
922 |
case STATE_OUTDATED:
|
923 |
case STATE_UNMODIFIED:
|
924 |
updateRow.setSelected(true);
|
925 |
break;
|
926 |
case STATE_NEW:
|
927 |
case STATE_OUTDATED_AND_MODIFIED:
|
928 |
case STATE_MODIFIED:
|
929 |
WorkspaceChangeRow change = changesTable.getByEntityAndDataCode( |
930 |
this,
|
931 |
entityCode, |
932 |
updateRow.getDataCode() |
933 |
); |
934 |
if( change==null ) { |
935 |
LOGGER.debug("===: UPDATE_PREPARE: no local modificacion, update local data");
|
936 |
// Solo actualizamos si no ha sido modificado localmente.
|
937 |
updateRow.setSelected(true);
|
938 |
} else {
|
939 |
LOGGER.debug("===: UPDATE_PREPARE: not update. Local data is modified. "+change.getData().replace('\n', ' ')); |
940 |
updateRow.setSelected(false);
|
941 |
} |
942 |
break;
|
943 |
} |
944 |
|
945 |
updateRow.insert(target); |
946 |
} |
947 |
LOGGER.debug("===: UPDATE_PREPARE: finish inserting updates");
|
948 |
target.finishEditing(); |
949 |
|
950 |
return ERR_NO_ERROR;
|
951 |
} catch (Exception ex) { |
952 |
LOGGER.warn("Can't prepare update.", ex);
|
953 |
if( target!=null ) { |
954 |
target.cancelEditingQuietly(); |
955 |
} |
956 |
return ERR_CANT_PREPARE_UPDATE;
|
957 |
} finally {
|
958 |
DisposeUtils.disposeQuietly(updates); |
959 |
DisposeUtils.disposeQuietly(request); |
960 |
DisposeUtils.disposeQuietly(target); |
961 |
} |
962 |
|
963 |
} |
964 |
|
965 |
@Override
|
966 |
public int updateClean(String tableName) { |
967 |
RemoteChangesTable remoteChangesTable = new RemoteChangesTable();
|
968 |
remoteChangesTable.deleteAll(this);
|
969 |
return ERR_NO_ERROR;
|
970 |
} |
971 |
|
972 |
@Override
|
973 |
public int update(String tableName) { |
974 |
return this.update(tableName, true); |
975 |
} |
976 |
|
977 |
@Override
|
978 |
public int update(String tableName, boolean prepare) { |
979 |
int errcode = ERR_NO_ERROR;
|
980 |
if( prepare ) {
|
981 |
errcode = this.updatePrepare(tableName);
|
982 |
if (errcode != ERR_NO_ERROR) {
|
983 |
return errcode;
|
984 |
} |
985 |
} |
986 |
LOGGER.debug("===: UPDATE "+this.getCode()+", '"+this.getLabel()+"', "+tableName); |
987 |
RemoteChangesTable updatesTable = new RemoteChangesTable();
|
988 |
|
989 |
EditableFeature ef; |
990 |
FeatureStore target = null;
|
991 |
DisposableFeatureSetIterable features = null;
|
992 |
try {
|
993 |
EntityRow lentity = this.getWorkspaceEntityByName(tableName);
|
994 |
String dataCodeFieldName = lentity.getFeatureIdFieldName();
|
995 |
|
996 |
try {
|
997 |
target = this.getFeatureStore(tableName);
|
998 |
} catch (Exception e){ |
999 |
LOGGER.warn("Can't get feature store from '"+tableName+"'", e); |
1000 |
} |
1001 |
if(target == null) { |
1002 |
this.create_table(lentity.getEntityName());
|
1003 |
target = this.getFeatureStore(tableName);
|
1004 |
} |
1005 |
|
1006 |
features = updatesTable.getSelectedsByEntityCodeAsIterator(this,lentity.getCode());
|
1007 |
if(!features.isEmpty()){
|
1008 |
target.edit(MODE_PASS_THROUGH); |
1009 |
this.addStoreIgnoreChanges(target);
|
1010 |
for (Feature feature : features) {
|
1011 |
RemoteChangeRow updateRow = new RemoteChangeRow(this, feature); |
1012 |
switch (updateRow.getOperation()) {
|
1013 |
case OP_INSERT:
|
1014 |
LOGGER.debug("===: UPDATE: insert");
|
1015 |
ef = target.createNewFeature(updateRow.getDataAsJson()); |
1016 |
target.insert(ef); |
1017 |
break;
|
1018 |
case OP_UPDATE:
|
1019 |
LOGGER.debug("===: UPDATE: update");
|
1020 |
ef = target.createNewFeature(updateRow.getDataAsJson()); |
1021 |
ef.setUpdatable(true);
|
1022 |
target.update(ef); |
1023 |
break;
|
1024 |
case OP_DELETE:
|
1025 |
LOGGER.debug("===: UPDATE: delete");
|
1026 |
target.delete("\"" + dataCodeFieldName + "\"='" + updateRow.getDataCode() + "'"); |
1027 |
break;
|
1028 |
} |
1029 |
} |
1030 |
target.finishEditing(); |
1031 |
} |
1032 |
lentity.setLocalRevisionCode(lentity.getRepositoryRevisionCode()); |
1033 |
lentity.update(); |
1034 |
|
1035 |
return ERR_NO_ERROR;
|
1036 |
} catch (Exception ex) { |
1037 |
LOGGER.warn("Can't update.", ex);
|
1038 |
if(target != null){ |
1039 |
target.cancelEditingQuietly(); |
1040 |
} |
1041 |
return ERR_CANT_UPDATE;
|
1042 |
} finally {
|
1043 |
this.removeStoreIgnoreChanges(target);
|
1044 |
DisposeUtils.disposeQuietly(features); |
1045 |
DisposeUtils.disposeQuietly(target); |
1046 |
} |
1047 |
} |
1048 |
|
1049 |
@Override
|
1050 |
public JDBCServerExplorer getExplorer() {
|
1051 |
return this.wsexplorer; |
1052 |
} |
1053 |
|
1054 |
@Override
|
1055 |
public int log(String tableName) { |
1056 |
FeatureStore target = null;
|
1057 |
int errcode = ERR_NO_ERROR;
|
1058 |
VCSGisLogRequest request = null;
|
1059 |
try {
|
1060 |
LOGGER.debug("===: LOG "+this.getCode()+", '"+this.getLabel()+"', "+tableName); |
1061 |
EntityRow entity = this.getWorkspaceEntityByName(tableName);
|
1062 |
request = this.repository.createLogRequest(tableName);
|
1063 |
|
1064 |
//TODO: BORRAR LOS REGISTROS DE LOCAL REVISIONS TABLE de la entidad TABLE_NAME/entity.getCode
|
1065 |
errcode = ERR_CANT_OPEN_STORE; |
1066 |
target = this.getFeatureStore(LocalRevisionsTable.TABLE_NAME);
|
1067 |
errcode = ERR_CANT_RETRIEVE_SOURCE_FEATURES; |
1068 |
DisposableIterable<VCSGisRepositoryRevision> data = request.getData(); |
1069 |
errcode = ERR_CANT_INSERT_FEATURES; |
1070 |
target.edit(FeatureStore.MODE_APPEND); |
1071 |
for (VCSGisRepositoryRevision d : data) {
|
1072 |
EditableFeature f = target.createNewFeature(); |
1073 |
LocalRevisionRow row = new LocalRevisionRow(this, f); |
1074 |
row.setUserCode(d.getUserCode()); |
1075 |
//TODO: ...
|
1076 |
row.insert(target); |
1077 |
} |
1078 |
target.finishEditing(); |
1079 |
return ERR_NO_ERROR;
|
1080 |
} catch (Exception ex) { |
1081 |
LOGGER.warn("Can't commit changes.", ex);
|
1082 |
return errcode;
|
1083 |
} finally {
|
1084 |
DisposeUtils.disposeQuietly(request); |
1085 |
if(target != null) { |
1086 |
this.removeStoreIgnoreChanges(target);
|
1087 |
} |
1088 |
DisposeUtils.disposeQuietly(target); |
1089 |
} |
1090 |
} |
1091 |
|
1092 |
@Override
|
1093 |
public List<VCSGisWorkspaceRevision> getRemoteRevisions(String tableName) { |
1094 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
1095 |
} |
1096 |
|
1097 |
@Override
|
1098 |
public List<VCSGisEntity> getRepositoryEntities() { |
1099 |
VCSGisEntitiesRequest request = this.getRepository().createEntitiesRequest();
|
1100 |
request.execute(); |
1101 |
List<VCSGisEntity> entities = request.getRepositoryEntities();
|
1102 |
request.dispose(); |
1103 |
return entities;
|
1104 |
} |
1105 |
|
1106 |
} |