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 / RevisionsRepoTable.java @ 3633

History | View | Annotate | Download (14.3 KB)

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

    
3
import java.sql.Timestamp;
4
import javax.json.JsonObject;
5
import org.gvsig.expressionevaluator.ExpressionBuilder;
6
import org.gvsig.expressionevaluator.ExpressionUtils;
7
import org.gvsig.fmap.dal.DALLocator;
8
import org.gvsig.fmap.dal.DataManager;
9
import org.gvsig.fmap.dal.feature.EditableFeatureType;
10
import org.gvsig.fmap.dal.feature.Feature;
11
import org.gvsig.fmap.dal.feature.FeatureQuery;
12
import org.gvsig.fmap.dal.feature.FeatureSet;
13
import org.gvsig.fmap.dal.feature.FeatureStore;
14
import org.gvsig.fmap.dal.feature.FeatureType;
15
import org.gvsig.json.JsonObjectBuilder;
16
import static org.gvsig.tools.dataTypes.DataTypeUtils.toTimestamp;
17
import org.gvsig.tools.dataTypes.DataTypes;
18
import org.gvsig.tools.dispose.DisposableIterable;
19
import org.gvsig.tools.dispose.DisposeUtils;
20
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
21
import org.gvsig.tools.dynobject.DynObjectValueItem;
22
import static org.gvsig.vcsgis.lib.VCSGisManager.TOPOLOGYPLAN_NOTPASSED;
23
import static org.gvsig.vcsgis.lib.VCSGisManager.TOPOLOGYPLAN_PASSED;
24
import static org.gvsig.vcsgis.lib.VCSGisManager.TOPOLOGYPLAN_UNKNOWN;
25
import static org.gvsig.vcsgis.lib.VCSGisManager.VCSGISCODELEN;
26
import org.gvsig.vcsgis.lib.VCSGisRevision;
27
import org.gvsig.vcsgis.lib.VCSGisRevisionImpl;
28
import org.gvsig.vcsgis.lib.repository.VCSGisRepositoryRevision;
29
import org.gvsig.vcsgis.lib.repository.localdb.VCSGisRepositoryLocaldb;
30

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

    
39
    public static final String COD_REVISION = "COD_REVISION";
40
    private static final String COD_USER = "COD_USER";
41
    private static final String COD_ENTITY = "COD_ENTITY";
42
    public static final String REVISION_NUMBER = "REV_NUMBER";
43
    private static final String REVISION_COMMENT = "REV_COMMENT";
44
    private static final String REVISION_EFECTIVEDATE = "REV_EFECTIVEDATE";
45
    private static final String REVISION_OPERATIONDATE = "REV_OPERATIONDATE";
46
    private static final String REVISION_TAGS = "REV_TAGS";
47
    private static final String REVISION_TOPOLOGYPLAN = "REV_TOPPLAN";
