Revision 44094 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
16 16
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
17 17
import org.gvsig.fmap.dal.feature.FeatureStore;
18 18
import org.gvsig.fmap.dal.feature.FeatureType;
19
import org.gvsig.timesupport.RelativeInterval;
20
import org.gvsig.timesupport.TimeSupportLocator;
19 21
import org.gvsig.tools.ToolsLocator;
20 22
import org.gvsig.tools.dynobject.DynStruct;
21 23
import org.gvsig.tools.persistence.PersistenceManager;
......
24 26
import org.gvsig.tools.persistence.exception.PersistenceException;
25 27

  
26 28

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

  
29 32
    private static final String DALFILE_PERSISTENCE_DEFINITION_NAME = "DALFile";
30
    private static final String CALCULATEDATTRIBUTE_PERSISTENCE_DEFINITION_NAME = "DALFileCalculatedAttribute";
33
    private static final String ATTRIBUTE_PERSISTENCE_DEFINITION_NAME = "DALFileAttribute";
31 34
    
32 35
    public static DALFile getDALFile() {
33 36
        DALFile f = new DALFile();
......
50 53
                null, 
51 54
                null
52 55
            );
53
            definition.addDynFieldList("calculatedAttributes")
54
                .setClassOfItems(CalculatedAttribute.class)
56
            definition.addDynFieldList("attributes")
57
                .setClassOfItems(Attribute.class)
55 58
                .setMandatory(true)
56 59
                .setPersistent(true);
57 60
        }
58
        if (manager.getDefinition(CALCULATEDATTRIBUTE_PERSISTENCE_DEFINITION_NAME) == null) {
61
        if (manager.getDefinition(ATTRIBUTE_PERSISTENCE_DEFINITION_NAME) == null) {
59 62
            DynStruct definition = manager.addDefinition(
60
                CalculatedAttribute.class,
61
                CALCULATEDATTRIBUTE_PERSISTENCE_DEFINITION_NAME, 
62
                CALCULATEDATTRIBUTE_PERSISTENCE_DEFINITION_NAME + " Persistent definition", 
63
                Attribute.class,
64
                ATTRIBUTE_PERSISTENCE_DEFINITION_NAME, 
65
                ATTRIBUTE_PERSISTENCE_DEFINITION_NAME + " Persistent definition", 
63 66
                null, 
64 67
                null
65 68
            );
......
80 83
    }
81 84

  
82 85
    
83
    public static class CalculatedAttribute implements Persistent {
86
    public static class Attribute implements Persistent {
84 87

  
85 88
        private String name;
86 89
        private int type;
87 90
        private FeatureAttributeEmulator emulator;
88 91
        private int featureTypeIndex;
92
        private RelativeInterval interval;
89 93
        
90
        public CalculatedAttribute() {
94
        public Attribute() {
91 95
            
92 96
        }
93 97

  
94
        private CalculatedAttribute(int featureTypeIndex, FeatureAttributeDescriptor attrdesc) {
98
        private Attribute(int featureTypeIndex, FeatureAttributeDescriptor attrdesc) {
95 99
            this.featureTypeIndex = featureTypeIndex;
96 100
            this.name = attrdesc.getName();
97 101
            this.type = attrdesc.getType();
98 102
            this.emulator = attrdesc.getFeatureAttributeEmulator();
103
            if( attrdesc.getInterval().isRelative() ) {
104
                this.interval = (RelativeInterval) attrdesc.getInterval();
105
            } else {
106
                this.interval = null;
107
            }
99 108
        }
100 109

  
110
        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
        
101 120
        @Override
102 121
        public void saveToState(PersistentState state) throws PersistenceException {
103 122
            state.set("name", name);
104 123
            state.set("type", type);
105 124
            state.set("featureTypeIndex", featureTypeIndex);
106 125
            state.set("emulator", emulator);
126
            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
            }
107 133
        }
108 134

  
109 135
        @Override
......
112 138
            this.type = state.getInt("type");
113 139
            this.featureTypeIndex = state.getInt("featureTypeIndex");
114 140
            this.emulator = (FeatureAttributeEmulator) state.get("emulator");
141
            if( state.get("intervalStart")!=null ) {
142
                this.interval = TimeSupportLocator.getManager().createRelativeInterval(
143
                        state.getLong("intervalStart"), 
144
                        state.getLong("intervalEnd")
145
                );
146
            }
115 147
        }
148

  
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
        }
116 155
        
117 156
        
118 157
    }
119 158
    
120
    private final List<CalculatedAttribute> calculatedAttributes;
159
    private final List<Attribute> attributes;
121 160
    
122 161
    public DALFile() {
123
        this.calculatedAttributes = new ArrayList<>();
162
        this.attributes = new ArrayList<>();
124 163
    }
125 164

  
126 165
    public boolean isEmpty() {
127
        if( CollectionUtils.isEmpty(this.calculatedAttributes) ) {
166
        if( CollectionUtils.isEmpty(this.attributes) ) {
128 167
            return true;
129 168
        }
130 169
        return false;
......
144 183
        }
145 184
    }
146 185

  
147
    @SuppressWarnings("UseSpecificCatch")
148 186
    public void read(File f) {
149 187
        FileInputStream in = null;
150 188
        try {
151 189
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
152 190
            in = new FileInputStream(f);
153 191
            DALFile x = (DALFile) manager.getObject(in);
154
            this.calculatedAttributes.clear();
155
            this.calculatedAttributes.addAll(x.calculatedAttributes);
192
            this.attributes.clear();
193
            this.attributes.addAll(x.attributes);
156 194
        } catch(Exception ex) {
157 195
            throw new RuntimeException("Can't read DAL file.",ex);
158 196
        } finally {
......
161 199
    }
162 200

  
163 201
    public void setStore(FeatureStore store) throws DataException {
164
        this.calculatedAttributes.clear();
202
        this.attributes.clear();
165 203
        
166 204
        List<FeatureType> types = store.getFeatureTypes();
167 205
        for( int n=0; n<types.size(); n++ ) {
168 206
            FeatureType type = types.get(n);
169 207
                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);
208
                    Attribute attribute = new Attribute(n,attrdesc);
209
                    if( attribute.needPersist() ) {
210
                        this.attributes.add(attribute);
174 211
                    }
175 212
            }
176 213
        }
......
180 217
        List<FeatureType> types = store.getFeatureTypes();
181 218
        Set<DefaultFeatureType> needFixTypes = new HashSet<>();
182 219
        
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);
220
        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
            }
190 230
            needFixTypes.add(ft);
191 231
        }
192 232
        for (DefaultFeatureType type : needFixTypes) {
......
196 236

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

  
202 242
    @Override
203 243
    public void loadFromState(PersistentState state) throws PersistenceException {
204
        this.calculatedAttributes.clear();
205
        Iterator<CalculatedAttribute> it = state.getIterator("calculatedAttributes");
244
        this.attributes.clear();
245
        Iterator<Attribute> it = state.getIterator("attributes");
206 246
        while( it.hasNext() ) {
207
            CalculatedAttribute calculatedAttribute = it.next();
208
            this.calculatedAttributes.add(calculatedAttribute);
247
            Attribute attribute = it.next();
248
            this.attributes.add(attribute);
209 249
        }
210 250
    }
211 251

  

Also available in: Unified diff