Statistics
| Revision:

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 / impl / GeometryPersistenceFactory.java @ 40435

History | View | Annotate | Download (3.53 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.impl;
23

    
24
import org.gvsig.fmap.geom.Geometry;
25
import org.gvsig.fmap.geom.GeometryException;
26
import org.gvsig.fmap.geom.GeometryLocator;
27
import org.gvsig.fmap.geom.GeometryManager;
28
import org.gvsig.fmap.geom.exception.CreateGeometryException;
29
import org.gvsig.fmap.geom.operation.GeometryOperationException;
30
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
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
/**
38
 * Factory to manage the persistence of {@link Geometry} objects.
39
 * 
40
 * @author gvSIG Team
41
 * @version $Id$
42
 *
43
 */
44
public class GeometryPersistenceFactory extends AbstractSinglePersistenceFactory {
45
    private static final String FIELD_STRING = "string";
46
    private static final String FIELD_SRS = "srs";
47

    
48
    private static final String DYNCLASS_NAME = "Geometry";
49
    private static final String DYNCLASS_DESCRIPTION = "gvSIG Geometry";
50

    
51
    private static final String OPERATION_FIELD_NAME = "";
52

    
53
    private static final GeometryManager geometryManager = GeometryLocator.getGeometryManager();
54

    
55
    /**
56
     * @param theClass
57
     * @param name
58
     * @param description
59
     * @param domainName
60
     * @param domainUrl
61
     */
62
    protected GeometryPersistenceFactory() {
63
        super(Geometry.class, DYNCLASS_NAME, DYNCLASS_DESCRIPTION, null, null);   
64

    
65
        DynStruct definition = this.getDefinition();
66

    
67
        definition.addDynFieldString(FIELD_STRING).setMandatory(true);
68
        definition.addDynFieldString(FIELD_SRS).setMandatory(false);
69
    }
70

    
71
    public Object createFromState(PersistentState state)
72
    throws PersistenceException {
73
        String string = state.getString(FIELD_STRING);
74
        String srs = state.getString(FIELD_SRS);
75

    
76
        try {
77
            return geometryManager.createFrom(string, srs);
78
        } catch (CreateGeometryException e) {
79
           throw new PersistenceException("Error creating the geometry", e);
80
        } catch (GeometryException e) {
81
            throw new PersistenceException("Error creating the geometry", e);
82
        }
83
    }
84

    
85
    public void saveToState(PersistentState state, Object obj)
86
    throws PersistenceException {
87
        try {
88
            state.set(FIELD_STRING, ((Geometry)obj).convertToWKT());
89
        } catch (GeometryOperationNotSupportedException e) {
90
            throw new PersistenceException("Error converting the geometry", e);
91
        } catch (GeometryOperationException e) {
92
            throw new PersistenceException("Error converting the geometry", e);
93
        }
94
    }
95
}