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 @ 44094

History | View | Annotate | Download (8.82 KB)

1 43954 jjdelcerro
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 44094 jjdelcerro
import org.gvsig.timesupport.RelativeInterval;
20
import org.gvsig.timesupport.TimeSupportLocator;
21 43954 jjdelcerro
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 44094 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
30 43954 jjdelcerro
public class DALFile implements Persistent {
31
32
    private static final String DALFILE_PERSISTENCE_DEFINITION_NAME = "DALFile";
33 44094 jjdelcerro
    private static final String ATTRIBUTE_PERSISTENCE_DEFINITION_NAME = "DALFileAttribute";
34 43954 jjdelcerro
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 43978 omartinez
                DALFile.class,
51 43954 jjdelcerro
                DALFILE_PERSISTENCE_DEFINITION_NAME,
52
                DALFILE_PERSISTENCE_DEFINITION_NAME + " Persistent definition",
53
                null,
54
                null
55
            );
56 44094 jjdelcerro
            definition.addDynFieldList("attributes")
57
                .setClassOfItems(Attribute.class)
58 43954 jjdelcerro
                .setMandatory(true)
59
                .setPersistent(true);
60
        }
61 44094 jjdelcerro
        if (manager.getDefinition(ATTRIBUTE_PERSISTENCE_DEFINITION_NAME) == null) {
62 43954 jjdelcerro
            DynStruct definition = manager.addDefinition(
63 44094 jjdelcerro
                Attribute.class,
64
                ATTRIBUTE_PERSISTENCE_DEFINITION_NAME,
65
                ATTRIBUTE_PERSISTENCE_DEFINITION_NAME + " Persistent definition",
66 43954 jjdelcerro
                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
        }
83
    }
84
85
86 44094 jjdelcerro
    public static class Attribute implements Persistent {
87 43954 jjdelcerro
88
        private String name;
89
        private int type;
90
        private FeatureAttributeEmulator emulator;
91
        private int featureTypeIndex;
92 44094 jjdelcerro
        private RelativeInterval interval;
93 43954 jjdelcerro
94 44094 jjdelcerro
        public Attribute() {
95 43954 jjdelcerro
96
        }
97
98 44094 jjdelcerro
        private Attribute(int featureTypeIndex, FeatureAttributeDescriptor attrdesc) {
99 43954 jjdelcerro
            this.featureTypeIndex = featureTypeIndex;
100
            this.name = attrdesc.getName();
101
            this.type = attrdesc.getType();
102
            this.emulator = attrdesc.getFeatureAttributeEmulator();
103 44094 jjdelcerro
            if( attrdesc.getInterval().isRelative() ) {
104
                this.interval = (RelativeInterval) attrdesc.getInterval();
105
            } else {
106
                this.interval = null;
107
            }
108 43954 jjdelcerro
        }
109
110 44094 jjdelcerro
        public boolean needPersist() {
111
            if( emulator!= null && emulator instanceof Persistent ) {
112
                return true;
113
            }
114
            if( this.interval!=null ) {
115
                return true;
116
            }
117
            return false;
118
        }
119
120 43954 jjdelcerro
        @Override
121
        public void saveToState(PersistentState state) throws PersistenceException {
122
            state.set("name", name);
123
            state.set("type", type);
124
            state.set("featureTypeIndex", featureTypeIndex);
125
            state.set("emulator", emulator);
126 44094 jjdelcerro
            if( this.interval!=null ) {
127
                state.set("intervalStart", interval.getStart().toMillis());
128
                state.set("intervalEnd", interval.getEnd().toMillis());
129
            } else {
130
                state.setNull("intervalStart");
131
                state.setNull("intervalEnd");
132
            }
133 43954 jjdelcerro
        }
134
135
        @Override
136
        public void loadFromState(PersistentState state) throws PersistenceException {
137
            this.name = state.getString("name");
138
            this.type = state.getInt("type");
139
            this.featureTypeIndex = state.getInt("featureTypeIndex");
140
            this.emulator = (FeatureAttributeEmulator) state.get("emulator");
141 44094 jjdelcerro
            if( state.get("intervalStart")!=null ) {
142
                this.interval = TimeSupportLocator.getManager().createRelativeInterval(
143
                        state.getLong("intervalStart"),
144
                        state.getLong("intervalEnd")
145
                );
146
            }
147 43954 jjdelcerro
        }
148 44094 jjdelcerro
149
        private void fetch(DefaultFeatureAttributeDescriptor attrdesc) {
150
            attrdesc.setDataType(this.type);
151
            attrdesc.setName(this.name);
152
            attrdesc.setFeatureAttributeEmulator(this.emulator);
153
            attrdesc.setInterval(this.interval);
154
        }
155 43954 jjdelcerro
156
157
    }
