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 @ 46078
History | View | Annotate | Download (6.46 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 | 45713 | jjdelcerro | import java.util.Objects; |
11 | 43954 | jjdelcerro | import org.apache.commons.collections4.CollectionUtils; |
12 | 44716 | jjdelcerro | import org.apache.commons.io.IOUtils; |
13 | 44128 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
14 | 43954 | jjdelcerro | 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 | 44190 | jjdelcerro | import org.slf4j.Logger; |
23 | import org.slf4j.LoggerFactory; |
||
24 | 43954 | jjdelcerro | |
25 | |||
26 | 44094 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
27 | 43954 | jjdelcerro | public class DALFile implements Persistent { |
28 | |||
29 | 44190 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(DALFile.class); |
30 | 43954 | jjdelcerro | |
31 | 44190 | jjdelcerro | private static final String DALFILE_PERSISTENCE_DEFINITION_NAME = "DALResources"; |
32 | |||
33 | 43954 | jjdelcerro | public static DALFile getDALFile() { |
34 | DALFile f = new DALFile();
|
||
35 | return f;
|
||
36 | } |
||
37 | |||
38 | 44297 | jjdelcerro | public static DALFile getDALFile(org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource resource) { |
39 | 43954 | jjdelcerro | DALFile df = new DALFile();
|
40 | 44160 | jjdelcerro | df.read(resource); |
41 | 43954 | jjdelcerro | 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 | 43978 | omartinez | DALFile.class, |
49 | 43954 | jjdelcerro | DALFILE_PERSISTENCE_DEFINITION_NAME, |
50 | DALFILE_PERSISTENCE_DEFINITION_NAME + " Persistent definition",
|
||
51 | null,
|
||
52 | null
|
||
53 | ); |
||
54 | 44190 | jjdelcerro | definition.addDynFieldString("defaultFeatureType");
|
55 | definition.addDynFieldList("featureTypes")
|
||
56 | .setClassOfItems(FeatureType.class) |
||
57 | 43954 | jjdelcerro | .setPersistent(true);
|
58 | } |
||
59 | } |
||
60 | |||
61 | |||
62 | 44190 | jjdelcerro | private final List<FeatureType> featureTypes; |
63 | private String defaultFeatureTypeId; |
||
64 | 43954 | jjdelcerro | |
65 | 43978 | omartinez | public DALFile() {
|
66 | 44190 | jjdelcerro | this.featureTypes = new ArrayList<>(); |
67 | 43954 | jjdelcerro | } |
68 | |||
69 | public boolean isEmpty() { |
||
70 | 44190 | jjdelcerro | if( CollectionUtils.isEmpty(this.featureTypes) ) { |
71 | 43954 | jjdelcerro | return true; |
72 | } |
||
73 | return false; |
||
74 | } |
||
75 | |||
76 | 44297 | jjdelcerro | public void write(org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource resource) { |
77 | 43954 | jjdelcerro | try {
|
78 | 44716 | jjdelcerro | 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 | 43954 | jjdelcerro | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
101 | PersistentState state = manager.getState(this);
|
||
102 | manager.saveState(state, out); |
||
103 | 44190 | jjdelcerro | } catch(Throwable ex) { |
104 | 44160 | jjdelcerro | throw new RuntimeException("Can't write DAL resource.",ex); |
105 | 43954 | jjdelcerro | } |
106 | } |
||
107 | |||
108 | 44297 | jjdelcerro | public void read(org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource resource) { |
109 | 45713 | jjdelcerro | if( resource==null ) { |
110 | throw new IllegalArgumentException("Resource null is not a valid resource"); |
||
111 | } |
||
112 | 43954 | jjdelcerro | try {
|
113 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
114 | 44160 | jjdelcerro | InputStream in = resource.asInputStream();
|
115 | 45713 | jjdelcerro | 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 | 43954 | jjdelcerro | DALFile x = (DALFile) manager.getObject(in); |
119 | 44190 | jjdelcerro | this.featureTypes.clear();
|
120 | this.featureTypes.addAll(x.featureTypes);
|
||
121 | } catch(Throwable ex) { |
||
122 | 45713 | jjdelcerro | throw new RuntimeException("Can't read DAL resource (resname="+resource.getName()+", resurl="+Objects.toString(resource.getURL())+", restype="+resource.getClass().getName()+").",ex); |
123 | 43954 | jjdelcerro | } |
124 | } |
||
125 | 44328 | jjdelcerro | |
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 | 43954 | jjdelcerro | |
132 | 44190 | jjdelcerro | public void setStore(DefaultFeatureStore store) throws DataException { |
133 | this.featureTypes.clear();
|
||
134 | 43954 | jjdelcerro | |
135 | 44190 | jjdelcerro | List<FeatureType> theTypes = store.getFeatureTypes();
|
136 | for (FeatureType theType : theTypes) {
|
||
137 | this.featureTypes.add(theType.getCopy());
|
||
138 | 43954 | jjdelcerro | } |
139 | 44190 | jjdelcerro | this.defaultFeatureTypeId = store.getDefaultFeatureType().getId();
|
140 | 43954 | jjdelcerro | } |
141 | |||
142 | 44190 | jjdelcerro | 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 | 44094 | jjdelcerro | } |
151 | 43954 | jjdelcerro | } |
152 | 44190 | jjdelcerro | if( theTypes.isEmpty() ) {
|
153 | return;
|
||
154 | 43954 | jjdelcerro | } |
155 | 44190 | jjdelcerro | if( defaultFeatureType==null ) { |
156 | defaultFeatureType = theTypes.get(0);
|
||
157 | } |
||
158 | store.setFeatureTypes(theTypes, defaultFeatureType); |
||
159 | 43954 | jjdelcerro | } |
160 | |||
161 | @Override
|
||
162 | public void saveToState(PersistentState state) throws PersistenceException { |
||
163 | 44190 | jjdelcerro | state.set("defaultFeatureType", defaultFeatureTypeId);
|
164 | state.set("featureTypes", featureTypes);
|
||
165 | 43954 | jjdelcerro | } |
166 | |||
167 | @Override
|
||
168 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
169 | 44190 | jjdelcerro | 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 | 43954 | jjdelcerro | } |
180 | } |
||
181 | |||
182 | } |