Revision 43954

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/impl/DALDefaultImplLibrary.java
33 33
import org.gvsig.fmap.dal.DataManager;
34 34
import org.gvsig.fmap.dal.DataTypes;
35 35
import org.gvsig.fmap.dal.feature.AbstractFeatureStoreTransform;
36
import org.gvsig.fmap.dal.feature.impl.DALFile;
36 37
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureQuery;
37 38
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureReference;
38 39
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureReferenceSelection;
......
119 120
        DefaultFeatureStore.registerPersistenceDefinition();
120 121
        DefaultDataServerExplorerPool.registerPersistenceDefinition();
121 122
        DefaultDataServerExplorerPoolEntry.registerPersistenceDefinition();
123
        
124
        DALFile.registerPersistenceDefinition();
122 125
        try {
123 126
            DefaultFeatureStore.registerMetadataDefinition();
124 127
        } catch (MetadataException e) {
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/DefaultFeatureStore.java
24 24

  
25 25
package org.gvsig.fmap.dal.feature.impl;
26 26

  
27
import java.io.File;
27 28
import org.gvsig.fmap.dal.feature.impl.editing.memory.SpatialManager;
28 29
import org.gvsig.fmap.dal.feature.impl.editing.memory.FeatureTypeManager;
29 30
import org.gvsig.fmap.dal.feature.impl.editing.memory.FeatureManager;
......
39 40
import java.util.Map;
40 41
import java.util.Map.Entry;
41 42
import java.util.Set;
43
import java.util.logging.Level;
42 44

  
43 45
import org.apache.commons.io.FilenameUtils;
44 46
import org.apache.commons.lang3.StringUtils;
......
351 353
        }
352 354
        this.notifyChange(DataStoreNotification.BEFORE_OPEN);
353 355
        this.provider.open();
356
        loadDALFile();
354 357
        this.notifyChange(DataStoreNotification.AFTER_OPEN);
355 358
    }
356 359

  
......
1465 1468
                        featureManager.getInserted(),
1466 1469
                        featureManager.getUpdated(),
1467 1470
                        featureTypeManager.getFeatureTypesChanged());
1468
                }
1471
                }  
1472
                saveDALFile();
1469 1473
                exitEditingMode();
1470 1474
                updateIndexes();
1471 1475
                notifyChange(FeatureStoreNotification.AFTER_FINISHEDITING);
......
1478 1482
        }
1479 1483
    }
1480 1484

  
1485
    @SuppressWarnings("UseSpecificCatch")
1486
    private void saveDALFile() {       
1487
        try {
1488
            DataServerExplorer explorer = this.getExplorer();
1489
            File f = explorer.getResourcePath(this, "dal");
1490
            if( f == null ) {
1491
                return;
1492
            }
1493
            DALFile dalFile = DALFile.getDALFile();
1494
            dalFile.setStore(this);
1495
            if( !dalFile.isEmpty() ) {
1496
                dalFile.write(f);
1497
            }
1498
        } catch (Exception ex) {
1499
            LOG.warn("Can't save DAL File", ex);
1500
        }
1501
    }
1502
    
1503
    @SuppressWarnings("UseSpecificCatch")
1504
    private void loadDALFile() {
1505
        try {
1506
            DataServerExplorer explorer = this.getExplorer();
1507
            File f = explorer.getResourcePath(this, "dal");
1508
            if( f == null ) {
1509
                return;
1510
            }
1511
            DALFile dalFile = DALFile.getDALFile(f);
1512
            if( !dalFile.isEmpty() ) {
1513
                dalFile.updateStore(this);
1514
            }
1515
        } catch (Exception ex) {
1516
            LOG.warn("Can't load DAL File", ex);
1517
        }
1518
    }
1519
    
