svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / type / persistence / GeometryTypePersistenceFactory.java @ 37322
History | View | Annotate | Download (3.5 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 |
*/
|
22 |
package org.gvsig.fmap.geom.type.persistence; |
23 |
|
24 |
import org.gvsig.fmap.geom.GeometryException; |
25 |
import org.gvsig.fmap.geom.GeometryLocator; |
26 |
import org.gvsig.fmap.geom.GeometryManager; |
27 |
import org.gvsig.fmap.geom.type.GeometryType; |
28 |
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException; |
29 |
import org.gvsig.tools.dynobject.DynStruct; |
30 |
import org.gvsig.tools.persistence.AbstractSinglePersistenceFactory; |
31 |
import org.gvsig.tools.persistence.PersistentState; |
32 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
33 |
|
34 |
/**
|
35 |
* Factory to persist {@link GeometryType} objects.
|
36 |
*
|
37 |
* @author gvSIG Team
|
38 |
* @version $Id$
|
39 |
*/
|
40 |
public class GeometryTypePersistenceFactory extends |
41 |
AbstractSinglePersistenceFactory { |
42 |
|
43 |
private static final GeometryManager geometryManager = GeometryLocator |
44 |
.getGeometryManager(); |
45 |
|
46 |
private static final String DYNCLASS_NAME = "GeometryType"; |
47 |
private static final String DYNCLASS_DESCRIPTION = "gvSIG Geometry type"; |
48 |
|
49 |
private static final String FIELD_TYPE = "type"; |
50 |
private static final String FIELD_SUBTYPE = "subtype"; |
51 |
|
52 |
/**
|
53 |
* Creates a new {@link GeometryTypePersistenceFactory}. It will persist
|
54 |
* the geometry type and subtype values, and use it to load
|
55 |
* {@link GeometryType}s from persistence by calling
|
56 |
* {@link GeometryManager#getGeometryType(int, int)}.
|
57 |
*/
|
58 |
public GeometryTypePersistenceFactory() {
|
59 |
super(GeometryType.class, DYNCLASS_NAME, DYNCLASS_DESCRIPTION, null, |
60 |
null);
|
61 |
|
62 |
DynStruct definition = this.getDefinition();
|
63 |
definition.addDynFieldInt(FIELD_TYPE).setMandatory(true);
|
64 |
definition.addDynFieldInt(FIELD_SUBTYPE).setMandatory(true);
|
65 |
} |
66 |
|
67 |
public Object createFromState(PersistentState state) |
68 |
throws PersistenceException {
|
69 |
int type = state.getInt(FIELD_TYPE);
|
70 |
int subType = state.getInt(FIELD_SUBTYPE);
|
71 |
|
72 |
try {
|
73 |
return geometryManager.getGeometryType(type, subType);
|
74 |
} catch (GeometryTypeNotSupportedException e) {
|
75 |
throw new PersistenceException( |
76 |
"Error creating the geometry type with type = " + type
|
77 |
+ ", subType = " + subType, e);
|
78 |
} catch (GeometryException e) {
|
79 |
throw new PersistenceException( |
80 |
"Error creating the geometry type with type = " + type
|
81 |
+ ", subType = " + subType, e);
|
82 |
} |
83 |
} |
84 |
|
85 |
public void saveToState(PersistentState state, Object obj) |
86 |
throws PersistenceException {
|
87 |
GeometryType type = (GeometryType) obj; |
88 |
state.set(FIELD_TYPE, type.getType()); |
89 |
state.set(FIELD_SUBTYPE, type.getSubType()); |
90 |
} |
91 |
|
92 |
} |