Revision 44190 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

View differences:

DALFile.java
4 4
import java.io.InputStream;
5 5
import java.io.OutputStream;
6 6
import java.util.ArrayList;
7
import java.util.HashSet;
8
import java.util.Iterator;
9 7
import java.util.List;
10
import java.util.Set;
11 8
import org.apache.commons.collections4.CollectionUtils;
12
import org.apache.commons.io.IOUtils;
13 9
import org.apache.commons.lang3.StringUtils;
14 10
import org.gvsig.fmap.dal.DataServerExplorer.DataResource;
15 11
import org.gvsig.fmap.dal.exception.DataException;
16
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
17
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
18
import org.gvsig.fmap.dal.feature.FeatureStore;
19 12
import org.gvsig.fmap.dal.feature.FeatureType;
20
import org.gvsig.timesupport.RelativeInterval;
21
import org.gvsig.timesupport.TimeSupportLocator;
22 13
import org.gvsig.tools.ToolsLocator;
23 14
import org.gvsig.tools.dynobject.DynStruct;
24 15
import org.gvsig.tools.persistence.PersistenceManager;
25 16
import org.gvsig.tools.persistence.Persistent;
26 17
import org.gvsig.tools.persistence.PersistentState;
27 18
import org.gvsig.tools.persistence.exception.PersistenceException;
19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
28 21

  
29 22

  
30 23
@SuppressWarnings("UseSpecificCatch")
31 24
public class DALFile implements Persistent {
32 25

  
33
    private static final String DALFILE_PERSISTENCE_DEFINITION_NAME = "DALFile";
34
    private static final String ATTRIBUTE_PERSISTENCE_DEFINITION_NAME = "DALFileAttribute";
26
    private static final Logger LOGGER = LoggerFactory.getLogger(DALFile.class);
35 27
    
28
    private static final String DALFILE_PERSISTENCE_DEFINITION_NAME = "DALResources";
29
    
36 30
    public static DALFile getDALFile() {
37 31
        DALFile f = new DALFile();
38 32
        return f;
......
54 48
                null, 
55 49
                null
56 50
            );
57
            definition.addDynFieldList("attributes")
58
                .setClassOfItems(Attribute.class)
59
                .setMandatory(true)
51
            definition.addDynFieldString("defaultFeatureType");
52
            definition.addDynFieldList("featureTypes")
53
                .setClassOfItems(FeatureType.class)
60 54
                .setPersistent(true);
61 55
        }
62
        if (manager.getDefinition(ATTRIBUTE_PERSISTENCE_DEFINITION_NAME) == null) {
63
            DynStruct definition = manager.addDefinition(
64
                Attribute.class,
65
                ATTRIBUTE_PERSISTENCE_DEFINITION_NAME, 
66
                ATTRIBUTE_PERSISTENCE_DEFINITION_NAME + " Persistent definition", 
67
                null, 
68
                null
69
            );
70
            definition.addDynFieldString("name")
71
                .setMandatory(true)
72
                .setPersistent(true);
73
            definition.addDynFieldInt("type")
74
                .setMandatory(true)
75
                .setPersistent(true);
76
            definition.addDynFieldInt("featureTypeIndex")
77
                .setMandatory(true)
78
                .setPersistent(true);
79
            definition.addDynFieldObject("emulator")
80
                .setClassOfValue(FeatureAttributeEmulator.class)
81
                .setMandatory(false)
82
                .setPersistent(true);
83
            definition.addDynFieldLong("IntervalStart")
84
                .setMandatory(false)
85
                .setPersistent(true);
86
            definition.addDynFieldLong("IntervalEnd")
87
                .setMandatory(false)
88
                .setPersistent(true);
89
            definition.addDynFieldString("dataProfile")
90
                .setMandatory(false)
91
                .setPersistent(true);
92
        }
93 56
    }
94 57

  
95 58
    
