String name = obj.str("name");
if (name == null) {
throw new BadRequestException("Layer object requires name");
}
try {
@SuppressWarnings("unused")
LayerInfo l = findLayer(wsName, name, cat);
throw new BadRequestException("Layer named '" + wsName + ":" + name
+ "' already exists");
} catch (NotFoundException good) {
}
LayerInfo l = null;
try {
if (obj.has("layer")) {
l = createLayerFromLayer(obj.object("layer"), ws, cat);
} else if (obj.has("resource")) {
l = createLayerFromResource(obj.object("resource"), ws, cat);
} else {
throw new BadRequestException("Layer create requires from (to create from existing layer) or resource " +
"(to create from store data)");
}
}
catch(IOException e) {
throw new RuntimeException("Failed to create layer: " + e.getMessage(), e);
}
// proj specified?
JSONObj proj = obj.object("proj");
if (proj != null) {
String srs = null;
try {
srs = IO.srs(proj);
} catch (IllegalArgumentException e) {
throw new BadRequestException(e.getMessage(), e);
}
ResourceInfo r = l.getResource();
r.setSRS(srs);
try {
new CatalogBuilder(cat).setupBounds(r);
} catch (IOException e) {
throw new RuntimeException("Unable to set projection on resource: " + e.getMessage(), e);
}
}
// restore name in case it was replaced by duplicate
l.getResource().setName(name);
l.setName(name);
// title
String title = obj.str("title");
if (title == null) {
title = WordUtils.capitalize(name);
}
l.getResource().setTitle(title);
l.setTitle(title);
// description
String desc = obj.str("description");
if (desc != null) {
l.getResource().setAbstract(desc);
l.setAbstract(desc);
}
// copy the style into it's own unique
try {
l.setDefaultStyle(copyStyle(l, ws, cat));
} catch (IOException e) {
throw new RuntimeException("Error copying style: " + e.getMessage(), e);
}
Date created = new Date();
Metadata.created(l, created);
cat.add(l.getDefaultStyle());
cat.add(l.getResource());
cat.add(l);
Metadata.modified(ws, created);
cat.save(ws);