Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libTools / src / org / gvsig / tools / persistence / impl / AbstractPersistenceManager.java @ 28076

History | View | Annotate | Download (2.71 KB)

1
package org.gvsig.tools.persistence.impl;
2

    
3
import java.io.Reader;
4
import java.util.HashMap;
5
import java.util.Iterator;
6
import java.util.Map;
7

    
8
import org.gvsig.tools.dynobject.DynStruct;
9
import org.gvsig.tools.persistence.PersistenceException;
10
import org.gvsig.tools.persistence.PersistenceManager;
11
import org.gvsig.tools.persistence.Persistent;
12
import org.gvsig.tools.persistence.PersistentState;
13
import org.gvsig.tools.persistence.impl.validation.DefaultValidationResult;
14
import org.gvsig.tools.persistence.validation.ValidationResult;
15

    
16
public abstract class AbstractPersistenceManager implements PersistenceManager {
17
        protected HashMap definitions = new HashMap();
18
        protected Map alias;
19
        
20
        public PersistentState getState(Persistent obj)
21
                        throws PersistenceException {
22
                PersistentState state = this.createStateInstance();
23
                obj.saveToState(state);
24
                return state;
25
        }
26

    
27
        protected AbstractPersistenceManager() {
28
                alias = new HashMap();
29
        }
30

    
31
        public void addAlias(String name, Class aClass) {
32
                alias.put(name, aClass);
33
        }
34
        
35
        public abstract PersistentState createStateInstance();
36

    
37
        public Object create(PersistentState state) throws PersistenceException {
38
                try {
39
                        AbstractPersistentState myState = (AbstractPersistentState) state;
40
                        String className = myState.getTheClassName();
41
                        if (className == null) {
42
                                throw new PersistenceException(null); // FIXME
43
                        }
44
                        try {
45
                                Class theClass;
46

    
47
                                Object x = alias.get(className);
48
                                if (x instanceof Class) {
49
                                        theClass = (Class) x;
50
                                } else if (x instanceof String) {
51
                                        theClass = Class.forName((String) x);
52
                                } else { // x is null
53
                                        theClass = Class.forName(className);
54
                                }
55
                                Persistent obj = (Persistent) theClass.newInstance();
56
                                obj.setState(state);
57
                                return obj;
58
                        } catch (ClassNotFoundException e) {
59
                                throw new PersistenceException(e);
60
                        } catch (InstantiationException e) {
61
                                throw new PersistenceException(e);
62
                        } catch (IllegalAccessException e) {
63
                                throw new PersistenceException(e);
64
                        }
65

    
66
                }
67
                catch (ClassCastException ex) {
68
                        throw new PersistenceException(ex);
69
                }
70
        }
71

    
72
        public void addDefinition(Class persistentClass, DynStruct definition) {
73
                definitions.put(persistentClass, definition);
74
        }
75

    
76
        public DynStruct getDefinition(Class persistentClass) {
77
                return (DynStruct) definitions.get(persistentClass);
78
        }
79

    
80
        public Iterator getPersistentClasses() {
81
                return definitions.keySet().iterator();
82
        }
83

    
84
        public void removeDefinition(Class persistentClass) {
85
                definitions.remove(persistentClass);
86
        }
87

    
88
        public ValidationResult validate(PersistentState state) {
89
                return new DefaultValidationResult();
90
        }
91

    
92
        public PersistentState loadState(Reader reader) throws PersistenceException {
93
                PersistentState state = createStateInstance();
94
                state.load(reader);
95
                return state;
96
        }
97
}