// create a new lock (token used to manage locks across datastores)
fLock = newFeatureLock(request);
// prepare the response object
LockFeatureResponse response = request.createResponse();
response.setLockId(fLock.getAuthorization());
// go thru each lock request, and try to perform locks on a feature
// by feature basis
// in order to allow for both "all" and "some" lock behaviour
// TODO: if the lock is the default this default, lock the whole
// query directly, should be a lot faster
for (int i = 0, n = locks.size(); i < n; i++) {
Lock lock = locks.get(i);
LOGGER.info("curLock is " + lock);
QName typeName = lock.getTypeName();
// get out the filter, and default to no filtering if none was
// provided
Filter filter = lock.getFilter();
if (filter == null) {
filter = Filter.INCLUDE;
}
FeatureTypeInfo meta;
FeatureSource<? extends FeatureType, ? extends Feature> source;
FeatureCollection<? extends FeatureType, ? extends Feature> features;
try {
meta = catalog.getFeatureTypeByName(typeName.getNamespaceURI(), typeName.getLocalPart());
if (meta == null) {
throw new WFSException(request, "Unknown feature type " + typeName.getPrefix() + ":"
+ typeName.getLocalPart());
}
source = meta.getFeatureSource(null,null);
// make sure all geometric elements in the filter have a crs, and that the filter
// is reprojected to store's native crs as well
CoordinateReferenceSystem declaredCRS = WFSReprojectionUtil.getDeclaredCrs(
source.getSchema(), request.getVersion());
filter = WFSReprojectionUtil.normalizeFilterCRS(filter, source.getSchema(), declaredCRS);
// now gather the features
features = source.getFeatures(filter);
if (source instanceof FeatureLocking) {
((FeatureLocking) source).setFeatureLock(fLock);
}
} catch (IOException e) {
throw new WFSException(request, e);
}
FeatureIterator reader = null;
int numberLocked = -1;
try {
for (reader = features.features(); reader.hasNext();) {
SimpleFeature feature = (SimpleFeature) reader.next();
FeatureId fid = fid(feature.getID());
Id fidFilter = fidFilter(fid);
if (!(source instanceof FeatureLocking)) {
LOGGER.fine("Lock " + fid + " not supported by data store (authID:"
+ fLock.getAuthorization() + ")");
response.addNotLockedFeature(fid);
// lockFailedFids.add(fid);
} else {
// DEFQuery is just some indirection, should be in
// the locking interface.
// int numberLocked =
// ((DEFQueryFeatureLocking)source).lockFeature(feature);
// HACK: Query.NO_NAMES isn't working in postgis
// right now,
// so we'll just use all.
Query query = new Query(meta.getName(), (Filter) fidFilter,
Query.DEFAULT_MAX, Query.ALL_NAMES, lock.getHandle());
numberLocked = ((FeatureLocking) source).lockFeatures(query);
if (numberLocked == 1) {
LOGGER.fine("Lock " + fid + " (authID:" + fLock.getAuthorization()
+ ")");
response.addLockedFeature( fid);
// lockedFids.add(fid);
} else if (numberLocked == 0) {
LOGGER.fine("Lock " + fid + " conflict (authID:"
+ fLock.getAuthorization() + ")");
response.addNotLockedFeature(fid);
// lockFailedFids.add(fid);
} else {
LOGGER.warning("Lock " + numberLocked + " " + fid + " (authID:"
+ fLock.getAuthorization() + ") duplicated FeatureID!");
response.addLockedFeature(fid);
// lockedFids.add(fid);
}
}
}
} catch (IOException ioe) {
throw new WFSException(request, ioe);
} finally {
if (reader != null) {
reader.close();
}
}
// refresh lock times, so they all start the same instant and we
// are nearer
// to the spec when it says the expiry should start when the
// lock
// feature response has been totally written
if (numberLocked > 0) {
Transaction t = new DefaultTransaction();
try {
try {
t.addAuthorization(fLock.getAuthorization());
DataStore dataStore = (DataStore) source.getDataStore();
dataStore.getLockingManager().refresh(fLock.getAuthorization(), t);
} finally {
t.commit();
}
} catch (IOException e) {
throw new WFSException(request, e);
} finally {
try {
t.close();
} catch(IOException e) {
throw new WFSException(request, e);
}
}
}
}
// should we releas all? if not set default to true
boolean lockAll = !request.isLockActionSome();
List notLocked = response.getNotLockedFeatures();
if (lockAll && (notLocked != null && !notLocked.isEmpty())) {
// I think we need to release and fail when lockAll fails
//
// abort will release the locks
throw new WFSException(request, "Could not aquire locks for:" + notLocked);