svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.exportto / org.gvsig.exportto.swing / org.gvsig.exportto.swing.prov / org.gvsig.exportto.swing.prov.jdbc / src / main / java / org / gvsig / exportto / swing / prov / jdbc / ExporrtoJDBCService.java @ 41638
History | View | Annotate | Download (13.3 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.exportto.swing.prov.jdbc; |
24 |
|
25 |
import java.util.Map; |
26 |
import org.apache.commons.lang3.StringUtils; |
27 |
import org.cresques.cts.ICoordTrans; |
28 |
import org.cresques.cts.IProjection; |
29 |
import org.gvsig.exportto.ExporttoService; |
30 |
import org.gvsig.exportto.ExporttoServiceException; |
31 |
import org.gvsig.exportto.ExporttoServiceFinishAction; |
32 |
import org.gvsig.fmap.dal.DALLocator; |
33 |
import org.gvsig.fmap.dal.DataManager; |
34 |
import org.gvsig.fmap.dal.DataTypes; |
35 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
36 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
37 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
38 |
import org.gvsig.fmap.dal.feature.Feature; |
39 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
40 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
41 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
42 |
import org.gvsig.fmap.dal.feature.FeatureType; |
43 |
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
44 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer; |
45 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
46 |
import org.gvsig.fmap.geom.Geometry; |
47 |
import org.gvsig.tools.dispose.DisposableIterator; |
48 |
import org.gvsig.tools.task.AbstractMonitorableTask; |
49 |
import org.slf4j.Logger; |
50 |
import org.slf4j.LoggerFactory; |
51 |
|
52 |
/**
|
53 |
* @author gvSIG Team
|
54 |
* @version $Id$
|
55 |
*
|
56 |
*/
|
57 |
public class ExporrtoJDBCService extends AbstractMonitorableTask implements |
58 |
ExporttoService { |
59 |
|
60 |
private static final Logger logger = LoggerFactory.getLogger(ExporrtoJDBCService.class); |
61 |
|
62 |
public static final int CHECK_NONE = 0; |
63 |
public static final int CHECK_IF_CORRUPT = 1; |
64 |
public static final int CHECK_IF_VALID = 2; |
65 |
|
66 |
public static final int ACTION_SET_GEOMETRY_TO_NULL = 0; |
67 |
public static final int ACTION_SKIP_FEATURE = 1; |
68 |
public static final int ACTION_ABORT = 2; |
69 |
|
70 |
private ExporttoServiceFinishAction exporttoServiceFinishAction = null; |
71 |
private ExporttoJDBCOptions options;
|
72 |
// private Map<String,String> translationIds;
|
73 |
|
74 |
public ExporrtoJDBCService(ExporttoJDBCOptions options) {
|
75 |
this.options = options;
|
76 |
} |
77 |
|
78 |
protected String getTranslatedIdentifier(String identifier) { |
79 |
String s = identifier;
|
80 |
if( this.options.getTranslateIdentifiersToLowerCase() ) { |
81 |
s = s.toLowerCase(); |
82 |
} |
83 |
if( this.options.getRemoveSpacesInIdentifiers() ) { |
84 |
s = StringUtils.normalizeSpace(s).replace(" ", "_"); |
85 |
} |
86 |
return s;
|
87 |
} |
88 |
|
89 |
protected void createTable(JDBCServerExplorer explorer) throws Exception { |
90 |
|
91 |
FeatureType targetFeatureType; |
92 |
EditableFeatureType targetEditableFeatureType; |
93 |
|
94 |
targetFeatureType = options.getSource().getDefaultFeatureType().getCopy(); |
95 |
targetEditableFeatureType = targetFeatureType.getEditable(); |
96 |
|
97 |
if( this.options.getTranslateIdentifiersToLowerCase() |
98 |
|| this.options.getRemoveSpacesInIdentifiers() ) {
|
99 |
for( int i=0; i<targetEditableFeatureType.size(); i++ ) { |
100 |
EditableFeatureAttributeDescriptor x = (EditableFeatureAttributeDescriptor)targetEditableFeatureType.get(i); |
101 |
x.setName(getTranslatedIdentifier(x.getName())); |
102 |
} |
103 |
} |
104 |
|
105 |
if (this.options.getPrimaryKey() != null) { |
106 |
EditableFeatureAttributeDescriptor pk = (EditableFeatureAttributeDescriptor) targetEditableFeatureType.get(getTranslatedIdentifier(options.getPrimaryKey())); |
107 |
if (pk == null) { |
108 |
pk = targetEditableFeatureType.add( |
109 |
getTranslatedIdentifier(this.options.getPrimaryKey()),
|
110 |
DataTypes.LONG |
111 |
); |
112 |
pk.setIsPrimaryKey(true);
|
113 |
pk.setIsAutomatic(true);
|
114 |
} else {
|
115 |
pk.setIsPrimaryKey(true);
|
116 |
} |
117 |
} |
118 |
|
119 |
if( this.options.getCreateIndexInGeometryRow() ) { |
120 |
EditableFeatureAttributeDescriptor x = (EditableFeatureAttributeDescriptor)targetEditableFeatureType.getDefaultGeometryAttribute(); |
121 |
x.setIsIndexed(true);
|
122 |
} |
123 |
|
124 |
// ======================================================
|
125 |
// Reprojection: set SRS of geometry field to target SRS
|
126 |
EditableFeatureAttributeDescriptor attrdescriptor |
127 |
= (EditableFeatureAttributeDescriptor) targetEditableFeatureType.getDefaultGeometryAttribute(); |
128 |
if (attrdescriptor != null) { |
129 |
attrdescriptor.setSRS(this.options.getTargetProjection());
|
130 |
} |
131 |
|
132 |
// ======================================
|
133 |
JDBCNewStoreParameters createTableParams = (JDBCNewStoreParameters) explorer.getAddParameters(); |
134 |
|
135 |
createTableParams.setDefaultFeatureType(targetEditableFeatureType); |
136 |
createTableParams.setTable(this.options.getTableName());
|
137 |
explorer.add(explorer.getStoreName(), createTableParams, true);
|
138 |
} |
139 |
|
140 |
private static class InvalidGeometryException extends ExporttoServiceException { |
141 |
|
142 |
public InvalidGeometryException(Feature feature, String checkMessage) { |
143 |
super(checkMessage, null, checkMessage, 0); |
144 |
this.feature = feature;
|
145 |
} |
146 |
} |
147 |
|
148 |
public void export(FeatureSet featureSet) throws ExporttoServiceException { |
149 |
Geometry.ValidationStatus geometryCheck; |
150 |
|
151 |
DisposableIterator it = null;
|
152 |
EditableFeature targetFeature = null;
|
153 |
FeatureStore target = null;
|
154 |
|
155 |
try {
|
156 |
|
157 |
// ======================================
|
158 |
// Reprojection
|
159 |
FeatureAttributeDescriptor geo_att = this.options.getSource().getDefaultFeatureType().getDefaultGeometryAttribute();
|
160 |
IProjection sourceProjection = null;
|
161 |
ICoordTrans coord_trans = null;
|
162 |
Geometry reproj_geom = null;
|
163 |
if (geo_att != null) { |
164 |
sourceProjection = geo_att.getSRS(); |
165 |
// this comparison is perhaps too preventive
|
166 |
// we could have two instances of same projection
|
167 |
// so we would do more computations than needed
|
168 |
if (sourceProjection != this.options.getTargetProjection()) { |
169 |
coord_trans = sourceProjection.getCT(this.options.getTargetProjection());
|
170 |
} |
171 |
} |
172 |
// ============================================
|
173 |
|
174 |
DataManager dataManager = DALLocator.getDataManager(); |
175 |
|
176 |
JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer( |
177 |
this.options.getExplorerParameters().getExplorerName(),
|
178 |
this.options.getExplorerParameters()
|
179 |
); |
180 |
|
181 |
if (this.options.canCreatetable() ) { |
182 |
this.createTable(explorer);
|
183 |
} |
184 |
|
185 |
JDBCStoreParameters openParams = (JDBCStoreParameters) explorer.getOpenParameters(); |
186 |
openParams.setTable(this.options.getTableName());
|
187 |
openParams.setCRS(this.options.getTargetProjection());
|
188 |
openParams.setDefaultGeometryField( |
189 |
this.options.getSource().getDefaultFeatureType().getDefaultGeometryAttributeName()
|
190 |
); |
191 |
target = (FeatureStore) explorer.open(openParams); |
192 |
|
193 |
FeatureType targetFeatureType = target.getDefaultFeatureType(); |
194 |
FeatureType sourceFeatureType = featureSet.getDefaultFeatureType(); |
195 |
|
196 |
target.edit(FeatureStore.MODE_APPEND); |
197 |
|
198 |
int featureCount = 1; |
199 |
taskStatus.setRangeOfValues(0, featureSet.getSize());
|
200 |
|
201 |
it = featureSet.fastIterator(); |
202 |
while (it.hasNext()) {
|
203 |
Feature feature = (Feature) it.next(); |
204 |
targetFeature = target.createNewFeature(targetFeatureType, feature); |
205 |
for( int i=0; i<sourceFeatureType.size(); i++ ) { |
206 |
FeatureAttributeDescriptor x = sourceFeatureType.getAttributeDescriptor(i); |
207 |
targetFeature.set(getTranslatedIdentifier(x.getName()), feature.get(x.getName())); |
208 |
} |
209 |
|
210 |
Geometry geometry = targetFeature.getDefaultGeometry(); |
211 |
if (geometry != null) { |
212 |
switch (this.options.getGeometryChecks()) { |
213 |
case CHECK_IF_CORRUPT:
|
214 |
geometryCheck = geometry.getValidationStatus(); |
215 |
if (geometryCheck.getStatusCode() == Geometry.ValidationStatus.CURRUPTED) {
|
216 |
switch (this.options.getGeometryChecksAction()) { |
217 |
case ACTION_SET_GEOMETRY_TO_NULL:
|
218 |
targetFeature.setDefaultGeometry(null);
|
219 |
break;
|
220 |
case ACTION_SKIP_FEATURE:
|
221 |
continue;
|
222 |
case ACTION_ABORT:
|
223 |
default:
|
224 |
throw new InvalidGeometryException(targetFeature, geometryCheck.getMessage()); |
225 |
} |
226 |
} |
227 |
|
228 |
break;
|
229 |
|
230 |
case CHECK_IF_VALID:
|
231 |
geometryCheck = geometry.getValidationStatus(); |
232 |
if (!geometryCheck.isValid()) {
|
233 |
Geometry g = null;
|
234 |
if (this.options.getTryToFixGeometry() ) { |
235 |
g = geometry.makeValid(); |
236 |
if (g != null) { |
237 |
targetFeature.setDefaultGeometry(g); |
238 |
} |
239 |
} |
240 |
if (g == null) { |
241 |
switch (this.options.getGeometryChecksAction()) { |
242 |
case ACTION_SET_GEOMETRY_TO_NULL:
|
243 |
targetFeature.setDefaultGeometry(null);
|
244 |
break;
|
245 |
case ACTION_SKIP_FEATURE:
|
246 |
continue;
|
247 |
case ACTION_ABORT:
|
248 |
default:
|
249 |
throw new InvalidGeometryException(targetFeature, geometryCheck.getMessage()); |
250 |
} |
251 |
} |
252 |
} |
253 |
|
254 |
break;
|
255 |
case CHECK_NONE:
|
256 |
default:
|
257 |
break;
|
258 |
} |
259 |
// ================================================
|
260 |
// Reprojection
|
261 |
if (geo_att != null && coord_trans != null) { |
262 |
reproj_geom = targetFeature.getDefaultGeometry(); |
263 |
reproj_geom = reproj_geom.cloneGeometry(); |
264 |
reproj_geom.reProject(coord_trans); |
265 |
targetFeature.setDefaultGeometry(reproj_geom); |
266 |
} |
267 |
// ================================================
|
268 |
} |
269 |
|
270 |
target.insert(targetFeature); |
271 |
this.taskStatus.setCurValue(featureCount);
|
272 |
|
273 |
if (this.taskStatus.isCancellationRequested()) { |
274 |
return;
|
275 |
} |
276 |
featureCount++; |
277 |
} |
278 |
targetFeature = null;
|
279 |
target.finishEditing(); |
280 |
|
281 |
if( this.options.getUpdateTableStatistics() ) { |
282 |
explorer.updateTableStatistics(openParams.tableID()); |
283 |
} |
284 |
|
285 |
if (exporttoServiceFinishAction != null) { |
286 |
exporttoServiceFinishAction.finished( |
287 |
this.options.getTableName() + " (" + explorer.getStoreName() + ")", |
288 |
openParams); |
289 |
} |
290 |
|
291 |
} catch (Exception e) { |
292 |
logger.warn("Can't export data.", e);
|
293 |
taskStatus.message(e.getMessage()); |
294 |
throw new ExporttoServiceException(e, targetFeature); |
295 |
|
296 |
} finally {
|
297 |
if (it != null) { |
298 |
it.dispose(); |
299 |
} |
300 |
featureSet.dispose(); |
301 |
if (target != null) { |
302 |
target.dispose(); |
303 |
} |
304 |
this.taskStatus.terminate();
|
305 |
this.taskStatus.remove();
|
306 |
} |
307 |
} |
308 |
|
309 |
public void setFinishAction( |
310 |
ExporttoServiceFinishAction exporttoServiceFinishAction) { |
311 |
this.exporttoServiceFinishAction = exporttoServiceFinishAction;
|
312 |
} |
313 |
|
314 |
} |