Skip to content

Commit

Permalink
Fix for tickets #955 and #958 - don't allow schema to be deleted if t…
Browse files Browse the repository at this point in the history
…here are

records in the catalog that still use that schema - after analysis its better
to do this - fewer changes required for 2.8.x and no compelling use case to
allow orphan records
  • Loading branch information
sppigot committed Jun 16, 2012
1 parent 4e1dedf commit 9383aaa
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 31 deletions.
30 changes: 15 additions & 15 deletions web/src/main/java/org/fao/geonet/guiservices/templates/Get.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.fao.geonet.GeonetContext;
import org.fao.geonet.constants.Edit;
import org.fao.geonet.constants.Geonet;
import org.fao.geonet.kernel.SchemaManager;
import org.fao.geonet.kernel.search.MetaSearcher;
import org.fao.geonet.kernel.search.SearchManager;
import org.jdom.Element;
Expand All @@ -51,8 +52,6 @@

public class Get implements Service
{
private String styleSheet;

private String arParams[] =
{
"extended", "off",
Expand All @@ -67,10 +66,7 @@ public class Get implements Service
//---
//--------------------------------------------------------------------------

public void init(String appPath, ServiceConfig params) throws Exception
{
styleSheet = appPath + Geonet.Path.STYLESHEETS +"/portal-present.xsl";
}
public void init(String appPath, ServiceConfig params) throws Exception {}

//--------------------------------------------------------------------------
//---
Expand All @@ -80,12 +76,14 @@ public void init(String appPath, ServiceConfig params) throws Exception

public Element exec(Element params, ServiceContext context) throws Exception
{
Element result = search(params, context).setName(Jeeves.Elem.RESPONSE);
Element root = new Element("root");

GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);

root.addContent(result);
SchemaManager schemaMan = gc.getSchemamanager();

Element result = search(params, context).setName(Jeeves.Elem.RESPONSE);

List list = Xml.transform(root, styleSheet).getChildren();
List list = result.getChildren();

Element response = new Element("dummy");

Expand All @@ -100,13 +98,14 @@ public Element exec(Element params, ServiceContext context) throws Exception
continue;
}

String id = info.getChildText(Edit.Info.Elem.ID);
String template = info.getChildText(Edit.Info.Elem.IS_TEMPLATE);
String displayOrder = info.getChildText(Edit.Info.Elem.DISPLAY_ORDER);
String template = elem.getChildText("isTemplate");
String displayOrder = elem.getChildText("displayOrder");
String schema = info.getChildText("schema");
String id = info.getChildText(Edit.Info.Elem.ID);

if (template.equals("y")) {
if (template.equals("y") && schemaMan.existsSchema(schema)) {
// heikki, GeoNovum: added displayOrder
responseRecords.add(buildRecord(id, elem.getChildText("title"), info.getChildText("schema"), displayOrder));
responseRecords.add(buildRecord(id, elem.getChildText("title"), schema, displayOrder));
}
}
// heikki, Geonovum: then process them to ensure displayOrder is not empty and is unique
Expand Down Expand Up @@ -159,6 +158,7 @@ private Element search(Element par, ServiceContext context) throws Exception

params.addContent(new Element("from").setText("1"));
params.addContent(new Element("to").setText(searcher.getSize() +""));
params.addContent(new Element("fast").setText("index"));

Element result = searcher.present(context, params, config);

Expand Down
73 changes: 57 additions & 16 deletions web/src/main/java/org/fao/geonet/services/schema/Delete.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.fao.geonet.constants.Geonet;
import org.fao.geonet.constants.Params;
import org.fao.geonet.kernel.SchemaManager;
import org.fao.geonet.kernel.search.MetaSearcher;
import org.fao.geonet.kernel.search.SearchManager;
import org.jdom.Element;

import java.util.List;
Expand Down Expand Up @@ -62,29 +64,68 @@ public Element exec(Element params, ServiceContext context) throws Exception {

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)) {
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;
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.getSearchmanager();
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);
} else {
try {
scm.deletePluginSchema(schema);
response.setAttribute("status", "ok");
response.setAttribute("message", "Schema "+schema+" deleted");
} catch (Exception e) {
e.printStackTrace();
response.setAttribute("status", "error");
response.setAttribute("message", "Could not delete schema, error if any was "+e.getMessage());
}
return response;
}
} else {
} 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", "Schema does not exist");
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");
response.setAttribute("message", "Could not delete schema, error if any was "+e.getMessage());
}
return response;
}
Expand Down

0 comments on commit 9383aaa

Please sign in to comment.