Revision 28468

View differences:

branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/persistence/PersistenceRuntimeException.java
1
package org.gvsig.tools.persistence;
2

  
3
import java.util.HashMap;
4
import java.util.Map;
5

  
6
import org.gvsig.tools.exception.BaseRuntimeException;
7

  
8
public class PersistenceRuntimeException extends BaseRuntimeException {
9

  
10
	/**
11
	 *
12
	 */
13
	private static final long serialVersionUID = -3729654883985281840L;
14
	private final static String MESSAGE_FORMAT = "Error getting or setting the state of the object.";
15
	private final static String MESSAGE_KEY = "_PersistenceRuntimeException";
16

  
17
	protected Map values = new HashMap();
18

  
19
	public PersistenceRuntimeException(String messageFormat, Throwable cause,
20
			String messageKey, long code) {
21
		super(messageFormat, cause, messageKey, code);
22
	}
23

  
24
	public PersistenceRuntimeException(String messageFormat, String messageKey,
25
			long code) {
26
		super(messageFormat, messageKey, code);
27
	}
28

  
29
	public PersistenceRuntimeException(Throwable cause) {
30
		super(MESSAGE_FORMAT, cause, MESSAGE_KEY, serialVersionUID);
31
	}
32

  
33
	protected void setValue(String name, String value) {
34
		this.values.put(name, value);
35
	}
36

  
37
	protected Map values() {
38
		return this.values;
39
	}
40
}
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/persistence/PersistenceManager.java
16 16
 * @author IVER T.I. <http://www.iver.es>
17 17
 */
18 18
public interface PersistenceManager {
19
	/**
20
	 * <p>Validation Mode -- MANDATORY: Validation is active, so 
21
	 * {@link PersistenceManager#create(org.gvsig.tools.persistence.PersistentState)}
22
	 * and
23
	 * {@link PersistenceManager#getState(org.gvsig.tools.persistence.Persistent)}
24
	 * will throw validation exceptions if validation errors are found.</p>
25
	 * <p>If an undeclared attribute or class is found, this will be considered
26
	 * a validation error.</p>
27
	 */
28
	static public final int MANDATORY = 3;
29
	/**
30
	 * <p>Validation Mode -- MANDATORY_IF_DECLARED: Validation is active, but
31
	 *  it will be only applied to Persistent objectswhich have been registered.
32
	 * {@link PersistenceManager#create(org.gvsig.tools.persistence.PersistentState)}
33
	 * and
34
	 * {@link PersistenceManager#getState(org.gvsig.tools.persistence.Persistent)}
35
	 * methods will throw validation exceptions if validation errors are found.</p>
36
	 */
37
	static public final int MANDATORY_IF_DECLARED = 2;
38
	/**
39
	 * <p>Validation Mode - DISABLED: No validation is performed at all.
40
	 * In this mode, {@link PersistenceManager#ge}</p>
41
	 */
42
	static public final int DISABLED = 0;
19 43

  
20 44
	/**
21 45
	 * <p>Creates a persistent state from an Persistent object.</p>
......
86 110
	public void removeDefinition(Class persistentClass);
87 111

  
88 112
	/**
89
	 * <p>Validate a persistent state by using the corresponding registered
113
	 * <p>Validates a persistent state by using the corresponding registered
90 114
	 * attribute definition. If there is no registered definition for the class
91 115
	 * represented by the PersistenteState, validation should fail.</p>
92 116
	 *
......
132 156
	 * @return
133 157
	 */
134 158
	public PersistentState loadState(Reader reader) throws PersistenceException;
159
	
160
	/**
161
	 * <p>Sets the validation which will be applied in
162
	 * {@link #getState(Persistent)}, {@link #create(PersistentState)}
163
	 * methods. Validation ensures that persisted or de-persisted objects
164
	 * match the declared definition (which must have been previously
165
	 * registered by using {@link #addDefinition(Class, DynStruct)}).</p>
166
	 * 
167
	 * <p>When automatic validation is enabled (MANDATORY or
168
	 * MANDATORY_IF_DECLARED), a ValidationException will be thrown by
169
	 * {@link #getState(Persistent)}, {@link #create(PersistentState)}
170
	 * if a validation error is found.</p>
171
	 * 
172
	 * @param validationMode On of the following values:
173
	 * {@link #DISABLED}, {@link #MANDATORY}
174
	 * or {@link #MANDATORY_IF_DECLARED}
135 175

  
176
	 * @see #validate(PersistentState)
177
	 * @see #addDefinition(Class, DynStruct)
178
	 * 
179
	 * @throws PersistenceException If the mode is not supported by this manager
180
	 */
181
	public void setAutoValidation(int validationMode) throws PersistenceException;
182
	
183
	/**
184
	 * <p>Gets the validation which will be applied in
185
	 * {@link #getState(Persistent)}, {@link #create(PersistentState)} methods.
186
	 *
187
	 * @return The current mode for automatic validation: {@link #DISABLED},
188
	 * {@link #MANDATORY} or {@link #MANDATORY_IF_DECLARED}
189
	 * 
190
	 * @see #validate(PersistentState)
191
	 * @see #addDefinition(Class, DynStruct)
192
	 */
193
	public int getAutoValidation();
194

  
136 195
}
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/persistence/Persistent.java
15 15
	 *
