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
History | View | Annotate | Download (8.71 KB)
1 | 43954 | jjdelcerro | |
---|---|---|---|
2 | package org.gvsig.fmap.dal.feature.impl; |
||
3 | |||
4 | 46655 | jjdelcerro | import java.io.ByteArrayOutputStream; |
5 | 44716 | jjdelcerro | import java.io.File; |
6 | import java.io.FileOutputStream; |
||
7 | 44160 | jjdelcerro | import java.io.InputStream; |
8 | import java.io.OutputStream; |
||
9 | 46655 | jjdelcerro | import java.nio.charset.StandardCharsets; |
10 | 43954 | jjdelcerro | import java.util.ArrayList; |
11 | import java.util.List; |
||
12 | 45713 | jjdelcerro | import java.util.Objects; |
13 | 46655 | jjdelcerro | import org.apache.commons.codec.binary.Hex; |
14 | 43954 | jjdelcerro | import org.apache.commons.collections4.CollectionUtils; |
15 | 46655 | jjdelcerro | import org.apache.commons.io.FileUtils; |
16 | 44716 | jjdelcerro | import org.apache.commons.io.IOUtils; |
17 | 46706 | fdiaz | import org.apache.commons.lang3.ArrayUtils; |
18 | 44128 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
19 | 43954 | jjdelcerro | import org.gvsig.fmap.dal.exception.DataException; |
20 | 46706 | fdiaz | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
21 | import org.gvsig.fmap.dal.feature.FeatureStoreProviderFactory; |
||
22 | 43954 | jjdelcerro | 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 | 44190 | jjdelcerro | import org.slf4j.Logger; |
30 | import org.slf4j.LoggerFactory; |
||
31 | 43954 | jjdelcerro | |
32 | |||
33 | 44094 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
34 | 43954 | jjdelcerro | public class DALFile implements Persistent { |
35 | |||
36 | 44190 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(DALFile.class); |
37 | 43954 | jjdelcerro | |
38 | 44190 | jjdelcerro | private static final String DALFILE_PERSISTENCE_DEFINITION_NAME = "DALResources"; |
39 | |||
40 | 43954 | jjdelcerro | public static DALFile getDALFile() { |
41 | DALFile f = new DALFile();
|
||
42 | return f;
|
||
43 | } |
||
44 | |||
45 | 44297 | jjdelcerro | public static DALFile getDALFile(org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource resource) { |
46 | 43954 | jjdelcerro | DALFile df = new DALFile();
|
47 | 44160 | jjdelcerro | df.read(resource); |
48 | 43954 | jjdelcerro | 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 | 43978 | omartinez | DALFile.class, |
56 | 43954 | jjdelcerro | DALFILE_PERSISTENCE_DEFINITION_NAME, |
57 | DALFILE_PERSISTENCE_DEFINITION_NAME + " Persistent definition",
|
||
58 | null,
|
||
59 | null
|
||
60 | ); |
||
61 | 44190 | jjdelcerro | definition.addDynFieldString("defaultFeatureType");
|
62 | definition.addDynFieldList("featureTypes")
|
||
63 | .setClassOfItems(FeatureType.class) |
||
64 | 43954 | jjdelcerro | .setPersistent(true);
|
65 | } |
||
66 | } |
||
67 | |||
68 | |||
69 | 44190 | jjdelcerro | private final List<FeatureType> featureTypes; |
70 | private String defaultFeatureTypeId; |
||
71 | 43954 | jjdelcerro | |
72 | 43978 | omartinez | public DALFile() {
|
73 | 44190 | jjdelcerro | this.featureTypes = new ArrayList<>(); |
74 | 43954 | jjdelcerro | } |
75 | |||
76 | public boolean isEmpty() { |
||
77 | 44190 | jjdelcerro | if( CollectionUtils.isEmpty(this.featureTypes) ) { |
78 | 43954 | jjdelcerro | return true; |
79 | } |
||
80 | return false; |
||
81 | } |
||
82 | |||
83 | 44297 | jjdelcerro | public void write(org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource resource) { |
84 | 43954 | jjdelcerro | try {
|
85 | 44716 | jjdelcerro | 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 | 46655 | jjdelcerro | 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 | 44716 | jjdelcerro | } |
128 | } |
||
129 | |||
130 | public void write(OutputStream out) { |
||
131 | try {
|
||
132 | 43954 | jjdelcerro | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
133 | PersistentState state = manager.getState(this);
|
||
134 | manager.saveState(state, out); |
||
135 | 44190 | jjdelcerro | } catch(Throwable ex) { |
136 | 44160 | jjdelcerro | throw new RuntimeException("Can't write DAL resource.",ex); |
137 | 43954 | jjdelcerro | } |
138 | } |
||
139 | |||
140 | 44297 | jjdelcerro | public void read(org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource resource) { |
141 | 45713 | jjdelcerro | if( resource==null ) { |
142 | throw new IllegalArgumentException("Resource null is not a valid resource"); |
||
143 | } |
||
144 | 43954 | jjdelcerro | try {
|
145 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
146 | 44160 | jjdelcerro | InputStream in = resource.asInputStream();
|
147 | 45713 | jjdelcerro | 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 | 43954 | jjdelcerro | DALFile x = (DALFile) manager.getObject(in); |
151 | 44190 | jjdelcerro | this.featureTypes.clear();
|
152 | this.featureTypes.addAll(x.featureTypes);
|
||
153 | } catch(Throwable ex) { |
||
154 | 45713 | jjdelcerro | throw new RuntimeException("Can't read DAL resource (resname="+resource.getName()+", resurl="+Objects.toString(resource.getURL())+", restype="+resource.getClass().getName()+").",ex); |
155 | 43954 | jjdelcerro | } |
156 | } |
||
157 | 44328 | jjdelcerro | |
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 | 43954 | jjdelcerro | |
164 | 44190 | jjdelcerro | public void setStore(DefaultFeatureStore store) throws DataException { |
165 | this.featureTypes.clear();
|
||
166 | 43954 | jjdelcerro | |
167 | 44190 | jjdelcerro | List<FeatureType> theTypes = store.getFeatureTypes();
|
168 | for (FeatureType theType : theTypes) {
|
||
169 | 46706 | fdiaz | FeatureType ft = theType.getCopy(); |
170 | ft = fixOID(store, ft); |
||
171 | this.featureTypes.add(ft);
|
||
172 | 43954 | jjdelcerro | } |
173 | 44190 | jjdelcerro | this.defaultFeatureTypeId = store.getDefaultFeatureType().getId();
|
174 | 43954 | jjdelcerro | } |
175 | 46706 | fdiaz | |
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 | 43954 | jjdelcerro | |
187 | 46706 | fdiaz | } |
188 | |||
189 | 44190 | jjdelcerro | 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 | 44094 | jjdelcerro | } |
198 | 43954 | jjdelcerro | } |
199 | 44190 | jjdelcerro | if( theTypes.isEmpty() ) {
|
200 | return;
|
||
201 | 43954 | jjdelcerro | } |
202 | 44190 | jjdelcerro | if( defaultFeatureType==null ) { |
203 | defaultFeatureType = theTypes.get(0);
|
||
204 | } |
||
205 | 46706 | fdiaz | FeatureType ft = fixOID(store, defaultFeatureType); |
206 | if(theTypes.get(0) == defaultFeatureType) { |
||
207 | theTypes.set(0,ft);
|
||
208 | } |
||
209 | store.setFeatureTypes(theTypes, ft); |
||
210 | 43954 | jjdelcerro | } |
211 | |||
212 | 47080 | jjdelcerro | 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 | 43954 | jjdelcerro | @Override
|
224 | public void saveToState(PersistentState state) throws PersistenceException { |
||
225 | 44190 | jjdelcerro | state.set("defaultFeatureType", defaultFeatureTypeId);
|
226 | state.set("featureTypes", featureTypes);
|
||
227 | 43954 | jjdelcerro | } |
228 | |||
229 | @Override
|
||
230 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
231 | 44190 | jjdelcerro | 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 | 43954 | jjdelcerro | } |
242 | } |
||
243 | |||
244 | } |