return new SubmitLink("save") {
@Override
public void onSubmit() {
try {
Catalog catalog = getCatalog();
ResourceInfo resourceInfo = getResourceInfo();
if (isNew) {
ReferencedEnvelope bounds;
// updating grid if is a coverage
if(resourceInfo instanceof CoverageInfo) {
// the coverage bounds computation path is a bit more linear, the
// readers always return the bounds and in the proper CRS (afaik)
CoverageInfo cinfo = (CoverageInfo) resourceInfo;
AbstractGridCoverage2DReader reader = (AbstractGridCoverage2DReader) cinfo.getGridCoverageReader(null, null);
bounds = new ReferencedEnvelope(reader.getOriginalEnvelope());
// apply the bounds, taking into account the reprojection policy if need be
if (resourceInfo.getProjectionPolicy() == ProjectionPolicy.REPROJECT_TO_DECLARED && bounds != null) {
try {
bounds = bounds.transform(resourceInfo.getCRS(), true);
GridGeometry grid = ((CoverageInfo) resourceInfo).getGrid();
((CoverageInfo) resourceInfo).setGrid(new GridGeometry2D(grid.getGridRange(),grid.getGridToCRS(), resourceInfo.getCRS()));
} catch(Exception e) {
throw (IOException) new IOException("transform error").initCause(e);
}
}
}
catalog.add(resourceInfo);
try {
catalog.add(getLayerInfo());
} catch (IllegalArgumentException e) {
catalog.remove(resourceInfo);
throw e;
}
} else {
ResourceInfo oldState = catalog.getResource(resourceInfo.getId(), ResourceInfo.class);
catalog.save(resourceInfo);
try {
catalog.save(getLayerInfo());
} catch (IllegalArgumentException e) {
catalog.save(oldState);