1481 1520
    /**
1482 1521
     * Save changes in the provider without leaving the edit mode.
1483 1522
     * Do not call observers to communicate a change of ediding mode.
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/DefaultFeatureAttributeDescriptor.java
32 32
import org.apache.commons.lang3.ArrayUtils;
33 33
import org.cresques.cts.IProjection;
34 34
import org.gvsig.fmap.crs.CRSFactory;
35
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
35 36
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
36 37
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
37 38
import org.gvsig.fmap.dal.feature.FeatureAttributeGetter;
......
218 219
        return this.dataType;
219 220
    }
220 221

  
222
    public FeatureAttributeDescriptor setDataType(int type) {
223
        this.dataType = ToolsLocator.getDataTypesManager().get(type);
224
        return this;
225
    }
226

  
221 227
    @Override
222 228
    public DateFormat getDateFormat() {
223 229
        return this.dateFormat;
......
284 290
    public String getName() {
285 291
        return this.name;
286 292
    }
287

  
293
    
294
    public FeatureAttributeDescriptor setName(String name) {
295
        this.name = name;
296
        return this;
297
    }
298
    
288 299
    @Override
289 300
    public Class getObjectClass() {
290 301
        if (getDataType().getType() == DataTypes.OBJECT) {
......
806 817
        return this.featureAttributeEmulator;
807 818
    }
808 819

  
820
    public FeatureAttributeDescriptor setFeatureAttributeEmulator(FeatureAttributeEmulator featureAttributeEmulator) {
821
        this.featureAttributeEmulator = featureAttributeEmulator;
822
        return this;
823
    }
824
        
809 825
    @Override
810 826
    public boolean isIndexed() {
811 827
        return this.indexed;
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/main/java/org/gvsig/fmap/dal/feature/impl/DALFile.java
1

  
2
package org.gvsig.fmap.dal.feature.impl;
3

  
4
import java.io.File;
5
import java.io.FileInputStream;
6
import java.io.FileOutputStream;
7
import java.util.ArrayList;
8
import java.util.HashSet;
9
import java.util.Iterator;
10
import java.util.List;
11
import java.util.Set;
12
import org.apache.commons.collections4.CollectionUtils;
13
import org.apache.commons.io.IOUtils;
14
import org.gvsig.fmap.dal.exception.DataException;
15
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
16
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
17
import org.gvsig.fmap.dal.feature.FeatureStore;
18
import org.gvsig.fmap.dal.feature.FeatureType;
19
import org.gvsig.tools.ToolsLocator;
20
import org.gvsig.tools.dynobject.DynStruct;
21
import org.gvsig.tools.persistence.PersistenceManager;
22
import org.gvsig.tools.persistence.Persistent;
23
import org.gvsig.tools.persistence.PersistentState;
24
import org.gvsig.tools.persistence.exception.PersistenceException;
25

  
26

  
27
public class DALFile implements Persistent {
28

  
29
    private static final String DALFILE_PERSISTENCE_DEFINITION_NAME = "DALFile";
30
    private static final String CALCULATEDATTRIBUTE_PERSISTENCE_DEFINITION_NAME = "DALFileCalculatedAttribute";
31
    
32
    public static DALFile getDALFile() {
33
        DALFile f = new DALFile();
34
        return f;
35
    }
36

  
37
    public static DALFile getDALFile(File f) {
38
        DALFile df = new DALFile();
39
        df.read(f);
40
        return df;
41
    }
42
    
43
    public static void registerPersistenceDefinition() {
44
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
45
        if (manager.getDefinition(DALFILE_PERSISTENCE_DEFINITION_NAME) == null) {
46
            DynStruct definition = manager.addDefinition(
47
                DefaultFeatureStore.class,
48
                DALFILE_PERSISTENCE_DEFINITION_NAME, 
49
                DALFILE_PERSISTENCE_DEFINITION_NAME + " Persistent definition", 
50
                null, 
51
                null
52
            );
53
            definition.addDynFieldList("calculatedAttributes")
54
                .setClassOfItems(CalculatedAttribute.class)
55
                .setMandatory(true)
56
                .setPersistent(true);
57
        }
58
        if (manager.getDefinition(CALCULATEDATTRIBUTE_PERSISTENCE_DEFINITION_NAME) == null) {
59
            DynStruct definition = manager.addDefinition(
60
                DefaultFeatureStore.class,
61
                CALCULATEDATTRIBUTE_PERSISTENCE_DEFINITION_NAME, 
62
                CALCULATEDATTRIBUTE_PERSISTENCE_DEFINITION_NAME + " Persistent definition", 
63
                null, 
64
                null
65
            );
66
            definition.addDynFieldString("name")
67
                .setMandatory(true)
68
                .setPersistent(true);
69
            definition.addDynFieldInt("type")
70
                .setMandatory(true)
71
                .setPersistent(true);
72
            definition.addDynFieldInt("featureTypeIndex")
73
                .setMandatory(true)
74
                .setPersistent(true);
75
            definition.addDynFieldObject("emulator")
76
                .setClassOfValue(FeatureAttributeEmulator.class)
77
                .setMandatory(true)
78
                .setPersistent(true);
79
        }
80
    }
81

  
82
    
83
    private class CalculatedAttribute implements Persistent {
84

  
85
        private String name;
86
        private int type;
87
        private FeatureAttributeEmulator emulator;
88
        private int featureTypeIndex;
89
        
90
        public CalculatedAttribute() {
91
            
92
        }
93

  
94
        private CalculatedAttribute(int featureTypeIndex, FeatureAttributeDescriptor attrdesc) {
95
            this.featureTypeIndex = featureTypeIndex;
96
            this.name = attrdesc.getName();
97
            this.type = attrdesc.getType();
98
            this.emulator = attrdesc.getFeatureAttributeEmulator();
99
        }
100

  
101
        @Override
102
        public void saveToState(PersistentState state) throws PersistenceException {
103
            state.set("name", name);
104
            state.set("type", type);
105
            state.set("featureTypeIndex", featureTypeIndex);
106
            state.set("emulator", emulator);
107
        }
108

  
109
        @Override
110
        public void loadFromState(PersistentState state) throws PersistenceException {
111
            this.name = state.getString("name");
112
            this.type = state.getInt("type");
113
            this.featureTypeIndex = state.getInt("featureTypeIndex");
114
            this.emulator = (FeatureAttributeEmulator) state.get("emulator");
115
        }
116
        
117
        
118
    }
119
    
120
    private final List<CalculatedAttribute> calculatedAttributes;
121
    
122
    private DALFile() {
123
        this.calculatedAttributes = new ArrayList<>();
124
    }
125

  
126
    public boolean isEmpty() {
127
        if( CollectionUtils.isEmpty(this.calculatedAttributes) ) {
128
            return true;
129
        }
130
        return false;
131
    }
132
    
133
    public void write(File f) {
134
        FileOutputStream out = null;
135
        try {
136
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
137
            PersistentState state = manager.getState(this);
138
            out = new FileOutputStream(f);
139
            manager.saveState(state, out);
140
        } catch(Exception ex) {
141
            throw new RuntimeException("Can't write DAL file.",ex);
142
        } finally {
143
            IOUtils.closeQuietly(out);
144
        }
145
    }
146

  
147
    @SuppressWarnings("UseSpecificCatch")
148
    public void read(File f) {
149
        FileInputStream in = null;
150
        try {
151
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
152
            in = new FileInputStream(f);
153
            DALFile x = (DALFile) manager.getObject(in);
154
            this.calculatedAttributes.clear();
155
            this.calculatedAttributes.addAll(x.calculatedAttributes);
156
        } catch(Exception ex) {
157
            throw new RuntimeException("Can't read DAL file.",ex);
158
        } finally {
159
            IOUtils.closeQuietly(in);
160
        }
161
    }
162

  
163
    public void setStore(FeatureStore store) throws DataException {
164
        this.calculatedAttributes.clear();
165
        
166
        List<FeatureType> types = store.getFeatureTypes();
167
        for( int n=0; n<types.size(); n++ ) {
168
            FeatureType type = types.get(n);
169
                for (FeatureAttributeDescriptor attrdesc : type) {
170
                    FeatureAttributeEmulator emulator = attrdesc.getFeatureAttributeEmulator();
171
                    if( emulator!= null && emulator instanceof Persistent ) {
172
                        CalculatedAttribute calculatedAttribute = new CalculatedAttribute(n,attrdesc);
173
                        this.calculatedAttributes.add(calculatedAttribute);
174
                    }
175
            }
176
        }
177
    }
178

  
179
    public void updateStore(FeatureStore store) throws DataException {
180
        List<FeatureType> types = store.getFeatureTypes();
181
        Set<DefaultFeatureType> needFixTypes = new HashSet<>();
182
        
183
        for (CalculatedAttribute calculatedAttribute : calculatedAttributes) {
184
            DefaultFeatureType ft = (DefaultFeatureType) types.get(calculatedAttribute.featureTypeIndex);
185
            DefaultFeatureAttributeDescriptor attrdesc = new DefaultFeatureAttributeDescriptor(ft);
186
            attrdesc.setDataType(calculatedAttribute.type);
187
            attrdesc.setName(calculatedAttribute.name);
188
            attrdesc.setFeatureAttributeEmulator(calculatedAttribute.emulator);
189
            ft.add(attrdesc);
190
            needFixTypes.add(ft);
191
        }
192
        for (DefaultFeatureType type : needFixTypes) {
193
            type.fixAll();
194
        }
195
    }
196

  
197
    @Override
198
    public void saveToState(PersistentState state) throws PersistenceException {
199
        state.set("calculatedAttributes", calculatedAttributes);
200
    }
201

  
202
    @Override
203
    public void loadFromState(PersistentState state) throws PersistenceException {
204
        this.calculatedAttributes.clear();
205
        Iterator<CalculatedAttribute> it = state.getIterator("calculatedAttributes");
206
        while( it.hasNext() ) {
207
            CalculatedAttribute calculatedAttribute = it.next();
208
            this.calculatedAttributes.add(calculatedAttribute);
209
        }
210
    }
211

  
212
}

Also available in: Unified diff