boolean forceCreation)
throws PostGisException {
DataStore dstDs=null;
SimpleFeatureSource fsLayer = null;
Transaction tx = new DefaultTransaction();
//== check schema: create new or check they are aligned
try {
// craete destination store
dstDs = createDatastore(dstPg);
// check if destinationlayer exists
boolean layerExists = existFeatureTable(dstDs, layer);
if( ! layerExists ) {
if(forceCreation) {
fsLayer = createEnrichedSchema(dstDs, (SimpleFeatureType) sourceFC.getSchema(), layer);
} else {
throw new PostGisException("The layer " + layer + " does not exist");
}
} else {
fsLayer = dstDs.getFeatureSource(layer);
checkAttributesMatch(sourceFC, ((JDBCFeatureStore)dstDs.getFeatureSource(layer)).getFeatureSource());
}
//== schemas are ok: transfer data
int iYear = Integer.parseInt(year);
int iMonth = month==null? -1 : Integer.parseInt(month);
int iDay = day==null? -1 : Integer.parseInt(day);
Date date = new Date(iYear-1900, iMonth==-1?0:iMonth-1, iDay==-1?1:iDay);
SimpleFeatureStore featureStoreData = (SimpleFeatureStore) fsLayer;
// update the layer store with the new SimpleFeature coming from the shape file
// data are saved itemsForPage elements at time
SimpleFeatureType dstSchema = dstDs.getSchema(layer);
featureStoreData.setTransaction(tx);
SimpleFeatureType srcSchema = sourceFC.getSchema();
List<AttributeDescriptor> srcAttributeDescriptor = srcSchema.getAttributeDescriptors();
SimpleFeatureBuilder featureBuilderData = new SimpleFeatureBuilder(dstSchema);
SimpleFeatureIterator featureIterator = sourceFC.features();
boolean hasFinished = false;
int i = 0;
Set<String> loggedMissingAttrib = new HashSet<String>();
while (!hasFinished) { // TODO: refacotr this nested loop!!!
//DamianoGiampaoli 03/11/2014
//TEST IT!!!
// Due to the upgrade needed from the Geotools 8-SNAPSHOT to geotools 10.8 in order to support GeoBatch 1.4.x
// The add(SimpleFeature) method of SimpleFeatureCollection cannot be used anymore so a List<SimpleFeature> must be used.
//SimpleFeatureCollection sfcData = FeatureCollections.newCollection();
List<SimpleFeature> sfcData = new ArrayList<SimpleFeature>();
boolean exitForIntermediateSaving = false;
while (featureIterator.hasNext() && !exitForIntermediateSaving) {
i++;
exitForIntermediateSaving = ((i % ITEM_X_PAGE) == 0);
SimpleFeature sf = featureIterator.next();
SimpleFeature data = featureBuilderData.buildFeature(sf.getID());
for (int j = 0; j < srcAttributeDescriptor.size(); j++) {
String srcName = srcAttributeDescriptor.get(j).getLocalName();
String dstName = srcName.toLowerCase(); // FIXME: this is a worksroung for SHP 2 PG attrib name conversion. make it general!
Property p = sf.getProperty(srcName);
if( p!= null) // be lenient about inexistent attributes: consistency checks have already bben performed.
data.setAttribute(dstName, sf.getAttribute(srcName));
else {
if(LOGGER.isDebugEnabled() && ! loggedMissingAttrib.contains(srcName) ) {
LOGGER.debug("Skipping attrib "+srcName+" in feature #"+i);
loggedMissingAttrib.add(srcName);
}
}
}
data.setAttribute(YEARATTRIBUTENAME, iYear);
if(iMonth != -1){
data.setAttribute(MONTHATTRIBUTENAME, month);
}
if(iDay != -1){
data.setAttribute(DAYATTRIBUTENAME, day);
}
data.setAttribute(DATEATTRIBUTENAME, date);
sfcData.add(data);
}
if (!exitForIntermediateSaving) {
hasFinished = true;
}
SimpleFeatureCollection featureCollection = new ListFeatureCollection(srcSchema, sfcData);
featureStoreData.addFeatures(featureCollection);
}
tx.commit();
LOGGER.info("Copied " + i + " features for "+ layer+"/"+year+"/"+month+"/"+day+ " to " + dstPg);
return i;
} catch (Exception e) {
quietRollbackTransaction(tx);