* if a lock failed and the lock specified all locks, or if an
* another error occurred processing the lock operation
*/
public LockFeatureResponseType lockFeature(LockFeatureType request)
throws WFSException {
FeatureLock fLock = null;
try {
// check we are dealing with a well formed request, there is at
// least on lock request?
List locks = request.getLock();
if ((locks == null) || locks.isEmpty()) {
String msg = "A LockFeature request must contain at least one LOCK element";
throw new WFSException(msg);
}
LOGGER.info("locks size is " + locks.size());
// create a new lock (token used to manage locks across datastores)
fLock = newFeatureLock(request);
// prepare the response object
LockFeatureResponseType response = WfsFactory.eINSTANCE.createLockFeatureResponseType();
response.setLockId(fLock.getAuthorization());
response.setFeaturesLocked(WfsFactory.eINSTANCE.createFeaturesLockedType());
response.setFeaturesNotLocked(WfsFactory.eINSTANCE.createFeaturesNotLockedType());
// 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++) {
LockType lock = (LockType) 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 = (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("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<SimpleFeatureType, SimpleFeature>) source).setFeatureLock(fLock);
}
} catch (IOException e) {
throw new WFSException(e);
}
Iterator reader = null;
int numberLocked = -1;
try {
for (reader = features.iterator(); 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.getFeaturesNotLocked().getFeatureId().add(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 DefaultQuery(meta.getName(), (Filter) fidFilter,
Query.DEFAULT_MAX, Query.ALL_NAMES, lock.getHandle());
numberLocked = ((FeatureLocking<SimpleFeatureType, SimpleFeature>) source)
.lockFeatures(query);
if (numberLocked == 1) {
LOGGER.fine("Lock " + fid + " (authID:" + fLock.getAuthorization()
+ ")");
response.getFeaturesLocked().getFeatureId().add(fid);
// lockedFids.add(fid);
} else if (numberLocked == 0) {
LOGGER.fine("Lock " + fid + " conflict (authID:"
+ fLock.getAuthorization() + ")");
response.getFeaturesNotLocked().getFeatureId().add(fid);
// lockFailedFids.add(fid);
} else {
LOGGER.warning("Lock " + numberLocked + " " + fid + " (authID:"
+ fLock.getAuthorization() + ") duplicated FeatureID!");
response.getFeaturesLocked().getFeatureId().add(fid);
// lockedFids.add(fid);
}
}
}
} catch (IOException ioe) {
throw new WFSException(ioe);
} finally {
if (reader != null) {
features.close(reader);
}
}
// 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(response.getLockId());
DataStore dataStore = (DataStore) source.getDataStore();
dataStore.getLockingManager().refresh(response.getLockId(), t);
} finally {
t.commit();
}
} catch (IOException e) {
throw new WFSException(e);
} finally {
try {
t.close();
} catch(IOException e) {
throw new WFSException(e);
}
}
}
}
// should we releas all? if not set default to true
boolean lockAll = !(request.getLockAction() == AllSomeType.SOME_LITERAL);
if (lockAll && !response.getFeaturesNotLocked().getFeatureId().isEmpty()) {
// I think we need to release and fail when lockAll fails
//
// abort will release the locks
throw new WFSException("Could not aquire locks for:"
+ response.getFeaturesNotLocked());
}
//remove empty parts of the response object
if (response.getFeaturesLocked().getFeatureId().isEmpty()) {
response.setFeaturesLocked(null);
}
if (response.getFeaturesNotLocked().getFeatureId().isEmpty()) {
response.setFeaturesNotLocked(null);
}
return response;
} catch (WFSException e) {
// release locks when something fails
if (fLock != null) {
try {
release(fLock.getAuthorization());
} catch (WFSException e1) {
// log it
LOGGER.log(Level.SEVERE, "Error occured releasing locks", e1);
}
}