svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libTools / src / org / gvsig / tools / persistence / impl / AbstractPersistentState.java @ 29961
History | View | Annotate | Download (8.65 KB)
1 | 29956 | jmvivo | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
4 | * of the Valencian Government (CIT)
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 2
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | */
|
||
22 | |||
23 | /*
|
||
24 | * AUTHORS (In addition to CIT):
|
||
25 | * 2009 IVER T.I {{Task}}
|
||
26 | */
|
||
27 | |||
28 | package org.gvsig.tools.persistence.impl; |
||
29 | |||
30 | 29961 | jmvivo | import java.io.Writer; |
31 | 29956 | jmvivo | import java.util.ArrayList; |
32 | import java.util.Collection; |
||
33 | import java.util.HashMap; |
||
34 | import java.util.HashSet; |
||
35 | import java.util.Iterator; |
||
36 | import java.util.List; |
||
37 | import java.util.Map; |
||
38 | import java.util.Set; |
||
39 | import java.util.Map.Entry; |
||
40 | |||
41 | import org.gvsig.tools.persistence.PersistenceException; |
||
42 | import org.gvsig.tools.persistence.Persistent; |
||
43 | import org.gvsig.tools.persistence.impl.exception.PersistenceIllegalStateTheClassNameNotSetException; |
||
44 | import org.gvsig.tools.persistence.impl.exception.PersistenceIllegalStateToSetTheClassNameException; |
||
45 | import org.gvsig.tools.persistence.impl.exception.PersistenceTypeNotSupportedException; |
||
46 | import org.gvsig.tools.persistence.impl.exception.PersistenceUnsuportedMapKeyTypeException; |
||
47 | import org.gvsig.tools.persistence.impl.exception.PersistenceValueNotFoundException; |
||
48 | |||
49 | |||
50 | public abstract class AbstractPersistentState implements |
||
51 | ImplementationPersistentState { |
||
52 | private Map values = new HashMap(); |
||
53 | private PersistentContext context;
|
||
54 | private Integer id; |
||
55 | private ImplementationPersistenceManager manager;
|
||
56 | private String theClassName = null; |
||
57 | |||
58 | public AbstractPersistentState(ImplementationPersistenceManager manager,
|
||
59 | PersistentContext context) { |
||
60 | this.manager = manager;
|
||
61 | values = new HashMap(); |
||
62 | this.context = context;
|
||
63 | } |
||
64 | |||
65 | |||
66 | public Object get(String name) throws PersistenceException { |
||
67 | Object ret = values.get(name);
|
||
68 | if (ret == null && !values.containsKey(name)) { |
||
69 | throw new PersistenceValueNotFoundException(name); |
||
70 | } |
||
71 | if( ret instanceof ObjectReference){ |
||
72 | return context.getObject(((ObjectReference) ret).id);
|
||
73 | } |
||
74 | return ret;
|
||
75 | } |
||
76 | |||
77 | public boolean getBoolean(String name) throws PersistenceException { |
||
78 | return ((Boolean) get(name)).booleanValue(); |
||
79 | } |
||
80 | |||
81 | public double getDouble(String name) throws PersistenceException { |
||
82 | return ((Double) get(name)).doubleValue(); |
||
83 | } |
||
84 | |||
85 | public float getFloat(String name) throws PersistenceException { |
||
86 | return ((Float) get(name)).floatValue(); |
||
87 | } |
||
88 | |||
89 | public int getInt(String name) throws PersistenceException { |
||
90 | return ((Integer) get(name)).intValue(); |
||
91 | } |
||
92 | |||
93 | public Iterator getIterator(String name) throws PersistenceException { |
||
94 | return ((Collection) get(name)).iterator(); |
||
95 | } |
||
96 | |||
97 | public long getLong(String name) throws PersistenceException { |
||
98 | return ((Long) get(name)).longValue(); |
||
99 | } |
||
100 | |||
101 | public Iterator getNames() { |
||
102 | return values.keySet().iterator();
|
||
103 | } |
||
104 | |||
105 | public String getString(String name) throws PersistenceException { |
||
106 | return ((String) get(name)); |
||
107 | } |
||
108 | |||
109 | public void set(String name, String value) throws PersistenceException { |
||
110 | setValue(name, value); |
||
111 | } |
||
112 | |||
113 | public void set(String name, int value) throws PersistenceException { |
||
114 | setValue(name, new Integer(value)); |
||
115 | } |
||
116 | |||
117 | public void set(String name, long value) throws PersistenceException { |
||
118 | setValue(name, new Long(value)); |
||
119 | } |
||
120 | |||
121 | public void set(String name, double value) throws PersistenceException { |
||
122 | setValue(name, new Double(value)); |
||
123 | |||
124 | } |
||
125 | |||
126 | public void set(String name, float value) throws PersistenceException { |
||
127 | setValue(name, new Float(value)); |
||
128 | |||
129 | } |
||
130 | |||
131 | public void set(String name, boolean value) throws PersistenceException { |
||
132 | setValue(name, new Boolean(value)); |
||
133 | } |
||
134 | |||
135 | public void set(String name, Persistent obj) throws PersistenceException { |
||
136 | setValue(name, getObjectToPersist(obj)); |
||
137 | } |
||
138 | |||
139 | public void set(String name, Object obj) throws PersistenceException { |
||
140 | setValue(name, getObjectToPersist(obj)); |
||
141 | } |
||
142 | |||
143 | /**
|
||
144 | * Checks class of <code>theOriginal</code> and transforms it if is
|
||
145 | * necessary.<br>
|
||
146 | *
|
||
147 | * @param theOriginal
|
||
148 | * @return
|
||
149 | * @throws PersistenceException
|
||
150 | */
|
||
151 | protected Object getObjectToPersist(Object theOriginal) |
||
152 | throws PersistenceException {
|
||
153 | if (theOriginal instanceof Number) { |
||
154 | return theOriginal;
|
||
155 | } else if (theOriginal instanceof String) { |
||
156 | return theOriginal;
|
||
157 | } else if (theOriginal instanceof Boolean){ |
||
158 | return theOriginal;
|
||
159 | } else if (theOriginal instanceof Map) { |
||
160 | Map result = new HashMap(((Map) theOriginal).size()); |
||
161 | storeValues(result, (Map) theOriginal);
|
||
162 | return result;
|
||
163 | |||
164 | } else if (theOriginal instanceof Set) { |
||
165 | Set result = new HashSet(((Set) theOriginal).size()); |
||
166 | storeValues(result, ((Set) theOriginal).iterator());
|
||
167 | return result;
|
||
168 | |||
169 | } else if (theOriginal instanceof List) { |
||
170 | List result = new ArrayList(((List) theOriginal).size()); |
||
171 | storeValues(result, (((List) theOriginal).iterator()));
|
||
172 | return result;
|
||
173 | |||
174 | } else if (theOriginal instanceof Persistent){ |
||
175 | |||
176 | Integer id = context.getId((Persistent) theOriginal);
|
||
177 | if (id == null) { |
||
178 | ImplementationPersistentState state = manager.createState( |
||
179 | (Persistent) theOriginal, context); |
||
180 | id = state.getId(); |
||
181 | } |
||
182 | return new ObjectReference(id); |
||
183 | } else {
|
||
184 | throw new PersistenceTypeNotSupportedException(theOriginal |
||
185 | .getClass()); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | public class ObjectReference { |
||
190 | public Integer id; |
||
191 | |||
192 | public ObjectReference(Integer id) { |
||
193 | this.id = id;
|
||
194 | } |
||
195 | } |
||
196 | |||
197 | /* (non-Javadoc)
|
||
198 | * @see org.gvsig.tools.persistence.impl.ImplementationPersistentState#setId(java.lang.Integer)
|
||
199 | */
|
||
200 | public void setId(Integer id) { |
||
201 | this.id = id;
|
||
202 | } |
||
203 | |||
204 | /* (non-Javadoc)
|
||
205 | * @see org.gvsig.tools.persistence.impl.ImplementationPersistentState#getId()
|
||
206 | */
|
||
207 | public Integer getId() { |
||
208 | return id;
|
||
209 | } |
||
210 | |||
211 | private void setValue(String name, Object value) |
||
212 | throws PersistenceIllegalStateTheClassNameNotSetException {
|
||
213 | if (this.theClassName == null) { |
||
214 | throw new PersistenceIllegalStateTheClassNameNotSetException(); |
||
215 | } |
||
216 | this.values.put(name,value);
|
||
217 | } |
||
218 | |||
219 | |||
220 | public String getTheClassName() { |
||
221 | return theClassName;
|
||
222 | } |
||
223 | |||
224 | |||
225 | /* (non-Javadoc)
|
||
226 | * @see org.gvsig.tools.persistence.impl.ImplementationPersistentState#setTheClassName(java.lang.String)
|
||
227 | */
|
||
228 | public void setTheClassName(String className) throws PersistenceException { |
||
229 | if (this.size() != 0){ |
||
230 | throw new PersistenceIllegalStateToSetTheClassNameException(); |
||
231 | } |
||
232 | theClassName = className; |
||
233 | } |
||
234 | |||
235 | /*
|
||
236 | * (non-Javadoc)
|
||
237 | *
|
||
238 | * @see org.gvsig.tools.persistence.PersistentState#set(java.lang.String,
|
||
239 | * java.util.Iterator)
|
||
240 | */
|
||
241 | public void set(String name, Iterator it) throws PersistenceException { |
||
242 | List list = new ArrayList(); |
||
243 | storeValues(list, it); |
||
244 | set(name, list); |
||
245 | } |
||
246 | |||
247 | private void storeValues(Collection storage, Iterator iter) |
||
248 | throws PersistenceException {
|
||
249 | while (iter.hasNext()) {
|
||
250 | storage.add(getObjectToPersist(iter.next())); |
||
251 | } |
||
252 | |||
253 | } |
||
254 | |||
255 | private void storeValues(Map storage, Map originalStorage) |
||
256 | throws PersistenceException {
|
||
257 | Iterator iter = originalStorage.entrySet().iterator();
|
||
258 | Entry orgEntry; |
||
259 | Object key;
|
||
260 | Object value;
|
||
261 | |||
262 | while (iter.hasNext()){
|
||
263 | orgEntry = (Entry) iter.next(); |
||
264 | key = orgEntry.getKey(); |
||
265 | |||
266 | if (key == null) { |
||
267 | // Nothing to do
|
||
268 | } else if (key instanceof String) { |
||
269 | // Nothing to do
|
||
270 | } else if (key instanceof Number) { |
||
271 | // Nothing to do
|
||
272 | } else if (key instanceof Boolean) { |
||
273 | // Nothing to do
|
||
274 | } else if (key instanceof Persistent) { |
||
275 | key = getObjectToPersist(key); |
||
276 | } else {
|
||
277 | throw new PersistenceUnsuportedMapKeyTypeException(orgEntry |
||
278 | .getKey().getClass()); |
||
279 | } |
||
280 | |||
281 | value = getObjectToPersist(orgEntry.getValue()); |
||
282 | |||
283 | storage.put(key, value); |
||
284 | } |
||
285 | |||
286 | } |
||
287 | |||
288 | |||
289 | /* (non-Javadoc)
|
||
290 | * @see org.gvsig.tools.persistence.impl.ImplementationPersistentState#getContext()
|
||
291 | */
|
||
292 | public PersistentContext getContext() {
|
||
293 | return context;
|
||
294 | } |
||
295 | |||
296 | /* (non-Javadoc)
|
||
297 | * @see org.gvsig.tools.persistence.impl.ImplementationPersistentState#size()
|
||
298 | */
|
||
299 | public int size() { |
||
300 | return this.values.size(); |
||
301 | } |
||
302 | |||
303 | /* (non-Javadoc)
|
||
304 | * @see org.gvsig.tools.persistence.impl.ImplementationPersistentState#hasValue(java.lang.String)
|
||
305 | */
|
||
306 | public boolean hasValue(String name) { |
||
307 | return this.values.containsKey(name); |
||
308 | } |
||
309 | 29961 | jmvivo | |
310 | |||
311 | /*
|
||
312 | * (non-Javadoc)
|
||
313 | *
|
||
314 | * @see org.gvsig.tools.persistence.PersistentState#save(java.io.Writer)
|
||
315 | */
|
||
316 | public void save(Writer writer) throws PersistenceException { |
||
317 | this.manager.saveState(this, writer); |
||
318 | |||
319 | } |
||
320 | 29956 | jmvivo | } |