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 / localdb / tables / EntitiesRepoTable.java @ 3633

History | View | Annotate | Download (18.7 KB)

1
package org.gvsig.vcsgis.lib.repository.localdb.tables;
2

    
3
import java.util.HashMap;
4
import java.util.List;
5
import java.util.Map;
6
import javax.json.JsonObject;
7
import org.apache.commons.lang3.StringUtils;
8
import org.gvsig.fmap.dal.DALLocator;
9
import org.gvsig.fmap.dal.DataManager;
10
import org.gvsig.fmap.dal.feature.EditableFeatureType;
11
import org.gvsig.fmap.dal.feature.Feature;
12
import org.gvsig.fmap.dal.feature.FeatureSet.DisposableFeatureSetIterable;
13
import org.gvsig.fmap.dal.feature.FeatureStore;
14
import org.gvsig.fmap.dal.feature.FeatureType;
15
import org.gvsig.json.Json;
16
import org.gvsig.json.JsonObjectBuilder;
17
import org.gvsig.tools.dataTypes.DataTypes;
18
import org.gvsig.tools.dispose.DisposeUtils;
19
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
20
import org.gvsig.tools.dynobject.DynObjectValueItem;
21
import org.gvsig.vcsgis.lib.VCSGisEntity;
22
import org.gvsig.vcsgis.lib.VCSGisEntityEditable;
23
import static org.gvsig.vcsgis.lib.VCSGisManager.TOPOLOGYPLAN_MODE_MANDATORY;
24
import static org.gvsig.vcsgis.lib.VCSGisManager.TOPOLOGYPLAN_MODE_RECOMMENDED;
25
import static org.gvsig.vcsgis.lib.VCSGisManager.VCSGISCODELEN;
26
import org.gvsig.vcsgis.lib.VCSGisUser;
27
import org.gvsig.vcsgis.lib.VCSGisUtils;
28
import org.gvsig.vcsgis.lib.repository.localdb.VCSGisRepositoryLocaldb;
29

    
30
/**
31
 *
32
 * @author gvSIG Team
33
 */
