Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / org.gvsig.fmap.dal.cache / src / main / java / org / gvsig / fmap / dal / cache / h2spatial / FeatureCacheH2SpatialProvider.java @ 32685

History | View | Annotate | Download (5.63 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

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2009 {Iver T.I.}   {Task}
26
 */
27

    
28
package org.gvsig.fmap.dal.cache.h2spatial;
29

    
30
import java.io.File;
31
import java.util.HashMap;
32
import java.util.Iterator;
33
import java.util.Map;
34

    
35
import org.gvsig.compat.CompatLocator;
36
import org.gvsig.compat.lang.StringUtils;
37
import org.gvsig.fmap.dal.DataTypes;
38
import org.gvsig.fmap.dal.cache.AbstractFeatureCacheProvider;
39
import org.gvsig.fmap.dal.exception.CreateException;
40
import org.gvsig.fmap.dal.exception.DataException;
41
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
42
import org.gvsig.fmap.dal.feature.EditableFeature;
43
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
44
import org.gvsig.fmap.dal.feature.EditableFeatureType;
45
import org.gvsig.fmap.dal.feature.Feature;
46
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
47
import org.gvsig.fmap.dal.feature.FeatureStore;
48
import org.gvsig.fmap.dal.feature.FeatureType;
49
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
50
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureReference;
51
import org.gvsig.fmap.geom.Geometry;
52

    
53
/**
54
 * @author Vicente Caballero Navarro
55
 */
56
public class FeatureCacheH2SpatialProvider extends AbstractFeatureCacheProvider {
57
        public static final String NAME = "H2SPATIAL.CACHE";
58
        public static final String DESCRIPTION = "Cache provider for H2Spatial";
59

    
60
        private static final String FIELD_SRS = "srs";
61
        private static StringUtils stringUtils = CompatLocator.getStringUtils();
62

    
63
        private Map fieldsPosition = null;
64

    
65
        public FeatureStore createDataStore(double scale) throws DataException {
66
                NewFeatureStoreParameters parameters = createDataStoreParameters(0);
67
                EditableFeatureType eft = featureStoreProviderServices
68
                                .getDefaultFeatureType().getEditable();
69
                EditableFeatureAttributeDescriptor efad = eft.add("IDPK",
70
                                DataTypes.LONG);
71
                efad.setIsPrimaryKey(true);
72
                efad.setIsAutomatic(true);
73
                parameters.setDefaultFeatureType(eft);
74
                parameters.setDynValue("pkfields", "IDPK");
75

    
76
                calculateFieldsPosition();
77

    
78
                dataServerExplorer.add(parameters, true);
79
                dataServerExplorer.dispose();
80
                try {
81
                        FeatureStore store = (FeatureStore) dataManager
82
                                        .createStore(parameters);
83
                        store.getParameters().setDynValue("pkfields", "IDPK");
84
                        return store;
85
                } catch (ValidateDataParametersException e) {
86
                        throw new CreateException("Cache for scale" + scale, e);
87
                }
88
        }
89

    
90
        private void calculateFieldsPosition() throws DataException {
91
                double d = 0;
92
                FeatureType featureType = featureStoreProviderServices
93
                                .getDefaultFeatureType().getEditable();
94
                Iterator it = featureType.iterator();
95
                fieldsPosition = new HashMap();
96
                int i = 0;
97
                while (it.hasNext()) {
98
                        FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) it
99
                                        .next();
100
                        if (attr.getDataType() != DataTypes.GEOMETRY) {
101
                                fieldsPosition.put(attr.getName(), i);
102
                                i++;
103
                        }
104
                }
105
        }
106

    
107
        private NewFeatureStoreParameters createDataStoreParameters(double scale)
108
                        throws DataException {
109
                NewFeatureStoreParameters parameters = (NewFeatureStoreParameters) dataServerExplorer
110
                                .getAddParameters("H2Spatial");
111
                parameters.setDynValue("dbname", System.getProperty("user.home")
112
                                + File.separator + "gvSIG"+File.separator+"cache" + File.separator + "cache");
113
                parameters.setDynValue("schema", "PUBLIC");
114

    
115
                parameters.setDynValue("table", getStoreIdByScale(scale));
116
                parameters.setDynValue(FIELD_SRS, sourceProjection);
117
                return parameters;
118
        }
119

    
120
        private String getStoreIdByScale(double scale) {
121
                String cacheName = featureStoreProvider.getSourceId().toString();
122
                cacheName = cacheName.substring(cacheName.lastIndexOf("/") + 1,
123
                                cacheName.length());
124
                cacheName = stringUtils.replaceAll(cacheName, "\\.", "_");
125
                cacheName = stringUtils.replaceAll(cacheName, ":", "_");
126
                return cacheName
127
                                + "_"
128
                                + stringUtils.replaceAll(String.valueOf(scale), "\\.", "_")
129
                                                .toUpperCase();
130
        }
131

    
132
        @Override
133
        protected void copyFeature(EditableFeature editableFeature,
134
                        Feature sourceFeature) {
135
                FeatureAttributeDescriptor[] attDescriptors = editableFeature.getType()
136
                                .getAttributeDescriptors();
137
                String geomField = editableFeature.getType()
138
                                .getDefaultGeometryAttributeName();
139
                FeatureAttributeDescriptor[] pks = editableFeature.getType()
140
                                .getPrimaryKey();
141
                for (int i = 0; i < attDescriptors.length; i++) {
142
                        FeatureAttributeDescriptor attr = attDescriptors[i];
143
                        if (attr.getDataType() != DataTypes.GEOMETRY) {
144
                                if (attr.equals(pks[0])) {
145
                                        editableFeature.set(attr.getName(),
146
                                                        ((DefaultFeatureReference) sourceFeature
147
                                                                        .getReference()).getOID());
148
                                } else {
149
                                        Object obj = sourceFeature.get(attr.getName());
150
                                        if (obj != null) {
151
                                                editableFeature.set(attr.getName(), obj);
152
                                        }
153
                                }
154
                        } else {
155
                                editableFeature.setGeometry(geomField, (Geometry) sourceFeature
156
                                                .get(attr.getName()));
157
                        }
158
                }
159
        }
160
}