svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libTools / src / org / gvsig / tools / persistence / impl / AbstractPersistentState.java @ 29956
History | View | Annotate | Download (8.42 KB)
1 |
/* 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 |
import java.util.ArrayList; |
31 |
import java.util.Collection; |
32 |
import java.util.HashMap; |
33 |
import java.util.HashSet; |
34 |
import java.util.Iterator; |
35 |
import java.util.List; |
36 |
import java.util.Map; |
37 |
import java.util.Set; |
38 |
import java.util.Map.Entry; |
39 |
|
40 |
import org.gvsig.tools.persistence.PersistenceException; |
41 |
import org.gvsig.tools.persistence.Persistent; |
42 |
import org.gvsig.tools.persistence.impl.exception.PersistenceIllegalStateTheClassNameNotSetException; |
43 |
import org.gvsig.tools.persistence.impl.exception.PersistenceIllegalStateToSetTheClassNameException; |
44 |
import org.gvsig.tools.persistence.impl.exception.PersistenceTypeNotSupportedException; |
45 |
import org.gvsig.tools.persistence.impl.exception.PersistenceUnsuportedMapKeyTypeException; |
46 |
import org.gvsig.tools.persistence.impl.exception.PersistenceValueNotFoundException; |
47 |
|
48 |
|
49 |
public abstract class AbstractPersistentState implements |
50 |
ImplementationPersistentState { |
51 |
private Map values = new HashMap(); |
52 |
private PersistentContext context;
|
53 |
private Integer id; |
54 |
private ImplementationPersistenceManager manager;
|
55 |
private String theClassName = null; |
56 |
|
57 |
public AbstractPersistentState(ImplementationPersistenceManager manager,
|
58 |
PersistentContext context) { |
59 |
this.manager = manager;
|
60 |
values = new HashMap(); |
61 |
this.context = context;
|
62 |
} |
63 |
|
64 |
|
65 |
public Object get(String name) throws PersistenceException { |
66 |
Object ret = values.get(name);
|
67 |
if (ret == null && !values.containsKey(name)) { |
68 |
throw new PersistenceValueNotFoundException(name); |
69 |
} |
70 |
if( ret instanceof ObjectReference){ |
71 |
return context.getObject(((ObjectReference) ret).id);
|
72 |
} |
73 |
return ret;
|
74 |
} |
75 |
|
76 |
public boolean getBoolean(String name) throws PersistenceException { |
77 |
return ((Boolean) get(name)).booleanValue(); |
78 |
} |
79 |
|
80 |
public double getDouble(String name) throws PersistenceException { |
81 |
return ((Double) get(name)).doubleValue(); |
82 |
} |
83 |
|
84 |
public float getFloat(String name) throws PersistenceException { |
85 |
return ((Float) get(name)).floatValue(); |
86 |
} |
87 |
|
88 |
public int getInt(String name) throws PersistenceException { |
89 |
return ((Integer) get(name)).intValue(); |
90 |
} |
91 |
|
92 |
public Iterator getIterator(String name) throws PersistenceException { |
93 |
return ((Collection) get(name)).iterator(); |
94 |
} |
95 |
|
96 |
public long getLong(String name) throws PersistenceException { |
97 |
return ((Long) get(name)).longValue(); |
98 |
} |
99 |
|
100 |
public Iterator getNames() { |
101 |
return values.keySet().iterator();
|
102 |
} |
103 |
|
104 |
public String getString(String name) throws PersistenceException { |
105 |
return ((String) get(name)); |
106 |
} |
107 |
|
108 |
public void set(String name, String value) throws PersistenceException { |
109 |
setValue(name, value); |
110 |
} |
111 |
|
112 |
public void set(String name, int value) throws PersistenceException { |
113 |
setValue(name, new Integer(value)); |
114 |
} |
115 |
|
116 |
public void set(String name, long value) throws PersistenceException { |
117 |
setValue(name, new Long(value)); |
118 |
} |
119 |
|
120 |
public void set(String name, double value) throws PersistenceException { |
121 |
setValue(name, new Double(value)); |
122 |
|
123 |
} |
124 |
|
125 |
public void set(String name, float value) throws PersistenceException { |
126 |
setValue(name, new Float(value)); |
127 |
|
128 |
} |
129 |
|
130 |
public void set(String name, boolean value) throws PersistenceException { |
131 |
setValue(name, new Boolean(value)); |
132 |
} |
133 |
|
134 |
public void set(String name, Persistent obj) throws PersistenceException { |
135 |
setValue(name, getObjectToPersist(obj)); |
136 |
} |
137 |
|
138 |
public void set(String name, Object obj) throws PersistenceException { |
139 |
setValue(name, getObjectToPersist(obj)); |
140 |
} |
141 |
|
142 |
/**
|
143 |
* Checks class of <code>theOriginal</code> and transforms it if is
|
144 |
* necessary.<br>
|
145 |
*
|
146 |
* @param theOriginal
|
147 |
* @return
|
148 |
* @throws PersistenceException
|
149 |
*/
|
150 |
protected Object getObjectToPersist(Object theOriginal) |
151 |
throws PersistenceException {
|
152 |
if (theOriginal instanceof Number) { |
153 |
return theOriginal;
|
154 |
} else if (theOriginal instanceof String) { |
155 |
return theOriginal;
|
156 |
} else if (theOriginal instanceof Boolean){ |
157 |
return theOriginal;
|
158 |
} else if (theOriginal instanceof Map) { |
159 |
Map result = new HashMap(((Map) theOriginal).size()); |
160 |
storeValues(result, (Map) theOriginal);
|
161 |
return result;
|
162 |
|
163 |
} else if (theOriginal instanceof Set) { |
164 |
Set result = new HashSet(((Set) theOriginal).size()); |
165 |
storeValues(result, ((Set) theOriginal).iterator());
|
166 |
return result;
|
167 |
|
168 |
} else if (theOriginal instanceof List) { |
169 |
List result = new ArrayList(((List) theOriginal).size()); |
170 |
storeValues(result, (((List) theOriginal).iterator()));
|
171 |
return result;
|
172 |
|
173 |
} else if (theOriginal instanceof Persistent){ |
174 |
|
175 |
Integer id = context.getId((Persistent) theOriginal);
|
176 |
if (id == null) { |
177 |
ImplementationPersistentState state = manager.createState( |
178 |
(Persistent) theOriginal, context); |
179 |
id = state.getId(); |
180 |
} |
181 |
return new ObjectReference(id); |
182 |
} else {
|
183 |
throw new PersistenceTypeNotSupportedException(theOriginal |
184 |
.getClass()); |
185 |
} |
186 |
} |
187 |
|
188 |
public class ObjectReference { |
189 |
public Integer id; |
190 |
|
191 |
public ObjectReference(Integer id) { |
192 |
this.id = id;
|
193 |
} |
194 |
} |
195 |
|
196 |
/* (non-Javadoc)
|
197 |
* @see org.gvsig.tools.persistence.impl.ImplementationPersistentState#setId(java.lang.Integer)
|
198 |
*/
|
199 |
public void setId(Integer id) { |
200 |
this.id = id;
|
201 |
} |
202 |
|
203 |
/* (non-Javadoc)
|
204 |
* @see org.gvsig.tools.persistence.impl.ImplementationPersistentState#getId()
|
205 |
*/
|
206 |
public Integer getId() { |
207 |
return id;
|
208 |
} |
209 |
|
210 |
private void setValue(String name, Object value) |
211 |
throws PersistenceIllegalStateTheClassNameNotSetException {
|
212 |
if (this.theClassName == null) { |
213 |
throw new PersistenceIllegalStateTheClassNameNotSetException(); |
214 |
} |
215 |
this.values.put(name,value);
|
216 |
} |
217 |
|
218 |
|
219 |
public String getTheClassName() { |
220 |
return theClassName;
|
221 |
} |
222 |
|
223 |
|
224 |
/* (non-Javadoc)
|
225 |
* @see org.gvsig.tools.persistence.impl.ImplementationPersistentState#setTheClassName(java.lang.String)
|
226 |
*/
|
227 |
public void setTheClassName(String className) throws PersistenceException { |
228 |
if (this.size() != 0){ |
229 |
throw new PersistenceIllegalStateToSetTheClassNameException(); |
230 |
} |
231 |
theClassName = className; |
232 |
} |
233 |
|
234 |
/*
|
235 |
* (non-Javadoc)
|
236 |
*
|
237 |
* @see org.gvsig.tools.persistence.PersistentState#set(java.lang.String,
|
238 |
* java.util.Iterator)
|
239 |
*/
|
240 |
public void set(String name, Iterator it) throws PersistenceException { |
241 |
List list = new ArrayList(); |
242 |
storeValues(list, it); |
243 |
set(name, list); |
244 |
} |
245 |
|
246 |
private void storeValues(Collection storage, Iterator iter) |
247 |
throws PersistenceException {
|
248 |
while (iter.hasNext()) {
|
249 |
storage.add(getObjectToPersist(iter.next())); |
250 |
} |
251 |
|
252 |
} |
253 |
|
254 |
private void storeValues(Map storage, Map originalStorage) |
255 |
throws PersistenceException {
|
256 |
Iterator iter = originalStorage.entrySet().iterator();
|
257 |
Entry orgEntry; |
258 |
Object key;
|
259 |
Object value;
|
260 |
|
261 |
while (iter.hasNext()){
|
262 |
orgEntry = (Entry) iter.next(); |
263 |
key = orgEntry.getKey(); |
264 |
|
265 |
if (key == null) { |
266 |
// Nothing to do
|
267 |
} else if (key instanceof String) { |
268 |
// Nothing to do
|
269 |
} else if (key instanceof Number) { |
270 |
// Nothing to do
|
271 |
} else if (key instanceof Boolean) { |
272 |
// Nothing to do
|
273 |
} else if (key instanceof Persistent) { |
274 |
key = getObjectToPersist(key); |
275 |
} else {
|
276 |
throw new PersistenceUnsuportedMapKeyTypeException(orgEntry |
277 |
.getKey().getClass()); |
278 |
} |
279 |
|
280 |
value = getObjectToPersist(orgEntry.getValue()); |
281 |
|
282 |
storage.put(key, value); |
283 |
} |
284 |
|
285 |
} |
286 |
|
287 |
|
288 |
/* (non-Javadoc)
|
289 |
* @see org.gvsig.tools.persistence.impl.ImplementationPersistentState#getContext()
|
290 |
*/
|
291 |
public PersistentContext getContext() {
|
292 |
return context;
|
293 |
} |
294 |
|
295 |
/* (non-Javadoc)
|
296 |
* @see org.gvsig.tools.persistence.impl.ImplementationPersistentState#size()
|
297 |
*/
|
298 |
public int size() { |
299 |
return this.values.size(); |
300 |
} |
301 |
|
302 |
/* (non-Javadoc)
|
303 |
* @see org.gvsig.tools.persistence.impl.ImplementationPersistentState#hasValue(java.lang.String)
|
304 |
*/
|
305 |
public boolean hasValue(String name) { |
306 |
return this.values.containsKey(name); |
307 |
} |
308 |
} |