// ---
// --------------------------------------------------------------------------
public Element exec(Element params, ServiceContext context) throws Exception {
GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
SchemaManager scm = gc.getBean(SchemaManager.class);
String schema = Util.getParam(params, Params.SCHEMA);
// see if the schema to be deleted actually exists
Element response = new Element("response");
if (!scm.existsSchema(schema)) {
response.setAttribute("status", "error");
response.setAttribute("message", "Schema does not exist");
return response;
}
// fast search to see if any records are present that use this schema
ServiceConfig config = new ServiceConfig();
SearchManager searchMan = gc.getBean(SearchManager.class);
Element searchParams = new Element("parameters");
searchParams.addContent(new Element("_schema").setText(schema));
MetaSearcher searcher = searchMan.newSearcher(SearchManager.LUCENE, Geonet.File.SEARCH_LUCENE);
try {
searcher.search(context, searchParams, config);
int results = searcher.getSize();
if (results == 0) { // check for templates
searchParams.addContent(new Element("_isTemplate").setText("y"));
searcher.search(context, searchParams, config);
results = searcher.getSize();
}
if (results > 0) {
String errStr = "Cannot remove schema "+schema+" because there are records that belong to this schema in the catalog";
context.error(errStr);
response.setAttribute("status", "error");
response.setAttribute("message", errStr);
return response;
}
} catch (Exception e) {
e.printStackTrace();
String errStr = "Cannot remove schema "+schema+" because the search for records that belong to this schema FAILED ("+e.getMessage()+")";
context.error(errStr);
response.setAttribute("status", "error");
response.setAttribute("message", errStr);
return response;
} finally {
searcher.close();
}
// check for any schemas that may be dependent on the schema to be deleted
List<String> dependsOnMe = scm.getSchemasThatDependOnMe(schema);
if (dependsOnMe.size() > 0) {
String errStr = "Cannot remove schema "+schema+" because the following schemas list it as a dependency: "+dependsOnMe;
context.error(errStr);
response.setAttribute("status", "error");
response.setAttribute("message", errStr);
return response;
}
// finally, try to delete the schema
try {
scm.deletePluginSchema(schema);
response.setAttribute("status", "ok");
response.setAttribute("message", "Schema "+schema+" deleted");
} catch (Exception e) {
e.printStackTrace();
response.setAttribute("status", "error");