gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / persistence / impl / AbstractPersistentState.java @ 1095
History | View | Annotate | Download (28.3 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
/*
|
25 |
* AUTHORS (In addition to CIT):
|
26 |
* 2009 IVER T.I {{Task}}
|
27 |
*/
|
28 |
|
29 |
package org.gvsig.tools.persistence.impl; |
30 |
|
31 |
import java.io.File; |
32 |
import java.lang.reflect.Array; |
33 |
import java.net.MalformedURLException; |
34 |
import java.net.URI; |
35 |
import java.net.URISyntaxException; |
36 |
import java.net.URL; |
37 |
import java.util.ArrayList; |
38 |
import java.util.Arrays; |
39 |
import java.util.Collection; |
40 |
import java.util.Collections; |
41 |
import java.util.Date; |
42 |
import java.util.HashMap; |
43 |
import java.util.Iterator; |
44 |
import java.util.LinkedHashMap; |
45 |
import java.util.LinkedHashSet; |
46 |
import java.util.List; |
47 |
import java.util.Map; |
48 |
import java.util.Map.Entry; |
49 |
import java.util.Set; |
50 |
import java.util.regex.Pattern; |
51 |
import org.apache.commons.io.FileUtils; |
52 |
|
53 |
import org.gvsig.tools.dataTypes.DataTypes; |
54 |
import org.gvsig.tools.dynobject.DynField; |
55 |
import org.gvsig.tools.dynobject.DynStruct; |
56 |
import org.gvsig.tools.persistence.PersistenceFactory; |
57 |
import org.gvsig.tools.persistence.PersistenceManager; |
58 |
import org.gvsig.tools.persistence.Persistent; |
59 |
import org.gvsig.tools.persistence.PersistentContext; |
60 |
import org.gvsig.tools.persistence.PersistentState; |
61 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
62 |
import org.gvsig.tools.persistence.exception.PersistenceTypeNotSupportedException; |
63 |
import org.gvsig.tools.persistence.impl.exception.PersistenceIllegalStateTheClassNameNotSetException; |
64 |
import org.gvsig.tools.persistence.impl.exception.PersistenceInvalidPropertyNameException; |
65 |
import org.gvsig.tools.persistence.impl.exception.PersistenceInvalidTypeForPropertyException; |
66 |
import org.gvsig.tools.persistence.impl.exception.PersistenceUnsuportedMapKeyTypeException; |
67 |
import org.gvsig.tools.persistence.impl.exception.PersistenceValueNotFoundException; |
68 |
import org.gvsig.tools.persistence.spi.PersistenceManagerServices; |
69 |
import org.gvsig.tools.persistence.spi.PersistentContextServices; |
70 |
import org.gvsig.tools.persistence.spi.PersistentContextServices.ObjectReference; |
71 |
import org.gvsig.tools.persistence.spi.PersistentIdentifier; |
72 |
import org.gvsig.tools.persistence.spi.PersistentStateServices; |
73 |
import org.slf4j.Logger; |
74 |
import org.slf4j.LoggerFactory; |
75 |
|
76 |
public abstract class AbstractPersistentState implements |
77 |
PersistentStateServices { |
78 |
|
79 |
private static final Logger logger = LoggerFactory |
80 |
.getLogger(PersistentState.class); |
81 |
|
82 |
private static final Pattern VALID_NAME_PATTERN = Pattern |
83 |
.compile("[\\w][\\d\\w_]*");
|
84 |
|
85 |
private static final List validStdTypeClasses = Collections |
86 |
.unmodifiableList(Arrays.asList(new Class[] { // All need to be |
87 |
// immutable
|
88 |
Integer.class, Long.class, Float.class, Double.class, |
89 |
Boolean.class, String.class, Date.class, |
90 |
File.class, URL.class, URI.class })); |
91 |
|
92 |
private Map values = new LinkedHashMap(); |
93 |
private PersistentContextServices context;
|
94 |
private PersistentIdentifier id;
|
95 |
private PersistenceManagerServices manager;
|
96 |
private String theClassName = null; |
97 |
private PersistenceFactory factory;
|
98 |
|
99 |
public AbstractPersistentState(PersistenceManagerServices manager,
|
100 |
PersistentContextServices context, PersistentIdentifier id) { |
101 |
this.manager = manager;
|
102 |
this.context = context;
|
103 |
this.id = id;
|
104 |
values = new HashMap(); |
105 |
} |
106 |
|
107 |
public void setFactory(PersistenceFactory factory) { |
108 |
this.factory = factory;
|
109 |
} |
110 |
|
111 |
public DynStruct getDefinition() {
|
112 |
DynStruct definition = this.factory.getDefinition(this.theClassName); |
113 |
return definition;
|
114 |
} |
115 |
|
116 |
private DynStruct getDefinitionCheckNull() {
|
117 |
DynStruct definition = getDefinition(); |
118 |
if (definition == null) { |
119 |
throw new NullPointerException( |
120 |
"Can't locate definition for the class '"
|
121 |
+ this.theClassName + "' in the factory '" |
122 |
+ this.factory.getClass().getName()
|
123 |
+ "', managed classes are '"
|
124 |
+ this.factory.getManagedClasses().toString()
|
125 |
+ "'.");
|
126 |
} |
127 |
return definition;
|
128 |
} |
129 |
|
130 |
public Object get(String name) throws PersistenceException { |
131 |
Object ret = values.get(name);
|
132 |
if (ret == null) { |
133 |
// We are asked for an attribute not defined in the dynstruct
|
134 |
if (!values.containsKey(name) && !definitionHasField(name)) {
|
135 |
throw new PersistenceValueNotFoundException(name); |
136 |
} |
137 |
return getDefaultValue(name);
|
138 |
} |
139 |
return get(ret);
|
140 |
} |
141 |
|
142 |
public List getList(String name) throws PersistenceException { |
143 |
return (List) get(name); |
144 |
} |
145 |
|
146 |
public Map getMap(String name) throws PersistenceException { |
147 |
return (Map) get(name); |
148 |
} |
149 |
|
150 |
public Set getSet(String name) throws PersistenceException { |
151 |
return (Set) get(name); |
152 |
} |
153 |
|
154 |
private Object get(Object obj) throws PersistenceException { |
155 |
if (obj instanceof ObjectReference) { |
156 |
ObjectReference ref = (ObjectReference) obj; |
157 |
return ref.getObject();
|
158 |
} |
159 |
if (obj instanceof List) { |
160 |
List result = new ArrayList(((List) obj).size()); |
161 |
getValues(result, (((List) obj).iterator()));
|
162 |
return result;
|
163 |
} |
164 |
return obj;
|
165 |
} |
166 |
|
167 |
private void getValues(List result, Iterator iterator) |
168 |
throws PersistenceException {
|
169 |
while (iterator.hasNext()) {
|
170 |
result.add(get(iterator.next())); |
171 |
} |
172 |
} |
173 |
|
174 |
public boolean getBoolean(String name) throws PersistenceException { |
175 |
return ((Boolean) get(name)).booleanValue(); |
176 |
} |
177 |
|
178 |
public double getDouble(String name) throws PersistenceException { |
179 |
return ((Double) get(name)).doubleValue(); |
180 |
} |
181 |
|
182 |
public File getFile(String name) throws PersistenceException { |
183 |
return (File) get(name); |
184 |
} |
185 |
|
186 |
public URL getURL(String name) throws PersistenceException { |
187 |
return (URL) get(name); |
188 |
} |
189 |
|
190 |
public URI getURI(String name) throws PersistenceException { |
191 |
return (URI) get(name); |
192 |
} |
193 |
|
194 |
public Date getDate(String name) throws PersistenceException { |
195 |
return (Date) get(name); |
196 |
} |
197 |
|
198 |
public float getFloat(String name) throws PersistenceException { |
199 |
return ((Float) get(name)).floatValue(); |
200 |
} |
201 |
|
202 |
public int getInt(String name) throws PersistenceException { |
203 |
return ((Integer) get(name)).intValue(); |
204 |
} |
205 |
|
206 |
public Iterator getIterator(String name) throws PersistenceException { |
207 |
return ((Collection) get(name)).iterator(); |
208 |
} |
209 |
|
210 |
public long getLong(String name) throws PersistenceException { |
211 |
return ((Long) get(name)).longValue(); |
212 |
} |
213 |
|
214 |
public Object[] getArray(String name, Class valueClass) |
215 |
throws PersistenceException {
|
216 |
List valueList = getList(name);
|
217 |
if (valueList == null) { |
218 |
return null; |
219 |
} |
220 |
Object array = Array.newInstance(valueClass, valueList.size()); |
221 |
return valueList.toArray((Object[]) array); |
222 |
} |
223 |
|
224 |
public boolean[] getBooleanArray(String name) throws PersistenceException { |
225 |
List valueList = getList(name);
|
226 |
if (valueList == null) { |
227 |
return null; |
228 |
} |
229 |
boolean[] values = new boolean[valueList.size()]; |
230 |
for (int i = 0; i < values.length; i++) { |
231 |
values[i] = ((Boolean) valueList.get(i)).booleanValue();
|
232 |
} |
233 |
return values;
|
234 |
} |
235 |
|
236 |
public int[] getIntArray(String name) throws PersistenceException { |
237 |
List valueList = getList(name);
|
238 |
if (valueList == null) { |
239 |
return null; |
240 |
} |
241 |
int[] values = new int[valueList.size()]; |
242 |
for (int i = 0; i < values.length; i++) { |
243 |
values[i] = ((Number) valueList.get(i)).intValue();
|
244 |
} |
245 |
return values;
|
246 |
} |
247 |
|
248 |
public long[] getLongArray(String name) throws PersistenceException { |
249 |
List valueList = getList(name);
|
250 |
if (valueList == null) { |
251 |
return null; |
252 |
} |
253 |
long[] values = new long[valueList.size()]; |
254 |
for (int i = 0; i < values.length; i++) { |
255 |
values[i] = ((Number) valueList.get(i)).longValue();
|
256 |
} |
257 |
return values;
|
258 |
} |
259 |
|
260 |
public float[] getFloatArray(String name) throws PersistenceException { |
261 |
List valueList = getList(name);
|
262 |
if (valueList == null) { |
263 |
return null; |
264 |
} |
265 |
float[] values = new float[valueList.size()]; |
266 |
for (int i = 0; i < values.length; i++) { |
267 |
values[i] = ((Number) valueList.get(i)).floatValue();
|
268 |
} |
269 |
return values;
|
270 |
} |
271 |
|
272 |
public double[] getDoubleArray(String name) throws PersistenceException { |
273 |
List valueList = getList(name);
|
274 |
if (valueList == null) { |
275 |
return null; |
276 |
} |
277 |
double[] values = new double[valueList.size()]; |
278 |
for (int i = 0; i < values.length; i++) { |
279 |
values[i] = ((Number) valueList.get(i)).doubleValue();
|
280 |
} |
281 |
return values;
|
282 |
} |
283 |
|
284 |
public Date[] getDateArray(String name) throws PersistenceException { |
285 |
return (Date[]) this.getArray(name, Date.class); |
286 |
} |
287 |
|
288 |
public String[] getStringArray(String name) throws PersistenceException { |
289 |
return (String[]) this.getArray(name, String.class); |
290 |
} |
291 |
|
292 |
public Iterator getNames() { |
293 |
return values.keySet().iterator();
|
294 |
} |
295 |
|
296 |
public String getString(String name) throws PersistenceException { |
297 |
return ((String) get(name)); |
298 |
} |
299 |
|
300 |
public void setNull(String name) throws PersistenceException { |
301 |
setValue(name, null);
|
302 |
} |
303 |
|
304 |
public void set(String name, String value) throws PersistenceException { |
305 |
setValue(name, value); |
306 |
} |
307 |
|
308 |
public void set(String name, File value) throws PersistenceException { |
309 |
setValue(name, value); |
310 |
} |
311 |
|
312 |
public void set(String name, URL value) throws PersistenceException { |
313 |
setValue(name, value); |
314 |
} |
315 |
|
316 |
public void set(String name, URI value) throws PersistenceException { |
317 |
setValue(name, value); |
318 |
} |
319 |
|
320 |
public void set(String name, int value) throws PersistenceException { |
321 |
setValue(name, new Integer(value)); |
322 |
} |
323 |
|
324 |
public void set(String name, long value) throws PersistenceException { |
325 |
setValue(name, new Long(value)); |
326 |
} |
327 |
|
328 |
public void set(String name, double value) throws PersistenceException { |
329 |
setValue(name, new Double(value)); |
330 |
|
331 |
} |
332 |
|
333 |
public void set(String name, float value) throws PersistenceException { |
334 |
setValue(name, new Float(value)); |
335 |
|
336 |
} |
337 |
|
338 |
public void set(String name, boolean value) throws PersistenceException { |
339 |
setValue(name, new Boolean(value)); |
340 |
} |
341 |
|
342 |
public void set(String name, Persistent obj) throws PersistenceException { |
343 |
setValue(name, getObjectToPersist(obj, name)); |
344 |
} |
345 |
|
346 |
public void set(String name, Object obj) throws PersistenceException { |
347 |
setValue(name, getObjectToPersist(obj, name)); |
348 |
} |
349 |
|
350 |
public void set(String name, Map obj) throws PersistenceException { |
351 |
setValue(name, getObjectToPersist(obj, name)); |
352 |
} |
353 |
|
354 |
public void set(String name, List obj) throws PersistenceException { |
355 |
setValue(name, getObjectToPersist(obj, name)); |
356 |
} |
357 |
|
358 |
public void set(String name, Iterator obj) throws PersistenceException { |
359 |
setValue(name, getObjectToPersist(obj, name)); |
360 |
} |
361 |
|
362 |
public void set(String name, Object[] values) throws PersistenceException { |
363 |
set(name, values == null ? null : Arrays.asList(values)); |
364 |
} |
365 |
|
366 |
public void set(String name, Date[] values) throws PersistenceException { |
367 |
set(name, values == null ? null : Arrays.asList(values)); |
368 |
} |
369 |
|
370 |
public void set(String name, String[] values) throws PersistenceException { |
371 |
set(name, values == null ? null : Arrays.asList(values)); |
372 |
} |
373 |
|
374 |
public void set(String name, boolean[] value) throws PersistenceException { |
375 |
if (value == null) { |
376 |
setValue(name, null);
|
377 |
} else {
|
378 |
List valueList = new ArrayList(value.length); |
379 |
for (int i = 0; i < value.length; i++) { |
380 |
valueList.add(value[i] ? Boolean.TRUE : Boolean.FALSE); |
381 |
} |
382 |
set(name, valueList); |
383 |
} |
384 |
} |
385 |
|
386 |
public void set(String name, int[] value) throws PersistenceException { |
387 |
if (value == null) { |
388 |
setValue(name, null);
|
389 |
} else {
|
390 |
List valueList = new ArrayList(value.length); |
391 |
for (int i = 0; i < value.length; i++) { |
392 |
valueList.add(new Integer(value[i])); |
393 |
} |
394 |
set(name, valueList); |
395 |
} |
396 |
} |
397 |
|
398 |
public void set(String name, long[] value) throws PersistenceException { |
399 |
if (value == null) { |
400 |
setValue(name, null);
|
401 |
} else {
|
402 |
List valueList = new ArrayList(value.length); |
403 |
for (int i = 0; i < value.length; i++) { |
404 |
valueList.add(new Long(value[i])); |
405 |
} |
406 |
set(name, valueList); |
407 |
} |
408 |
} |
409 |
|
410 |
public void set(String name, float[] value) throws PersistenceException { |
411 |
if (value == null) { |
412 |
setValue(name, null);
|
413 |
} else {
|
414 |
List valueList = new ArrayList(value.length); |
415 |
for (int i = 0; i < value.length; i++) { |
416 |
valueList.add(new Float(value[i])); |
417 |
} |
418 |
set(name, valueList); |
419 |
} |
420 |
} |
421 |
|
422 |
public void set(String name, double[] value) throws PersistenceException { |
423 |
if (value == null) { |
424 |
setValue(name, null);
|
425 |
} else {
|
426 |
List valueList = new ArrayList(value.length); |
427 |
for (int i = 0; i < value.length; i++) { |
428 |
valueList.add(new Double(value[i])); |
429 |
} |
430 |
set(name, valueList); |
431 |
} |
432 |
} |
433 |
|
434 |
public void set(String name, Set obj) throws PersistenceException { |
435 |
setValue(name, getObjectToPersist(obj, name)); |
436 |
} |
437 |
|
438 |
public void set(String name, Boolean obj) throws PersistenceException { |
439 |
setValue(name, getObjectToPersist(obj, name)); |
440 |
} |
441 |
|
442 |
public void set(String name, Integer obj) throws PersistenceException { |
443 |
setValue(name, getObjectToPersist(obj, name)); |
444 |
} |
445 |
|
446 |
public void set(String name, Long obj) throws PersistenceException { |
447 |
setValue(name, getObjectToPersist(obj, name)); |
448 |
} |
449 |
|
450 |
public void set(String name, Float obj) throws PersistenceException { |
451 |
setValue(name, getObjectToPersist(obj, name)); |
452 |
} |
453 |
|
454 |
public void set(String name, Double obj) throws PersistenceException { |
455 |
setValue(name, getObjectToPersist(obj, name)); |
456 |
} |
457 |
|
458 |
public void set(String name, Date obj) throws PersistenceException { |
459 |
setValue(name, getObjectToPersist(obj, name)); |
460 |
} |
461 |
|
462 |
protected Object getObjectToPersist(Object theOriginal) |
463 |
throws PersistenceException {
|
464 |
return getObjectToPersist(theOriginal, null); |
465 |
} |
466 |
|
467 |
/**
|
468 |
* Checks class of <code>theOriginal</code> and transforms it if is
|
469 |
* necessary.<br>
|
470 |
*
|
471 |
* @param theOriginal
|
472 |
* @return
|
473 |
* @throws PersistenceException
|
474 |
*/
|
475 |
private Object getObjectToPersist(Object theOriginal, String propName) |
476 |
throws PersistenceException {
|
477 |
DynStruct definition = null;
|
478 |
DynField field = null;
|
479 |
try {
|
480 |
if (theOriginal == null) { |
481 |
return null; |
482 |
} else if (validStdTypeClasses.contains(theOriginal.getClass())) { |
483 |
// FIXME: No esta comprobando que el tipo sea el correcto
|
484 |
return theOriginal;
|
485 |
} |
486 |
if (theOriginal instanceof File) { |
487 |
// File no es una clase final, asi que convertimos
|
488 |
// a File para curarnos en salud.
|
489 |
// FIXME: No esta comprobando que el tipo sea el correcto
|
490 |
return new File(((File) theOriginal).toURI()); |
491 |
} |
492 |
definition = this.getDefinitionCheckNull();
|
493 |
if (propName != null) { |
494 |
field = definition.getDynField(propName); |
495 |
} |
496 |
if (theOriginal instanceof Persistent) { |
497 |
if (field != null) { |
498 |
Class classOfValue = field.getClassOfValue();
|
499 |
if (classOfValue == null) { |
500 |
throw new NullPointerException( |
501 |
"Class of value for field '"
|
502 |
+ definition.getFullName() + "' of '"
|
503 |
+ field.getName() + "' can't be null.");
|
504 |
} |
505 |
if (!classOfValue.isAssignableFrom(theOriginal.getClass())) {
|
506 |
throw new PersistenceInvalidTypeForPropertyException( |
507 |
propName, definition, field, theOriginal); |
508 |
} |
509 |
} |
510 |
ObjectReference ref = context.get(theOriginal); |
511 |
if (ref == null) { |
512 |
PersistentStateServices state = manager.createState( |
513 |
theOriginal, context); |
514 |
ref = context.get(state); |
515 |
} |
516 |
return ref;
|
517 |
|
518 |
} else if ((manager.getFactories().get(theOriginal)) != null) { |
519 |
if (field != null) { |
520 |
Class classOfValue = field.getClassOfValue();
|
521 |
if (classOfValue == null) { |
522 |
throw new NullPointerException( |
523 |
"Class of value for field '"
|
524 |
+ definition.getFullName() + "' of '"
|
525 |
+ field.getName() + "' can't be null.");
|
526 |
} |
527 |
if (!classOfValue.isAssignableFrom(theOriginal.getClass())) {
|
528 |
throw new PersistenceInvalidTypeForPropertyException( |
529 |
propName, definition, field, theOriginal); |
530 |
} |
531 |
} |
532 |
ObjectReference ref = context.get(theOriginal); |
533 |
if (ref == null) { |
534 |
PersistentStateServices state = manager.createState( |
535 |
theOriginal, context); |
536 |
ref = context.get(state); |
537 |
} |
538 |
return ref;
|
539 |
|
540 |
} else if (theOriginal instanceof Map) { |
541 |
if (field != null && field.getType() != DataTypes.MAP) { |
542 |
throw new PersistenceInvalidTypeForPropertyException( |
543 |
propName, definition, field, theOriginal); |
544 |
} |
545 |
Map result = new LinkedHashMap(((Map) theOriginal).size()); |
546 |
storeValues(result, (Map) theOriginal);
|
547 |
return result;
|
548 |
|
549 |
} else if (theOriginal instanceof Set) { |
550 |
if (field != null && field.getType() != DataTypes.SET) { |
551 |
throw new PersistenceInvalidTypeForPropertyException( |
552 |
propName, definition, field, theOriginal); |
553 |
} |
554 |
Set result = new LinkedHashSet(((Set) theOriginal).size()); |
555 |
storeValues(result, ((Set) theOriginal).iterator());
|
556 |
return result;
|
557 |
|
558 |
} else if (theOriginal instanceof List) { |
559 |
if (field != null |
560 |
&& !(field.getType() == DataTypes.LIST || field |
561 |
.getType() == DataTypes.ARRAY)) { |
562 |
throw new PersistenceInvalidTypeForPropertyException( |
563 |
propName, definition, field, theOriginal); |
564 |
} |
565 |
List result = new ArrayList(((List) theOriginal).size()); |
566 |
storeValues(result, (((List) theOriginal).iterator()));
|
567 |
return result;
|
568 |
|
569 |
} else if (theOriginal instanceof Iterator) { |
570 |
if (field != null |
571 |
&& !(field.getType() == DataTypes.LIST || field |
572 |
.getType() == DataTypes.ARRAY)) { |
573 |
throw new PersistenceInvalidTypeForPropertyException( |
574 |
propName, definition, field, theOriginal); |
575 |
} |
576 |
List result = new ArrayList(); |
577 |
|
578 |
storeValues(result, (Iterator) theOriginal);
|
579 |
return result;
|
580 |
|
581 |
} else {
|
582 |
if (this.manager.getAutoValidation() == PersistenceManager.MANDATORY) { |
583 |
throw new PersistenceTypeNotSupportedException( |
584 |
theOriginal.getClass(), propName); |
585 |
} else {
|
586 |
logger.error("Persistence type '"
|
587 |
+ theOriginal.getClass().getName() |
588 |
+ "' not supported.");
|
589 |
return null; |
590 |
} |
591 |
} |
592 |
} catch (PersistenceException ex) {
|
593 |
PersistentContextServices context = (PersistentContextServices) this
|
594 |
.getContext(); |
595 |
if (!context.getCollectErrors()) {
|
596 |
throw ex;
|
597 |
} |
598 |
context.addError(ex); |
599 |
return null; |
600 |
} catch (Exception ex) { |
601 |
PersistentContextServices context = (PersistentContextServices) this
|
602 |
.getContext(); |
603 |
if (!context.getCollectErrors()) {
|
604 |
throw new PersistenceException(ex); |
605 |
} |
606 |
context.addError(ex); |
607 |
return null; |
608 |
} |
609 |
} |
610 |
|
611 |
/*
|
612 |
* (non-Javadoc)
|
613 |
*
|
614 |
* @see
|
615 |
* org.gvsig.tools.persistence.impl.ImplementationPersistentState#getId()
|
616 |
*/
|
617 |
public PersistentIdentifier getId() {
|
618 |
return id;
|
619 |
} |
620 |
|
621 |
public void setValue(String name, Object value) throws PersistenceException { |
622 |
try {
|
623 |
if (this.theClassName == null) { |
624 |
throw new PersistenceIllegalStateTheClassNameNotSetException(); |
625 |
} |
626 |
if (!VALID_NAME_PATTERN.matcher(name).matches()) {
|
627 |
throw new PersistenceInvalidPropertyNameException(name); |
628 |
} |
629 |
this.values.put(name, value);
|
630 |
|
631 |
} catch (PersistenceException ex) {
|
632 |
PersistentContextServices context = (PersistentContextServices) this
|
633 |
.getContext(); |
634 |
if (!context.getCollectErrors()) {
|
635 |
throw ex;
|
636 |
} |
637 |
context.addError(ex); |
638 |
} catch (Exception ex) { |
639 |
PersistentContextServices context = (PersistentContextServices) this
|
640 |
.getContext(); |
641 |
if (!context.getCollectErrors()) {
|
642 |
throw new PersistenceException(ex); |
643 |
} |
644 |
context.addError(ex); |
645 |
} |
646 |
} |
647 |
|
648 |
public String getTheClassName() { |
649 |
return theClassName;
|
650 |
} |
651 |
|
652 |
/*
|
653 |
* (non-Javadoc)
|
654 |
*
|
655 |
* @see org.gvsig.tools.persistence.impl.ImplementationPersistentState#
|
656 |
* setTheClassName(java.lang.String)
|
657 |
*/
|
658 |
public void setTheClassName(String className) { |
659 |
theClassName = className; |
660 |
} |
661 |
|
662 |
private void storeValues(Collection storage, Iterator iter) |
663 |
throws PersistenceException {
|
664 |
while (iter.hasNext()) {
|
665 |
storage.add(getObjectToPersist(iter.next())); |
666 |
} |
667 |
|
668 |
} |
669 |
|
670 |
private void storeValues(Map storage, Map originalStorage) |
671 |
throws PersistenceException {
|
672 |
Iterator iter = originalStorage.entrySet().iterator();
|
673 |
Entry orgEntry; |
674 |
Object key;
|
675 |
Object value;
|
676 |
|
677 |
while (iter.hasNext()) {
|
678 |
orgEntry = (Entry) iter.next(); |
679 |
key = orgEntry.getKey(); |
680 |
|
681 |
if (key == null) { |
682 |
// Nothing to do
|
683 |
} else if (key instanceof String) { |
684 |
// Nothing to do
|
685 |
} else if (key instanceof Number) { |
686 |
// Nothing to do
|
687 |
} else if (key instanceof Boolean) { |
688 |
// Nothing to do
|
689 |
} else if (key instanceof Persistent) { |
690 |
key = getObjectToPersist(key); |
691 |
} else {
|
692 |
throw new PersistenceUnsuportedMapKeyTypeException(orgEntry |
693 |
.getKey().getClass()); |
694 |
} |
695 |
|
696 |
value = getObjectToPersist(orgEntry.getValue()); |
697 |
|
698 |
storage.put(key, value); |
699 |
} |
700 |
|
701 |
} |
702 |
|
703 |
/*
|
704 |
* (non-Javadoc)
|
705 |
*
|
706 |
* @see
|
707 |
* org.gvsig.tools.persistence.impl.ImplementationPersistentState#getContext
|
708 |
* ()
|
709 |
*/
|
710 |
public PersistentContext getContext() {
|
711 |
return context;
|
712 |
} |
713 |
|
714 |
// private int size() {
|
715 |
// return this.values.size();
|
716 |
// }
|
717 |
//
|
718 |
/*
|
719 |
* (non-Javadoc)
|
720 |
*
|
721 |
* @see
|
722 |
* org.gvsig.tools.persistence.impl.ImplementationPersistentState#hasValue
|
723 |
* (java.lang.String)
|
724 |
*/
|
725 |
public boolean hasValue(String name) { |
726 |
return this.values.containsKey(name); |
727 |
} |
728 |
|
729 |
public Object getValue(String name) { |
730 |
return values.get(name);
|
731 |
} |
732 |
|
733 |
//
|
734 |
// private ObjectReference createReference(Integer id) {
|
735 |
// return new DefaultObjectReference(id);
|
736 |
// }
|
737 |
|
738 |
/**
|
739 |
* Returns if the definition related to this state has a defined field
|
740 |
*/
|
741 |
private boolean definitionHasField(String name) { |
742 |
DynStruct definition = manager.getDefinition(getTheClassName()); |
743 |
return definition != null && definition.getDynField(name) != null; |
744 |
} |
745 |
|
746 |
private Object getDefaultValue(String name) { |
747 |
DynStruct definition = manager.getDefinition(getTheClassName()); |
748 |
if( definition == null ) { |
749 |
return null; |
750 |
} |
751 |
if( definition.getDynField(name)==null ) { |
752 |
return null; |
753 |
} |
754 |
return definition.getDynField(name).getDefaultValue();
|
755 |
} |
756 |
|
757 |
public void relativizeFiles(File rootFolder) { |
758 |
PersistentContext context = this.getContext();
|
759 |
|
760 |
URI cwd = new File(System.getProperty("user.dir")).toURI(); |
761 |
Iterator statesIterator = context.iterator();
|
762 |
while (statesIterator.hasNext()) {
|
763 |
PersistentState aState = (PersistentState) statesIterator.next(); |
764 |
DynStruct definition = aState.getDefinition(); |
765 |
DynField[] fields = definition.getDynFields();
|
766 |
for( int i=0; i<fields.length; i++) { |
767 |
DynField field = fields[i]; |
768 |
if (field.getType() == DataTypes.FILE
|
769 |
|| field.getType() == DataTypes.FOLDER) { |
770 |
try {
|
771 |
File value = aState.getFile(field.getName());
|
772 |
value = FileTools.relativizeFile(rootFolder, value); |
773 |
aState.set(field.getName(), value); |
774 |
} catch (PersistenceException e) {
|
775 |
logger.warn( |
776 |
"Can't relativice field '" + field.getName()
|
777 |
+ "' for class '"
|
778 |
+ definition.getName() + "'.", e);
|
779 |
} |
780 |
} else if (field.getType() == DataTypes.URL) { |
781 |
try {
|
782 |
URL value = aState.getURL(field.getName());
|
783 |
if ("FILE".equalsIgnoreCase(value.getProtocol())) { |
784 |
File file = FileUtils.toFile(value);
|
785 |
if (FileTools.isInDifferentDrive(rootFolder,file)) {
|
786 |
/*
|
787 |
* Different drives
|
788 |
* (should happen only in Windows)
|
789 |
*/
|
790 |
value = file.toURI().toURL(); |
791 |
} else {
|
792 |
/*
|
793 |
* Both in the Same drive (C: D: etc) or Unix-style
|
794 |
*/
|
795 |
file = FileTools.relativizeFile(rootFolder,file); |
796 |
value = new URL("file","",file.toURI().toString().substring(cwd.toString().length())); |
797 |
} |
798 |
aState.set(field.getName(), value); |
799 |
} |
800 |
} catch (PersistenceException e) {
|
801 |
// do nothind
|
802 |
} catch (MalformedURLException e) { |
803 |
// do nothind
|
804 |
} |
805 |
} else if (field.getType() == DataTypes.URI) { |
806 |
try {
|
807 |
URI value = aState.getURI(field.getName());
|
808 |
if (value.getScheme() == null || "FILE".equalsIgnoreCase(value.getScheme())) { |
809 |
File file = new File(value.getPath()); |
810 |
if (FileTools.isInDifferentDrive(rootFolder,file)) {
|
811 |
/*
|
812 |
* Different drives
|
813 |
* (should happen only in Windows)
|
814 |
*/
|
815 |
value = file.toURI(); |
816 |
} else {
|
817 |
/*
|
818 |
* Both in the Same drive (C: D: etc) or Unix-style
|
819 |
*/
|
820 |
file = FileTools.relativizeFile(rootFolder,file); |
821 |
value = new URI(file.getPath()); |
822 |
} |
823 |
aState.set(field.getName(), value); |
824 |
} |
825 |
} catch (PersistenceException e) {
|
826 |
// do nothind
|
827 |
} catch (URISyntaxException e) { |
828 |
// do nothind
|
829 |
} |
830 |
} |
831 |
} |
832 |
} |
833 |
} |
834 |
|
835 |
public void derelativizeFiles(File rootFolder) { |
836 |
PersistentContext context = this.getContext();
|
837 |
//URI cwd = new File(System.getProperty("user.dir")).toURI();
|
838 |
|
839 |
Iterator statesIterator = context.iterator();
|
840 |
String className = null; |
841 |
|
842 |
while (statesIterator.hasNext()) {
|
843 |
PersistentState aState = (PersistentState) statesIterator.next(); |
844 |
try {
|
845 |
className = aState.getTheClassName(); |
846 |
} catch (Throwable e) { |
847 |
className = "Unknown";
|
848 |
} |
849 |
try {
|
850 |
DynStruct definition = aState.getDefinition(); |
851 |
DynField[] fields = definition.getDynFields();
|
852 |
for (int i = 0; i < fields.length; i++) { |
853 |
DynField field = fields[i]; |
854 |
if (field.getType() == DataTypes.FILE
|
855 |
|| field.getType() == DataTypes.FOLDER) { |
856 |
try {
|
857 |
File value = aState.getFile(field.getName());
|
858 |
value = FileTools.derelativizeFile(rootFolder, value); |
859 |
aState.set(field.getName(), value); |
860 |
} catch (PersistenceException e) {
|
861 |
logger.warn( |
862 |
"Can't fix field '" + field.getName()
|
863 |
+ "' for class '"
|
864 |
+ definition.getName() + "'.", e);
|
865 |
} |
866 |
} else if (field.getType() == DataTypes.URL) { |
867 |
try {
|
868 |
URL value = aState.getURL(field.getName());
|
869 |
if ("FILE".equalsIgnoreCase(value.getProtocol())) { |
870 |
if (!value.getFile().startsWith("/")) { |
871 |
File file = FileUtils.toFile(value);
|
872 |
file = FileTools.derelativizeFile(rootFolder, file); |
873 |
value = file.toURI().toURL(); |
874 |
aState.set(field.getName(), value); |
875 |
} |
876 |
} |
877 |
} catch (PersistenceException e) {
|
878 |
// do nothing
|
879 |
} catch (MalformedURLException e) { |
880 |
// do nothing
|
881 |
} |
882 |
|
883 |
} else if (field.getType() == DataTypes.URI) { |
884 |
try {
|
885 |
URI value = aState.getURI(field.getName());
|
886 |
if (value.getScheme() == null || "FILE".equalsIgnoreCase(value.getScheme())) { |
887 |
if (!value.getPath().startsWith("/")) { |
888 |
File file = FileUtils.toFile(value.toURL());
|
889 |
file = FileTools.derelativizeFile(rootFolder, file); |
890 |
aState.set(field.getName(), file.toURI()); |
891 |
} |
892 |
} |
893 |
} catch (PersistenceException e) {
|
894 |
// do nothing
|
895 |
} catch (MalformedURLException ex) { |
896 |
// do nothing
|
897 |
} |
898 |
} |
899 |
} |
900 |
} catch (RuntimeException e) { |
901 |
logger.info("Can't fix relative paths in " + className, e);
|
902 |
} |
903 |
} |
904 |
} |
905 |
|
906 |
|
907 |
} |