48

    
49
    public static class RevisionRepoRow extends AbstractRow implements VCSGisRepositoryRevision{
50

    
51
        public RevisionRepoRow(VCSGisRepositoryLocaldb repository) {
52
            super(repository, TABLE_NAME, COD_REVISION, null);
53
        }
54
        
55
        public RevisionRepoRow(VCSGisRepositoryLocaldb repository, Feature feature) {
56
            super(repository, TABLE_NAME, COD_REVISION, feature);
57
        }
58

    
59
        @Override
60
        public String getUserCode() {
61
            return this.feature.getString(COD_USER);
62
        }
63

    
64
        @Override
65
        public String getRevisionCode() {
66
            return this.getCode();
67
        }
68

    
69
        @Override
70
        public String getEntityCode() {
71
            return this.feature.getString(COD_ENTITY);
72
        }
73
        
74
        @Override
75
        public Timestamp getRevisionDate() {
76
            return (Timestamp) this.feature.getTimestamp(REVISION_OPERATIONDATE);
77
        }
78
        
79
        @Override
80
        public Timestamp getEfectiveDate() {
81
            return (Timestamp) this.feature.getTimestamp(REVISION_EFECTIVEDATE);
82
        }
83
        
84
        @Override
85
        public String getTags() {
86
            return this.feature.getString(REVISION_TAGS);
87
        }
88
        
89
        @Override
90
        public long getNumber() {
91
            return this.feature.getLong(REVISION_NUMBER);
92
        }
93
        
94
        @Override
95
        public int getTopologyPlan() {
96
            return this.feature.getInt(REVISION_TOPOLOGYPLAN);
97
        }
98
        
99
        @Override
100
        public String getComment() {
101
            return this.feature.getString(REVISION_COMMENT);
102
        }
103
        
104
        public void setUserCode(String code) {
105
            this.feature.set(COD_USER, code);
106
        }
107
        
108
        public void setOperationDate(Timestamp date) {
109
            this.feature.set(REVISION_OPERATIONDATE, date);
110
        }
111
        
112
        public void setEfectiveDate(Timestamp date) {
113
            this.feature.set(REVISION_EFECTIVEDATE, date);
114
        }
115
        
116
        public void setRevisionDate(java.util.Date date) {
117
            this.feature.set(REVISION_OPERATIONDATE, toTimestamp(date));
118
        }
119
        
120
        public void setEfectiveDate(java.util.Date date) {
121
            this.feature.set(REVISION_EFECTIVEDATE, toTimestamp(date));
122
        }
123
        
124
        public void setTopologyPlan(int topologyPlan) {
125
            this.feature.set(REVISION_TOPOLOGYPLAN,topologyPlan);
126
        }
127

    
128
        public void setTags(String tags) {
129
            this.feature.set(REVISION_TAGS,tags);
130
        }
131

    
132
        public void setComment(String comment) {
133
            this.feature.set(REVISION_COMMENT, comment);
134
        }
135

    
136
        public void setEntityCode(String code) {
137
            this.feature.set(COD_ENTITY, code);
138

    
139
        }
140

    
141
        void calculateRevisionNumber() {
142
            FeatureStore store = null;
143
            FeatureSet fset = null;
144
            try {
145
                store = this.repository.getFeatureStore(TABLE_NAME);
146
                fset = store.getFeatureSet();
147
                this.feature.set(REVISION_NUMBER, fset.size64());                
148
//                FeatureQuery query = store.createFeatureQuery();
149
//                query.addAggregate("MAX", REVISION_NUMBER);
150
//                Feature f = store.first();
151
//                if( f == null ) {
152
//                    this.feature.set(REVISION_NUMBER, 1);                
153
//                } else {
154
//                    this.feature.set(REVISION_NUMBER, f.getInt(REVISION_NUMBER)+1);                
155
//                }
156
            } catch(Exception ex) {
157
                throw new RuntimeException("Can't calculate revision number.", ex);
158
            } finally {
159
                DisposeUtils.disposeQuietly(fset);
160
                DisposeUtils.disposeQuietly(store);
161
            }
162
        }
163

    
164
        @Override
165
        public void insert(FeatureStore store) {
166
            this.calculateRevisionNumber();
167
            super.insert(store);
168
        }
169
        
170
        @Override
171
        public void copyFrom(VCSGisRevision other) {
172
            this.setCode(other.getRevisionCode());
173
            this.setComment(other.getComment());
174
            this.setEfectiveDate(other.getEfectiveDate());
175
            this.setEntityCode(other.getEntityCode());
176
            this.setRevisionDate(other.getRevisionDate());
177
            this.setTags(other.getTags());
178
            this.setUserCode(other.getUserCode());
179
        }
180

    
181
        @Override
182
        public RevisionRepoRow getValue() {
183
            return this;
184
        }
185

    
186
        @Override
187
        public String getLabel() {
188
            return VCSGisRevisionImpl.getLabel(this);
189
        }
190

    
191
        @Override
192
        public JsonObject toJson() {
193
            return this.toJsonBuilder().build();
194
        }
195

    
196
        @Override
197
        public JsonObjectBuilder toJsonBuilder() {
198
            return VCSGisRevisionImpl.toJsonBuilder(this);
199
        }
200
        
201
        @Override
202
        public String toString() {
203
            return VCSGisRevisionImpl.toString(this);
204
        }
205

    
206
    }
