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 |
|
---|---|
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 |
} |
83 |
} |
84 |
|
85 |
|
86 |
public static class Attribute implements Persistent { |
87 |
|
88 |
private String name; |
89 |
private int type; |
90 |
private FeatureAttributeEmulator emulator;
|
91 |
private int featureTypeIndex; |
92 |
private RelativeInterval interval;
|
93 |
|
94 |
public Attribute() { |
95 |
|
96 |
} |
97 |
|
98 |
private Attribute(int featureTypeIndex, FeatureAttributeDescriptor attrdesc) { |
99 |
this.featureTypeIndex = featureTypeIndex;
|
100 |
this.name = attrdesc.getName();
|
101 |
this.type = attrdesc.getType();
|
102 |
this.emulator = attrdesc.getFeatureAttributeEmulator();
|
103 |
if( attrdesc.getInterval().isRelative() ) {
|
104 |
this.interval = (RelativeInterval) attrdesc.getInterval();
|
105 |
} else {
|
106 |
this.interval = null; |
107 |
} |
108 |
} |
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 |
|
120 |
@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 |
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 |
} |
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 |
if( state.get("intervalStart")!=null ) { |
142 |
this.interval = TimeSupportLocator.getManager().createRelativeInterval(
|
143 |
state.getLong("intervalStart"),
|
144 |
state.getLong("intervalEnd")
|
145 |
); |
146 |
} |
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 |
} |
155 |
|
156 |
|
157 |
} |
158 |
|
159 |
private final List<Attribute> attributes; |
160 |
|
161 |
public DALFile() {
|
162 |
this.attributes = new ArrayList<>(); |
163 |
} |
164 |
|
165 |
public boolean isEmpty() { |
166 |
if( CollectionUtils.isEmpty(this.attributes) ) { |
167 |
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 |
this.attributes.clear();
|
193 |
this.attributes.addAll(x.attributes);
|
194 |
} 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 |
this.attributes.clear();
|
203 |
|
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 |
Attribute attribute = new Attribute(n,attrdesc); |
209 |
if( attribute.needPersist() ) {
|
210 |
this.attributes.add(attribute);
|
211 |
} |
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 |
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 |
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 |
state.set("attributes", attributes);
|
240 |
} |
241 |
|
242 |
@Override
|
243 |
public void loadFromState(PersistentState state) throws PersistenceException { |
244 |
this.attributes.clear();
|
245 |
Iterator<Attribute> it = state.getIterator("attributes"); |
246 |
while( it.hasNext() ) {
|
247 |
Attribute attribute = it.next();
|
248 |
this.attributes.add(attribute);
|
249 |
} |
250 |
} |
251 |
|
252 |
} |