16 16
	 * @param state
17 17
	 */
18
	public void setState(PersistentState state) throws PersistenceException;
18
	public void loadFromState(PersistentState state) throws PersistenceException;
19 19

  
20 20
}
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/persistence/PersistentState.java
70 70
			throws PersistenceValueNotFoundException;
71 71

  
72 72
	/**
73
	 * Gets a <code>PersistentState</code> property.
74
	 * @param name The name of the property to get
75
	 * 
76
	 * @return The <code>PersistentState</code> property associated to the provided name
77
	 * @throws PersistenceValueNotFoundException
78
	 */
79
	public PersistentState getState(String name)
80
			throws PersistenceValueNotFoundException, PersistenceException;
81

  
82
	/**
83 73
	 * Gets an <code>Object</code> property.
84 74
	 * @param name The name of the property to get
85 75
	 * 
......
161 151
	public void set(String name, boolean value);
162 152

  
163 153
	/**
164
	 * <p>Sets a property of type PersistentState.</p>
165
	 * 
166
	 * @param name The name of the property to store
167
	 * @param it The PersistentState object to be stored in the state.
168
	 * 
169
	 * @throws PersistenceException
170
	 */
171
	public void set(String name, PersistentState state) throws PersistenceException;
172

  
173
	/**
174 154
	 * <p>Sets a property of type Persistent.</p>
175 155
	 * 
176 156
	 * @param name The name of the property to store
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/persistence/impl/AbstractPersistenceManager.java
53 53
					theClass = Class.forName(className);
54 54
				}
55 55
				Persistent obj = (Persistent) theClass.newInstance();
56
				obj.setState(state);
56
				obj.loadFromState(state);
57 57
				return obj;
58 58
			} catch (ClassNotFoundException e) {
59 59
				throw new PersistenceException(e);
......
94 94
		state.load(reader);
95 95
		return state;
96 96
	}
97

  
98
	public int getAutoValidation() {
99
		return DISABLED;
100
	}
101

  
102
	public void setAutoValidation(int validationMode) throws PersistenceException {
103
		if (validationMode!=DISABLED) {
104
			throw new PersistenceException("Validation not implemented");
105
		}
106
	}
97 107
}
branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/persistence/PersistenceException.java
3 3
import java.util.HashMap;
4 4
import java.util.Map;
5 5

  
6
import org.gvsig.tools.exception.BaseException;
6
import org.gvsig.tools.exception.ListBaseException;
7 7

  
8
public class PersistenceException extends BaseException {
8
public class PersistenceException extends ListBaseException {
9 9
	private static final long serialVersionUID = -3729654883985281840L;
10 10
	private final static String MESSAGE_FORMAT = "Error getting or setting the state of the object.";
11 11
	private final static String MESSAGE_KEY = "_PersistenceException";
branches/v2_0_0_prep/libraries/libIverUtiles/src-test/com/iver/utiles/TestXMLEntityPersistenceManager.java
392 392
		try {
393 393
			PersistentState state = ToolsLocator.getPersistenceManager().getState(originalList);
394 394
			MyPersistentList resultList = new MyPersistentList();
395
			resultList.setState(state);
395
			resultList.loadFromState(state);
396 396
			checkEquals(originalList, resultList);
397 397
 			
398 398
		} catch (LocatorException e) {
......
444 444
			
445 445
		}
446 446

  
447
		public void setState(PersistentState state) throws PersistenceException {
447
		public void loadFromState(PersistentState state) throws PersistenceException {
448 448
			Iterator it = state.getIterator("myList");
449 449
			list = new ArrayList();
450 450
			while (it.hasNext()) {
......
483 483
			value = val;
484 484
		}
485 485

  
486
		public void setState(PersistentState state) throws PersistenceException {
486
		public void loadFromState(PersistentState state) throws PersistenceException {
487 487
			value = state.getString("miValor");
488 488
			intValue = state.getInt("numero");
489 489
		}
......
528 528
			value3 = val;
529 529
		}
530 530

  
531
		public void setState(PersistentState state) throws PersistenceException {
531
		public void loadFromState(PersistentState state) throws PersistenceException {
532 532
			value1 = state.get("miObjetito1");
533 533
			value2 = state.get("miObjetito2");
534 534
			value3 = state.get("miObjetito3");
branches/v2_0_0_prep/libraries/libIverUtiles/src/org/gvsig/tools/persistence/xmlentity/XMLEntityState.java
98 98
		return manager.create(manager.createState(value));
99 99
	}
100 100

  
101
	public PersistentState getState(String name)
102
			throws PersistenceException {
103
		XMLEntity value = this.getData().firstChild(name);
104
		if (value == null) {
105
			throw new PersistenceValueNotFoundException(name);
106
		}
107
		return manager.createState(value);
108
	}
109

  
110 101
	public boolean getBoolean(String name)
111 102
			throws PersistenceValueNotFoundException {
112 103
		try {
......
178 169
		this.getData().putProperty(name, value);
179 170
	}
180 171

  
181
	public void set(String name, PersistentState state) throws PersistenceException {
182
		if (state instanceof XMLEntityState) {
183
			XMLEntityState lstate = (XMLEntityState) state;
184

  
185
			XMLEntityState wrappingState = manager.createStateInstance();
186
			wrappingState.setTheClass(PersistentState.class);
187
			wrappingState.setName(name);
188
			wrappingState.getData().addChild(lstate.getXMLEntity());
189
			this.getData().addChild(wrappingState.xmlEntity);
190
		}
191
		else {
192
			// TODO if the state comes from a different implementation, use 
193
			// TODO the names iterator to get all the values and persist them 
194
			throw new PersistenceException("set(String, PersistentState): Not yet implemented");
195
		}
196
	}
197

  
198 172
	public void set(String name, Persistent obj)
199 173
			throws PersistenceException {
200 174
		XMLEntityState state = (XMLEntityState) manager.getState(obj);
......
254 228
		public void remove() {
255 229
			throw new UnsupportedOperationException();
256 230
		}
257
		public void setState(PersistentState state) throws PersistenceException {
231
		public void loadFromState(PersistentState state) throws PersistenceException {
258 232
			if (state instanceof XMLEntityState) {
259 233
				this.state = (XMLEntityState) state;
260 234
				current = 0;

Also available in: Unified diff