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

History | View | Annotate | Download (6.31 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.db.h2spatial.impl.H2SpatialNewStoreParameters;
40
import org.gvsig.fmap.dal.exception.CreateException;
41
import org.gvsig.fmap.dal.exception.DataException;
42
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
43
import org.gvsig.fmap.dal.feature.EditableFeature;
44
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
45
import org.gvsig.fmap.dal.feature.EditableFeatureType;
46
import org.gvsig.fmap.dal.feature.Feature;
47
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
48
import org.gvsig.fmap.dal.feature.FeatureStore;
49
import org.gvsig.fmap.dal.feature.FeatureType;
50
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
51
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureReference;
52
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureStore;
53
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
54
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices;
55
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
56
import org.gvsig.fmap.geom.Geometry;
57

    
58
/**
59
 * @author Vicente Caballero Navarro
60
 */
61
public class FeatureCacheH2SpatialProvider extends AbstractFeatureCacheProvider  {
62
        public static final String NAME = "H2SPATIAL.CACHE";
63
        public static final String DESCRIPTION = "Cache provider for H2Spatial";
64
        
65
        private static final String FIELD_SRS = "srs";
66
        private static StringUtils stringUtils = CompatLocator.getStringUtils();
67
        
68
        private Map fieldsPosition = null;                        
69
        
70
        public FeatureStore createDataStore(double scale) throws DataException {
71
                NewFeatureStoreParameters parameters = createDataStoreParameters(0);
72
                EditableFeatureType eft=featureStoreProviderServices.getDefaultFeatureType().getEditable();
73
                EditableFeatureAttributeDescriptor efad=eft.add("IDPK", DataTypes.LONG);
74
                efad.setIsPrimaryKey(true);
75
                efad.setIsAutomatic(true);
76
                parameters.setDefaultFeatureType(eft);
77
                parameters.setDynValue("pkfields", "IDPK");
78
                
79
                calculateFieldsPosition();
80
                
81
                dataServerExplorer.add(parameters, true);
82
                dataServerExplorer.dispose();
83
                try {
84
                        FeatureStore store=(FeatureStore)dataManager.createStore(parameters);
85
                        store.getParameters().setDynValue("pkfields", "IDPK");
86
                        return store;                        
87
                } catch (ValidateDataParametersException e) {
88
                        throw new CreateException("Cache for scale" + scale, e);
89
                }                
90
        }        
91
        
92
        private void calculateFieldsPosition() throws DataException{
93
                double d=0;
94
                FeatureType featureType = featureStoreProviderServices.getDefaultFeatureType().getEditable();
95
                Iterator it = featureType.iterator();
96
                fieldsPosition = new HashMap();
97
                int i=0;
98
                while( it.hasNext() ) {
99
                        FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) it.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) throws DataException {
108
                NewFeatureStoreParameters parameters = (NewFeatureStoreParameters)dataServerExplorer.getAddParameters("H2Spatial");
109
                parameters.setDynValue("dbname",  System.getProperty("user.home")+File.separator+"gvsigcache"+File.separator+"cache");
110
                parameters.setDynValue("schema", "PUBLIC");
111
                
112
                parameters.setDynValue("table",getStoreIdByScale(scale));
113
                parameters.setDynValue(FIELD_SRS, sourceProjection);
114
                return parameters;
115
        }
116

    
117
        private String getStoreIdByScale(double scale) {
118
                String cacheName = featureStoreProvider.getSourceId().toString();
119
                cacheName = cacheName.substring(cacheName.lastIndexOf("/")+1, cacheName.length());
120
                cacheName = stringUtils.replaceAll(cacheName, "\\.", "_"); 
121
                cacheName = stringUtils.replaceAll(cacheName, ":", "_"); 
122
                return cacheName + "_" + stringUtils.replaceAll(String.valueOf(scale), "\\.", "_").toUpperCase();
123
//                return "tablaprueba";
124
        }
125

    
126
        @Override
127
        protected void copyFeature(EditableFeature editableFeature,
128
                        Feature sourceFeature) {
129
                FeatureAttributeDescriptor[] attDescriptors = editableFeature.getType().getAttributeDescriptors();                
130
                String geomField = editableFeature.getType().getDefaultGeometryAttributeName();
131
                 FeatureAttributeDescriptor[] pks=editableFeature.getType().getPrimaryKey();
132
                for (int i=0 ; i<attDescriptors.length ; i++){
133
                        FeatureAttributeDescriptor attr = attDescriptors[i];
134
                        if (attr.getDataType() != DataTypes.GEOMETRY){
135
                                if (attr.equals(pks[0])){
136
                                        editableFeature.set(attr.getName(),((DefaultFeatureReference)sourceFeature.getReference()).getOID());
137
                                }else{
138
                                        Object obj = sourceFeature.get(attr.getName());
139
                                        if (obj != null){
140
                                                editableFeature.set(attr.getName(), obj);
141
                                        }
142
                                }
143
                        }else{
144
                                editableFeature.setGeometry(geomField, (Geometry)sourceFeature.get(attr.getName()));
145
                        }
146
                }
147
        }
148

    
149
        public void apply(
150
                        FeatureStoreProviderServices featureStoreProviderServices,
151
                        FeatureStoreProvider featureStoreProvider) throws DataException {
152
//                EditableFeatureType eft = featureStoreProviderServices.getDefaultFeatureType().getEditable();
153
//                if (featureType.getDefaultGeometryAttribute().getGeometryType() == TYPES.GEOMETRY){
154
//                        throw new NotSupportMultipleGeometriesException();
155
//                }
156
                
157
                
158
                super.apply(featureStoreProviderServices, featureStoreProvider);
159
        }
160
}
161