root / branches / v2_0_0_prep / libraries / org.gvsig.fmap.dal.cache / src / main / java / org / gvsig / fmap / dal / cache / h2spatial / FeatureCacheH2SpatialProvider.java @ 32678
History | View | Annotate | Download (6.79 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.CloseException; |
40 |
import org.gvsig.fmap.dal.exception.CreateException; |
41 |
import org.gvsig.fmap.dal.exception.DataException; |
42 |
import org.gvsig.fmap.dal.exception.OpenException; |
43 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
44 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
45 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
46 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
47 |
import org.gvsig.fmap.dal.feature.Feature; |
48 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
49 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
50 |
import org.gvsig.fmap.dal.feature.FeatureType; |
51 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
52 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureReference; |
53 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider; |
54 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices; |
55 |
import org.gvsig.fmap.geom.Geometry; |
56 |
|
57 |
/**
|
58 |
* @author Vicente Caballero Navarro
|
59 |
*/
|
60 |
public class FeatureCacheH2SpatialProvider extends AbstractFeatureCacheProvider { |
61 |
@Override
|
62 |
public void clear() { |
63 |
// TODO Auto-generated method stub
|
64 |
super.clear();
|
65 |
} |
66 |
|
67 |
@Override
|
68 |
public void close() throws CloseException { |
69 |
// TODO Auto-generated method stub
|
70 |
super.close();
|
71 |
} |
72 |
|
73 |
@Override
|
74 |
public void open() throws OpenException { |
75 |
// TODO Auto-generated method stub
|
76 |
super.open();
|
77 |
} |
78 |
|
79 |
public static final String NAME = "H2SPATIAL.CACHE"; |
80 |
public static final String DESCRIPTION = "Cache provider for H2Spatial"; |
81 |
|
82 |
private static final String FIELD_SRS = "srs"; |
83 |
private static StringUtils stringUtils = CompatLocator.getStringUtils(); |
84 |
|
85 |
private Map fieldsPosition = null; |
86 |
|
87 |
public FeatureStore createDataStore(double scale) throws DataException { |
88 |
NewFeatureStoreParameters parameters = createDataStoreParameters(0);
|
89 |
EditableFeatureType eft = featureStoreProviderServices |
90 |
.getDefaultFeatureType().getEditable(); |
91 |
EditableFeatureAttributeDescriptor efad = eft.add("IDPK",
|
92 |
DataTypes.LONG); |
93 |
efad.setIsPrimaryKey(true);
|
94 |
efad.setIsAutomatic(true);
|
95 |
parameters.setDefaultFeatureType(eft); |
96 |
parameters.setDynValue("pkfields", "IDPK"); |
97 |
|
98 |
calculateFieldsPosition(); |
99 |
// try {
|
100 |
// parameters.invokeDynMethod("initialize", null);
|
101 |
// } catch (DynMethodException e1) {
|
102 |
// e1.printStackTrace();
|
103 |
// }
|
104 |
dataServerExplorer.add(parameters, true);
|
105 |
dataServerExplorer.dispose(); |
106 |
try {
|
107 |
FeatureStore store = (FeatureStore) dataManager |
108 |
.createStore(parameters); |
109 |
store.getParameters().setDynValue("pkfields", "IDPK"); |
110 |
return store;
|
111 |
} catch (ValidateDataParametersException e) {
|
112 |
throw new CreateException("Cache for scale" + scale, e); |
113 |
} |
114 |
} |
115 |
|
116 |
private void calculateFieldsPosition() throws DataException { |
117 |
double d = 0; |
118 |
FeatureType featureType = featureStoreProviderServices |
119 |
.getDefaultFeatureType().getEditable(); |
120 |
Iterator it = featureType.iterator();
|
121 |
fieldsPosition = new HashMap(); |
122 |
int i = 0; |
123 |
while (it.hasNext()) {
|
124 |
FeatureAttributeDescriptor attr = (FeatureAttributeDescriptor) it |
125 |
.next(); |
126 |
if (attr.getDataType() != DataTypes.GEOMETRY) {
|
127 |
fieldsPosition.put(attr.getName(), i); |
128 |
i++; |
129 |
} |
130 |
} |
131 |
} |
132 |
|
133 |
private NewFeatureStoreParameters createDataStoreParameters(double scale) |
134 |
throws DataException {
|
135 |
NewFeatureStoreParameters parameters = (NewFeatureStoreParameters) dataServerExplorer |
136 |
.getAddParameters("H2Spatial");
|
137 |
parameters.setDynValue("dbname", System.getProperty("user.home") |
138 |
+ File.separator + "gvSIG"+File.separator+"cache" + File.separator + "cache"); |
139 |
parameters.setDynValue("schema", "PUBLIC"); |
140 |
|
141 |
parameters.setDynValue("table", getStoreIdByScale(scale));
|
142 |
parameters.setDynValue(FIELD_SRS, sourceProjection); |
143 |
return parameters;
|
144 |
} |
145 |
|
146 |
private String getStoreIdByScale(double scale) { |
147 |
String cacheName = featureStoreProvider.getSourceId().toString();
|
148 |
cacheName = cacheName.substring(cacheName.lastIndexOf("/") + 1, |
149 |
cacheName.length()); |
150 |
cacheName = stringUtils.replaceAll(cacheName, "\\.", "_"); |
151 |
cacheName = stringUtils.replaceAll(cacheName, ":", "_"); |
152 |
return cacheName
|
153 |
+ "_"
|
154 |
+ stringUtils.replaceAll(String.valueOf(scale), "\\.", "_") |
155 |
.toUpperCase(); |
156 |
// return "tablaprueba";
|
157 |
} |
158 |
|
159 |
@Override
|
160 |
protected void copyFeature(EditableFeature editableFeature, |
161 |
Feature sourceFeature) { |
162 |
FeatureAttributeDescriptor[] attDescriptors = editableFeature.getType()
|
163 |
.getAttributeDescriptors(); |
164 |
String geomField = editableFeature.getType()
|
165 |
.getDefaultGeometryAttributeName(); |
166 |
FeatureAttributeDescriptor[] pks = editableFeature.getType()
|
167 |
.getPrimaryKey(); |
168 |
for (int i = 0; i < attDescriptors.length; i++) { |
169 |
FeatureAttributeDescriptor attr = attDescriptors[i]; |
170 |
if (attr.getDataType() != DataTypes.GEOMETRY) {
|
171 |
if (attr.equals(pks[0])) { |
172 |
editableFeature.set(attr.getName(), |
173 |
((DefaultFeatureReference) sourceFeature |
174 |
.getReference()).getOID()); |
175 |
} else {
|
176 |
Object obj = sourceFeature.get(attr.getName());
|
177 |
if (obj != null) { |
178 |
editableFeature.set(attr.getName(), obj); |
179 |
} |
180 |
} |
181 |
} else {
|
182 |
editableFeature.setGeometry(geomField, (Geometry) sourceFeature |
183 |
.get(attr.getName())); |
184 |
} |
185 |
} |
186 |
} |
187 |
|
188 |
public void apply( |
189 |
FeatureStoreProviderServices featureStoreProviderServices, |
190 |
FeatureStoreProvider featureStoreProvider) throws DataException {
|
191 |
// EditableFeatureType eft =
|
192 |
// featureStoreProviderServices.getDefaultFeatureType().getEditable();
|
193 |
// if (featureType.getDefaultGeometryAttribute().getGeometryType() ==
|
194 |
// TYPES.GEOMETRY){
|
195 |
// throw new NotSupportMultipleGeometriesException();
|
196 |
// }
|
197 |
|
198 |
super.apply(featureStoreProviderServices, featureStoreProvider);
|
199 |
} |
200 |
} |