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 / tables / EntitiesTable.java @ 3310
History | View | Annotate | Download (19.1 KB)
1 |
package org.gvsig.vcsgis.lib.workspace.tables; |
---|---|
2 |
|
3 |
import javax.json.JsonObject; |
4 |
import org.apache.commons.lang3.StringUtils; |
5 |
import org.gvsig.fmap.dal.DALLocator; |
6 |
import org.gvsig.fmap.dal.DataManager; |
7 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
8 |
import org.gvsig.fmap.dal.feature.Feature; |
9 |
import org.gvsig.fmap.dal.feature.FeatureSet.DisposableFeatureSetIterable; |
10 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
11 |
import org.gvsig.fmap.dal.feature.FeatureType; |
12 |
import org.gvsig.json.Json; |
13 |
import org.gvsig.json.JsonObjectBuilder; |
14 |
import org.gvsig.tools.dataTypes.DataTypes; |
15 |
import org.gvsig.tools.dispose.DisposeUtils; |
16 |
import org.gvsig.vcsgis.lib.VCSGisEntity; |
17 |
import org.gvsig.vcsgis.lib.VCSGisEntityEditable; |
18 |
import static org.gvsig.vcsgis.lib.VCSGisManager.STATE_CONFLICT; |
19 |
import org.gvsig.vcsgis.lib.VCSGisUtils; |
20 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace; |
21 |
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceEntity; |
22 |
import org.gvsig.vcsgis.lib.workspace.tables.AbstractTable.AbstractRow; |
23 |
import static org.gvsig.vcsgis.lib.VCSGisManager.STATE_LOCAL_UNMODIFIED; |
24 |
import static org.gvsig.vcsgis.lib.VCSGisManager.STATE_LOCAL_NEW; |
25 |
import static org.gvsig.vcsgis.lib.VCSGisManager.STATE_LOCAL_MODIFIED; |
26 |
import static org.gvsig.vcsgis.lib.VCSGisManager.STATE_LOCAL_OUTDATED; |
27 |
import static org.gvsig.vcsgis.lib.VCSGisManager.STATE_LOCAL_OUTDATED_AND_MODIFIED; |
28 |
|
29 |
/**
|
30 |
*
|
31 |
* @author gvSIG Team
|
32 |
*/
|
33 |
@SuppressWarnings("UseSpecificCatch") |
34 |
public class EntitiesTable extends AbstractTable { |
35 |
|
36 |
public static final String TABLE_NAME = "VCSGIS_ENTITIES"; |
37 |
|
38 |
public static final String COD_ENTITY = "COD_ENTITY"; |
39 |
private static final String ENTITY_NAME = "ENT_NAME"; |
40 |
private static final String ENTITY_STATE = "ENT_STATE"; |
41 |
private static final String DATATABLE_NAME = "ENT_DATATABLE"; |
42 |
private static final String FEATUREID_FIELD_NAME = "ENT_FEATURECODE"; |
43 |
private static final String GEOMETRY_FIELD_NAME = "ENT_GEOMNAME"; |
44 |
private static final String DESCRIPTION = "ENT_DESCRIPTION"; |
45 |
private static final String FIELD_FOR_LABEL = "ENT_FIELDFORLABEL"; |
46 |
private static final String FEATURETYPE = "ENT_FEATURETYPE"; |
47 |
private static final String COD_LOCAL_REVISION = "COD_LOCAL_REVISION"; |
48 |
private static final String COD_REPOSITORY_REVISION = "COD_REPO_REVISION"; |
49 |
private static final String COD_USER = "COD_USER"; |
50 |
private static final String COD_TOPOLOGYPLAN = "COD_TOPOLOGYPLAN"; |
51 |
private static final String CATEGORY = "ENT_CATEGORY"; |
52 |
private static final String LABEL = "ENT_LABEL"; |
53 |
private static final String AUTHORIZATIONS = "ENT_AUTORIZATIONS"; |
54 |
private static final String TOPOLOGYPLAN_MODE = "ENT_TOPOLOGYPLAN_MODE"; |
55 |
|
56 |
public static class EntityRow extends AbstractRow implements VCSGisWorkspaceEntity { |
57 |
|
58 |
private FeatureType featureType;
|
59 |
|
60 |
public EntityRow(VCSGisWorkspace workspace) {
|
61 |
super(workspace, TABLE_NAME, COD_ENTITY);
|
62 |
} |
63 |
|
64 |
public EntityRow(VCSGisWorkspace workspace, Feature feature) {
|
65 |
super(workspace, TABLE_NAME, COD_ENTITY,feature);
|
66 |
} |
67 |
|
68 |
@Override
|
69 |
public String getEntityName() { |
70 |
return this.getString(ENTITY_NAME); |
71 |
} |
72 |
|
73 |
@Override
|
74 |
public String getDataTableName() { |
75 |
return this.getString(DATATABLE_NAME); |
76 |
} |
77 |
|
78 |
@Override
|
79 |
public String getFeatureIdFieldName() { |
80 |
return this.getString(FEATUREID_FIELD_NAME); |
81 |
} |
82 |
|
83 |
|
84 |
@Override
|
85 |
public String getGeometryFieldName() { |
86 |
return this.getString(GEOMETRY_FIELD_NAME); |
87 |
} |
88 |
|
89 |
@Override
|
90 |
public String getDescription() { |
91 |
return this.getString(DESCRIPTION); |
92 |
} |
93 |
|
94 |
@Override
|
95 |
public String getFieldForLabel() { |
96 |
return this.getString(FIELD_FOR_LABEL); |
97 |
} |
98 |
|
99 |
@Override
|
100 |
public String getUserCode() { |
101 |
return this.getString(COD_USER); |
102 |
} |
103 |
|
104 |
@Override
|
105 |
public String getTopologyPlanCode() { |
106 |
return this.getString(COD_TOPOLOGYPLAN); |
107 |
} |
108 |
|
109 |
@Override
|
110 |
public String getEntityCode() { |
111 |
return this.getCode(); |
112 |
} |
113 |
|
114 |
@Override
|
115 |
public String getLocalRevisionCode() { |
116 |
return this.getString(COD_LOCAL_REVISION); |
117 |
} |
118 |
|
119 |
@Override
|
120 |
public String getRepositoryRevisionCode() { |
121 |
return this.getString(COD_REPOSITORY_REVISION); |
122 |
} |
123 |
|
124 |
@Override
|
125 |
public String getCategory() { |
126 |
return this.getString(CATEGORY); |
127 |
} |
128 |
|
129 |
@Override
|
130 |
public String getAuthorizations() { |
131 |
return this.getString(AUTHORIZATIONS); |
132 |
} |
133 |
|
134 |
@Override
|
135 |
public int getTopologyPlanMode() { |
136 |
return this.getInt(TOPOLOGYPLAN_MODE); |
137 |
} |
138 |
|
139 |
@Override
|
140 |
public String getLabel() { |
141 |
return this.getString(LABEL); |
142 |
} |
143 |
|
144 |
@Override
|
145 |
public String getFeatureTypeAsJson() { |
146 |
return this.getString(FEATURETYPE); |
147 |
} |
148 |
|
149 |
@Override
|
150 |
public FeatureType getFeatureType() {
|
151 |
if (this.featureType == null) { |
152 |
JsonObject json = Json.createObject(this.getFeatureTypeAsJson());
|
153 |
this.featureType = DALLocator.getDataManager().createFeatureType(json);
|
154 |
} |
155 |
return this.featureType; |
156 |
} |
157 |
|
158 |
|
159 |
@Override
|
160 |
public EntityRow setEntityName(String entityName) { |
161 |
this.set(ENTITY_NAME, entityName);
|
162 |
if( StringUtils.isBlank(this.getLabel())) { |
163 |
this.setLabel(entityName);
|
164 |
} |
165 |
return this; |
166 |
} |
167 |
|
168 |
@Override
|
169 |
public EntityRow setDataTableName(String dataTableName) { |
170 |
this.set(DATATABLE_NAME, dataTableName);
|
171 |
return this; |
172 |
} |
173 |
|
174 |
@Override
|
175 |
public EntityRow setFeatureIdFieldName(String name) { |
176 |
this.set(FEATUREID_FIELD_NAME, name);
|
177 |
return this; |
178 |
} |
179 |
|
180 |
@Override
|
181 |
public EntityRow setGeometryFieldName(String name) { |
182 |
this.set(GEOMETRY_FIELD_NAME, name);
|
183 |
return this; |
184 |
} |
185 |
|
186 |
@Override
|
187 |
public EntityRow setDescription(String description) { |
188 |
this.set(DESCRIPTION, description);
|
189 |
return this; |
190 |
} |
191 |
|
192 |
@Override
|
193 |
public EntityRow setFieldForLabel(String fieldForLabel) { |
194 |
this.set(FIELD_FOR_LABEL, fieldForLabel);
|
195 |
return this; |
196 |
} |
197 |
|
198 |
@Override
|
199 |
public EntityRow setFeatureTypeAsJson(String json) { |
200 |
this.set(FEATURETYPE, json);
|
201 |
this.featureType = null; |
202 |
return this; |
203 |
} |
204 |
|
205 |
public EntityRow setFeatureType(FeatureType featureType) {
|
206 |
this.set(FEATURETYPE, featureType.toJson().toString());
|
207 |
return this; |
208 |
} |
209 |
|
210 |
@Override
|
211 |
public EntityRow setLocalRevisionCode(String code) { |
212 |
this.set(COD_LOCAL_REVISION, code);
|
213 |
return this; |
214 |
} |
215 |
|
216 |
@Override
|
217 |
public EntityRow setRepositoryRevisionCode(String code) { |
218 |
this.set(COD_REPOSITORY_REVISION, code);
|
219 |
return this; |
220 |
} |
221 |
|
222 |
|
223 |
@Override
|
224 |
public VCSGisEntityEditable setUserCode(String userCode) { |
225 |
this.set(COD_USER, userCode);
|
226 |
return this; |
227 |
} |
228 |
|
229 |
@Override
|
230 |
public VCSGisEntityEditable setTopologyPlanCode(String topologyplanCode) { |
231 |
this.set(COD_TOPOLOGYPLAN, topologyplanCode);
|
232 |
return this; |
233 |
} |
234 |
|
235 |
@Override
|
236 |
public VCSGisEntityEditable setCategory(String category) { |
237 |
this.set(CATEGORY, category);
|
238 |
return this; |
239 |
} |
240 |
|
241 |
@Override
|
242 |
public VCSGisEntityEditable setAuthorizations(String authorizations) { |
243 |
this.set(AUTHORIZATIONS, authorizations);
|
244 |
return this; |
245 |
} |
246 |
|
247 |
@Override
|
248 |
public VCSGisEntityEditable setTopologyPlanMode(int topologyPlanMode) { |
249 |
this.set(TOPOLOGYPLAN_MODE, topologyPlanMode);
|
250 |
return this; |
251 |
} |
252 |
|
253 |
@Override
|
254 |
public VCSGisEntityEditable setLabel(String label) { |
255 |
if(label != null){ |
256 |
this.set(LABEL, label);
|
257 |
} |
258 |
return this; |
259 |
} |
260 |
|
261 |
@Override
|
262 |
public long getCountLocalChanges() { |
263 |
WorkspaceChangesTable changesTable = new WorkspaceChangesTable();
|
264 |
return changesTable.getCountLocalChangesOfEntity(workspace, this.getCode()); |
265 |
} |
266 |
|
267 |
private void updateState() { |
268 |
long countChanges;
|
269 |
int state = this.getInt(ENTITY_STATE, -1); |
270 |
if( state == -1 ) { |
271 |
state = STATE_LOCAL_UNMODIFIED; |
272 |
this.setState(state);
|
273 |
} |
274 |
switch (state) {
|
275 |
case STATE_LOCAL_UNMODIFIED:
|
276 |
countChanges = this.getCountLocalChanges();
|
277 |
if (countChanges > 0) { |
278 |
state = STATE_LOCAL_MODIFIED; |
279 |
if(this.isOutdated()){ |
280 |
state = STATE_LOCAL_OUTDATED_AND_MODIFIED; |
281 |
} |
282 |
this.setState(state);
|
283 |
} else if(this.isOutdated()){ |
284 |
state = STATE_LOCAL_OUTDATED; |
285 |
this.setState(state);
|
286 |
} |
287 |
break;
|
288 |
case STATE_LOCAL_OUTDATED:
|
289 |
if(!this.isOutdated()){ |
290 |
countChanges = this.getCountLocalChanges();
|
291 |
if (countChanges > 0) { |
292 |
state = STATE_LOCAL_MODIFIED; |
293 |
} else {
|
294 |
state = STATE_LOCAL_UNMODIFIED; |
295 |
} |
296 |
} else {
|
297 |
countChanges = this.getCountLocalChanges();
|
298 |
if (countChanges > 0) { |
299 |
state = STATE_LOCAL_OUTDATED_AND_MODIFIED; |
300 |
this.setState(state);
|
301 |
} |
302 |
} |
303 |
this.setState(state);
|
304 |
break;
|
305 |
case STATE_LOCAL_MODIFIED:
|
306 |
countChanges = this.getCountLocalChanges();
|
307 |
if (countChanges == 0) { |
308 |
state = STATE_LOCAL_UNMODIFIED; |
309 |
if(this.isOutdated()){ |
310 |
state = STATE_LOCAL_OUTDATED; |
311 |
} |
312 |
} else {
|
313 |
if(this.isOutdated()){ |
314 |
state = STATE_LOCAL_OUTDATED_AND_MODIFIED; |
315 |
} |
316 |
} |
317 |
|
318 |
this.setState(state);
|
319 |
break;
|
320 |
case STATE_LOCAL_OUTDATED_AND_MODIFIED:
|
321 |
if(!this.isOutdated()){ |
322 |
countChanges = this.getCountLocalChanges();
|
323 |
if (countChanges > 0) { |
324 |
state = STATE_LOCAL_MODIFIED; |
325 |
this.setState(state);
|
326 |
} else {
|
327 |
state = STATE_LOCAL_UNMODIFIED; |
328 |
} |
329 |
} else {
|
330 |
countChanges = this.getCountLocalChanges();
|
331 |
if (countChanges > 0) { |
332 |
state = STATE_LOCAL_OUTDATED_AND_MODIFIED; |
333 |
this.setState(state);
|
334 |
} else {
|
335 |
state = STATE_LOCAL_OUTDATED; |
336 |
} |
337 |
} |
338 |
this.setState(state);
|
339 |
break;
|
340 |
|
341 |
case STATE_LOCAL_NEW:
|
342 |
countChanges = this.getCountLocalChanges();
|
343 |
if (countChanges > 0) { |
344 |
state = STATE_LOCAL_MODIFIED; |
345 |
if(this.isOutdated()){ |
346 |
state = STATE_LOCAL_OUTDATED_AND_MODIFIED; |
347 |
} |
348 |
this.setState(state);
|
349 |
} else {
|
350 |
state = STATE_LOCAL_UNMODIFIED; |
351 |
} |
352 |
this.setState(state);
|
353 |
break;
|
354 |
case STATE_CONFLICT:
|
355 |
break;
|
356 |
default:
|
357 |
if(this.isOutdated()){ |
358 |
state = STATE_LOCAL_OUTDATED; |
359 |
this.setState(state);
|
360 |
} |
361 |
|
362 |
|
363 |
} |
364 |
} |
365 |
|
366 |
@Override
|
367 |
public int getState() { |
368 |
this.updateState();
|
369 |
int state = this.getInt(ENTITY_STATE); |
370 |
return state;
|
371 |
} |
372 |
|
373 |
@Override
|
374 |
public boolean isOutdated() { |
375 |
final String repositoryRevisionCode = this.getRepositoryRevisionCode(); |
376 |
if(StringUtils.isBlank(repositoryRevisionCode)){
|
377 |
return false; |
378 |
} |
379 |
return !StringUtils.equalsIgnoreCase(repositoryRevisionCode,this.getLocalRevisionCode()); |
380 |
} |
381 |
|
382 |
public String getStateLabel() { |
383 |
return VCSGisUtils.getStateLabel(this.getState()); |
384 |
} |
385 |
|
386 |
public void setState(int state) { |
387 |
this.set(ENTITY_STATE, state);
|
388 |
} |
389 |
|
390 |
@Override
|
391 |
public void update() { |
392 |
this.updateState();
|
393 |
super.update();
|
394 |
} |
395 |
|
396 |
@Override
|
397 |
public VCSGisWorkspaceEntity getValue() {
|
398 |
return this; |
399 |
} |
400 |
|
401 |
@Override
|
402 |
public String toString() { |
403 |
if( StringUtils.isBlank(this.getLabel()) ) { |
404 |
return this.getEntityName(); |
405 |
} |
406 |
return this.getLabel(); |
407 |
} |
408 |
|
409 |
@Override
|
410 |
public EntityRow setEntityCode(String code) { |
411 |
this.setCode(code);
|
412 |
return this; |
413 |
} |
414 |
|
415 |
@Override
|
416 |
public void copyfrom(VCSGisEntity entity) { |
417 |
VCSGisUtils.copy(entity, this, null); |
418 |
} |
419 |
|
420 |
@Override
|
421 |
public void copyto(VCSGisEntityEditable entity) { |
422 |
VCSGisUtils.copy(this, entity, null); |
423 |
} |
424 |
|
425 |
@Override
|
426 |
public JsonObject toJson() {
|
427 |
return this.toJsonBuilder().build(); |
428 |
} |
429 |
|
430 |
@Override
|
431 |
public JsonObjectBuilder toJsonBuilder() {
|
432 |
return VCSGisUtils.toJsonBuilder(this, null); |
433 |
} |
434 |
|
435 |
@Override
|
436 |
public void fromJson(JsonObject json) { |
437 |
VCSGisUtils.fromJson(this, json);
|
438 |
} |
439 |
|
440 |
@Override
|
441 |
public boolean isAuthorized(String userName, String operation) { |
442 |
return VCSGisUtils.isAuthoriced(this, operation, userName); |
443 |
} |
444 |
} |
445 |
|
446 |
|
447 |
public EntitiesTable() {
|
448 |
super(TABLE_NAME, featureType());
|
449 |
} |
450 |
|
451 |
public EntityRow getByEntityName(VCSGisWorkspace workspace, String entityName) { |
452 |
FeatureStore store = null;
|
453 |
try {
|
454 |
store = workspace.getFeatureStore(TABLE_NAME); |
455 |
Feature f = store.findFirst("\"" + ENTITY_NAME + "\"='" + entityName + "'"); |
456 |
if (f == null) { |
457 |
return null; |
458 |
} |
459 |
EntityRow row = new EntityRow(workspace, f);
|
460 |
return row;
|
461 |
} catch (Exception ex) { |
462 |
throw new RuntimeException("Can't retrieve entity '" + entityName + "'.", ex); |
463 |
} finally {
|
464 |
DisposeUtils.disposeQuietly(store); |
465 |
} |
466 |
} |
467 |
|
468 |
public EntityRow getByEntityCode(VCSGisWorkspace workspace, String entityCode) { |
469 |
FeatureStore store = null;
|
470 |
try {
|
471 |
store = workspace.getFeatureStore(TABLE_NAME); |
472 |
Feature f = store.findFirst("\"" + COD_ENTITY + "\"='" + entityCode + "'"); |
473 |
if (f == null) { |
474 |
return null; |
475 |
} |
476 |
EntityRow row = new EntityRow(workspace, f);
|
477 |
return row;
|
478 |
} catch (Exception ex) { |
479 |
throw new RuntimeException("Can't retrieve entity '" + entityCode + "'.", ex); |
480 |
} finally {
|
481 |
DisposeUtils.disposeQuietly(store); |
482 |
} |
483 |
} |
484 |
|
485 |
public DisposableFeatureSetIterable getAll(VCSGisWorkspace workspace) {
|
486 |
FeatureStore store = null;
|
487 |
try {
|
488 |
store = workspace.getFeatureStore(TABLE_NAME); |
489 |
DisposableFeatureSetIterable items = store.getFeatureSet().iterable(); |
490 |
return items;
|
491 |
} catch (Exception ex) { |
492 |
throw new RuntimeException("Can't retrieve all entities.", ex); |
493 |
} finally {
|
494 |
if (store != null) { |
495 |
DisposeUtils.dispose(store); |
496 |
} |
497 |
} |
498 |
} |
499 |
|
500 |
public static final FeatureType featureType() { |
501 |
DataManager dataManager = DALLocator.getDataManager(); |
502 |
EditableFeatureType ft = dataManager.createFeatureType(); |
503 |
ft.setLabel("VCSGIS Entities");
|
504 |
ft.getTags().set("ID", TABLE_NAME);
|
505 |
ft.add(COD_ENTITY, DataTypes.STRING) |
506 |
.setSize(40)
|
507 |
.setIsPrimaryKey(true)
|
508 |
.setLabel("Code");
|
509 |
ft.add(COD_USER, DataTypes.STRING) |
510 |
.setSize(40)
|
511 |
.setIsPrimaryKey(false)
|
512 |
.setLabel("Code");
|
513 |
ft.add(COD_TOPOLOGYPLAN, DataTypes.STRING) |
514 |
.setSize(40)
|
515 |
.setIsPrimaryKey(false)
|
516 |
.setLabel("Code");
|
517 |
ft.add(ENTITY_NAME, DataTypes.STRING) |
518 |
.setIsIndexed(true)
|
519 |
.setAllowIndexDuplicateds(false)
|
520 |
.setSize(200)
|
521 |
.setLabel("Name");
|
522 |
ft.add(DATATABLE_NAME, DataTypes.STRING) |
523 |
.setSize(200)
|
524 |
.setLabel("Table name")
|
525 |
.setDescription("Name of the table in which the data is stored");
|
526 |
ft.add(FEATUREID_FIELD_NAME, DataTypes.STRING) |
527 |
.setSize(200)
|
528 |
.setLabel("Pk field name")
|
529 |
.setDescription("Name of the primary key of the table.");
|
530 |
ft.add(COD_LOCAL_REVISION, DataTypes.STRING) |
531 |
.setSize(40)
|
532 |
.setLabel("Revision")
|
533 |
.setDescription("Local revision.");
|
534 |
ft.add(COD_REPOSITORY_REVISION, DataTypes.STRING) |
535 |
.setSize(40)
|
536 |
.setLabel("Revision")
|
537 |
.setDescription("Last revision in repository.");
|
538 |
ft.add(ENTITY_STATE, DataTypes.INTEGER) |
539 |
.setLabel("State")
|
540 |
.setDescription("State of entity.");
|
541 |
ft.add(GEOMETRY_FIELD_NAME, DataTypes.STRING) |
542 |
.setSize(200)
|
543 |
.setLabel("Geometry field name")
|
544 |
.setDescription("Name of the geometry attribute.");
|
545 |
ft.add(FIELD_FOR_LABEL, DataTypes.STRING) |
546 |
.setSize(200)
|
547 |
.setLabel("Field for label");
|
548 |
ft.add(CATEGORY, DataTypes.STRING) |
549 |
.setSize(200)
|
550 |
.setLabel("Category");
|
551 |
ft.add(LABEL, DataTypes.STRING) |
552 |
.setSize(200)
|
553 |
.setLabel("Label");
|
554 |
ft.add(AUTHORIZATIONS, DataTypes.STRING) |
555 |
.setSize(10240)
|
556 |
.setLabel("Authorizations");
|
557 |
ft.add(DESCRIPTION, DataTypes.STRING) |
558 |
.setSize(10240)
|
559 |
.setLabel("Description");
|
560 |
ft.add(FEATURETYPE, DataTypes.STRING) |
561 |
.setSize(10240)
|
562 |
.setLabel("Feature type");
|
563 |
ft.add(TOPOLOGYPLAN_MODE, DataTypes.INTEGER) |
564 |
.setLabel("Topology plan mode");
|
565 |
|
566 |
return ft.getNotEditableCopy();
|
567 |
} |
568 |
|
569 |
} |