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 @ 44500
History | View | Annotate | Download (5.27 KB)
1 | 43954 | jjdelcerro | |
---|---|---|---|
2 | package org.gvsig.fmap.dal.feature.impl; |
||
3 | |||
4 | 44160 | jjdelcerro | import java.io.InputStream; |
5 | import java.io.OutputStream; |
||
6 | 43954 | jjdelcerro | import java.util.ArrayList; |
7 | import java.util.List; |
||
8 | import org.apache.commons.collections4.CollectionUtils; |
||
9 | 44128 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
10 | 43954 | jjdelcerro | import org.gvsig.fmap.dal.exception.DataException; |
11 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
12 | import org.gvsig.tools.ToolsLocator; |
||
13 | import org.gvsig.tools.dynobject.DynStruct; |
||
14 | import org.gvsig.tools.persistence.PersistenceManager; |
||
15 | import org.gvsig.tools.persistence.Persistent; |
||
16 | import org.gvsig.tools.persistence.PersistentState; |
||
17 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
18 | 44190 | jjdelcerro | import org.slf4j.Logger; |
19 | import org.slf4j.LoggerFactory; |
||
20 | 43954 | jjdelcerro | |
21 | |||
22 | 44094 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
23 | 43954 | jjdelcerro | public class DALFile implements Persistent { |
24 | |||
25 | 44190 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(DALFile.class); |
26 | 43954 | jjdelcerro | |
27 | 44190 | jjdelcerro | private static final String DALFILE_PERSISTENCE_DEFINITION_NAME = "DALResources"; |
28 | |||
29 | 43954 | jjdelcerro | public static DALFile getDALFile() { |
30 | DALFile f = new DALFile();
|
||
31 | return f;
|
||
32 | } |
||
33 | |||
34 | 44297 | jjdelcerro | public static DALFile getDALFile(org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource resource) { |
35 | 43954 | jjdelcerro | DALFile df = new DALFile();
|
36 | 44160 | jjdelcerro | df.read(resource); |
37 | 43954 | jjdelcerro | return df;
|
38 | } |
||
39 | |||
40 | public static void registerPersistenceDefinition() { |
||
41 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
42 | if (manager.getDefinition(DALFILE_PERSISTENCE_DEFINITION_NAME) == null) { |
||
43 | DynStruct definition = manager.addDefinition( |
||
44 | 43978 | omartinez | DALFile.class, |
45 | 43954 | jjdelcerro | DALFILE_PERSISTENCE_DEFINITION_NAME, |
46 | DALFILE_PERSISTENCE_DEFINITION_NAME + " Persistent definition",
|
||
47 | null,
|
||
48 | null
|
||
49 | ); |
||
50 | 44190 | jjdelcerro | definition.addDynFieldString("defaultFeatureType");
|
51 | definition.addDynFieldList("featureTypes")
|
||
52 | .setClassOfItems(FeatureType.class) |
||
53 | 43954 | jjdelcerro | .setPersistent(true);
|
54 | } |
||
55 | } |
||
56 | |||
57 | |||
58 | 44190 | jjdelcerro | private final List<FeatureType> featureTypes; |
59 | private String defaultFeatureTypeId; |
||
60 | 43954 | jjdelcerro | |
61 | 43978 | omartinez | public DALFile() {
|
62 | 44190 | jjdelcerro | this.featureTypes = new ArrayList<>(); |
63 | 43954 | jjdelcerro | } |
64 | |||
65 | public boolean isEmpty() { |
||
66 | 44190 | jjdelcerro | if( CollectionUtils.isEmpty(this.featureTypes) ) { |
67 | 43954 | jjdelcerro | return true; |
68 | } |
||
69 | return false; |
||
70 | } |
||
71 | |||
72 | 44297 | jjdelcerro | public void write(org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource resource) { |
73 | 43954 | jjdelcerro | try {
|
74 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
75 | PersistentState state = manager.getState(this);
|
||
76 | 44160 | jjdelcerro | OutputStream out = resource.asOutputStream();
|
77 | 43954 | jjdelcerro | manager.saveState(state, out); |
78 | 44190 | jjdelcerro | } catch(Throwable ex) { |
79 | 44160 | jjdelcerro | throw new RuntimeException("Can't write DAL resource.",ex); |
80 | 43954 | jjdelcerro | } |
81 | } |
||
82 | |||
83 | 44297 | jjdelcerro | public void read(org.gvsig.tools.resourcesstorage.ResourcesStorage.Resource resource) { |
84 | 43954 | jjdelcerro | try {
|
85 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
86 | 44160 | jjdelcerro | InputStream in = resource.asInputStream();
|
87 | 43954 | jjdelcerro | DALFile x = (DALFile) manager.getObject(in); |
88 | 44190 | jjdelcerro | this.featureTypes.clear();
|
89 | this.featureTypes.addAll(x.featureTypes);
|
||
90 | } catch(Throwable ex) { |
||
91 | throw new RuntimeException("Can't read DAL resource.",ex); |
||
92 | 43954 | jjdelcerro | } |
93 | } |
||
94 | 44328 | jjdelcerro | |
95 | public void setFeatureType(FeatureType featureType) throws DataException { |
||
96 | this.featureTypes.clear();
|
||
97 | this.featureTypes.add(featureType.getCopy());
|
||
98 | this.defaultFeatureTypeId = featureType.getId();
|
||
99 | } |
||
100 | 43954 | jjdelcerro | |
101 | 44190 | jjdelcerro | public void setStore(DefaultFeatureStore store) throws DataException { |
102 | this.featureTypes.clear();
|
||
103 | 43954 | jjdelcerro | |
104 | 44190 | jjdelcerro | List<FeatureType> theTypes = store.getFeatureTypes();
|
105 | for (FeatureType theType : theTypes) {
|
||
106 | this.featureTypes.add(theType.getCopy());
|
||
107 | 43954 | jjdelcerro | } |
108 | 44190 | jjdelcerro | this.defaultFeatureTypeId = store.getDefaultFeatureType().getId();
|
109 | 43954 | jjdelcerro | } |
110 | |||
111 | 44190 | jjdelcerro | public void updateStore(DefaultFeatureStore store) throws DataException { |
112 | List<FeatureType> theTypes = new ArrayList<>(); |
||
113 | FeatureType defaultFeatureType = null;
|
||
114 | for (FeatureType type : this.featureTypes) { |
||
115 | ((DefaultFeatureType)type).setStore(store); |
||
116 | theTypes.add(type); |
||
117 | if( StringUtils.equals(defaultFeatureTypeId, type.getId()) ) {
|
||
118 | defaultFeatureType = type; |
||
119 | 44094 | jjdelcerro | } |
120 | 43954 | jjdelcerro | } |
121 | 44190 | jjdelcerro | if( theTypes.isEmpty() ) {
|
122 | return;
|
||
123 | 43954 | jjdelcerro | } |
124 | 44190 | jjdelcerro | if( defaultFeatureType==null ) { |
125 | defaultFeatureType = theTypes.get(0);
|
||
126 | } |
||
127 | store.setFeatureTypes(theTypes, defaultFeatureType); |
||
128 | 43954 | jjdelcerro | } |
129 | |||
130 | @Override
|
||
131 | public void saveToState(PersistentState state) throws PersistenceException { |
||
132 | 44190 | jjdelcerro | state.set("defaultFeatureType", defaultFeatureTypeId);
|
133 | state.set("featureTypes", featureTypes);
|
||
134 | 43954 | jjdelcerro | } |
135 | |||
136 | @Override
|
||
137 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
138 | 44190 | jjdelcerro | try {
|
139 | defaultFeatureTypeId = state.getString("defaultFeatureType");
|
||
140 | List<FeatureType> theTypes = state.getList("featureTypes"); |
||
141 | featureTypes.clear(); |
||
142 | for (FeatureType theType : theTypes) {
|
||
143 | this.featureTypes.add(theType);
|
||
144 | } |
||
145 | } catch(Throwable th) { |
||
146 | LOGGER.warn("Can't load DAL resource.", th);
|
||
147 | throw new PersistenceException(th); |
||
148 | 43954 | jjdelcerro | } |
149 | } |
||
150 | |||
151 | } |