Revision 32572

View differences:

branches/v2_0_0_prep/libraries/org.gvsig.fmap.dal.cache/src/main/java/org/gvsig/fmap/dal/cache/h2spatial/FeatureCacheH2SpatialProvider.java
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.EditableFeatureType;
44
import org.gvsig.fmap.dal.feature.Feature;
45
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
46
import org.gvsig.fmap.dal.feature.FeatureStore;
47
import org.gvsig.fmap.dal.feature.FeatureType;
48
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
49
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
50
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices;
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.getDefaultFeatureType().getEditable();
68
//		eft.setDefaultGeometryAttributeName("GEOMETRY");
69
//		EditableFeatureType eft1=featureStoreProviderServices.createFeatureType();
70
//		FeatureAttributeDescriptor[] fads=eft.getAttributeDescriptors();
71
//		for (int i = 0; i < fads.length; i++) {
72
//			EditableFeatureAttributeDescriptor efad=null;
73
//			if (fads[i].getDataType()==DataTypes.GEOMETRY){
74
//				efad=eft1.add("GEOMETRY", fads[i].getDataType());
75
//				efad.setGeometryType(fads[i].getGeometryType());
76
//				efad.setSRS(fads[i].getSRS());
77
//			}else{
78
//				efad=eft1.add(fads[i].getName(), fads[i].getDataType());
79
//			}
80
//			efad.setSize(fads[i].getSize());
81
//		}
82
		
83
		parameters.setDefaultFeatureType(eft);
84
		calculateFieldsPosition();
85
		
86
		dataServerExplorer.add(parameters, true);
87
		dataServerExplorer.dispose();
88
		try {
89
			FeatureStore store=(FeatureStore)dataManager.createStore(parameters);
90
//			FeatureSet setAll=store.getFeatureSet();
91
//			DisposableIterator itAll=setAll.iterator();
92
//			while (itAll.hasNext()) {
93
//				Feature f = (Feature) itAll.next();
94
//				System.err.println("All-createDataStore()= "+f.toString());
95
//			}
96
//		
97
			return store;			
98
		} catch (ValidateDataParametersException e) {
99
			throw new CreateException("Cache for scale" + scale, e);
100
		}		
101
	}	
102
	
103
	private void calculateFieldsPosition() throws DataException{
104
		FeatureType featureType = featureStoreProviderServices.getDefaultFeatureType();
105
		Iterator it = featureType.iterator();
106
		fieldsPosition = new HashMap();
107
		int i=0;
108
		while( it.hasNext() ) {
109
			FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) it.next();
110
			if (attr.getDataType() != DataTypes.GEOMETRY){
111
				fieldsPosition.put(attr.getName(), i);
112
				i++;
113
			}
114
		}
115
	}
116

  
117
	private NewFeatureStoreParameters createDataStoreParameters(double scale) throws DataException {
118
		NewFeatureStoreParameters parameters = (NewFeatureStoreParameters)dataServerExplorer.getAddParameters("H2Spatial");
119
//		File rootFile = ((FilesystemServerExplorer)dataServerExplorer).getCurrentPath();
120
		parameters.setDynValue("dbname",  System.getProperty("user.home")+File.separator+"gvsigcache"+File.separator+"cache.db");
121
//		parameters.setUrl(rootFile.getAbsolutePath());
122
		parameters.setDynValue("table",getStoreIdByScale(scale));
123
		parameters.setDynValue(FIELD_SRS, sourceProjection);
124
		return parameters;
125
	}
126

  
127
	private String getStoreIdByScale(double scale) {
128
//		String cacheName = featureStoreProvider.getSourceId().toString();
129
//		cacheName = cacheName.substring(cacheName.lastIndexOf("/"), cacheName.length());
130
//		cacheName = stringUtils.replaceAll(cacheName, "\\.", "_"); 
131
//		cacheName = stringUtils.replaceAll(cacheName, ":", "_"); 
132
//		return cacheName + "_" + stringUtils.replaceAll(String.valueOf(scale), "\\.", "_");
133
		return "tablaprueba";
134
	}
135

  
136
	@Override
137
	protected void copyFeature(EditableFeature editableFeature,
138
			Feature sourceFeature) {
139
 		FeatureAttributeDescriptor[] attDescriptors = sourceFeature.getType().getAttributeDescriptors();		
140
		String geomField = editableFeature.getType().getDefaultGeometryAttributeName();
141
 		for (int i=0 ; i<attDescriptors.length ; i++){
142
			FeatureAttributeDescriptor attr = attDescriptors[i];
143
			if (attr.getDataType() != DataTypes.GEOMETRY){
144
				Object obj = sourceFeature.get(attr.getIndex());
145
				if (obj != null){
146
					editableFeature.set((Integer)fieldsPosition.get(attr.getName()), obj);
147
				}
148
			}else{
149
				editableFeature.setGeometry(geomField, (Geometry)sourceFeature.get(attr.getIndex()));
150
			}
151
		}
152
	}
153

  
154
	public void apply(
155
			FeatureStoreProviderServices featureStoreProviderServices,
156
			FeatureStoreProvider featureStoreProvider) throws DataException {
157
//		FeatureType featureType = featureStoreProviderServices.getFeatureStore().getDefaultFeatureType();
158
//		if (featureType.getDefaultGeometryAttribute().getGeometryType() == TYPES.GEOMETRY){
159
//			throw new NotSupportMultipleGeometriesException();
160
//		}
161
		super.apply(featureStoreProviderServices, featureStoreProvider);
162
	}
163
}
164

  
0 165

  

Also available in: Unified diff