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 | 43954 | jjdelcerro | |
---|---|---|---|
2 | package org.gvsig.fmap.dal.feature.impl; |
||
3 | |||
4 | 44716 | jjdelcerro | import java.io.File; |
5 | import java.io.FileOutputStream; |
||
6 | 44160 | jjdelcerro | import java.io.InputStream; |
7 | import java.io.OutputStream; |
||
8 | 43954 | jjdelcerro | import java.util.ArrayList; |
9 | import java.util.List; |
||
10 | import org.apache.commons.collections4.CollectionUtils; |
||
11 | 44716 | jjdelcerro | import org.apache.commons.io.IOUtils; |
12 | 44128 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
13 | 43954 | jjdelcerro | 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 | 44190 | jjdelcerro | import org.slf4j.Logger; |
22 | import org.slf4j.LoggerFactory; |
||
23 | 43954 | jjdelcerro | |
24 | |||
25 | 44094 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
26 | 43954 | jjdelcerro | public class DALFile implements Persistent { |
27 | |||
28 | 44190 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(DALFile.class); |
29 | 43954 | jjdelcerro | |
30 | 44190 | jjdelcerro | private static final String DALFILE_PERSISTENCE_DEFINITION_NAME = "DALResources"; |
31 | |||
32 | 43954 | jjdelcerro | public static DALFile getDALFile() { |
33 | DALFile f = new DALFile();
|
||
34 | return f;
|
||
35 | } |
||
36 | |||
37 | 44297 | jjdelcerro | public static DALFile getDALFile(org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource resource) { |
38 | 43954 | jjdelcerro | DALFile df = new DALFile();
|
39 | 44160 | jjdelcerro | df.read(resource); |
40 | 43954 | jjdelcerro | 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 | 43978 | omartinez | DALFile.class, |
48 | 43954 | jjdelcerro | DALFILE_PERSISTENCE_DEFINITION_NAME, |
49 | DALFILE_PERSISTENCE_DEFINITION_NAME + " Persistent definition",
|
||
50 | null,
|
||
51 | null
|
||
52 | ); |
||
53 | 44190 | jjdelcerro | definition.addDynFieldString("defaultFeatureType");
|
54 | definition.addDynFieldList("featureTypes")
|
||
55 | .setClassOfItems(FeatureType.class) |
||
56 | 43954 | jjdelcerro | .setPersistent(true);
|
57 | } |
||
58 | } |
||
59 | |||
60 | |||
61 | 44190 | jjdelcerro | private final List<FeatureType> featureTypes; |
62 | private String defaultFeatureTypeId; |
||
63 | 43954 | jjdelcerro | |
64 | 43978 | omartinez | public DALFile() {
|
65 | 44190 | jjdelcerro | this.featureTypes = new ArrayList<>(); |
66 | 43954 | jjdelcerro | } |
67 | |||
68 | public boolean isEmpty() { |
||
69 | 44190 | jjdelcerro | if( CollectionUtils.isEmpty(this.featureTypes) ) { |
70 | 43954 | jjdelcerro | return true; |
71 | } |
||
72 | return false; |
||
73 | } |
||
74 | |||
75 | 44297 | jjdelcerro | public void write(org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource resource) { |
76 | 43954 | jjdelcerro | try {
|
77 | 44716 | jjdelcerro | 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 | 43954 | jjdelcerro | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
100 | PersistentState state = manager.getState(this);
|
||
101 | manager.saveState(state, out); |
||
102 | 44190 | jjdelcerro | } catch(Throwable ex) { |
103 | 44160 | jjdelcerro | throw new RuntimeException("Can't write DAL resource.",ex); |
104 | 43954 | jjdelcerro | } |
105 | } |
||
106 | |||
107 | 44297 | jjdelcerro | public void read(org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource resource) { |
108 | 43954 | jjdelcerro | try {
|
109 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
110 | 44160 | jjdelcerro | InputStream in = resource.asInputStream();
|
111 | 43954 | jjdelcerro | DALFile x = (DALFile) manager.getObject(in); |
112 | 44190 | jjdelcerro | this.featureTypes.clear();
|
113 | this.featureTypes.addAll(x.featureTypes);
|
||
114 | } catch(Throwable ex) { |
||
115 | throw new RuntimeException("Can't read DAL resource.",ex); |
||
116 | 43954 | jjdelcerro | } |
117 | } |
||
118 | 44328 | jjdelcerro | |
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 | 43954 | jjdelcerro | |
125 | 44190 | jjdelcerro | public void setStore(DefaultFeatureStore store) throws DataException { |
126 | this.featureTypes.clear();
|
||
127 | 43954 | jjdelcerro | |
128 | 44190 | jjdelcerro | List<FeatureType> theTypes = store.getFeatureTypes();
|
129 | for (FeatureType theType : theTypes) {
|
||
130 | this.featureTypes.add(theType.getCopy());
|
||
131 | 43954 | jjdelcerro | } |
132 | 44190 | jjdelcerro | this.defaultFeatureTypeId = store.getDefaultFeatureType().getId();
|
133 | 43954 | jjdelcerro | } |
134 | |||
135 | 44190 | jjdelcerro | 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 | 44094 | jjdelcerro | } |
144 | 43954 | jjdelcerro | } |
145 | 44190 | jjdelcerro | if( theTypes.isEmpty() ) {
|
146 | return;
|
||
147 | 43954 | jjdelcerro | } |
148 | 44190 | jjdelcerro | if( defaultFeatureType==null ) { |
149 | defaultFeatureType = theTypes.get(0);
|
||
150 | } |
||
151 | store.setFeatureTypes(theTypes, defaultFeatureType); |
||
152 | 43954 | jjdelcerro | } |
153 | |||
154 | @Override
|
||
155 | public void saveToState(PersistentState state) throws PersistenceException { |
||
156 | 44190 | jjdelcerro | state.set("defaultFeatureType", defaultFeatureTypeId);
|
157 | state.set("featureTypes", featureTypes);
|
||
158 | 43954 | jjdelcerro | } |
159 | |||
160 | @Override
|
||
161 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
162 | 44190 | jjdelcerro | 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 | 43954 | jjdelcerro | } |
173 | } |
||
174 | |||
175 | } |