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

History | View | Annotate | Download (6.28 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.DefaultFeatureStore;
52
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
53
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices;
54
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
55
import org.gvsig.fmap.geom.Geometry;
56

    
57
/**
58
 * @author Vicente Caballero Navarro
59
 */
60
public class FeatureCacheH2SpatialProvider extends AbstractFeatureCacheProvider  {
61
        public static final String NAME = "H2SPATIAL.CACHE";
62
        public static final String DESCRIPTION = "Cache provider for H2Spatial";
63
        
64
        private static final String FIELD_SRS = "srs";
65
        private static StringUtils stringUtils = CompatLocator.getStringUtils();
66
        
67
        private Map fieldsPosition = null;                        
68
        
69
        public FeatureStore createDataStore(double scale) throws DataException {
70
                NewFeatureStoreParameters parameters = createDataStoreParameters(0);
71
                EditableFeatureType eft=featureStoreProviderServices.getDefaultFeatureType().getEditable();
72
                EditableFeatureAttributeDescriptor efad=eft.add("IDPK", DataTypes.LONG);
73
                efad.setIsPrimaryKey(true);
74
                efad.setIsAutomatic(true);
75
                parameters.setDefaultFeatureType(eft);
76
                parameters.setDynValue("pkfields", "IDPK");
77
                
78
                calculateFieldsPosition();
79
                
80
                dataServerExplorer.add(parameters, true);
81
                dataServerExplorer.dispose();
82
                try {
83
                        FeatureStore store=(FeatureStore)dataManager.createStore(parameters);
84
                        store.getParameters().setDynValue("pkfields", "IDPK");
85
                        return store;                        
86
                } catch (ValidateDataParametersException e) {
87
                        throw new CreateException("Cache for scale" + scale, e);
88
                }                
89
        }        
90
        
91
        private void calculateFieldsPosition() throws DataException{
92
                double d=0;
93
                FeatureType featureType = featureStoreProviderServices.getDefaultFeatureType().getEditable();
94
                Iterator it = featureType.iterator();
95
                fieldsPosition = new HashMap();
96
                int i=0;
97
                while( it.hasNext() ) {
98
                        FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) it.next();
99
                        if (attr.getDataType() != DataTypes.GEOMETRY){
100
                                fieldsPosition.put(attr.getName(), i);
101
                                i++;
102
                        }
103
                }
104
        }
105

    
106
        private NewFeatureStoreParameters createDataStoreParameters(double scale) throws DataException {
107
                NewFeatureStoreParameters parameters = (NewFeatureStoreParameters)dataServerExplorer.getAddParameters("H2Spatial");
108
                parameters.setDynValue("dbname",  System.getProperty("user.home")+File.separator+"gvsigcache"+File.separator+"cache");
109
                parameters.setDynValue("schema", "PUBLIC");
110
                
111
                parameters.setDynValue("table",getStoreIdByScale(scale));
112
                parameters.setDynValue(FIELD_SRS, sourceProjection);
113
                return parameters;
114
        }
115

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

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

    
148
        public void apply(
149
                        FeatureStoreProviderServices featureStoreProviderServices,
150
                        FeatureStoreProvider featureStoreProvider) throws DataException {
151
//                EditableFeatureType eft = featureStoreProviderServices.getDefaultFeatureType().getEditable();
152
//                if (featureType.getDefaultGeometryAttribute().getGeometryType() == TYPES.GEOMETRY){
153
//                        throw new NotSupportMultipleGeometriesException();
154
//                }
155
                
156
                
157
                super.apply(featureStoreProviderServices, featureStoreProvider);
158
        }
159
        public Object createNewOID() {
160
                return new Long((long)(Math.random()*100000));
161
        }
162
}
163