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 / workspace / tables / WorkspaceChangesTable.java @ 2701

History | View | Annotate | Download (15.4 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.FeatureQuery;
10
import org.gvsig.fmap.dal.feature.FeatureSet;
11
import org.gvsig.fmap.dal.feature.FeatureSet.DisposableFeatureSetIterable;
12
import org.gvsig.fmap.dal.feature.FeatureStore;
13
import org.gvsig.fmap.dal.feature.FeatureType;
14
import org.gvsig.json.Json;
15
import org.gvsig.tools.dataTypes.DataTypes;
16
import org.gvsig.tools.dispose.DisposeUtils;
17
import org.gvsig.tools.dynobject.DynObjectValueItem;
18
import org.gvsig.tools.util.GetItemWithSize64;
19
import org.gvsig.tools.util.GetItemWithSizeAndIterator64;
20
import org.gvsig.vcsgis.lib.workspace.tables.AbstractTable.AbstractRow;
21
import org.gvsig.vcsgis.lib.workspace.tables.EntitiesTable.EntityRow;
22
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_ADD_ENTITY;
23
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_DELETE;
24
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_INSERT;
25
import static org.gvsig.vcsgis.lib.VCSGisManager.OP_UPDATE;
26
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace;
27
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspaceChange;
28

    
29
/**
30
 *
31
 * @author gvSIG Team
32
 */
33
@SuppressWarnings("UseSpecificCatch")
34
public class WorkspaceChangesTable extends AbstractTable {
35

    
36
    
37
    public static final String TABLE_NAME = "VCSGIS_WSCHANGES";
38
    
39
    public static final String COD_CHANGE = "COD_WSCHANGE";
40
    private static final String COD_ENTITY = "COD_ENTITY";
41
    public static final String SELECTED = "WSCH_SELECTED";
42
    private static final String FEATUREID = "WSCH_FEATURECODE";
43
    private static final String OPERATION = "WSCH_OPERATION";
44

    
45
    private static final String LABEL = "WSCH_LABEL";
46
    private static final int MAX_SIZE_LABEL = 40;
47

    
48
    @SuppressWarnings("UseSpecificCatch")
49
    public static class WorkspaceChangeRow extends AbstractRow implements VCSGisWorkspaceChange {
50

    
51
        public WorkspaceChangeRow(VCSGisWorkspace workspace) {
52
            this(workspace, null);
53
        }
54
        
55
        public WorkspaceChangeRow(VCSGisWorkspace workspace, Feature feature) {
56
            super(workspace, TABLE_NAME, COD_CHANGE, feature);
57
        }
58
        
59
        @Override
60
        public String getEntityCode() {
61
            return this.feature.getString(COD_ENTITY);
62
        }
63

    
64
        @Override
65
        public String getRelatedFeatureCode() {
66
            return this.feature.getString(FEATUREID);
67
        }
68
        
69
        @Override
70
        public int getOperation() {
71
            return this.feature.getInt(OPERATION);
72
        }
73
        
74
        @Override
75
        public String getOperationLabel() {
76
            return this.feature.getLabelOfValue(OPERATION);
77
        }
78
        
79
        @Override
80
        public String getLabel() {
81
            return this.feature.getString(LABEL);
82
        }
83

    
84
        @Override
85
        public boolean isSelected() {
86
            return this.feature.getBoolean(SELECTED);
87
        }
88
        
89
        public void setEntityCode(String code) {
90
            this.feature.set(COD_ENTITY, code);
91
        }
92

    
93
        public void setFeatureCode(String code) {
94
            this.feature.set(FEATUREID, code);
95
        }
96
        
97
        public void setOperation(int op) {
98
            this.feature.set(OPERATION, op);
99
        }
100
        
101
        @Override
102
        public void setSelected(boolean selected) {
103
            this.feature.set(SELECTED, selected);
104
        }
105

    
106
        public void setLabel(String label) {
107
            this.feature.set(LABEL, StringUtils.abbreviate(label, MAX_SIZE_LABEL));
108
        }
109
        
110
        @Override
111
        public String getData() {
112
            String entityName = "unknown";
113
            String code = "unknown";
114
            try {
115
                entityName = this.getEntity().getEntityName();
116
                code = this.getRelatedFeatureCode();
117
                FeatureStore store = workspace.getFeatureStore(entityName);
118
                Feature f = store.findFirst("\""+this.getEntity().getFeatureIdFieldName()+"\"='"+code+"'");
119
                if( f==null ) {
120
                    throw new RuntimeException("Feature '"+entityName+"/"+code+"'not found.");
121
                }
122
                return f.toJson().toString();
123
            } catch (Exception ex) {
124
                throw new RuntimeException("Can't retrieve feature '"+entityName+"/"+code+"'.",ex);
125
            }
126
        }    
127

    
128
        @Override
129
        public JsonObject getDataAsJson() {
130
            String s = this.getData();
131
            return Json.createObject(s);
132
        }
133
        
134
        @Override
135
        public String toString() {
136
            switch (this.getOperation()) {
137
                case OP_ADD_ENTITY:
138
                    return "{ OP:'ADD_ENTITY', ENTITYCODE:'"+this.getEntityCode()+"', }";
139
                case OP_INSERT:
140
                    return "{ OP:'INSERT', ENTITYCODE:'"+this.getEntityCode()+"', FEATURECODE:'"+this.getRelatedFeatureCode()+"', DATA:'"+this.getData()+"' }";
141
                case OP_UPDATE:
142
                    return "{ OP:'UPDATE', ENTITYCODE:'"+this.getEntityCode()+"', FEATURECODE:'"+this.getRelatedFeatureCode()+"', DATA:'"+this.getData()+"' }";
143
                case OP_DELETE:
144
                    return "{ OP:'DELETE', ENTITYCODE:'"+this.getEntityCode()+"', FEATURECODE:'"+this.getRelatedFeatureCode()+"' }";
145
                default:
146
                    return "{ OP:'"+this.getOperation()+"', ENTITYCODE:'"+this.getEntityCode()+"', FEATURECODE:'"+this.getRelatedFeatureCode()+"' }";
147
            }
148
        }
149
        
150
        @Override
151
        public Feature getRelatedFeature() {
152
            String entityName = "unknown";
153
            String code = "unknown";
154
            try {
155
                entityName = this.getEntity().getEntityName();
156
                code = this.getRelatedFeatureCode();
157
                FeatureStore store = workspace.getFeatureStore(entityName);
158
                Feature f = store.findFirst("\""+this.getEntity().getFeatureIdFieldName()+"\"='"+code+"'");
159
                if( f==null ) {
160
                    throw new RuntimeException("Feature '"+entityName+"/"+code+"'not found.");
161
                }
162
                return f;
163
            } catch (Exception ex) {
164
                throw new RuntimeException("Can't retrieve feature '"+entityName+"/"+code+"'.",ex);
165
            }
166
        }        
167

    
168
        public EntityRow getEntity() {
169
            if( this.entity == null ) {
170
                this.entity = (EntityRow) this.workspace.getWorkspaceEntityByCode(this.getEntityCode());
171
            }
172
            return this.entity;
173
        }
174

    
175
    }
176
    
177
    public WorkspaceChangesTable() {
178
        super(TABLE_NAME, featureType());
179
    }
180

    
181
    public DisposableFeatureSetIterable getByOperation(VCSGisWorkspace workspace, int op) {
182
        FeatureStore store = null;
183
        try {
184
            store = workspace.getFeatureStore(TABLE_NAME);
185
            FeatureQuery query = store.createFeatureQuery();
186
            query.addFilter("\""+WorkspaceChangesTable.OPERATION+"\"="+op);
187
            query.retrievesAllAttributes();
188
            DisposableFeatureSetIterable changes = store.getFeatureSet(query).iterable();
189
            return changes;
190
        } catch (Exception ex) {
191
            throw new RuntimeException("Can't retrieve changes for operartion "+op+".", ex);
192
        } finally {
193
            if( store!=null ) {
194
                DisposeUtils.dispose(store);
195
            }
196
        }
197
    }
198
    
199
    public DisposableFeatureSetIterable getGroupedByEntity(VCSGisWorkspace workspace) {
200
        FeatureStore store = null;
201
        try {
202
            store = workspace.getFeatureStore(TABLE_NAME);
203
            FeatureQuery query = store.createFeatureQuery();
204

    
205
            query.getOrder().add(WorkspaceChangesTable.COD_ENTITY,true);
206
            query.getGroupByColumns().add(WorkspaceChangesTable.COD_ENTITY);
207
            query.retrievesAllAttributes();
208
            DisposableFeatureSetIterable changes = store.getFeatureSet(query).iterable();
209
            return changes;
210
        } catch (Exception ex) {
211
            throw new RuntimeException("Can't retrieve changes grouped by entity.", ex);
212
        } finally {
213
            if( store!=null ) {
214
                DisposeUtils.dispose(store);
215
            }
216
        }
217
    }
218

    
219
    public DisposableFeatureSetIterable getSelectedsWithoutAddEntity(VCSGisWorkspace workspace) {
220
        FeatureStore store = null;
221
        try {
222
            store = workspace.getFeatureStore(TABLE_NAME);
223
            FeatureQuery query = store.createFeatureQuery();
224

    
225
            query.setFilter("\""+SELECTED+"\" AND \""+OPERATION+"\" <> "+OP_ADD_ENTITY);
226
            query.retrievesAllAttributes();
227
            DisposableFeatureSetIterable changes = store.getFeatureSet(query).iterable();
228
            if( changes.isEmpty() ) {
229
                DisposeUtils.disposeQuietly(changes);
230
                return null;
231
            }
232
            return changes;
233
        } catch (Exception ex) {
234
            throw new RuntimeException("Can't retrieve all changes.", ex);
235
        } finally {
236
            if( store!=null ) {
237
                DisposeUtils.dispose(store);
238
            }
239
        }
240
    }
241
    
242
    public DisposableFeatureSetIterable getAll(VCSGisWorkspace workspace) {
243
        FeatureStore store = null;
244
        try {
245
            store = workspace.getFeatureStore(TABLE_NAME);
246
            DisposableFeatureSetIterable changes = store.getFeatureSet().iterable();
247
            return changes;
248
        } catch (Exception ex) {
249
            throw new RuntimeException("Can't retrieve all changes.", ex);
250
        } finally {
251
            if( store!=null ) {
252
                DisposeUtils.dispose(store);
253
            }
254
        }
255
    }
256

    
257
    public GetItemWithSize64<Feature> getByEntityCode(VCSGisWorkspace workspace, String entityCode) {
258
        FeatureStore store = null;
259
        try {
260
            store = workspace.getFeatureStore(TABLE_NAME);
261
            if( StringUtils.isBlank(entityCode) ) {
262
                GetItemWithSizeAndIterator64<Feature> changes = store.getFeatures64(
263
                        null, 
264
                        "\""+COD_ENTITY+"\",-\""+OPERATION+"\",\""+COD_CHANGE+"\"", 
265
                        true);
266
                return changes;
267
            }
268
            GetItemWithSizeAndIterator64<Feature> changes = store.getFeatures64(
269
                    ""+COD_ENTITY+"='"+entityCode+"'",
270
                    "-\""+OPERATION+"\",\""+COD_CHANGE+"\"", 
271
                    true
272
            );
273
            return changes;
274
        } catch (Exception ex) {
275
            throw new RuntimeException("Can't retrieve changes by entity.", ex);
276
        } finally {
277
            if( store!=null ) {
278
                DisposeUtils.dispose(store);
279
            }
280
        }
281
    }
282
    
283
    public long getCountLocalChangesOfEntity(VCSGisWorkspace workspace, String entityCode) {
284
        FeatureStore store = null;
285
        FeatureSet changes = null;
286
        try {
287
            store = workspace.getFeatureStore(TABLE_NAME);
288
            if( StringUtils.isBlank(entityCode) ) {
289
                throw new IllegalArgumentException("entityCode is required.");
290
            }
291
            changes = store.getFeatureSet(
292
                    "\""+COD_ENTITY+"\"='"+entityCode+"'"
293
            );
294
            return changes.size64();
295
        } catch (Exception ex) {
296
            throw new RuntimeException("Can't retrieve changes by entity.", ex);
297
        } finally {
298
            DisposeUtils.disposeQuietly(changes);
299
            DisposeUtils.disposeQuietly(store);
300
        }
301
    }
302

    
303
    public WorkspaceChangeRow getByEntityAndDataCode(
304
            VCSGisWorkspace workspace, 
305
            String entityCode, 
306
            String featureCode
307
        ) {
308
        FeatureStore store = null;
309
        try {
310
            store = workspace.getFeatureStore(TABLE_NAME);
311
            Feature f = store.findFirst(
312
                    "\"" + COD_ENTITY + "\"='" + entityCode + "' AND \""+FEATUREID+"\"='"+featureCode+"'"
313
            );
314
            if (f == null) {
315
                return null;
316
            }
317
            WorkspaceChangeRow row = new WorkspaceChangeRow(workspace, f);
318
            return row;
319
        } catch (Exception ex) {
320
            throw new RuntimeException("Can't retrieve change for 'FEATURECODE[" + featureCode + "]'.", ex);
321
        } finally {
322
            DisposeUtils.disposeQuietly(store);
323
        }
324
    }
325

    
326
    public void deleteAll(VCSGisWorkspace workspace) {
327
        FeatureStore store = null;
328
        try {
329
            store = workspace.getFeatureStore(TABLE_NAME);
330
            store.delete("TRUE");
331
        } catch (Exception ex) {
332
            throw new RuntimeException("Can't delete all changes.", ex);
333
        } finally {
334
            if( store!=null ) {
335
                DisposeUtils.dispose(store);
336
            }
337
        }
338
    }
339

    
340
    public void deleteSelecteds(VCSGisWorkspace workspace) {
341
        FeatureStore store = null;
342
        try {
343
            store = workspace.getFeatureStore(TABLE_NAME);
344
            store.delete("\""+SELECTED+"\"");
345
        } catch (Exception ex) {
346
            throw new RuntimeException("Can't delete selected changes.", ex);
347
        } finally {
348
            if( store!=null ) {
349
                DisposeUtils.dispose(store);
350
            }
351
        }
352

    
353
    }
354

    
355
    public WorkspaceChangeRow find(VCSGisWorkspace workspace, FeatureStore store, String entityCode, String featureCode) {
356
        try {
357
            Feature f = store.findFirst(
358
                    "\""+FEATUREID+"\"='"+featureCode+"'"
359
//                    "\""+COD_ENTITY+"\"='"+entityCode+"' AND \""+FEATUREID+"\"='"+featureCode+"'"
360
            );
361
            if(f==null){
362
                return null;
363
            }
364
            return new WorkspaceChangeRow(workspace, f);
365
        } catch (Exception ex) {
366
            throw new RuntimeException("Can't retrieve change.", ex);
367
        }
368
    }
369
    
370
    public static final FeatureType featureType() {
371
        DataManager dataManager = DALLocator.getDataManager();
372
        EditableFeatureType ft = dataManager.createFeatureType();
373
        ft.setLabel("VCSGIS Local changes");
374
        ft.getTags().set("ID", TABLE_NAME);
375
        ft.add(COD_CHANGE, DataTypes.STRING)
376
                .setSize(40)
377
                .setIsPrimaryKey(true)
378
                .setLabel("Code")
379
                .setReadOnly(false);        
380
        ft.add(COD_ENTITY, DataTypes.STRING)
381
                .setIsIndexed(true)
382
                .setAllowIndexDuplicateds(true)
383
                .setSize(40)
384
                .setLabel("Entity code");
385
        ft.add(SELECTED, DataTypes.BOOLEAN)
386
                .setIsIndexed(true)
387
                .setAllowIndexDuplicateds(true)
388
                .setLabel("Selected");
389
        ft.add(FEATUREID, DataTypes.STRING)
390
                .setIsIndexed(false)
391
                .setSize(40)
392
                .setLabel("Identifier")
393
                .setDescription("The identifier of the feature");
394
        ft.add(OPERATION, DataTypes.INTEGER)
395
                .setLabel("Operation")
396
                .setAvailableValues(new DynObjectValueItem[] {
397
                    new DynObjectValueItem(OP_ADD_ENTITY, "Add entity"),
398
                    new DynObjectValueItem(OP_INSERT, "Insert"),
399
                    new DynObjectValueItem(OP_UPDATE, "Update"),
400
                    new DynObjectValueItem(OP_DELETE, "Delete")
401
                });
402
        ft.add(LABEL, DataTypes.STRING)
403
                .setIsIndexed(false)
404
                .setSize(MAX_SIZE_LABEL)
405
                .setLabel("Label")
406
                .setDescription("The label of the feature");
407
        
408
        return ft.getNotEditableCopy();
409
    }
410
}