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