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