Revision 24019

View differences:

trunk/libraries/libTools/src/org/gvsig/tools/persistence/PersistenceValueNotFoundException.java
1
package org.gvsig.tools.persistence;
2

  
3
public class PersistenceValueNotFoundException extends PersistenceException {
4

  
5
	/**
6
	 *
7
	 */
8
	private static final long serialVersionUID = -8365980563346330001L;
9
	private final static String MESSAGE_FORMAT = "Value '%(name)s' not found in persistent state.";
10
	private final static String MESSAGE_KEY = "_PersistenceValueNotFoundException";
11

  
12
	public PersistenceValueNotFoundException(String name, Throwable cause) {
13
		super(MESSAGE_FORMAT, cause, MESSAGE_KEY, serialVersionUID);
14
		setValue("name", name);
15
	}
16

  
17
	public PersistenceValueNotFoundException(String name) {
18
		super(MESSAGE_FORMAT, MESSAGE_KEY, serialVersionUID);
19
		setValue("name", name);
20
	}
21

  
22

  
23
}
0 24

  
trunk/libraries/libTools/src/org/gvsig/tools/persistence/PersistenceManager.java
1
package org.gvsig.tools.persistence;
2

  
3
public interface PersistenceManager {
4

  
5
	public PersistentState createState() throws PersistenceException;
6

  
7
	public Object create(PersistentState state) throws PersistenceException;
8

  
9
	public void addAlias(String name, Class aClass);
10

  
11
	public void addAlias(String name, String className);
12

  
13
}
0 14

  
trunk/libraries/libTools/src/org/gvsig/tools/persistence/Persistent.java
1
package org.gvsig.tools.persistence;
2

  
3
public interface Persistent {
4

  
5
	/**
6
	 * Get the persistent state of the object and append to the passed state.
7
	 *
8
	 * @param state
9
	 */
10
	public void getState(PersistentState state) throws PersistenceException;
11

  
12
	/**
13
	 * Set the state of the object from the state passed as parameter.
14
	 * 
15
	 * @param state
16
	 */
17
	public void setState(PersistentState state) throws PersistenceException;
18

  
19
}
0 20

  
trunk/libraries/libTools/src/org/gvsig/tools/persistence/PersistentState.java
1
package org.gvsig.tools.persistence;
2

  
3
public interface PersistentState {
4

  
5
	public void setTheClass(Object obj);
6

  
7
	public void setTheClass(Class theClass);
8

  
9
	public void setTheClass(String name);
10

  
11
	public String getTheClassName();
12

  
13
	public PersistentState createState() throws PersistenceException;
14

  
15
	public int getInt(String name) throws PersistenceValueNotFoundException;
16

  
17
	public long getLong(String name) throws PersistenceValueNotFoundException;
18

  
19
	public double getDouble(String name)
20
			throws PersistenceValueNotFoundException;
21

  
22
	public float getFloat(String name) throws PersistenceValueNotFoundException;
23

  
24
	public boolean getBoolean(String name)
25
			throws PersistenceValueNotFoundException;
26

  
27
	public PersistentState getState(String name)
28
			throws PersistenceValueNotFoundException, PersistenceException;
29

  
30
	public Object get(String name) throws PersistenceValueNotFoundException,
31
			PersistenceException;
32

  
33
	public PersistentState set(String name, String value);
34

  
35
	public PersistentState set(String name, int value);
36

  
37
	public PersistentState set(String name, long value);
38

  
39
	public PersistentState set(String name, double value);
40

  
41
	public PersistentState set(String name, float value);
42

  
43
	public PersistentState set(String name, boolean value);
44

  
45
	public PersistentState set(String name, PersistentState state);
46

  
47
	public PersistentState set(String name, Persistent obj)
48
			throws PersistenceException;
49
}
0 50

  
trunk/libraries/libTools/src/org/gvsig/tools/persistence/AbstractPersistenceManager.java
1
package org.gvsig.tools.persistence;
2

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

  
6
public abstract class AbstractPersistenceManager implements PersistenceManager {
7

  
8
	protected Map alias;
9

  
10
	protected AbstractPersistenceManager() {
11
		alias = new HashMap();
12
	}
13

  
14
	public void addAlias(String name, Class aClass) {
15
		alias.put(name, aClass);
16
	}
17

  
18
	public void addAlias(String name, String className) {
19
		alias.put(name, className);
20
	}
21

  
22
	public Object create(PersistentState state) throws PersistenceException {
23
		String className = state.getTheClassName();
24
		if (className == null) {
25
			throw new PersistenceException(null); // FIXME
26
		}
27
		try {
28
			Class theClass;
29

  
30
			Object x = alias.get(className);
31
			if (x instanceof Class) {
32
				theClass = (Class) x;
33
			} else if (x instanceof String) {
34
				theClass = Class.forName((String) x);
35
			} else { // x is null
36
				theClass = Class.forName(className);
37
			}
38
			Persistent obj = (Persistent) theClass.newInstance();
39
			obj.setState(state);
40
			return obj;
41
		} catch (ClassNotFoundException e) {
42
			throw new PersistenceException(e);
43
		} catch (InstantiationException e) {
44
			throw new PersistenceException(e);
45
		} catch (IllegalAccessException e) {
46
			throw new PersistenceException(e);
47
		}
48
	}
49

  
50
}
0 51

  
trunk/libraries/libTools/src/org/gvsig/tools/persistence/PersistenceException.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.BaseException;
7

  
8
public class PersistenceException extends BaseException {
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 = "_PersistenceException";
16

  
17
	protected Map values = new HashMap();
18

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

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

  
29
	public PersistenceException(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
}
0 41

  
trunk/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
}
0 41

  
trunk/libraries/libTools/src/org/gvsig/tools/persistence/PersistenceLocator.java
1
package org.gvsig.tools.persistence;
2

  
3
import org.gvsig.tools.locator.AbstractLocator;
4
import org.gvsig.tools.locator.Locator;
5
import org.gvsig.tools.locator.LocatorException;
6

  
7
public class PersistenceLocator extends AbstractLocator {
8

  
9
	private static final String LOCATOR_NAME = "PersistenceLocator";
10

  
11
	public static final String DATA_MANAGER_NAME = "PersistenceManager";
12

  
13
	private static final String DATA_MANAGER_DESCRIPTION = "PersistenceManager of gvSIG";
14

  
15
	/**
16
	 * Unique instance.
17
	 */
18
	private static final PersistenceLocator instance = new PersistenceLocator();
19

  
20
	/**
21
	 * Return the singleton instance.
22
	 *
23
	 * @return the singleton instance
24
	 */
25
	public static PersistenceLocator getInstance() {
26
		return instance;
27
	}
28

  
29
	public String getLocatorName() {
30
		return LOCATOR_NAME;
31
	}
32

  
33
	/**
34
	 * Return a reference to PersistenceManager.
35
	 *
36
	 * @return a reference to PersistenceManager
37
	 * @throws LocatorException
38
	 *             if there is no access to the class or the class cannot be
39
	 *             instantiated
40
	 * @see Locator#get(String)
41
	 */
42
	public static PersistenceManager getPersistenceManager()
43
			throws LocatorException {
44
		return (PersistenceManager) getInstance().get(DATA_MANAGER_NAME);
45
	}
46

  
47
	/**
48
	 * Registers the Class implementing the PersistenceManager interface.
49
	 *
50
	 * @param clazz
51
	 *            implementing the DataManager interface
52
	 */
53
	public static void registerPersistenceManager(Class clazz) {
54
		getInstance().register(DATA_MANAGER_NAME, DATA_MANAGER_DESCRIPTION,
55
				clazz);
56
	}
57
}
0 58

  
branches/v2_0_0_prep/libraries/libIverUtiles/src/com/iver/utiles/xmlentity/XMLEntityState.java
1
package com.iver.utiles.xmlentity;
2

  
3
import org.gvsig.tools.persistence.PersistenceException;
4
import org.gvsig.tools.persistence.PersistenceValueNotFoundException;
5
import org.gvsig.tools.persistence.Persistent;
6
import org.gvsig.tools.persistence.PersistentState;
7

  
8
import com.iver.utiles.NotExistInXMLEntity;
9
import com.iver.utiles.XMLEntity;
10

  
11
public class XMLEntityState implements PersistentState {
12

  
13
	final private static String KEY_CLASSNAME = "_classname_";
14
	final private static String KEY_CHILD = "_child_";
15

  
16
	protected XMLEntity xmlEntity;
17
	protected XMLEntityManager manager;
18

  
19
	public XMLEntityState(XMLEntityManager manager) {
20
		this.manager = manager;
21
		this.xmlEntity = new XMLEntity();
22
	}
23

  
24
	public XMLEntityState(XMLEntityManager manager, XMLEntity xmlEntity) {
25
		this.manager = manager;
26
		this.xmlEntity = xmlEntity;
27
	}
28

  
29
	public XMLEntity getXMLEntity() {
30
		return this.xmlEntity;
31
	}
32

  
33
	private PersistentState createState(XMLEntity xmlEntity)
34
			throws PersistenceException {
35
		return manager.createState(xmlEntity);
36
	}
37

  
38
	public PersistentState createState() throws PersistenceException {
39
		return manager.createState();
40
	}
41

  
42
	public void setTheClass(Object obj) {
43
		this.xmlEntity.putProperty(KEY_CLASSNAME, obj.getClass().getName());
44
	}
45

  
46
	public void setTheClass(Class theClass) {
47
		this.xmlEntity.putProperty(KEY_CLASSNAME, theClass.getName());
48
	}
49

  
50
	public void setTheClass(String name) {
51
		this.xmlEntity.putProperty(KEY_CLASSNAME, name);
52
	}
53

  
54
	public String getTheClassName() {
55
		try {
56
			return this.xmlEntity.getStringProperty(KEY_CLASSNAME);
57
		} catch (NotExistInXMLEntity e) {
58
			return null;
59
		}
60
	}
61

  
62
	public Object get(String name) throws PersistenceException {
63
		XMLEntity value = this.xmlEntity.firstChild(KEY_CHILD, name);
64
		if( value == null ) {
65
			try {
66
				return this.xmlEntity.getStringProperty(name);
67
			} catch (NotExistInXMLEntity e) {
68
				throw new PersistenceValueNotFoundException(name, e);
69
			}
70
		}
71
		return manager.create(createState(value));
72
	}
73

  
74
	public PersistentState getState(String name)
75
			throws PersistenceException {
76
		XMLEntity value = this.xmlEntity.firstChild(KEY_CHILD, name);
77
		if (value == null) {
78
			throw new PersistenceValueNotFoundException(name);
79
		}
80
		return createState(value);
81
	}
82

  
83
	public boolean getBoolean(String name)
84
			throws PersistenceValueNotFoundException {
85
		try {
86
			return this.xmlEntity.getBooleanProperty(name);
87
		} catch (NotExistInXMLEntity e) {
88
			throw new PersistenceValueNotFoundException(name, e);
89
		}
90
	}
91

  
92
	public double getDouble(String name)
93
			throws PersistenceValueNotFoundException {
94
		try {
95
			return this.xmlEntity.getDoubleProperty(name);
96
		} catch (NotExistInXMLEntity e) {
97
			throw new PersistenceValueNotFoundException(name, e);
98
		}
99
	}
100

  
101
	public float getFloat(String name) throws PersistenceValueNotFoundException {
102
		try {
103
			return this.xmlEntity.getFloatProperty(name);
104
		} catch (NotExistInXMLEntity e) {
105
			throw new PersistenceValueNotFoundException(name, e);
106
		}
107
	}
108

  
109
	public int getInt(String name) throws PersistenceValueNotFoundException {
110
		try {
111
			return this.xmlEntity.getIntProperty(name);
112
		} catch (NotExistInXMLEntity e) {
113
			throw new PersistenceValueNotFoundException(name, e);
114
		}
115
	}
116

  
117
	public long getLong(String name) throws PersistenceValueNotFoundException {
118
		try {
119
			return this.xmlEntity.getLongProperty(name);
120
		} catch (NotExistInXMLEntity e) {
121
			throw new PersistenceValueNotFoundException(name, e);
122
		}
123
	}
124

  
125
	public PersistentState set(String name, String value) {
126
		this.xmlEntity.putProperty(name, value);
127
		return this;
128
	}
129

  
130
	public PersistentState set(String name, int value) {
131
		this.xmlEntity.putProperty(name, value);
132
		return this;
133
	}
134

  
135
	public PersistentState set(String name, long value) {
136
		this.xmlEntity.putProperty(name, value);
137
		return this;
138
	}
139

  
140
	public PersistentState set(String name, double value) {
141
		this.xmlEntity.putProperty(name, value);
142
		return this;
143
	}
144

  
145
	public PersistentState set(String name, float value) {
146
		this.xmlEntity.putProperty(name, value);
147
		return this;
148
	}
149

  
150
	public PersistentState set(String name, boolean value) {
151
		this.xmlEntity.putProperty(name, value);
152
		return this;
153
	}
154

  
155
	public PersistentState set(String name, PersistentState state) {
156
		XMLEntityState lstate = (XMLEntityState) state;
157
		lstate.xmlEntity.putProperty("_child_", name);
158
		this.xmlEntity.addChild(lstate.xmlEntity);
159
		return this;
160
	}
161

  
162
	public PersistentState set(String name, Persistent obj)
163
			throws PersistenceException {
164
		XMLEntityState state = (XMLEntityState) createState();
165
		obj.getState(state);
166
		state.xmlEntity.putProperty("_child_", name);
167
		this.xmlEntity.addChild(state.xmlEntity);
168
		return this;
169
	}
170

  
171
}
0 172

  
branches/v2_0_0_prep/libraries/libIverUtiles/src/com/iver/utiles/xmlentity/XMLEntityManager.java
1
package com.iver.utiles.xmlentity;
2

  
3
import org.gvsig.tools.persistence.AbstractPersistenceManager;
4
import org.gvsig.tools.persistence.PersistenceException;
5
import org.gvsig.tools.persistence.PersistentState;
6

  
7
import com.iver.utiles.XMLEntity;
8

  
9
public class XMLEntityManager extends AbstractPersistenceManager {
10

  
11
	public PersistentState createState() throws PersistenceException {
12
		return new XMLEntityState(this);
13
	}
14

  
15
	public PersistentState createState(XMLEntity xmlEntity)
16
			throws PersistenceException {
17
		return new XMLEntityState(this, xmlEntity);
18
	}
19

  
20
}
0 21

  
branches/v2_0_0_prep/libraries/libFMap_data/src/org/gvsig/fmap/data/feature/impl/DefaultFeatureStore.java
40 40
import org.gvsig.fmap.data.feature.exceptions.CreateFeatureException;
41 41
import org.gvsig.fmap.data.feature.exceptions.DataExportException;
42 42
import org.gvsig.fmap.data.feature.exceptions.DataIndexException;
43
import org.gvsig.fmap.data.feature.exceptions.FinishEditingException;
44
import org.gvsig.fmap.data.feature.exceptions.GetFeatureTypeException;
43 45
import org.gvsig.fmap.data.feature.exceptions.IllegalFeatureException;
44 46
import org.gvsig.fmap.data.feature.exceptions.IllegalFeatureTypeException;
45
import org.gvsig.fmap.data.feature.exceptions.FinishEditingException;
46
import org.gvsig.fmap.data.feature.exceptions.GetFeatureTypeException;
47 47
import org.gvsig.fmap.data.feature.exceptions.NeedEditingModeException;
48 48
import org.gvsig.fmap.data.feature.exceptions.NoNewFeatureInsertException;
49 49
import org.gvsig.fmap.data.feature.exceptions.NullFeatureTypeException;
......
88 88

  
89 89
final public class DefaultFeatureStore implements
90 90
		FeatureStoreProviderServices,
