Statistics
| Revision:

svn-gvsig-desktop / 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 @ 44114

History | View | Annotate | Download (9.11 KB)

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.timesupport.RelativeInterval;
20
import org.gvsig.timesupport.TimeSupportLocator;
21
import org.gvsig.tools.ToolsLocator;
22
import org.gvsig.tools.dynobject.DynStruct;
23
import org.gvsig.tools.persistence.PersistenceManager;
24
import org.gvsig.tools.persistence.Persistent;
25
import org.gvsig.tools.persistence.PersistentState;
26
import org.gvsig.tools.persistence.exception.PersistenceException;
27

    
28

    
29
@SuppressWarnings("UseSpecificCatch")
30
public class DALFile implements Persistent {
31

    
32
    private static final String DALFILE_PERSISTENCE_DEFINITION_NAME = "DALFile";
33
    private static final String ATTRIBUTE_PERSISTENCE_DEFINITION_NAME = "DALFileAttribute";
34
    
35
    public static DALFile getDALFile() {
36
        DALFile f = new DALFile();
37
        return f;
38
    }
39

    
40
    public static DALFile getDALFile(File f) {
41
        DALFile df = new DALFile();
42
        df.read(f);
43
        return df;
44
    }
45
    
46
    public static void registerPersistenceDefinition() {
47
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
48
        if (manager.getDefinition(DALFILE_PERSISTENCE_DEFINITION_NAME) == null) {
49
            DynStruct definition = manager.addDefinition(
50
                DALFile.class,
51
                DALFILE_PERSISTENCE_DEFINITION_NAME, 
52
                DALFILE_PERSISTENCE_DEFINITION_NAME + " Persistent definition", 
53
                null, 
54
                null
55
            );
56
            definition.addDynFieldList("attributes")
57
                .setClassOfItems(Attribute.class)
58
                .setMandatory(true)
59
                .setPersistent(true);
60
        }
61
        if (manager.getDefinition(ATTRIBUTE_PERSISTENCE_DEFINITION_NAME) == null) {
62
            DynStruct definition = manager.addDefinition(
63
                Attribute.class,
64
                ATTRIBUTE_PERSISTENCE_DEFINITION_NAME, 
65
                ATTRIBUTE_PERSISTENCE_DEFINITION_NAME + " Persistent definition", 
66
                null, 
67
                null
68
            );
69
            definition.addDynFieldString("name")
70
                .setMandatory(true)
71
                .setPersistent(true);
72
            definition.addDynFieldInt("type")
73
                .setMandatory(true)
74
                .setPersistent(true);
75
            definition.addDynFieldInt("featureTypeIndex")
76
                .setMandatory(true)
77
                .setPersistent(true);
78
            definition.addDynFieldObject("emulator")
79
                .setClassOfValue(FeatureAttributeEmulator.class)
80
                .setMandatory(true)
81
                .setPersistent(true);
82
            definition.addDynFieldLong("IntervalStart")
83
                .setMandatory(false)
84
                .setPersistent(true);
85
            definition.addDynFieldLong("IntervalEnd")
86
                .setMandatory(false)
87
                .setPersistent(true);
88
        }
89
    }
90

    
91
    
92
    public static class Attribute implements Persistent {
93

    
94
        private String name;
95
        private int type;
96
        private FeatureAttributeEmulator emulator;
97
        private int featureTypeIndex;
98
        private RelativeInterval interval;
99
        
100
        public Attribute() {
101
            
102
        }
103

    
104
        private Attribute(int featureTypeIndex, FeatureAttributeDescriptor attrdesc) {
105
            this.featureTypeIndex = featureTypeIndex;
106
            this.name = attrdesc.getName();
107
            this.type = attrdesc.getType();
108
            this.emulator = attrdesc.getFeatureAttributeEmulator();
109
            if( attrdesc.getInterval()!=null && attrdesc.getInterval().isRelative() ) {
110
                this.interval = (RelativeInterval) attrdesc.getInterval();
111
            } else {
112
                this.interval = null;
113
            }
114
        }
115

    
116
        public boolean needPersist() {
117
            if( emulator!= null && emulator instanceof Persistent ) {
118
                return true;
119
            }
120
            if( this.interval!=null ) {
121
                return true;
122
            }
123
            return false;
124
        }
125
        
126
        @Override
127
        public void saveToState(PersistentState state) throws PersistenceException {
128
            state.set("name", name);
129
            state.set("type", type);
130
            state.set("featureTypeIndex", featureTypeIndex);
131
            state.set("emulator", emulator);
132
            if( this.interval!=null ) {
133
                state.set("intervalStart", interval.getStart().toMillis());
134
                state.set("intervalEnd", interval.getEnd().toMillis());
135
            } else {
136
                state.setNull("intervalStart");
137
                state.setNull("intervalEnd");
138
            }
139
        }
140

    
141
        @Override
142
        public void loadFromState(PersistentState state) throws PersistenceException {
143
            this.name = state.getString("name");
144
            this.type = state.getInt("type");
145
            this.featureTypeIndex = state.getInt("featureTypeIndex");
146
            this.emulator = (FeatureAttributeEmulator) state.get("emulator");
147
            if( state.get("intervalStart")!=null ) {
148
                this.interval = TimeSupportLocator.getManager().createRelativeInterval(
149
                        state.getLong("intervalStart"), 
150
                        state.getLong("intervalEnd")
151
                );
152
            }
153
        }
154

    
155
        private void fetch(DefaultFeatureAttributeDescriptor attrdesc) {
156
            attrdesc.setDataType(this.type);
157
            attrdesc.setName(this.name);
158
            attrdesc.setFeatureAttributeEmulator(this.emulator);
159
            attrdesc.setInterval(this.interval);
160
        }
161
        
162
        
163
    }
164
    
165
    private final List<Attribute> attributes;
166
    
167
    public DALFile() {
168
        this.attributes = new ArrayList<>();
169
    }
170

    
171
    public boolean isEmpty() {
172
        if( CollectionUtils.isEmpty(this.attributes) ) {
173
            return true;
174
        }
175
        return false;
176
    }
177
    
178
    public void write(File f) {
179
        FileOutputStream out = null;
180
        try {
181
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
182
            PersistentState state = manager.getState(this);
183
            out = new FileOutputStream(f);
184
            manager.saveState(state, out);
185
        } catch(Exception ex) {
186
            throw new RuntimeException("Can't write DAL file.",ex);
187
        } finally {
188
            IOUtils.closeQuietly(out);
189
        }
190
    }
191

    
192
    public void read(File f) {
193
        FileInputStream in = null;
194
        try {
195
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
196
            in = new FileInputStream(f);
197
            DALFile x = (DALFile) manager.getObject(in);
198
            this.attributes.clear();
199
            this.attributes.addAll(x.attributes);
200
        } catch(Exception ex) {
201
            throw new RuntimeException("Can't read DAL file.",ex);
202
        } finally {
203
            IOUtils.closeQuietly(in);
204
        }
205
    }
206

    
207
    public void setStore(FeatureStore store) throws DataException {
208
        this.attributes.clear();
209
        
210
        List<FeatureType> types = store.getFeatureTypes();
211
        for( int n=0; n<types.size(); n++ ) {
212
            FeatureType type = types.get(n);
213
                for (FeatureAttributeDescriptor attrdesc : type) {
214
                    Attribute attribute = new Attribute(n,attrdesc);
215
                    if( attribute.needPersist() ) {
216
                        this.attributes.add(attribute);
217
                    }
218
            }
219
        }
220
    }
221

    
222
    public void updateStore(FeatureStore store) throws DataException {
223
        List<FeatureType> types = store.getFeatureTypes();
224
        Set<DefaultFeatureType> needFixTypes = new HashSet<>();
225
        
226
        for (Attribute attribute : this.attributes) {
227
            DefaultFeatureType ft = (DefaultFeatureType) types.get(attribute.featureTypeIndex);
228
            DefaultFeatureAttributeDescriptor attrdesc = (DefaultFeatureAttributeDescriptor) ft.get(attribute.name);
229
            if( ft.get(attribute.name)==null ) {
230
                attrdesc = new DefaultFeatureAttributeDescriptor(ft);
231
                attribute.fetch(attrdesc);
232
                ft.add(attrdesc);
233
            } else {
234
                attribute.fetch(attrdesc);
235
            }
236
            needFixTypes.add(ft);
237
        }
238
        for (DefaultFeatureType type : needFixTypes) {
239
            type.fixAll();
240
        }
241
    }
242

    
243
    @Override
244
    public void saveToState(PersistentState state) throws PersistenceException {
245
        state.set("attributes", attributes);
246
    }
247

    
248
    @Override
249
    public void loadFromState(PersistentState state) throws PersistenceException {
250
        this.attributes.clear();
251
        Iterator<Attribute> it = state.getIterator("attributes");
252
        while( it.hasNext() ) {
253
            Attribute attribute = it.next();
254
            this.attributes.add(attribute);
255
        }
256
    }
257

    
258
}