}
public void execute(TransactionElement element, TransactionRequest request, Map featureStores,
TransactionResponse response, TransactionListener listener) throws WFSTransactionException {
Replace replace = (Replace) element;
List<SimpleFeature> newFeatures = replace.getFeatures();
SimpleFeatureStore featureStore =
DataUtilities.simple((FeatureStore) featureStores.values().iterator().next());
if (featureStore == null) {
throw new WFSException(element, "Could not obtain feature store");
}
//check the supplied filter matches the number of supplied features
String featureTypeName = featureStore.getSchema().getTypeName();
//ids of replaced featres
Collection<FeatureId> replaced = new ArrayList();
try {
Query query = new Query(featureTypeName, replace.getFilter());
SimpleFeatureCollection features = featureStore.getFeatures(replace.getFilter());
if (newFeatures.size() != features.size()) {
throw new WFSException(element, String.format("Specified filter matched %d features but " +
"%d were supplied", features.size(), newFeatures.size()));
}
//replace the features in order...
//JD, TODO: a better mechanism for replace... this is sort of a hack based on a combo of
// feature ids and orders
// may want to check if the store is making feature id's writable and attempt
// to actually update the ID's
//load all the existing features into memory
Map<String,SimpleFeature> oldFeatures = new LinkedHashMap<String, SimpleFeature>();
SimpleFeatureIterator it = features.features();
try {
while(it.hasNext()) {
SimpleFeature f = it.next();
oldFeatures.put(f.getID(), f);
}
}
finally {
it.close();
}
//first pass update all the features that match by id
List<SimpleFeature> leftovers = new ArrayList();
for (SimpleFeature newFeature : newFeatures) {
SimpleFeature oldFeature = oldFeatures.get(newFeature.getID());
if (oldFeature == null) {
leftovers.add(newFeature);
continue;
}
//matching feature found, update it
replace(oldFeature, newFeature, featureStore, oldFeatures, replaced);
}
//do left overs
for (SimpleFeature newFeature : leftovers) {
//grab the "next" old feature
SimpleFeature oldFeature = oldFeatures.values().iterator().next();
replace(oldFeature, newFeature, featureStore, oldFeatures, replaced);
}
}
catch(IOException e) {
throw new WFSException(element, "Transaction REPLACE failed", e);
}
response.setTotalReplaced(BigInteger.valueOf(replaced.size()));
response.addReplacedFeatures(replace.getHandle(), replaced);
}