Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libProjection / src / org / gvsig / fmap / crs / persistence / ProjectionPersistenceFactory.java @ 30754

History | View | Annotate | Download (4.58 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Gobernment (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

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2009 {DiSiD Technologies}  {{Task}}
26
 */
27
package org.gvsig.fmap.crs.persistence;
28

    
29
import java.util.Collections;
30
import java.util.Map;
31

    
32
import org.cresques.cts.IProjection;
33
import org.gvsig.fmap.crs.CRSFactory;
34
import org.gvsig.tools.ToolsLocator;
35
import org.gvsig.tools.dataTypes.DataTypes;
36
import org.gvsig.tools.dynobject.DynField;
37
import org.gvsig.tools.dynobject.DynObjectManager;
38
import org.gvsig.tools.dynobject.DynStruct;
39
import org.gvsig.tools.persistence.PersistenceException;
40
import org.gvsig.tools.persistence.PersistenceFactory;
41
import org.gvsig.tools.persistence.PersistenceManager;
42
import org.gvsig.tools.persistence.PersistentState;
43
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
45

    
46
/**
47
 * Factory to persist {@link IProjection} objects. The information about the
48
 * {@link IProjection} which will be persisted is its full code, containing
49
 * all required information to be able to reconstruct the {@link IProjection}
50
 * object through the {@link CRSFactory#getCRS(String)} method.
51
 * <p>
52
 * <strong>NOTE:</strong>To activate this factory, it must be instanced and
53
 * registered in the {@link PersistenceManager}. This will be usually performed
54
 * from the project Library.
55
 * </p>
56
 * 
57
 * @author <a href="mailto:cordinyana@gvsig.org">C?sar Ordi?ana</a>
58
 */
59
public class ProjectionPersistenceFactory implements PersistenceFactory {
60

    
61
        private static final Logger LOG = LoggerFactory
62
                        .getLogger(ProjectionPersistenceFactory.class);
63

    
64
        // Persisted atribute name with the projection code
65
        private static final String FULLCODE = "fullcode";
66

    
67
        private static final String PROJ_DYNCLASS_NAME = "Projection";
68

    
69
        private DynStruct projectionDynStruct;
70

    
71
        /**
72
         * Creates a new {@link ProjectionPersistenceFactory}.
73
         */
74
        public ProjectionPersistenceFactory() {
75

    
76
                // Create the DynStruct related to the IProjection objects persisted
77
                // data, which will be its full code.
78
                DynObjectManager dynman = ToolsLocator.getDynObjectManager();
79

    
80
                projectionDynStruct = dynman.add(PROJ_DYNCLASS_NAME, "Projection");
81

    
82
                DynField field = projectionDynStruct.addDynField(FULLCODE);
83
                field.setTheTypeOfAvailableValues(DynField.SINGLE);
84
                field.setDescription("Projection abbreviature");
85
                field.setMandatory(true);
86
                field.setType(DataTypes.STRING);
87
        }
88

    
89
        public Object createFromState(PersistentState state, Class classToUse)
90
                        throws PersistenceException {
91
                return CRSFactory.getCRS(state.getString(FULLCODE));
92
        }
93

    
94
        public void loadFromState(PersistentState state, Object object)
95
                        throws PersistenceException {
96
                // Nothing to do here, the Projection object hasn't any more
97
                // properties to set.
98
        }
99

    
100
        public boolean manages(Object object) {
101
                return object instanceof IProjection;
102
        }
103

    
104
        public boolean manages(PersistentState state) {
105
                return manages(state.getTheClassName());
106
        }
107

    
108
        public void saveToState(PersistentState state, Object obj)
109
                        throws PersistenceException {
110
                state.set(FULLCODE, ((IProjection) obj).getFullCode());
111
        }
112

    
113
        public Map getDefinitions() {
114
                // What can we do here, we don't know the IProjection implementation
115
                // classes
116
                return Collections.singletonMap(IProjection.class, projectionDynStruct);
117
        }
118
        
119
        public DynStruct getDefinition(String persistentClassName) {
120
                return manages(persistentClassName) ? projectionDynStruct : null;
121
        }
122
        
123
        public String getDomainName() {
124
                // Use the default domain name
125
                return null;
126
        }
127
        
128
        public String getDomainURL() {
129
                // Use the default domain url
130
                return null;
131
        }
132
        
133
        private boolean manages(String className) {
134
                try {
135
                        Class projectionClass = Class.forName(className);
136
                        return IProjection.class.isAssignableFrom(projectionClass);
137
                } catch (ClassNotFoundException e) {
138
                        LOG.error("Error loading the Class: " + className, e);
139
                }
140
                return false;
141
                
142
        }
143
}