return new SubmitLink("save") {
@Override
public void onSubmit() {
try {
Catalog catalog = getCatalog();
ResourceInfo resourceInfo = getResourceInfo();
if (isNew) {
// 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, GeoTools.getDefaultHints());
// get bounds
final ReferencedEnvelope bounds = new ReferencedEnvelope(reader.getOriginalEnvelope());
// apply the bounds, taking into account the reprojection policy if need be
final ProjectionPolicy projectionPolicy=resourceInfo.getProjectionPolicy();
if (projectionPolicy != ProjectionPolicy.NONE && bounds != null) {
// we need to fix the registered grid for this coverage
final GridGeometry grid = cinfo.getGrid();
cinfo.setGrid(new GridGeometry2D(grid.getGridRange(),grid.getGridToCRS(), resourceInfo.getCRS()));
}
}
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 {
LayerInfo layer = getLayerInfo();
layer.setResource(resourceInfo);
catalog.save(layer);