Author: lgiessmann Date: Wed Sep 21 06:02:11 2011 New Revision: 955
Log: gdl-frontend: Widgets: finalised the implementation of the delete-operation of environment topics
Modified: branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/isidorus/DeleteCallback.java branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/isidorus/HashObjectPsiContainer.java
Modified: branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/isidorus/DeleteCallback.java ============================================================================== --- branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/isidorus/DeleteCallback.java Wed Sep 21 04:20:42 2011 (r954) +++ branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/isidorus/DeleteCallback.java Wed Sep 21 06:02:11 2011 (r955) @@ -23,6 +23,7 @@ import us.isidor.gdl.anaToMia.TopicMaps.TopicMapsModel.TopicMapsTypes; import us.isidor.gdl.anaToMia.Widgets.base.TmHelper; import us.isidor.gdl.anaToMia.Widgets.base.Utils; +import us.isidor.gdl.anaToMia.Widgets.environment.ExecutionException; import us.isidor.gdl.anaToMia.Widgets.environment.IDeleteCallback; import us.isidor.gdl.anaToMia.Widgets.environment.Pair;
@@ -38,7 +39,7 @@ "?topics model:environment env:"; private final String TM_SPARQL_QUERY_PART_2 = ".\n}"; private GdlWebPage gdlWebPage = null; - private HashObjectPsiContainer hashObjectPsiContainer = new HashObjectPsiContainer(); + private HashObjectPsiContainer hashObjectPsiContainer = null; @SuppressWarnings("unused") @@ -83,13 +84,23 @@ private void deleteEnvironmentTopic(Topic env){ String url = URL.encode(this.TM_SPARQL_URL); RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url); - try{ - DeleteCallback.this.gdlWebPage.createLoadScreenPanel("Wating for Data", "Requesting all PSIs of instances of " + IsidorusConstants.HASH_OBJECT_PSI + " that are associated with " + env.getSubjectIdentifiers().get(0).getReference() + " from " + url); - builder.sendRequest(this.createTmSparqlQuery(env), new GetHashObjectPsisRequest()); - }catch(RequestException e){ - e.printStackTrace(); - Window.alert("could not request existing instances of " + IsidorusConstants.HASH_OBJECT_PSI + ", because(" + e.getClass() + "): " + e.getMessage()); + if(env.getSubjectIdentifiers().length() == 0){ + Window.alert("illegal environment topic detected, because: the topic " + env + " has no subject identifier"); DeleteCallback.this.gdlWebPage.resetPage(); + } else { + try{ + this.hashObjectPsiContainer = new HashObjectPsiContainer(env); + DeleteCallback.this.gdlWebPage.createLoadScreenPanel("Wating for Data", "Requesting all PSIs of instances of " + IsidorusConstants.HASH_OBJECT_PSI + " that are associated with " + env.getSubjectIdentifiers().get(0).getReference() + " from " + url); + builder.sendRequest(this.createTmSparqlQuery(env), new GetHashObjectPsisRequest()); + }catch(RequestException e){ + e.printStackTrace(); + Window.alert("could not delete the topic " + TmHelper.getAnyIdOfTopic(env) + ", because(" + e.getClass() + "): " + e.getMessage()); + DeleteCallback.this.gdlWebPage.resetPage(); + } catch(ExecutionException e){ + e.printStackTrace(); + Window.alert("could not delete the topic " + TmHelper.getAnyIdOfTopic(env) + ", because(" + e.getClass() + "): " + e.getMessage()); + DeleteCallback.this.gdlWebPage.resetPage(); + } } }
Modified: branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/isidorus/HashObjectPsiContainer.java ============================================================================== --- branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/isidorus/HashObjectPsiContainer.java Wed Sep 21 04:20:42 2011 (r954) +++ branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/isidorus/HashObjectPsiContainer.java Wed Sep 21 06:02:11 2011 (r955) @@ -1,11 +1,24 @@ package us.isidor.gdl.anaToMia.Widgets.isidorus;
- import java.util.ArrayList; - +import us.isidor.gdl.anaToMia.TopicMaps.TopicMapsModel.Topic; +import us.isidor.gdl.anaToMia.TopicMaps.TopicMapsModel.TopicMap; +import us.isidor.gdl.anaToMia.Widgets.base.Utils; +import us.isidor.gdl.anaToMia.Widgets.environment.ExecutionException; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; +import com.google.gwt.http.client.Request; +import com.google.gwt.http.client.RequestBuilder; +import com.google.gwt.http.client.RequestCallback; +import com.google.gwt.http.client.RequestException; +import com.google.gwt.http.client.Response; +import com.google.gwt.http.client.URL; +import com.google.gwt.json.client.JSONArray; +import com.google.gwt.json.client.JSONObject; +import com.google.gwt.json.client.JSONParser; +import com.google.gwt.json.client.JSONString; +import com.google.gwt.json.client.JSONValue; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Button; @@ -22,20 +35,24 @@ private Button commitButton = new Button("delete the Environment topic and all its Hash-Object topics"); private final String ERROR_STYLE_CLASS = "bad_input"; private CheckBoxClickHandler cbxClickHandler = new CheckBoxClickHandler(); + private final String DELETE_URL = IsidorusConstants.DELETE_REQUEST_URL; + private Topic environmentTopic = null; - public HashObjectPsiContainer(){ + public HashObjectPsiContainer(Topic environmentTopic) throws ExecutionException { + if(environmentTopic == null) throw new ExecutionException("environmentTopic must not be null"); + this.environmentTopic = environmentTopic; this.initWidget(this.containerPanel); this.containerPanel.add(this.commitButton); - this.commitButton.addClickHandler(new HashObjectPsiContainerClickHandler()); + this.commitButton.addClickHandler(new HashObjectPsiContainerClickHandler(this.environmentTopic)); DOM.setStyleAttribute(this.commitButton.getElement(), "width", "100%"); DOM.setStyleAttribute(this.commitButton.getElement(), "marginTop", "1em"); } - public void addHashObjectPsi(String psi){ + public void addHashObjectPsi(String psi) { if(psi == null) return; - + this.commitButton.removeFromParent(); CheckBox cbx = new CheckBox(psi); this.psiCheckBoxes.add(cbx); @@ -47,7 +64,7 @@ this.containerPanel.insert(cbx, widgetIdx); break; } - } + } cbx.addClickHandler(this.cbxClickHandler); @@ -69,7 +86,12 @@ private class HashObjectPsiContainerClickHandler implements ClickHandler { - public HashObjectPsiContainerClickHandler(){} + private Topic environmentTopic = null; + + + public HashObjectPsiContainerClickHandler(Topic environmentTopic){ + this.environmentTopic = environmentTopic; + } @Override @@ -85,9 +107,131 @@ if(!allValuesChecked){ Window.alert("to delete the an environment topic, please select explicit all associated hash-object topics to be deleted!"); } else { - // TODO: delete all hash-objects - // TODO: delete the environment + DeleteAllHashObjectsRequest reqCallback = new DeleteAllHashObjectsRequest(HashObjectPsiContainer.this.psiCheckBoxes, this.environmentTopic); + for (CheckBox cbx : HashObjectPsiContainer.this.psiCheckBoxes) HashObjectPsiContainer.this.deleteHashObject(cbx.getText(), reqCallback); } } } + + + private void deleteHashObject(String psi, DeleteAllHashObjectsRequest reqCallback){ + if(psi != null || reqCallback != null){ + JSONObject delObj = this.createObjectToBeDelete(psi); + try{ + String url = URL.encode(this.DELETE_URL); + RequestBuilder builder = new RequestBuilder(RequestBuilder.DELETE, url); + builder.setHeader("Content-type", "application/json"); + + builder.sendRequest(delObj.toString(), reqCallback); + }catch(RequestException e){ + Window.alert("could not delete the topic: " + psi + ", because(" + e.getClass() + "): " + e.getMessage()); + } + } + } + + + private JSONObject createObjectToBeDelete(String psi){ + JSONArray psis = new JSONArray(); + psis.set(0, new JSONString(psi)); + + JSONObject delObj = new JSONObject(); + delObj.put("type", new JSONString("Topic")); + + String[] pref = Utils.splitUriByLastFragment(psi); + JSONValue val = JSONParser.parseStrict("{"version":"1.1","prefixes":{"pref":" + pref[0] + "},"subject_identifiers":["[pref:" + pref[1] + "]"]}"); + delObj.put("delete", val.isObject()); + + return delObj; + } + + + private class DeleteAllHashObjectsRequest implements RequestCallback { + private ArrayList<String> hashObjectsToDelete = new ArrayList<String>(); + private int objectsDeleted = 0; + private Topic environmentTopic = null; + private String environmentPsi = null; + + + @SuppressWarnings("unused") + private DeleteAllHashObjectsRequest(){} + + + public DeleteAllHashObjectsRequest(ArrayList<CheckBox> hashObjectToDelete, Topic environmentTopic){ + if(hashObjectToDelete != null) for (CheckBox cbx : hashObjectToDelete) this.hashObjectsToDelete.add(cbx.getText()); + this.environmentTopic = environmentTopic; + this.environmentPsi = this.environmentTopic.getSubjectIdentifiers().get(0).getReference(); // it's ensured that the passed topic has at least one psi before + } + + + @Override + public void onResponseReceived(Request request, Response response) { + if (200 == response.getStatusCode()) { + ++this.objectsDeleted; + if(this.objectsDeleted == this.hashObjectsToDelete.size()){ + TopicMap tm = environmentTopic.getTopicMap(); + for (String psi : this.hashObjectsToDelete) { + Topic top = tm.getTopicBySubjectIdentifier(tm.createLocator(psi)); + top.remove(); + } + + JSONObject delObj = HashObjectPsiContainer.this.createObjectToBeDelete(this.environmentPsi); + String url = URL.encode(HashObjectPsiContainer.this.DELETE_URL); + RequestBuilder builder = new RequestBuilder(RequestBuilder.DELETE, url); + builder.setHeader("Content-type", "application/json"); + try{ + builder.sendRequest(delObj.toString(), new EnvironemntDeleteRequest(delObj, this.environmentTopic)); + }catch(RequestException e){ + Window.alert("delete operation of the object " + delObj + " failed, because: (" + e.getClass() + ")\n" + e.getMessage()); + } + } + } else { + Window.alert("could not delete all topics of " + Utils.arrayToString(this.hashObjectsToDelete) + ", because(" + response.getStatusCode() + "): " + response.getStatusText()); + } + } + + + @Override + public void onError(Request request, Throwable exception) { + String message = null; + Class<? extends Throwable> eClass = null; + if(exception != null){ + message = exception.getMessage(); + eClass = exception.getClass(); + } + Window.alert("could not delete all topics of " + Utils.arrayToString(this.hashObjectsToDelete) + ", because(" + eClass + "): " + message); + } + } + + + private class EnvironemntDeleteRequest implements RequestCallback { + private JSONObject objectToBeSend = null; + private Topic topicToBeDeleted = null; + + + @SuppressWarnings("unused") + private EnvironemntDeleteRequest(){} + + + public EnvironemntDeleteRequest(JSONObject objectToBeDeleted, Topic topicToBeDeleted){ + this.objectToBeSend = objectToBeDeleted; + this.topicToBeDeleted = topicToBeDeleted; + } + + + @Override + public void onResponseReceived(Request request, Response response) { + if (200 == response.getStatusCode()) { + Window.Location.reload(); + this.topicToBeDeleted.remove(); + } else { + Window.alert("delete operation of the object " + this.objectToBeSend + " failed: " + response.getStatusCode() + "(" + response.getStatusText() + ")\n" + response.getText()); + } + } + + + @Override + public void onError(Request request, Throwable exception) { + Window.alert("could not delete the data: " + this.objectToBeSend); + } + } }