96
    public static class Attribute implements Persistent {
97

  
98
        private String name;
99
        private int type;
100
        private FeatureAttributeEmulator emulator;
101
        private int featureTypeIndex;
102
        private RelativeInterval interval;
103
        private String dataProfile;
104
        
105
        public Attribute() {
106
            
107
        }
108

  
109
        private Attribute(int featureTypeIndex, FeatureAttributeDescriptor attrdesc) {
110
            this.featureTypeIndex = featureTypeIndex;
111
            this.name = attrdesc.getName();
112
            this.type = attrdesc.getType();
113
            this.emulator = attrdesc.getFeatureAttributeEmulator();
114
            if( attrdesc.getInterval()!=null && attrdesc.getInterval().isRelative() ) {
115
                this.interval = (RelativeInterval) attrdesc.getInterval();
116
            } else {
117
                this.interval = null;
118
            }
119
            this.dataProfile = attrdesc.getDataProfileName();
120
        }
121

  
122
        public boolean needPersist() {
123
            if( emulator!= null && emulator instanceof Persistent ) {
124
                return true;
125
            }
126
            if( this.interval!=null ) {
127
                return true;
128
            }
129
            if( !StringUtils.isBlank(this.dataProfile) ) {
130
                return true;
131
            }
132
            return false;
133
        }
134
        
135
        @Override
136
        public void saveToState(PersistentState state) throws PersistenceException {
137
            state.set("name", name);
138
            state.set("type", type);
139
            state.set("dataProfile", dataProfile);
140
            state.set("featureTypeIndex", featureTypeIndex);
141
            state.set("emulator", emulator);
142
            if( this.interval!=null ) {
143
                state.set("intervalStart", interval.getStart().toMillis());
144
                state.set("intervalEnd", interval.getEnd().toMillis());
145
            } else {
146
                state.setNull("intervalStart");
147
                state.setNull("intervalEnd");
148
            }
149
        }
150

  
151
        @Override
152
        public void loadFromState(PersistentState state) throws PersistenceException {
153
            this.name = state.getString("name");
154
            this.type = state.getInt("type");
155
            this.dataProfile = state.getString("dataProfile");
156
            this.featureTypeIndex = state.getInt("featureTypeIndex");
157
            this.emulator = (FeatureAttributeEmulator) state.get("emulator");
158
            if( state.get("intervalStart")!=null ) {
159
                this.interval = TimeSupportLocator.getManager().createRelativeInterval(
160
                        state.getLong("intervalStart"), 
161
                        state.getLong("intervalEnd")
162
                );
163
            }
164
        }
165

  
166
        private void fetch(DefaultFeatureAttributeDescriptor attrdesc) {
167
            attrdesc.setDataType(this.type);
168
            attrdesc.setName(this.name);
169
            attrdesc.setFeatureAttributeEmulator(this.emulator);
170
            attrdesc.setInterval(this.interval);
171
            attrdesc.setDataProfileName(this.dataProfile);
172
        }
173
        
174
        
175
    }
59
    private final List<FeatureType> featureTypes;
60
    private String defaultFeatureTypeId;
176 61
    
177
    private final List<Attribute> attributes;
178
    
179 62
    public DALFile() {
180
        this.attributes = new ArrayList<>();
63
        this.featureTypes = new ArrayList<>();
181 64
    }
182 65

  
183 66
    public boolean isEmpty() {
184
        if( CollectionUtils.isEmpty(this.attributes) ) {
67
        if( CollectionUtils.isEmpty(this.featureTypes) ) {
185 68
            return true;
186 69
        }
187 70
        return false;
......
193 76
            PersistentState state = manager.getState(this);
194 77
            OutputStream out = resource.asOutputStream();
195 78
            manager.saveState(state, out);
196
        } catch(Exception ex) {
79
        } catch(Throwable ex) {
197 80
            throw new RuntimeException("Can't write DAL resource.",ex);
198
        } finally {
199
            IOUtils.closeQuietly(resource);
200 81
        }
201 82
    }
202 83

  
......
205 86
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
206 87
            InputStream in = resource.asInputStream();
207 88
            DALFile x = (DALFile) manager.getObject(in);
208
            this.attributes.clear();
209
            this.attributes.addAll(x.attributes);