158
159 44094 jjdelcerro
    private final List<Attribute> attributes;
160 43954 jjdelcerro
161 43978 omartinez
    public DALFile() {
162 44094 jjdelcerro
        this.attributes = new ArrayList<>();
163 43954 jjdelcerro
    }
164
165
    public boolean isEmpty() {
166 44094 jjdelcerro
        if( CollectionUtils.isEmpty(this.attributes) ) {
167 43954 jjdelcerro
            return true;
168
        }
169
        return false;
170
    }
171
172
    public void write(File f) {
173
        FileOutputStream out = null;
174
        try {
175
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
176
            PersistentState state = manager.getState(this);
177
            out = new FileOutputStream(f);
178
            manager.saveState(state, out);
179
        } catch(Exception ex) {
180
            throw new RuntimeException("Can't write DAL file.",ex);
181
        } finally {
182
            IOUtils.closeQuietly(out);
183
        }
184
    }
185
186
    public void read(File f) {
187
        FileInputStream in = null;
188
        try {
189
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
190
            in = new FileInputStream(f);
191
            DALFile x = (DALFile) manager.getObject(in);
192 44094 jjdelcerro
            this.attributes.clear();
193
            this.attributes.addAll(x.attributes);
194 43954 jjdelcerro
        } catch(Exception ex) {
195
            throw new RuntimeException("Can't read DAL file.",ex);
196
        } finally {
197
            IOUtils.closeQuietly(in);
198
        }
199
    }
200
201
    public void setStore(FeatureStore store) throws DataException {
202 44094 jjdelcerro
        this.attributes.clear();
203 43954 jjdelcerro
204
        List<FeatureType> types = store.getFeatureTypes();
205
        for( int n=0; n<types.size(); n++ ) {
206
            FeatureType type = types.get(n);
207
                for (FeatureAttributeDescriptor attrdesc : type) {
208 44094 jjdelcerro
                    Attribute attribute = new Attribute(n,attrdesc);
209
                    if( attribute.needPersist() ) {
210
                        this.attributes.add(attribute);
211 43954 jjdelcerro
                    }
212
            }
213
        }
214
    }
215
216
    public void updateStore(FeatureStore store) throws DataException {
217
        List<FeatureType> types = store.getFeatureTypes();
218
        Set<DefaultFeatureType> needFixTypes = new HashSet<>();
219
220 44094 jjdelcerro
        for (Attribute attribute : this.attributes) {
221
            DefaultFeatureType ft = (DefaultFeatureType) types.get(attribute.featureTypeIndex);
222
            DefaultFeatureAttributeDescriptor attrdesc = (DefaultFeatureAttributeDescriptor) ft.get(attribute.name);
223
            if( ft.get(attribute.name)==null ) {
224
                attrdesc = new DefaultFeatureAttributeDescriptor(ft);
225
                attribute.fetch(attrdesc);
226
                ft.add(attrdesc);
227
            } else {
228
                attribute.fetch(attrdesc);
229
            }
230 43954 jjdelcerro
            needFixTypes.add(ft);
231
        }
232
        for (DefaultFeatureType type : needFixTypes) {
233
            type.fixAll();
234
        }
235
    }
236
237
    @Override
238
    public void saveToState(PersistentState state) throws PersistenceException {
239 44094 jjdelcerro
        state.set("attributes", attributes);
240 43954 jjdelcerro
    }
241
242
    @Override
243
    public void loadFromState(PersistentState state) throws PersistenceException {
244 44094 jjdelcerro
        this.attributes.clear();
245
        Iterator<Attribute> it = state.getIterator("attributes");
246 43954 jjdelcerro
        while( it.hasNext() ) {
247 44094 jjdelcerro
            Attribute attribute = it.next();
248
            this.attributes.add(attribute);
249 43954 jjdelcerro
        }
250
    }
251
252
}