Revision 29956 branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/persistence/impl/AbstractPersistenceManager.java
AbstractPersistenceManager.java | ||
---|---|---|
1 | 1 |
package org.gvsig.tools.persistence.impl; |
2 | 2 |
|
3 |
import java.io.Reader; |
|
4 | 3 |
import java.util.HashMap; |
5 | 4 |
import java.util.Iterator; |
6 | 5 |
import java.util.Map; |
7 | 6 |
|
7 |
import org.gvsig.tools.dynobject.DynField; |
|
8 | 8 |
import org.gvsig.tools.dynobject.DynStruct; |
9 | 9 |
import org.gvsig.tools.persistence.PersistenceException; |
10 | 10 |
import org.gvsig.tools.persistence.PersistenceManager; |
11 |
import org.gvsig.tools.persistence.PersistenceValidateExceptions; |
|
11 | 12 |
import org.gvsig.tools.persistence.Persistent; |
12 | 13 |
import org.gvsig.tools.persistence.PersistentState; |
13 |
import org.gvsig.tools.persistence.impl.validation.DefaultValidationResult; |
|
14 |
import org.gvsig.tools.persistence.validation.ValidationResult; |
|
14 |
import org.gvsig.tools.persistence.impl.exception.PersistenceInvalidValidateModeException; |
|
15 |
import org.gvsig.tools.persistence.impl.exception.PersistenceValidateMissingDefinitionException; |
|
16 |
import org.gvsig.tools.persistence.impl.exception.PersistenceValidateMissingPropertyException; |
|
15 | 17 |
|
16 |
public abstract class AbstractPersistenceManager implements PersistenceManager { |
|
18 |
public abstract class AbstractPersistenceManager implements |
|
19 |
ImplementationPersistenceManager { |
|
17 | 20 |
protected HashMap definitions = new HashMap(); |
18 | 21 |
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 |
} |
|
22 |
protected int autoValidationMode = PersistenceManager.DISABLED; |
|
26 | 23 |
|
24 |
|
|
27 | 25 |
protected AbstractPersistenceManager() { |
28 | 26 |
alias = new HashMap(); |
29 | 27 |
} |
... | ... | |
31 | 29 |
public void addAlias(String name, Class aClass) { |
32 | 30 |
alias.put(name, aClass); |
33 | 31 |
} |
34 |
|
|
35 |
public abstract PersistentState createStateInstance(); |
|
36 | 32 |
|
37 |
public Object create(PersistentState state) throws PersistenceException { |
|
33 |
/* (non-Javadoc) |
|
34 |
* @see org.gvsig.tools.persistence.impl.ImplementationPersistenceManager#createInstaceOfObject(org.gvsig.tools.persistence.PersistentState) |
|
35 |
*/ |
|
36 |
public Persistent createInstaceOfObject(PersistentState state) |
|
37 |
throws PersistenceException { |
|
38 |
String className = state.getTheClassName(); |
|
39 |
if (className == null) { |
|
40 |
throw new PersistenceException("The class name is not stored in the state."); |
|
41 |
} |
|
38 | 42 |
try { |
39 |
AbstractPersistentState myState = (AbstractPersistentState) state; |
|
40 |
String className = myState.getTheClassName(); |
|
41 |
if (className == null) { |
|
42 |
throw new PersistenceException("The class name is not stored in the state."); |
|
43 |
} |
|
44 |
try { |
|
45 |
Class theClass; |
|
43 |
Class theClass; |
|
46 | 44 |
|
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.loadFromState(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); |
|
45 |
Object x = alias.get(className); |
|
46 |
if (x instanceof Class) { |
|
47 |
theClass = (Class) x; |
|
48 |
} else if (x instanceof String) { |
|
49 |
theClass = Class.forName((String) x); |
|
50 |
} else { // x is null |
|
51 |
theClass = Class.forName(className); |
|
64 | 52 |
} |
53 |
return (Persistent) theClass.newInstance(); |
|
54 |
} catch (ClassNotFoundException e) { |
|
55 |
throw new PersistenceException(e); |
|
56 |
} catch (InstantiationException e) { |
|
57 |
throw new PersistenceException(e); |
|
58 |
} catch (IllegalAccessException e) { |
|
59 |
throw new PersistenceException(e); |
|
60 |
} |
|
65 | 61 |
|
62 |
} |
|
63 |
|
|
64 |
|
|
65 |
public Persistent create(PersistentState state) throws PersistenceException { |
|
66 |
String className = state.getTheClassName(); |
|
67 |
if (className == null) { |
|
68 |
throw new PersistenceException("The class name is not stored in the state."); |
|
66 | 69 |
} |
67 |
catch (ClassCastException ex) { |
|
68 |
throw new PersistenceException(ex); |
|
69 |
} |
|
70 |
Persistent obj = createInstaceOfObject(state); |
|
71 |
obj.loadFromState(state); |
|
72 |
return obj; |
|
73 |
|
|
70 | 74 |
} |
71 | 75 |
|
72 | 76 |
public void addDefinition(Class persistentClass, DynStruct definition) { |
73 |
definitions.put(persistentClass, definition); |
|
77 |
definitions.put(persistentClass.getName(), definition);
|
|
74 | 78 |
} |
75 | 79 |
|
76 | 80 |
public DynStruct getDefinition(Class persistentClass) { |
77 |
return (DynStruct) definitions.get(persistentClass); |
|
81 |
return (DynStruct) definitions.get(persistentClass.getName());
|
|
78 | 82 |
} |
79 | 83 |
|
84 |
/* (non-Javadoc) |
|
85 |
* @see org.gvsig.tools.persistence.impl.ImplementationPersistenceManager#getDefinition(java.lang.String) |
|
86 |
*/ |
|
87 |
public DynStruct getDefinition(String persistentClassName) { |
|
88 |
return (DynStruct) definitions.get(persistentClassName); |
|
89 |
} |
|
90 |
|
|
91 |
|
|
80 | 92 |
public Iterator getPersistentClasses() { |
81 | 93 |
return definitions.keySet().iterator(); |
82 | 94 |
} |
83 | 95 |
|
84 | 96 |
public void removeDefinition(Class persistentClass) { |
85 |
definitions.remove(persistentClass); |
|
97 |
definitions.remove(persistentClass.getName());
|
|
86 | 98 |
} |
87 | 99 |
|
88 |
public ValidationResult validate(PersistentState state) {
|
|
89 |
return new DefaultValidationResult();
|
|
100 |
public void validate(PersistentState state) throws PersistenceValidateExceptions {
|
|
101 |
validate(state,PersistenceManager.MANDATORY);
|
|
90 | 102 |
} |
91 | 103 |
|
92 |
public PersistentState loadState(Reader reader) throws PersistenceException { |
|
93 |
PersistentState state = createStateInstance(); |
|
94 |
state.load(reader); |
|
95 |
return state; |
|
104 |
public void validate(PersistentState state, int mode) |
|
105 |
throws PersistenceValidateExceptions { |
|
106 |
|
|
107 |
if (mode == PersistenceManager.DISABLED){ |
|
108 |
return; |
|
109 |
} |
|
110 |
|
|
111 |
PersistenceValidateExceptions exceptionList = new PersistenceValidateExceptions(); |
|
112 |
|
|
113 |
PersistentContext context = ((ImplementationPersistentState) state) |
|
114 |
.getContext(); |
|
115 |
|
|
116 |
Iterator statesIter = context.statesIterator(); |
|
117 |
ImplementationPersistentState curState; |
|
118 |
DynStruct curDefinition; |
|
119 |
DynField[] fields; |
|
120 |
|
|
121 |
while (statesIter.hasNext()) { |
|
122 |
curState = (ImplementationPersistentState) statesIter.next(); |
|
123 |
curDefinition = this.getDefinition(curState.getTheClassName()); |
|
124 |
if (curDefinition == null){ |
|
125 |
if (mode == PersistenceManager.MANDATORY){ |
|
126 |
exceptionList |
|
127 |
.add(new PersistenceValidateMissingDefinitionException( |
|
128 |
curState.getTheClassName())); |
|
129 |
} |
|
130 |
continue; |
|
131 |
} |
|
132 |
fields = curDefinition.getDeclaredDynFields(); |
|
133 |
for (int i=0;i<fields.length;i++){ |
|
134 |
if (!curState.hasValue(fields[i].getName())) { |
|
135 |
exceptionList |
|
136 |
.add(new PersistenceValidateMissingPropertyException( |
|
137 |
curState.getTheClassName(), fields[i] |
|
138 |
.getName())); |
|
139 |
} |
|
140 |
} |
|
141 |
} |
|
142 |
|
|
143 |
if (exceptionList.size() > 0) { |
|
144 |
throw exceptionList; |
|
145 |
} |
|
96 | 146 |
} |
97 | 147 |
|
98 | 148 |
public int getAutoValidation() { |
99 |
return DISABLED;
|
|
149 |
return autoValidationMode;
|
|
100 | 150 |
} |
101 | 151 |
|
102 | 152 |
public void setAutoValidation(int validationMode) throws PersistenceException { |
103 |
if (validationMode!=DISABLED) { |
|
104 |
throw new PersistenceException("Validation not implemented"); |
|
153 |
switch (validationMode) { |
|
154 |
case DISABLED: |
|
155 |
case MANDATORY: |
|
156 |
case MANDATORY_IF_DECLARED: |
|
157 |
autoValidationMode = validationMode; |
|
158 |
break; |
|
159 |
default: |
|
160 |
throw new PersistenceInvalidValidateModeException(validationMode); |
|
105 | 161 |
} |
106 | 162 |
} |
163 |
|
|
164 |
public PersistentState getState(Persistent obj) |
|
165 |
throws PersistenceException, PersistenceValidateExceptions { |
|
166 |
PersistentContext context = new DefaultPersistentContext(this); |
|
167 |
ImplementationPersistentState state = createState(obj, context); |
|
168 |
context.setRoot(state.getId()); |
|
169 |
this.validate(state, this.autoValidationMode); |
|
170 |
return state; |
|
171 |
} |
|
172 |
|
|
173 |
/* (non-Javadoc) |
|
174 |
* @see org.gvsig.tools.persistence.impl.ImplementationPersistenceManager#createState(org.gvsig.tools.persistence.Persistent, org.gvsig.tools.persistence.impl.PersistentContext) |
|
175 |
*/ |
|
176 |
public ImplementationPersistentState createState(Persistent theOriginal, |
|
177 |
PersistentContext context) |
|
178 |
throws PersistenceException { |
|
179 |
|
|
180 |
ImplementationPersistentState state = createPersistentStateInstance(context); |
|
181 |
state.setTheClassName(theOriginal.getClass().getName()); |
|
182 |
context.addReference(theOriginal, state); |
|
183 |
theOriginal.saveToState(state); |
|
184 |
return state; |
|
185 |
} |
|
186 |
|
|
187 |
/* (non-Javadoc) |
|
188 |
* @see org.gvsig.tools.persistence.impl.ImplementationPersistenceManager#createPersistentStateInstance(org.gvsig.tools.persistence.impl.PersistentContext) |
|
189 |
*/ |
|
190 |
public abstract ImplementationPersistentState createPersistentStateInstance( |
|
191 |
PersistentContext context); |
|
107 | 192 |
} |
Also available in: Unified diff