91
		Observer {
91
		Observer /* , Persistent */{
92 92

  
93 93
	private DataStoreParameters parameters = null;
94 94
	private FeatureSelection selection;
......
353 353
		return xml;
354 354
	}
355 355

  
356
	/*
357

  
358
	public void getState(PersistentState state) throws PersistenceException {
359
		state.setTheClass(this);
360
		state.set("dataStoreName", this.getName());
361
		state.set("parameters", this.parameters);
362
		state.set("provider", this.provider);
363
		state.set("selection", this.selection);
364
	}
365

  
366
	public void setState(PersistentState state) throws PersistenceException {
367
		if (this.provider != null) {
368
			throw new PersistenceException(null);
369
		}
370
		if (this.getManager() == null) {
371
			this.dataManager = (DefaultDataManager) DALLocator.getDataManager();
372
		}
373

  
374
		DataStoreParameters params = (DataStoreParameters) state.get("parameters");
375
		FeatureStoreProvider provider = (FeatureStoreProvider) state.get("provider");
376

  
377
		initialize(this.getManager(), params, provider);
378

  
379
		setSelection((FeatureSelection) state.get("selection"));
380
	}
381
	*/
382

  
356 383
	//
357 384
	// ====================================================================
358 385
	// Gestion de la seleccion

Also available in: Unified diff