210
        } catch(Exception ex) {
211
            throw new RuntimeException("Can't read DAL file.",ex);
212
        } finally {
213
            IOUtils.closeQuietly(resource);
89
            this.featureTypes.clear();
90
            this.featureTypes.addAll(x.featureTypes);
91
        } catch(Throwable ex) {
92
            throw new RuntimeException("Can't read DAL resource.",ex);
214 93
        }
215 94
    }
216 95

  
217
    public void setStore(FeatureStore store) throws DataException {
218
        this.attributes.clear();
96
    public void setStore(DefaultFeatureStore store) throws DataException {
97
        this.featureTypes.clear();
219 98
        
220
        List<FeatureType> types = store.getFeatureTypes();
221
        for( int n=0; n<types.size(); n++ ) {
222
            FeatureType type = types.get(n);
223
                for (FeatureAttributeDescriptor attrdesc : type) {
224
                    Attribute attribute = new Attribute(n,attrdesc);
225
                    if( attribute.needPersist() ) {
226
                        this.attributes.add(attribute);
227
                    }
228
            }
99
        List<FeatureType> theTypes = store.getFeatureTypes();
100
        for (FeatureType theType : theTypes) {
101
            this.featureTypes.add(theType.getCopy());
229 102
        }
103
        this.defaultFeatureTypeId = store.getDefaultFeatureType().getId();
230 104
    }
231 105

  
232
    public void updateStore(FeatureStore store) throws DataException {
233
        List<FeatureType> types = store.getFeatureTypes();
234
        Set<DefaultFeatureType> needFixTypes = new HashSet<>();
235
        
236
        for (Attribute attribute : this.attributes) {
237
            DefaultFeatureType ft = (DefaultFeatureType) types.get(attribute.featureTypeIndex);
238
            DefaultFeatureAttributeDescriptor attrdesc = (DefaultFeatureAttributeDescriptor) ft.get(attribute.name);
239
            if( ft.get(attribute.name)==null ) {
240
                attrdesc = new DefaultFeatureAttributeDescriptor(ft);
241
                attribute.fetch(attrdesc);
242
                ft.add(attrdesc);
243
            } else {
244
                attribute.fetch(attrdesc);
106
    public void updateStore(DefaultFeatureStore store) throws DataException {
107
        List<FeatureType> theTypes = new ArrayList<>();
108
        FeatureType defaultFeatureType = null;
109
        for (FeatureType type : this.featureTypes) {
110
            ((DefaultFeatureType)type).setStore(store);
111
            theTypes.add(type);
112
            if( StringUtils.equals(defaultFeatureTypeId, type.getId()) ) {
113
                defaultFeatureType = type;
245 114
            }
246
            needFixTypes.add(ft);
247 115
        }
248
        for (DefaultFeatureType type : needFixTypes) {
249
            type.fixAll();
116
        if( theTypes.isEmpty() ) {
117
            return;
250 118
        }
119
        if( defaultFeatureType==null ) {
120
            defaultFeatureType = theTypes.get(0);
121
        }
122
        store.setFeatureTypes(theTypes, defaultFeatureType);
251 123
    }
252 124

  
253 125
    @Override
254 126
    public void saveToState(PersistentState state) throws PersistenceException {
255
        state.set("attributes", attributes);
127
        state.set("defaultFeatureType", defaultFeatureTypeId);
128
        state.set("featureTypes", featureTypes);
256 129
    }
257 130

  
258 131
    @Override
259 132
    public void loadFromState(PersistentState state) throws PersistenceException {
260
        this.attributes.clear();
261
        Iterator<Attribute> it = state.getIterator("attributes");
262
        while( it.hasNext() ) {
263
            Attribute attribute = it.next();
264
            this.attributes.add(attribute);
133
        try {
134
            defaultFeatureTypeId = state.getString("defaultFeatureType");
135
            List<FeatureType> theTypes = state.getList("featureTypes");
136
            featureTypes.clear();
137
            for (FeatureType theType : theTypes) {
138
                this.featureTypes.add(theType);
139
            }
140
        } catch(Throwable th) {
141
            LOGGER.warn("Can't load DAL resource.", th);
142
            throw new PersistenceException(th);
265 143
        }
266 144
    }
267 145

  

Also available in: Unified diff