34
@SuppressWarnings("UseSpecificCatch")
35
public class EntitiesRepoTable extends AbstractRepoTable {
36
    
37
    public static final String TABLE_NAME = "VCSGISREPO_ENTITIES";
38

    
39
    public static final String COD_ENTITY = "COD_ENTITY";
40
    private static final String COD_USER = "COD_USER";
41
    private static final String COD_TOPOLOGYPLAN = "COD_TOPOLOGYPLAN";
42
    private static final String COD_REVISION = "COD_REVISION";
43
    public static final String ENTITY_NAME = "ENT_NAME";
44
    private static final String DATATABLE_NAME = "ENT_DATATABLE";
45
    private static final String FEATUREID_FIELD_NAME = "ENT_FEATURECODE";
46
    private static final String GEOMETRY_FIELD_NAME = "ENT_GEOMNAME";
47
    private static final String DESCRIPTION = "ENT_DESCRIPTION";
48
    private static final String FEATURETYPE = "ENT_FEATURETYPE";
49
    private static final String FIELD_FOR_LABEL = "ENT_FIELDFORLABEL";
50
    private static final String CATEGORY = "ENT_CATEGORY";
51
    private static final String LABEL = "ENT_LABEL";
52
    private static final String AUTHORIZATIONS = "ENT_AUTORIZATIONS";
53
    private static final String TOPOLOGYPLAN_MODE = "ENT_TOPOLOGYPLAN_MODE";
54
    public static final String RESOURCES = "ENT_RESOURCES";
55
    public static final String DATA_MODELS = "ENT_DATA_MODELS";
56
    
57
    public static class EntityRepoRow extends AbstractRow implements VCSGisEntityEditable {
58

    
59
        FeatureType featureType;
60
        
61
        public EntityRepoRow(VCSGisRepositoryLocaldb repository) {
62
            super(repository, TABLE_NAME, COD_ENTITY, null);
63
        }
64
        
65
        public EntityRepoRow(VCSGisRepositoryLocaldb repository, Feature feature) {
66
            super(repository, TABLE_NAME, COD_ENTITY, feature);
67
        }
68

    
69
        @Override
70
        public String getEntityCode() {
71
            return super.getCode();
72
        }
73

    
74
        @Override
75
        public VCSGisEntityEditable setEntityCode(String code) {
76
            super.setCode(code);
77
            return this;
78
        }
79
        
80
        @Override
81
        public String getEntityName() {
82
            return this.feature.getString(ENTITY_NAME);
83
        }
84
        
85
        @Override
86
        public String getRepositoryRevisionCode() {
87
            return this.feature.getString(COD_REVISION);
88
        }
89
        
90
        @Override
91
        public String getTopologyPlanCode() {
92
            return this.feature.getString(COD_TOPOLOGYPLAN);
93
        }
94

    
95
        @Override
96
        public String getUserCode() {
97
            return this.feature.getString(COD_USER);
98
        }
99
        
100
        @Override
101
        public String getDataTableName() {
102
            return this.feature.getString(DATATABLE_NAME);
103
        }
104

    
105
        @Override
106
        public String getFeatureIdFieldName() {
107
            return this.feature.getString(FEATUREID_FIELD_NAME);
108
        }
109

    
110
        @Override
111
        public String getGeometryFieldName() {
112
            return this.feature.getString(GEOMETRY_FIELD_NAME);
113
        }
114

    
115
        @Override
116
        public String getDescription() {
117
            return this.feature.getString(DESCRIPTION);
118
        }
119

    
120
        @Override
121
        public String getFieldForLabel() {
122
            return this.feature.getString(FIELD_FOR_LABEL);
123
        }
124

    
125
        @Override
126
        public String getCategory() {
127
            return this.feature.getString(CATEGORY);
128
        }
129

    
130
        @Override
131
        public List<String> getCategoriesAsList() {
132
            String s = getCategory();
133
            return VCSGisUtils.getAsList(s);
134
        }
135

    
136
        @Override
137
        public String getAuthorizations() {
138
            return this.feature.getString(AUTHORIZATIONS);
139
        }
140

    
141
        @Override
142
        public int getTopologyPlanMode() {
143
            return this.feature.getIntOrDefault(TOPOLOGYPLAN_MODE, TOPOLOGYPLAN_MODE_RECOMMENDED);
144
        }
145
        
146
        @Override
147
        public String getLabel() {
148
            return this.feature.getString(LABEL);
149
        }
150

    
151
        @Override
152
        public String getFeatureTypeAsJson() {
153
            return this.feature.getString(FEATURETYPE);
154
        }
155

    
156
        @Override
157
        public FeatureType getFeatureType() {
158
            if( this.featureType==null ) {
159
                JsonObject json = Json.createObject(this.getFeatureTypeAsJson());
160
                this.featureType = DALLocator.getDataManager().createFeatureType(json);
161
            }
162
            return this.featureType;
163
        }
164

    
165
        @Override
166
        public VCSGisEntityEditable setEntityName(String entityName) {
167
            this.feature.set(ENTITY_NAME, entityName);
168
            if( StringUtils.isBlank(this.getLabel())) {
169
                this.setLabel(entityName);
170
            }
171
            return this;
172
        }
173
        
174
        @Override
175
        public VCSGisEntityEditable setRepositoryRevisionCode(String code) {
176
            this.feature.set(COD_REVISION, code);
177
            return this;
178
        }
179
        
180
        @Override
181
        public VCSGisEntityEditable setDataTableName(String dataTableName) {
182
            this.feature.set(DATATABLE_NAME, dataTableName);
183
            return this;
184
        }
185

    
186
        @Override
187
        public VCSGisEntityEditable setFeatureIdFieldName(String name) {
188
            this.feature.set(FEATUREID_FIELD_NAME, name);
189
            return this;
190
        }
191

    
192
        @Override
193
        public VCSGisEntityEditable setGeometryFieldName(String name) {
194
            this.feature.set(GEOMETRY_FIELD_NAME, name);
195
            return this;
196
        }
197

    
198
        @Override
199
        public VCSGisEntityEditable setDescription(String description) {
200
            this.feature.set(DESCRIPTION, description);
201
            return this;
202
        }
203
        
204
        @Override
205
        public VCSGisEntityEditable setFieldForLabel(String fieldForLabel) {
206
            this.feature.set(FIELD_FOR_LABEL, fieldForLabel);
207
            return this;
208
        }
209

    
210
        @Override
211
        public VCSGisEntityEditable setFeatureTypeAsJson(String json) {
212
            this.feature.set(FEATURETYPE, json);
213
            this.featureType = null;
214
            return this;
215
        }
216

    
217
        public void setFeatureType(FeatureType featureType) {
218
            this.feature.set(FEATURETYPE, featureType.toJson().toString());
219
            this.featureType = featureType;
220
        }
221

    
222
        @Override
223
        public VCSGisEntityEditable setTopologyPlanCode(String code) {
224
            this.feature.set(COD_TOPOLOGYPLAN, code);
225
            return this;
226
        }
227
        
228
        @Override
229
        public VCSGisEntityEditable setUserCode(String code) {
230
            this.feature.set(COD_USER, code);
231
            return this;
232
        }
233

    
234
        @Override
235
        public VCSGisEntityEditable setCategory(String category) {
236
            this.feature.set(CATEGORY, category);
237
            return this;
238
        }
239

    
240
        @Override
241
        public VCSGisEntityEditable setAuthorizations(String authorizations) {
242
            this.feature.set(AUTHORIZATIONS, authorizations);
243
            return this;
244
        }
245

    
246
        @Override
247
        public VCSGisEntityEditable setTopologyPlanMode(int topologyPlanMode) {
248
            this.feature.set(TOPOLOGYPLAN_MODE, topologyPlanMode);
249
            return this;
250
        }
251

    
252
        @Override
253
        public VCSGisEntityEditable setLabel(String label) {
254
            this.feature.set(LABEL, label);
255
            return this;
256
        }
257

    
258
        @Override
259
        public void copyfrom(VCSGisEntity entity) {
260
            VCSGisUtils.copy(entity, this, null);
261
        }
262

    
263
        @Override
264
        public void copyto(VCSGisEntityEditable entity) {
265
            VCSGisUtils.copy(this, entity, null);
266
        }
267

    
268
        @Override
269
        public JsonObject toJson() {
270
            return VCSGisUtils.toJsonBuilder(this, null).build();
271
        }
272

    
273
        @Override
274
        public JsonObjectBuilder toJsonBuilder() {
275
            return VCSGisUtils.toJsonBuilder(this, null);
276
        }
277

    
278
        @Override
279
        public void fromJson(JsonObject json) {
280
            VCSGisUtils.fromJson(this, json);
281
        }
282

    
283
        @Override
284
        public EntityRepoRow getValue() {
285
            return this;
286
        }
287

    
288
        @Override
289
        public String toString() {
290
            if( StringUtils.isBlank(this.getLabel()) ) {
291
                return this.getEntityName();
292
            }
293
            return this.getLabel();
294
        }
295

    
296
        @Override
297
        public boolean isAuthorized(VCSGisUser user, String operation) {
298
            return VCSGisUtils.isAuthorized(this, operation, user);
299
        }
300

    
301
        @Override
302
        public VCSGisEntityEditable setResources(String resources) {
303
            this.feature.set(RESOURCES, resources);
304
            return this;
305
        }
306

    
307
        @Override
308
        public VCSGisEntityEditable setDataModels(String dataModels) {
309
            this.feature.set(DATA_MODELS, dataModels);
310
            return this;
311
        }
312

    
313
        @Override
314
        public String getResources() {
315
            return this.getString(RESOURCES);
316
        }
317

    
318
        @Override
319
        public String getDataModels() {
320
            return this.getString(DATA_MODELS);
321
        }
322

    
323
        @Override
324
        public List<String> getDataModelsAsList() {
325
            String s = getDataModels();
326
            return VCSGisUtils.getAsList(s, false);
327
        }
328

    
329
    }
330
    
331
    public EntitiesRepoTable() {
332
        super(TABLE_NAME, featureType());
333
    }
334

    
335
    public EntityRepoRow getByEntityCode(VCSGisRepositoryLocaldb repository, String entityCode) {
336
        FeatureStore store = null;
337
        try {
338
            store = repository.getFeatureStore(TABLE_NAME);
339
            Feature f = store.findFirst("\""+COD_ENTITY+"\"='"+entityCode+"'");
340
            if( f==null ) {
341
                return null;
342
            }
343
            EntityRepoRow row = new EntityRepoRow(repository, f);
344
            return row;
345
        } catch (Exception ex) {
346
            throw new RuntimeException("Can't retrieve entity '"+entityCode+"'.", ex);
347
        } finally {
348
            DisposeUtils.disposeQuietly(store);
349
        }
350
    }
351
    
352
    public EntityRepoRow getByEntityName(VCSGisRepositoryLocaldb repository, String entityName) {
353
        FeatureStore store = null;
354
        try {
355
            store = repository.getFeatureStore(TABLE_NAME);
356
            Feature f = store.findFirst("\""+ENTITY_NAME+"\"='"+entityName+"'");
357
            if( f==null ) {
358
                return null;
359
            }
360
            EntityRepoRow row = new EntityRepoRow(repository, f);
361
            return row;
362
        } catch (Exception ex) {
363
            throw new RuntimeException("Can't retrieve entity '"+entityName+"'.", ex);
364
        } finally {
365
            DisposeUtils.disposeQuietly(store);
366
        }
367
    }
368
    
369
    public Map<String, EntityRepoRow> getAllAsMap(VCSGisRepositoryLocaldb repository) {
370
        Map<String, EntityRepoRow> entitiesMap = new HashMap<>();
371

    
372
        DisposableFeatureSetIterable entities = this.getAll(repository);
373
        for (Feature fentity : entities) {
374
            EntityRepoRow entity = new EntityRepoRow(repository, fentity);
375
            entitiesMap.put(entity.getCode(), entity);
376
        }
377
        entities = null;
378
        DisposeUtils.disposeQuietly(entities);        
379
        return entitiesMap;
380
    }
381
    
382
    public DisposableFeatureSetIterable getAll(VCSGisRepositoryLocaldb repository) {
383
        FeatureStore store = null;
384
        try {
385
            store = repository.getFeatureStore(TABLE_NAME);
386
            DisposableFeatureSetIterable changes = store.getFeatureSet().iterable();
387
            return changes;
388
        } catch (Exception ex) {
389
            throw new RuntimeException("Can't retrieve all entities.", ex);
390
        } finally {
391
            if( store!=null ) {
392
                DisposeUtils.dispose(store);
393
            }
394
        }
395
    }
396
    
397
    public static final FeatureType featureType() {
398
        DataManager dataManager = DALLocator.getDataManager();
399
        EditableFeatureType ft = dataManager.createFeatureType();
400
        ft.setLabel("VCSGIS Entities");
401
        ft.getTags().set("ID", TABLE_NAME);
402
        ft.getTags().set(DynFormSPIManager.TAG_DYNFORM_HEIGHT, 475);
403
        ft.getTags().set(DynFormSPIManager.TAG_DYNFORM_WIDTH, 570);
404
        ft.getTags().set(DynFormSPIManager.TAG_DYNFORM_LAYOUTMODE, DynFormSPIManager.TAG_DYNFORM_LAYOUTMODE_VALUE_TABS);
405
        ft.add(COD_ENTITY, DataTypes.STRING)
406
                .setSize(VCSGISCODELEN)
407
                .setIsPrimaryKey(true)
408
                .setLabel("Code")
409
                .setOrder(10);
410
        ft.add(COD_REVISION, DataTypes.STRING)
411
                .setForeingkey(
412
                        true, 
413
                        false, 
414
                        RevisionsRepoTable.TABLE_NAME, 
415
                        RevisionsRepoTable.COD_REVISION, 
416
                        "FORMAT('r%d %s', REV_NUMBER, REV_OPERATIONDATE)"
417
                )
418
                .setIsIndexed(true)
419
                .setAllowIndexDuplicateds(true)
420
                .setSize(VCSGISCODELEN)
421
                .setLabel("Head revision of this entity")
422
                .setOrder(40);
423
        ft.add(COD_USER, DataTypes.STRING)
424
                .setForeingkey(
425
                        true, 
426
                        false, 
427
                        UsersRepoTable.TABLE_NAME, 
428
                        UsersRepoTable.COD_USER, 
429
                        UsersRepoTable.USER_ID
430
                )
431
                .setIsIndexed(true)
432
                .setAllowIndexDuplicateds(true)
433
                .setSize(VCSGISCODELEN)
434
                .setLabel("Cod. user")
435
                .setOrder(30);
436
        ft.add(COD_TOPOLOGYPLAN, DataTypes.STRING)
437
                .setForeingkey(
438
                        true, 
439
                        false, 
440
                        TopologyplanRepoTable.TABLE_NAME, 
441
                        TopologyplanRepoTable.COD_TOPOLOGYPLAN, 
442
                        TopologyplanRepoTable.TOPPLAN_NAME
443
                )
444
                .setSize(VCSGISCODELEN)
445
                .setIsIndexed(true)
446
                .setAllowIndexDuplicateds(true)
447
                .setLabel("Topology plan")
448
                .setOrder(90);                  
449
        ft.add(ENTITY_NAME, DataTypes.STRING)
450
                .setIsIndexed(true)
451
                .setAllowIndexDuplicateds(false)
452
                .setSize(200)
453
                .setLabel("Name")
454
                .setOrder(20);
455
        ft.add(DATATABLE_NAME, DataTypes.STRING)
456
                .setSize(200)
457
                .setLabel("Name of the table in which the data is stored")
458
                .setOrder(50);
459
        ft.add(FEATUREID_FIELD_NAME, DataTypes.STRING)
460
                .setSize(200)
461
                .setLabel("Name of the primary key attribute.")
462
                .setOrder(60);
463
        ft.add(GEOMETRY_FIELD_NAME, DataTypes.STRING)
464
                .setSize(200)
465
                .setLabel("Name of the geometry attribute.")
466
                .setOrder(70);
467
        ft.add(FIELD_FOR_LABEL, DataTypes.STRING)
468
                .setSize(200)
469
                .setLabel("Field for label")
470
                .setOrder(10);
471
        ft.add(CATEGORY, DataTypes.STRING)
472
                .setSize(200)
473
                .setLabel("Category")
474
                .setOrder(80);
475
        ft.add(LABEL, DataTypes.STRING)
476
                .setSize(200)
477
                .setLabel("Label")
478
                .setOrder(25);
479
        ft.add(AUTHORIZATIONS, DataTypes.STRING)               
480
                .setTag(DynFormSPIManager.TAG_DYNFORM_RESIZEWEIGHT, 60) 
481
                .setTag(DynFormSPIManager.TAG_DYNFORM_LABEL_EMPTY, true) 
482
                .setDataProfileName("Text")
483
                .setSize(10240)
484
                .setLabel("Authorizations")
485
                .setGroup("Authorizations")
486
                .setOrder(10)
487
                .setDefaultFieldValue("{\n" +
488
"  \"add\": [ \"*\" ],\n" +
489
"  \"checkout\": [ \"*\" ],\n" +
490
"  \"commit\": [ \"*\" ],\n" +
491
"  \"entities\": [ \"*\" ],\n" +
492
"  \"history\": [ \"*\" ],\n" +
493
"  \"topologyplans\": [ \"*\" ],\n" +
494
"  \"update\": [ \"*\" ],\n" +
495
"  \"users\": [ \"*\" ]\n" +
496
"}")
497
                .setDescription("Json with the information of what operations the different users can do.\nExample:\n{\n" +
498
"  \"add\": [ \"user1\", \"$admin\"],\n" +
499
"  \"checkout\": [ \"user1\", \"user2\"],\n" +
500
"  \"commit\": [ \"user1\" ],\n" +
501
"  \"entities\": [ \"user1\", \"user2\"],\n" +
502
"  \"history\": [ \"user1\" ],\n" +
503
"  \"topologyplans\": [ \"user1\", \"user2\"],\n" +
504
"  \"update\": [ \"user1\", \"user2\"],\n" +
505
"  \"users\": [ \"user1\", \"user2\"]\n" +
506
"}\n\n" +
507
"use $ before the name to indicate a role name instead of a username."
508
                );
509
        ft.add(DESCRIPTION, DataTypes.STRING)
510
                .setTag(DynFormSPIManager.TAG_DYNFORM_RESIZEWEIGHT, 60)
511
                .setDataProfileName("Text")
512
                .setSize(10240)
513
                .setLabel("Description")
514
                .setOrder(110);
515
        ft.add(FEATURETYPE, DataTypes.STRING)
516
                .setTag(DynFormSPIManager.TAG_DYNFORM_RESIZEWEIGHT, 60)
517
                .setTag(DynFormSPIManager.TAG_DYNFORM_LABEL_EMPTY, true)
518
                .setDataProfileName("Text")
519
                .setSize(10240)
520
                .setLabel("Feature type")
521
                .setGroup("Feature type")
522
                .setOrder(10);
523
        ft.add(TOPOLOGYPLAN_MODE, DataTypes.INTEGER)
524
                .setLabel("Topology plan mode")                
525
                .setAvailableValues(new DynObjectValueItem[] {
526
                    new DynObjectValueItem(TOPOLOGYPLAN_MODE_RECOMMENDED, "Recommended"),
527
                    new DynObjectValueItem(TOPOLOGYPLAN_MODE_MANDATORY, "Mandatory")
528
                })
529
                .setOrder(100);
530
        ft.add(RESOURCES, DataTypes.STRING)
531
                .setSize(200)
532
                .setLabel("Resources");
533
        ft.add(DATA_MODELS, DataTypes.STRING)
534
                .setSize(200)
535
                .setLabel("Data models");
536

    
537
        return ft.getNotEditableCopy();
538
    }
539
    
540
}