207
    
208
    public RevisionsRepoTable() {
209
        super(TABLE_NAME, featureType());
210
    }
211

    
212
    public RevisionRepoRow getByRevisionCode(VCSGisRepositoryLocaldb repository, String revisionCode) {
213
        FeatureStore store = null;
214
        try {
215
            store = repository.getFeatureStore(TABLE_NAME);
216
            Feature f = store.findFirst("\""+COD_REVISION+"\"='"+revisionCode+"'");
217
            if( f==null ) {
218
                return null;
219
            }
220
            RevisionRepoRow row = new RevisionRepoRow(repository, f);
221
            return row;
222
        } catch (Exception ex) {
223
            throw new RuntimeException("Can't retrieve revision '"+revisionCode+"'.", ex);
224
        } finally {
225
            DisposeUtils.disposeQuietly(store);
226
        }
227
    }
228

    
229
    public static final FeatureType featureType() {
230
        DataManager dataManager = DALLocator.getDataManager();
231
        EditableFeatureType ft = dataManager.createFeatureType();
232
        ft.setLabel("VCSGIS Revisions");
233
        ft.getTags().set("ID", TABLE_NAME);
234
        ft.getTags().set(DynFormSPIManager.TAG_DYNFORM_HEIGHT, 225);
235
        ft.getTags().set(DynFormSPIManager.TAG_DYNFORM_WIDTH, 570);
236
        ft.add(COD_REVISION, DataTypes.STRING)
237
                .setSize(VCSGISCODELEN)
238
                .setIsPrimaryKey(true)
239
                .setLabel("Code")
240
                .setReadOnly(false)
241
                .setDefaultFieldValue("<%=replace(UUID(),'-','')%>");
242
        ft.add(COD_USER, DataTypes.STRING)
243
                .setForeingkey(
244
                        true, 
245
                        false, 
246
                        UsersRepoTable.TABLE_NAME, 
247
                        UsersRepoTable.COD_USER, 
248
                        UsersRepoTable.USER_ID
249
                )                
250
                .setIsIndexed(true)
251
                .setAllowIndexDuplicateds(true)
252
                .setSize(VCSGISCODELEN)
253
                .setLabel("Cod. user");
254
        ft.add(COD_ENTITY, DataTypes.STRING)
255
                .setForeingkey(
256
                        true, 
257
                        false, 
258
                        EntitiesRepoTable.TABLE_NAME, 
259
                        EntitiesRepoTable.COD_ENTITY, 
260
                        EntitiesRepoTable.ENTITY_NAME
261
                )                
262
                .setIsIndexed(true)
263
                .setAllowIndexDuplicateds(true)
264
                .setSize(VCSGISCODELEN)
265
                .setLabel("Cod. entity");
266
        ft.add(REVISION_OPERATIONDATE, DataTypes.TIMESTAMP)
267
                .setIsIndexed(true)
268
                .setAllowIndexDuplicateds(true)
269
                .setLabel("Revision date");
270
        ft.add(REVISION_EFECTIVEDATE, DataTypes.TIMESTAMP)
271
                .setIsIndexed(true)
272
                .setAllowIndexDuplicateds(true)
273
                .setLabel("Efective Date");
274
        ft.add(REVISION_NUMBER, DataTypes.LONG)
275
                .setIsIndexed(true)
276
                .setAllowIndexDuplicateds(true)
277
                .setLabel("Revision number");
278
        ft.add(REVISION_TOPOLOGYPLAN, DataTypes.INT)
279
                .setIsIndexed(false)
280
                .setLabel("Topology plan")
281
                .setAvailableValues(new DynObjectValueItem[] {
282
                    new DynObjectValueItem(TOPOLOGYPLAN_UNKNOWN, "Unknown"),
283
                    new DynObjectValueItem(TOPOLOGYPLAN_PASSED, "Passed"),
284
                    new DynObjectValueItem(TOPOLOGYPLAN_NOTPASSED, "Not passed"),
285
                });
286
        ft.add(REVISION_TAGS, DataTypes.STRING)
287
                .setSize(200)
288
                .setLabel("Tags");
289
        ft.add(REVISION_COMMENT, DataTypes.STRING)
290
                .setDataProfileName("Text")
291
                .setSize(500)
292
                .setLabel("Comment");
293
        
294
        return ft.getNotEditableCopy();
295
    }
