void loadIntoDataStore(ImportTask task, DataStoreInfo store, VectorFormat format,
VectorTransformChain tx) throws Exception {
ImportData data = task.getData();
FeatureReader reader = null;
FeatureWriter writer = null;
// using this exception to throw at the end
Exception error = null;
try {
reader = format.read(data, task);
SimpleFeatureType featureType = (SimpleFeatureType) reader.getFeatureType();
final String featureTypeName = featureType.getName().getLocalPart();
DataStore dataStore = (DataStore) store.getDataStore(null);
FeatureDataConverter featureDataConverter = FeatureDataConverter.DEFAULT;
if (isShapefileDataStore(dataStore)) {
featureDataConverter = FeatureDataConverter.TO_SHAPEFILE;
}
else if (isOracleDataStore(dataStore)) {
featureDataConverter = FeatureDataConverter.TO_ORACLE;
}
else if (isPostGISDataStore(dataStore)) {
featureDataConverter = FeatureDataConverter.TO_POSTGIS;
}
featureType = featureDataConverter.convertType(featureType, format, data, task);
UpdateMode updateMode = task.getUpdateMode();
final String uniquifiedFeatureTypeName;
if (updateMode == UpdateMode.CREATE) {
//find a unique type name in the target store
uniquifiedFeatureTypeName = findUniqueNativeFeatureTypeName(featureType, store);
task.setOriginalLayerName(featureTypeName);
if (!uniquifiedFeatureTypeName.equals(featureTypeName)) {
//update the metadata
task.getLayer().getResource().setName(uniquifiedFeatureTypeName);
task.getLayer().getResource().setNativeName(uniquifiedFeatureTypeName);
//retype
SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
typeBuilder.setName(uniquifiedFeatureTypeName);
typeBuilder.addAll(featureType.getAttributeDescriptors());
featureType = typeBuilder.buildFeatureType();
}
// @todo HACK remove this at some point when timezone issues are fixed
// this will force postgis to create timezone w/ timestamp fields
if (dataStore instanceof JDBCDataStore) {
JDBCDataStore ds = (JDBCDataStore) dataStore;
// sniff for postgis (h2 is used in tests and will cause failure if this occurs)
if (ds.getSqlTypeNameToClassMappings().containsKey("timestamptz")) {
ds.getSqlTypeToSqlTypeNameOverrides().put(java.sql.Types.TIMESTAMP, "timestamptz");
}
}
//apply the feature type transform
featureType = tx.inline(task, dataStore, featureType);
dataStore.createSchema(featureType);
} else {
// @todo what to do if featureType transform is present?
// @todo implement me - need to specify attribute used for id
if (updateMode == UpdateMode.UPDATE) {
throw new UnsupportedOperationException("updateMode UPDATE is not supported yet");
}
uniquifiedFeatureTypeName = featureTypeName;
}
Transaction transaction = new DefaultTransaction();
if (updateMode == UpdateMode.REPLACE) {
FeatureStore fs = (FeatureStore) dataStore.getFeatureSource(featureTypeName);
fs.setTransaction(transaction);
fs.removeFeatures(Filter.INCLUDE);
}
//start writing features
// @todo ability to collect transformation errors for use in a dry-run (auto-rollback)
ProgressMonitor monitor = task.progress();
// @todo need better way to communicate to client
int skipped = 0;
int cnt = 0;
// metrics
long startTime = System.currentTimeMillis();
task.clearMessages();
task.setTotalToProcess(format.getFeatureCount(task.getData(), task));
LOGGER.info("begining import");
try {
writer = dataStore.getFeatureWriterAppend(uniquifiedFeatureTypeName, transaction);
while(reader.hasNext()) {
if (monitor.isCanceled()){
break;
}
SimpleFeature feature = (SimpleFeature) reader.next();
SimpleFeature next = (SimpleFeature) writer.next();
//(JD) TODO: some formats will rearrange the geometry type (like shapefile) which
// makes the goemetry the first attribute reagardless, so blindly copying over
// attributes won't work unless the source type also has the geometry as the