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 @ 44189
History | View | Annotate | Download (9.7 KB)
1 |
|
---|---|
2 |
package org.gvsig.fmap.dal.feature.impl; |
3 |
|
4 |
import java.io.InputStream; |
5 |
import java.io.OutputStream; |
6 |
import java.util.ArrayList; |
7 |
import java.util.HashSet; |
8 |
import java.util.Iterator; |
9 |
import java.util.List; |
10 |
import java.util.Set; |
11 |
import org.apache.commons.collections4.CollectionUtils; |
12 |
import org.apache.commons.io.IOUtils; |
13 |
import org.apache.commons.lang3.StringUtils; |
14 |
import org.gvsig.fmap.dal.DataServerExplorer.DataResource; |
15 |
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 |
import org.gvsig.fmap.dal.feature.FeatureType; |
20 |
import org.gvsig.timesupport.RelativeInterval; |
21 |
import org.gvsig.timesupport.TimeSupportLocator; |
22 |
import org.gvsig.tools.ToolsLocator; |
23 |
import org.gvsig.tools.dynobject.DynStruct; |
24 |
import org.gvsig.tools.persistence.PersistenceManager; |
25 |
import org.gvsig.tools.persistence.Persistent; |
26 |
import org.gvsig.tools.persistence.PersistentState; |
27 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
28 |
|
29 |
|
30 |
@SuppressWarnings("UseSpecificCatch") |
31 |
public class DALFile implements Persistent { |
32 |
|
33 |
private static final String DALFILE_PERSISTENCE_DEFINITION_NAME = "DALFile"; |
34 |
private static final String ATTRIBUTE_PERSISTENCE_DEFINITION_NAME = "DALFileAttribute"; |
35 |
|
36 |
public static DALFile getDALFile() { |
37 |
DALFile f = new DALFile();
|
38 |
return f;
|
39 |
} |
40 |
|
41 |
public static DALFile getDALFile(DataResource resource) { |
42 |
DALFile df = new DALFile();
|
43 |
df.read(resource); |
44 |
return df;
|
45 |
} |
46 |
|
47 |
public static void registerPersistenceDefinition() { |
48 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
49 |
if (manager.getDefinition(DALFILE_PERSISTENCE_DEFINITION_NAME) == null) { |
50 |
DynStruct definition = manager.addDefinition( |
51 |
DALFile.class, |
52 |
DALFILE_PERSISTENCE_DEFINITION_NAME, |
53 |
DALFILE_PERSISTENCE_DEFINITION_NAME + " Persistent definition",
|
54 |
null,
|
55 |
null
|
56 |
); |
57 |
definition.addDynFieldList("attributes")
|
58 |
.setClassOfItems(Attribute.class)
|
59 |
.setMandatory(true)
|
60 |
.setPersistent(true);
|
61 |
} |
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 |
} |
94 |
|
95 |
|
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 |
} |
176 |
|
177 |
private final List<Attribute> attributes; |
178 |
|
179 |
public DALFile() {
|
180 |
this.attributes = new ArrayList<>(); |
181 |
} |
182 |
|
183 |
public boolean isEmpty() { |
184 |
if( CollectionUtils.isEmpty(this.attributes) ) { |
185 |
return true; |
186 |
} |
187 |
return false; |
188 |
} |
189 |
|
190 |
public void write(DataResource resource) { |
191 |
try {
|
192 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
193 |
PersistentState state = manager.getState(this);
|
194 |
OutputStream out = resource.asOutputStream();
|
195 |
manager.saveState(state, out); |
196 |
} catch(Exception ex) { |
197 |
throw new RuntimeException("Can't write DAL resource.",ex); |
198 |
} finally {
|
199 |
IOUtils.closeQuietly(resource); |
200 |
} |
201 |
} |
202 |
|
203 |
public void read(DataResource resource) { |
204 |
try {
|
205 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
206 |
InputStream in = resource.asInputStream();
|
207 |
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); |
214 |
} |
215 |
} |
216 |
|
217 |
public void setStore(FeatureStore store) throws DataException { |
218 |
this.attributes.clear();
|
219 |
|
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 |
} |
229 |
} |
230 |
} |
231 |
|
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); |
245 |
} |
246 |
needFixTypes.add(ft); |
247 |
} |
248 |
for (DefaultFeatureType type : needFixTypes) {
|
249 |
type.fixAll(); |
250 |
} |
251 |
} |
252 |
|
253 |
@Override
|
254 |
public void saveToState(PersistentState state) throws PersistenceException { |
255 |
state.set("attributes", attributes);
|
256 |
} |
257 |
|
258 |
@Override
|
259 |
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);
|
265 |
} |
266 |
} |
267 |
|
268 |
} |