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 @ 44036
History | View | Annotate | Download (7.77 KB)
1 | 43954 | jjdelcerro | |
---|---|---|---|
2 | package org.gvsig.fmap.dal.feature.impl; |
||
3 | |||
4 | import java.io.File; |
||
5 | import java.io.FileInputStream; |
||
6 | import java.io.FileOutputStream; |
||
7 | import java.util.ArrayList; |
||
8 | import java.util.HashSet; |
||
9 | import java.util.Iterator; |
||
10 | import java.util.List; |
||
11 | import java.util.Set; |
||
12 | import org.apache.commons.collections4.CollectionUtils; |
||
13 | import org.apache.commons.io.IOUtils; |
||
14 | import org.gvsig.fmap.dal.exception.DataException; |
||
15 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
16 | import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator; |
||
17 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
18 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
19 | import org.gvsig.tools.ToolsLocator; |
||
20 | import org.gvsig.tools.dynobject.DynStruct; |
||
21 | import org.gvsig.tools.persistence.PersistenceManager; |
||
22 | import org.gvsig.tools.persistence.Persistent; |
||
23 | import org.gvsig.tools.persistence.PersistentState; |
||
24 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
25 | |||
26 | |||
27 | public class DALFile implements Persistent { |
||
28 | |||
29 | private static final String DALFILE_PERSISTENCE_DEFINITION_NAME = "DALFile"; |
||
30 | private static final String CALCULATEDATTRIBUTE_PERSISTENCE_DEFINITION_NAME = "DALFileCalculatedAttribute"; |
||
31 | |||
32 | public static DALFile getDALFile() { |
||
33 | DALFile f = new DALFile();
|
||
34 | return f;
|
||
35 | } |
||
36 | |||
37 | public static DALFile getDALFile(File f) { |
||
38 | DALFile df = new DALFile();
|
||
39 | df.read(f); |
||
40 | 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 | definition.addDynFieldList("calculatedAttributes")
|
||
54 | .setClassOfItems(CalculatedAttribute.class) |
||
55 | .setMandatory(true)
|
||
56 | .setPersistent(true);
|
||
57 | } |
||
58 | if (manager.getDefinition(CALCULATEDATTRIBUTE_PERSISTENCE_DEFINITION_NAME) == null) { |
||
59 | DynStruct definition = manager.addDefinition( |
||
60 | 43978 | omartinez | CalculatedAttribute.class, |
61 | 43954 | jjdelcerro | CALCULATEDATTRIBUTE_PERSISTENCE_DEFINITION_NAME, |
62 | CALCULATEDATTRIBUTE_PERSISTENCE_DEFINITION_NAME + " Persistent definition",
|
||
63 | null,
|
||
64 | null
|
||
65 | ); |
||
66 | definition.addDynFieldString("name")
|
||
67 | .setMandatory(true)
|
||
68 | .setPersistent(true);
|
||
69 | definition.addDynFieldInt("type")
|
||
70 | .setMandatory(true)
|
||
71 | .setPersistent(true);
|
||
72 | definition.addDynFieldInt("featureTypeIndex")
|
||
73 | .setMandatory(true)
|
||
74 | .setPersistent(true);
|
||
75 | definition.addDynFieldObject("emulator")
|
||
76 | .setClassOfValue(FeatureAttributeEmulator.class) |
||
77 | .setMandatory(true)
|
||
78 | .setPersistent(true);
|
||
79 | } |
||
80 | } |
||
81 | |||
82 | |||
83 | 43978 | omartinez | public static class CalculatedAttribute implements Persistent { |
84 | 43954 | jjdelcerro | |
85 | private String name; |
||
86 | private int type; |
||
87 | private FeatureAttributeEmulator emulator;
|
||
88 | private int featureTypeIndex; |
||
89 | |||
90 | public CalculatedAttribute() {
|
||
91 | |||
92 | } |
||
93 | |||
94 | private CalculatedAttribute(int featureTypeIndex, FeatureAttributeDescriptor attrdesc) { |
||
95 | this.featureTypeIndex = featureTypeIndex;
|
||
96 | this.name = attrdesc.getName();
|
||
97 | this.type = attrdesc.getType();
|
||
98 | this.emulator = attrdesc.getFeatureAttributeEmulator();
|
||
99 | } |
||
100 | |||
101 | @Override
|
||
102 | public void saveToState(PersistentState state) throws PersistenceException { |
||
103 | state.set("name", name);
|
||
104 | state.set("type", type);
|
||
105 | state.set("featureTypeIndex", featureTypeIndex);
|
||
106 | state.set("emulator", emulator);
|
||
107 | } |
||
108 | |||
109 | @Override
|
||
110 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
111 | this.name = state.getString("name"); |
||
112 | this.type = state.getInt("type"); |
||
113 | this.featureTypeIndex = state.getInt("featureTypeIndex"); |
||
114 | this.emulator = (FeatureAttributeEmulator) state.get("emulator"); |
||
115 | } |
||
116 | |||
117 | |||
118 | } |
||
119 | |||
120 | private final List<CalculatedAttribute> calculatedAttributes; |
||
121 | |||
122 | 43978 | omartinez | public DALFile() {
|
123 | 43954 | jjdelcerro | this.calculatedAttributes = new ArrayList<>(); |
124 | } |
||
125 | |||
126 | public boolean isEmpty() { |
||
127 | if( CollectionUtils.isEmpty(this.calculatedAttributes) ) { |
||
128 | return true; |
||
129 | } |
||
130 | return false; |
||
131 | } |
||
132 | |||
133 | public void write(File f) { |
||
134 | FileOutputStream out = null; |
||
135 | try {
|
||
136 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
137 | PersistentState state = manager.getState(this);
|
||
138 | out = new FileOutputStream(f); |
||
139 | manager.saveState(state, out); |
||
140 | } catch(Exception ex) { |
||
141 | throw new RuntimeException("Can't write DAL file.",ex); |
||
142 | } finally {
|
||
143 | IOUtils.closeQuietly(out); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | @SuppressWarnings("UseSpecificCatch") |
||
148 | public void read(File f) { |
||
149 | FileInputStream in = null; |
||
150 | try {
|
||
151 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
152 | in = new FileInputStream(f); |
||
153 | DALFile x = (DALFile) manager.getObject(in); |
||
154 | this.calculatedAttributes.clear();
|
||
155 | this.calculatedAttributes.addAll(x.calculatedAttributes);
|
||
156 | } catch(Exception ex) { |
||
157 | throw new RuntimeException("Can't read DAL file.",ex); |
||
158 | } finally {
|
||
159 | IOUtils.closeQuietly(in); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | public void setStore(FeatureStore store) throws DataException { |
||
164 | this.calculatedAttributes.clear();
|
||
165 | |||
166 | List<FeatureType> types = store.getFeatureTypes();
|
||
167 | for( int n=0; n<types.size(); n++ ) { |
||
168 | FeatureType type = types.get(n); |
||
169 | for (FeatureAttributeDescriptor attrdesc : type) {
|
||
170 | FeatureAttributeEmulator emulator = attrdesc.getFeatureAttributeEmulator(); |
||
171 | if( emulator!= null && emulator instanceof Persistent ) { |
||
172 | CalculatedAttribute calculatedAttribute = new CalculatedAttribute(n,attrdesc);
|
||
173 | this.calculatedAttributes.add(calculatedAttribute);
|
||
174 | } |
||
175 | } |
||
176 | } |
||
177 | } |
||
178 | |||
179 | public void updateStore(FeatureStore store) throws DataException { |
||
180 | List<FeatureType> types = store.getFeatureTypes();
|
||
181 | Set<DefaultFeatureType> needFixTypes = new HashSet<>(); |
||
182 | |||
183 | for (CalculatedAttribute calculatedAttribute : calculatedAttributes) {
|
||
184 | DefaultFeatureType ft = (DefaultFeatureType) types.get(calculatedAttribute.featureTypeIndex); |
||
185 | DefaultFeatureAttributeDescriptor attrdesc = new DefaultFeatureAttributeDescriptor(ft);
|
||
186 | attrdesc.setDataType(calculatedAttribute.type); |
||
187 | attrdesc.setName(calculatedAttribute.name); |
||
188 | attrdesc.setFeatureAttributeEmulator(calculatedAttribute.emulator); |
||
189 | ft.add(attrdesc); |
||
190 | needFixTypes.add(ft); |
||
191 | } |
||
192 | for (DefaultFeatureType type : needFixTypes) {
|
||
193 | type.fixAll(); |
||
194 | } |
||
195 | } |
||
196 | |||
197 | @Override
|
||
198 | public void saveToState(PersistentState state) throws PersistenceException { |
||
199 | state.set("calculatedAttributes", calculatedAttributes);
|
||
200 | } |
||
201 | |||
202 | @Override
|
||
203 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
204 | this.calculatedAttributes.clear();
|
||
205 | Iterator<CalculatedAttribute> it = state.getIterator("calculatedAttributes"); |
||
206 | while( it.hasNext() ) {
|
||
207 | CalculatedAttribute calculatedAttribute = it.next(); |
||
208 | this.calculatedAttributes.add(calculatedAttribute);
|
||
209 | } |
||
210 | } |
||
211 | |||
212 | } |