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 @ 47080
History | View | Annotate | Download (8.71 KB)
1 |
|
---|---|
2 |
package org.gvsig.fmap.dal.feature.impl; |
3 |
|
4 |
import java.io.ByteArrayOutputStream; |
5 |
import java.io.File; |
6 |
import java.io.FileOutputStream; |
7 |
import java.io.InputStream; |
8 |
import java.io.OutputStream; |
9 |
import java.nio.charset.StandardCharsets; |
10 |
import java.util.ArrayList; |
11 |
import java.util.List; |
12 |
import java.util.Objects; |
13 |
import org.apache.commons.codec.binary.Hex; |
14 |
import org.apache.commons.collections4.CollectionUtils; |
15 |
import org.apache.commons.io.FileUtils; |
16 |
import org.apache.commons.io.IOUtils; |
17 |
import org.apache.commons.lang3.ArrayUtils; |
18 |
import org.apache.commons.lang3.StringUtils; |
19 |
import org.gvsig.fmap.dal.exception.DataException; |
20 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
21 |
import org.gvsig.fmap.dal.feature.FeatureStoreProviderFactory; |
22 |
import org.gvsig.fmap.dal.feature.FeatureType; |
23 |
import org.gvsig.tools.ToolsLocator; |
24 |
import org.gvsig.tools.dynobject.DynStruct; |
25 |
import org.gvsig.tools.persistence.PersistenceManager; |
26 |
import org.gvsig.tools.persistence.Persistent; |
27 |
import org.gvsig.tools.persistence.PersistentState; |
28 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
29 |
import org.slf4j.Logger; |
30 |
import org.slf4j.LoggerFactory; |
31 |
|
32 |
|
33 |
@SuppressWarnings("UseSpecificCatch") |
34 |
public class DALFile implements Persistent { |
35 |
|
36 |
private static final Logger LOGGER = LoggerFactory.getLogger(DALFile.class); |
37 |
|
38 |
private static final String DALFILE_PERSISTENCE_DEFINITION_NAME = "DALResources"; |
39 |
|
40 |
public static DALFile getDALFile() { |
41 |
DALFile f = new DALFile();
|
42 |
return f;
|
43 |
} |
44 |
|
45 |
public static DALFile getDALFile(org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource resource) { |
46 |
DALFile df = new DALFile();
|
47 |
df.read(resource); |
48 |
return df;
|
49 |
} |
50 |
|
51 |
public static void registerPersistenceDefinition() { |
52 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
53 |
if (manager.getDefinition(DALFILE_PERSISTENCE_DEFINITION_NAME) == null) { |
54 |
DynStruct definition = manager.addDefinition( |
55 |
DALFile.class, |
56 |
DALFILE_PERSISTENCE_DEFINITION_NAME, |
57 |
DALFILE_PERSISTENCE_DEFINITION_NAME + " Persistent definition",
|
58 |
null,
|
59 |
null
|
60 |
); |
61 |
definition.addDynFieldString("defaultFeatureType");
|
62 |
definition.addDynFieldList("featureTypes")
|
63 |
.setClassOfItems(FeatureType.class) |
64 |
.setPersistent(true);
|
65 |
} |
66 |
} |
67 |
|
68 |
|
69 |
private final List<FeatureType> featureTypes; |
70 |
private String defaultFeatureTypeId; |
71 |
|
72 |
public DALFile() {
|
73 |
this.featureTypes = new ArrayList<>(); |
74 |
} |
75 |
|
76 |
public boolean isEmpty() { |
77 |
if( CollectionUtils.isEmpty(this.featureTypes) ) { |
78 |
return true; |
79 |
} |
80 |
return false; |
81 |
} |
82 |
|
83 |
public void write(org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource resource) { |
84 |
try {
|
85 |
OutputStream out = resource.asOutputStream();
|
86 |
this.write(out);
|
87 |
// Don't close the output stream
|
88 |
} catch(Throwable ex) { |
89 |
throw new RuntimeException("Can't write DAL resource.",ex); |
90 |
} |
91 |
} |
92 |
|
93 |
public void write(File outfile) { |
94 |
this.write(outfile, "bin"); |
95 |
} |
96 |
|
97 |
public void write(File outfile, String format) { |
98 |
switch(StringUtils.upperCase(format) ) {
|
99 |
case "HEX": |
100 |
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
101 |
try {
|
102 |
this.write(buffer);
|
103 |
FileUtils.write( |
104 |
outfile, |
105 |
Hex.encodeHexString(buffer.toByteArray()), |
106 |
StandardCharsets.UTF_8 |
107 |
); |
108 |
} catch(Throwable ex) { |
109 |
throw new RuntimeException("Can't write DAL file.",ex); |
110 |
} finally {
|
111 |
IOUtils.closeQuietly(buffer); |
112 |
} |
113 |
break;
|
114 |
|
115 |
case "BIN": |
116 |
default:
|
117 |
OutputStream out = null; |
118 |
try {
|
119 |
out = new FileOutputStream(outfile); |
120 |
this.write(out);
|
121 |
} catch(Throwable ex) { |
122 |
throw new RuntimeException("Can't write DAL file.",ex); |
123 |
} finally {
|
124 |
IOUtils.closeQuietly(out); |
125 |
} |
126 |
break;
|
127 |
} |
128 |
} |
129 |
|
130 |
public void write(OutputStream out) { |
131 |
try {
|
132 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
133 |
PersistentState state = manager.getState(this);
|
134 |
manager.saveState(state, out); |
135 |
} catch(Throwable ex) { |
136 |
throw new RuntimeException("Can't write DAL resource.",ex); |
137 |
} |
138 |
} |
139 |
|
140 |
public void read(org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource resource) { |
141 |
if( resource==null ) { |
142 |
throw new IllegalArgumentException("Resource null is not a valid resource"); |
143 |
} |
144 |
try {
|
145 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
146 |
InputStream in = resource.asInputStream();
|
147 |
if( in == null ) { |
148 |
throw new RuntimeException("Can't read DAL resource, input stream is null (resname="+resource.getName()+", resurl="+Objects.toString(resource.getURL())+", restype="+resource.getClass().getName()+")."); |
149 |
} |
150 |
DALFile x = (DALFile) manager.getObject(in); |
151 |
this.featureTypes.clear();
|
152 |
this.featureTypes.addAll(x.featureTypes);
|
153 |
} catch(Throwable ex) { |
154 |
throw new RuntimeException("Can't read DAL resource (resname="+resource.getName()+", resurl="+Objects.toString(resource.getURL())+", restype="+resource.getClass().getName()+").",ex); |
155 |
} |
156 |
} |
157 |
|
158 |
public void setFeatureType(FeatureType featureType) throws DataException { |
159 |
this.featureTypes.clear();
|
160 |
this.featureTypes.add(featureType.getCopy());
|
161 |
this.defaultFeatureTypeId = featureType.getId();
|
162 |
} |
163 |
|
164 |
public void setStore(DefaultFeatureStore store) throws DataException { |
165 |
this.featureTypes.clear();
|
166 |
|
167 |
List<FeatureType> theTypes = store.getFeatureTypes();
|
168 |
for (FeatureType theType : theTypes) {
|
169 |
FeatureType ft = theType.getCopy(); |
170 |
ft = fixOID(store, ft); |
171 |
this.featureTypes.add(ft);
|
172 |
} |
173 |
this.defaultFeatureTypeId = store.getDefaultFeatureType().getId();
|
174 |
} |
175 |
|
176 |
private FeatureType fixOID(DefaultFeatureStore store, FeatureType featureType) {
|
177 |
if (((FeatureStoreProviderFactory) store.getProviderFactory()).supportNumericOID()) {
|
178 |
if (!featureType.hasOID() && ArrayUtils.isEmpty(featureType.getPrimaryKey())) {
|
179 |
EditableFeatureType eft = featureType.getEditable(); |
180 |
eft.setHasOID(true);
|
181 |
FeatureType necft = eft.getNotEditableCopy(); |
182 |
return necft;
|
183 |
} |
184 |
} |
185 |
return featureType;
|
186 |
|
187 |
} |
188 |
|
189 |
public void updateStore(DefaultFeatureStore store) throws DataException { |
190 |
List<FeatureType> theTypes = new ArrayList<>(); |
191 |
FeatureType defaultFeatureType = null;
|
192 |
for (FeatureType type : this.featureTypes) { |
193 |
((DefaultFeatureType)type).setStore(store); |
194 |
theTypes.add(type); |
195 |
if( StringUtils.equals(defaultFeatureTypeId, type.getId()) ) {
|
196 |
defaultFeatureType = type; |
197 |
} |
198 |
} |
199 |
if( theTypes.isEmpty() ) {
|
200 |
return;
|
201 |
} |
202 |
if( defaultFeatureType==null ) { |
203 |
defaultFeatureType = theTypes.get(0);
|
204 |
} |
205 |
FeatureType ft = fixOID(store, defaultFeatureType); |
206 |
if(theTypes.get(0) == defaultFeatureType) { |
207 |
theTypes.set(0,ft);
|
208 |
} |
209 |
store.setFeatureTypes(theTypes, ft); |
210 |
} |
211 |
|
212 |
public List<FeatureType> getFeatureTypes() throws DataException { |
213 |
List<FeatureType> theTypes = new ArrayList<>(); |
214 |
for (FeatureType type : this.featureTypes) { |
215 |
theTypes.add(type); |
216 |
} |
217 |
if( theTypes.isEmpty() ) {
|
218 |
return null; |
219 |
} |
220 |
return theTypes;
|
221 |
} |
222 |
|
223 |
@Override
|
224 |
public void saveToState(PersistentState state) throws PersistenceException { |
225 |
state.set("defaultFeatureType", defaultFeatureTypeId);
|
226 |
state.set("featureTypes", featureTypes);
|
227 |
} |
228 |
|
229 |
@Override
|
230 |
public void loadFromState(PersistentState state) throws PersistenceException { |
231 |
try {
|
232 |
defaultFeatureTypeId = state.getString("defaultFeatureType");
|
233 |
List<FeatureType> theTypes = state.getList("featureTypes"); |
234 |
featureTypes.clear(); |
235 |
for (FeatureType theType : theTypes) {
|
236 |
this.featureTypes.add(theType);
|
237 |
} |
238 |
} catch(Throwable th) { |
239 |
LOGGER.warn("Can't load DAL resource.", th);
|
240 |
throw new PersistenceException(th); |
241 |
} |
242 |
} |
243 |
|
244 |
} |