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 @ 44276
History | View | Annotate | Download (5 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.List; |
8 |
import org.apache.commons.collections4.CollectionUtils; |
9 |
import org.apache.commons.lang3.StringUtils; |
10 |
import org.gvsig.fmap.dal.exception.DataException; |
11 |
import org.gvsig.fmap.dal.feature.FeatureType; |
12 |
import org.gvsig.tools.ToolsLocator; |
13 |
import org.gvsig.tools.dynobject.DynStruct; |
14 |
import org.gvsig.tools.persistence.PersistenceManager; |
15 |
import org.gvsig.tools.persistence.Persistent; |
16 |
import org.gvsig.tools.persistence.PersistentState; |
17 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
18 |
import org.slf4j.Logger; |
19 |
import org.slf4j.LoggerFactory; |
20 |
|
21 |
|
22 |
@SuppressWarnings("UseSpecificCatch") |
23 |
public class DALFile implements Persistent { |
24 |
|
25 |
private static final Logger LOGGER = LoggerFactory.getLogger(DALFile.class); |
26 |
|
27 |
private static final String DALFILE_PERSISTENCE_DEFINITION_NAME = "DALResources"; |
28 |
|
29 |
public static DALFile getDALFile() { |
30 |
DALFile f = new DALFile();
|
31 |
return f;
|
32 |
} |
33 |
|
34 |
public static DALFile getDALFile(org.gvsig.tools.util.ResourcesStorage.Resource resource) { |
35 |
DALFile df = new DALFile();
|
36 |
df.read(resource); |
37 |
return df;
|
38 |
} |
39 |
|
40 |
public static void registerPersistenceDefinition() { |
41 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
42 |
if (manager.getDefinition(DALFILE_PERSISTENCE_DEFINITION_NAME) == null) { |
43 |
DynStruct definition = manager.addDefinition( |
44 |
DALFile.class, |
45 |
DALFILE_PERSISTENCE_DEFINITION_NAME, |
46 |
DALFILE_PERSISTENCE_DEFINITION_NAME + " Persistent definition",
|
47 |
null,
|
48 |
null
|
49 |
); |
50 |
definition.addDynFieldString("defaultFeatureType");
|
51 |
definition.addDynFieldList("featureTypes")
|
52 |
.setClassOfItems(FeatureType.class) |
53 |
.setPersistent(true);
|
54 |
} |
55 |
} |
56 |
|
57 |
|
58 |
private final List<FeatureType> featureTypes; |
59 |
private String defaultFeatureTypeId; |
60 |
|
61 |
public DALFile() {
|
62 |
this.featureTypes = new ArrayList<>(); |
63 |
} |
64 |
|
65 |
public boolean isEmpty() { |
66 |
if( CollectionUtils.isEmpty(this.featureTypes) ) { |
67 |
return true; |
68 |
} |
69 |
return false; |
70 |
} |
71 |
|
72 |
public void write(org.gvsig.tools.util.ResourcesStorage.Resource resource) { |
73 |
try {
|
74 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
75 |
PersistentState state = manager.getState(this);
|
76 |
OutputStream out = resource.asOutputStream();
|
77 |
manager.saveState(state, out); |
78 |
} catch(Throwable ex) { |
79 |
throw new RuntimeException("Can't write DAL resource.",ex); |
80 |
} |
81 |
} |
82 |
|
83 |
public void read(org.gvsig.tools.util.ResourcesStorage.Resource resource) { |
84 |
try {
|
85 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
86 |
InputStream in = resource.asInputStream();
|
87 |
DALFile x = (DALFile) manager.getObject(in); |
88 |
this.featureTypes.clear();
|
89 |
this.featureTypes.addAll(x.featureTypes);
|
90 |
} catch(Throwable ex) { |
91 |
throw new RuntimeException("Can't read DAL resource.",ex); |
92 |
} |
93 |
} |
94 |
|
95 |
public void setStore(DefaultFeatureStore store) throws DataException { |
96 |
this.featureTypes.clear();
|
97 |
|
98 |
List<FeatureType> theTypes = store.getFeatureTypes();
|
99 |
for (FeatureType theType : theTypes) {
|
100 |
this.featureTypes.add(theType.getCopy());
|
101 |
} |
102 |
this.defaultFeatureTypeId = store.getDefaultFeatureType().getId();
|
103 |
} |
104 |
|
105 |
public void updateStore(DefaultFeatureStore store) throws DataException { |
106 |
List<FeatureType> theTypes = new ArrayList<>(); |
107 |
FeatureType defaultFeatureType = null;
|
108 |
for (FeatureType type : this.featureTypes) { |
109 |
((DefaultFeatureType)type).setStore(store); |
110 |
theTypes.add(type); |
111 |
if( StringUtils.equals(defaultFeatureTypeId, type.getId()) ) {
|
112 |
defaultFeatureType = type; |
113 |
} |
114 |
} |
115 |
if( theTypes.isEmpty() ) {
|
116 |
return;
|
117 |
} |
118 |
if( defaultFeatureType==null ) { |
119 |
defaultFeatureType = theTypes.get(0);
|
120 |
} |
121 |
store.setFeatureTypes(theTypes, defaultFeatureType); |
122 |
} |
123 |
|
124 |
@Override
|
125 |
public void saveToState(PersistentState state) throws PersistenceException { |
126 |
state.set("defaultFeatureType", defaultFeatureTypeId);
|
127 |
state.set("featureTypes", featureTypes);
|
128 |
} |
129 |
|
130 |
@Override
|
131 |
public void loadFromState(PersistentState state) throws PersistenceException { |
132 |
try {
|
133 |
defaultFeatureTypeId = state.getString("defaultFeatureType");
|
134 |
List<FeatureType> theTypes = state.getList("featureTypes"); |
135 |
featureTypes.clear(); |
136 |
for (FeatureType theType : theTypes) {
|
137 |
this.featureTypes.add(theType);
|
138 |
} |
139 |
} catch(Throwable th) { |
140 |
LOGGER.warn("Can't load DAL resource.", th);
|
141 |
throw new PersistenceException(th); |
142 |
} |
143 |
} |
144 |
|
145 |
} |