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