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 @ 40559

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

    
26
import org.gvsig.fmap.geom.Geometry;
27
import org.gvsig.fmap.geom.GeometryException;
28
import org.gvsig.fmap.geom.GeometryLocator;
29
import org.gvsig.fmap.geom.GeometryManager;
30
import org.gvsig.fmap.geom.exception.CreateGeometryException;
31
import org.gvsig.fmap.geom.operation.GeometryOperationException;
32
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
33
import org.gvsig.tools.dynobject.DynStruct;
34
import org.gvsig.tools.persistence.AbstractSinglePersistenceFactory;
35
import org.gvsig.tools.persistence.PersistentState;
36
import org.gvsig.tools.persistence.exception.PersistenceException;
37

    
38

    
39
/**
40
 * Factory to manage the persistence of {@link Geometry} objects.
41
 * 
42
 * @author gvSIG Team
43
 * @version $Id$
44
 *
45
 */
46
public class GeometryPersistenceFactory extends AbstractSinglePersistenceFactory {
47
    private static final String FIELD_STRING = "string";
48
    private static final String FIELD_SRS = "srs";
49

    
50
    private static final String DYNCLASS_NAME = "Geometry";
51
    private static final String DYNCLASS_DESCRIPTION = "gvSIG Geometry";
52

    
53
    private static final String OPERATION_FIELD_NAME = "";
54

    
55
    private static final GeometryManager geometryManager = GeometryLocator.getGeometryManager();
56

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

    
67
        DynStruct definition = this.getDefinition();
68

    
69
        definition.addDynFieldString(FIELD_STRING).setMandatory(true);
70
        definition.addDynFieldString(FIELD_SRS).setMandatory(false);
71
    }
72

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

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

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