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