296

    
297
    public RevisionsRepoTable(String tableName, FeatureType featureType) {
298
        super(tableName, featureType);
299
    }
300

    
301
    public DisposableIterable<Feature> getRevisionsByDate(VCSGisRepositoryLocaldb repository, String entityCode, Timestamp maxDate, Timestamp minDate, int maxNumberOfRevisions) {
302
        FeatureStore store = null;
303
        DisposableIterable<Feature> features = null;
304
        try {
305
            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
306
            builder.and(
307
                    builder.eq(
308
                            builder.column(COD_ENTITY), 
309
                            builder.constant(entityCode)
310
                    )
311
            );
312
            builder.and(
313
                    builder.ge(
314
                            builder.column(REVISION_OPERATIONDATE), 
315
                            builder.constant(minDate)
316
                    )
317
            );
318
            builder.and(
319
                    builder.le(
320
                            builder.column(REVISION_OPERATIONDATE), 
321
                            builder.constant(maxDate)
322
                    )
323
            );
324
            store = repository.openFeatureStore(TABLE_NAME, null);
325
            FeatureQuery query = store.createFeatureQuery(builder.toString(), REVISION_NUMBER, false);
326
            query.setLimit(maxNumberOfRevisions);
327
            FeatureSet fset = store.getFeatureSet(query);
328
            features = fset.iterable();
329
            return features;
330
        } catch (Exception ex) {
331
            DisposeUtils.disposeQuietly(features);
332
            throw new RuntimeException("Can't retrieve revisions of 'ENTITY["+entityCode+"].", ex);
333
        } finally {
334
            if( store!=null ) {
335
                DisposeUtils.dispose(store);
336
            }
337
        }
338
    }
339

    
340
    public DisposableIterable<Feature> getRevisionsByRevisionNumber(VCSGisRepositoryLocaldb repository, String entityCode, long maxRevisionNumber, long minRevisionNumber, int maxNumberOfRevisions) {
341
        FeatureStore store = null;
342
        DisposableIterable<Feature> features = null;
343
        try {
344
            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
345
            builder.and(
346
                    builder.eq(
347
                            builder.column(COD_ENTITY), 
348
                            builder.constant(entityCode)
349
                    )
350
            );
351
            if( minRevisionNumber>=0 ) {
352
                builder.and(
353
                        builder.ge(
354
                                builder.column(REVISION_NUMBER), 
355
                                builder.constant(minRevisionNumber)
356
                        )
357
                );
358
            }
359
            if( maxRevisionNumber>=0 ) {
360
                builder.and(
361
                        builder.lt(
362
                                builder.column(REVISION_NUMBER), 
363
                                builder.constant(maxRevisionNumber)
364
                        )
365
                );
366
            }
367
            store = repository.openFeatureStore(TABLE_NAME, null);
368
            FeatureQuery query = store.createFeatureQuery(builder.toString(), REVISION_NUMBER, false);
369
            query.setLimit(maxNumberOfRevisions);
370
            FeatureSet fset = store.getFeatureSet(query);
371
            features = fset.iterable();
372
            return features;
373
        } catch (Exception ex) {
374
            DisposeUtils.disposeQuietly(features);
375
            throw new RuntimeException("Can't retrieve revisions of 'ENTITY["+entityCode+"].", ex);
376
        } finally {
377
            if( store!=null ) {
378
                DisposeUtils.dispose(store);
379
            }
380
        }
381
    }
382

    
383
}