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