Author: lgiessmann Date: Tue Sep 13 02:07:39 2011 New Revision: 882
Log: gdl-frontend: Widgets: added a check for existing instances of http://textgrid.org/serviceregistry/model/types/Hash-Object instances, so a commit will succeed only of there is no duplicate topic existent
Modified: branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/isidorus/CommitCallback.java
Modified: branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/isidorus/CommitCallback.java ============================================================================== --- branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/isidorus/CommitCallback.java Tue Sep 13 01:35:44 2011 (r881) +++ branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/isidorus/CommitCallback.java Tue Sep 13 02:07:39 2011 (r882) @@ -11,11 +11,6 @@ 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.Window; import us.isidor.gdl.anaToMia.TopicMaps.TmEngineModel.ExporterException; import us.isidor.gdl.anaToMia.TopicMaps.TmEngineModel.TmEngine; @@ -160,7 +155,8 @@ Name env = assoc.getRoles(TmHelper.getTopicByPsi("http://textgrid.org/serviceregistry/model/environment", tm)).get(0).getPlayer().getNames().get(0); Occurrence key = top.getOccurrences(TmHelper.getTopicByPsi("http://textgrid.org/serviceregistry/model/key", tm)).get(0); - Locator psi = tm.createLocator("http://textgrid.org/serviceregistry/hash-object/" + env.getValue() + "/" + key.getValue()); + String psiValue = "http://textgrid.org/serviceregistry/hash-object/" + env.getValue() + "/" + key.getValue(); + Locator psi = tm.createLocator(psiValue); this.removeSubjectIdentifiers(top); top.addSubjectIdentifier(psi); @@ -171,14 +167,10 @@ jtm = tmEngine.exportTm(topics, associations); } - String commitUrl = URL.encode(this.COMMIT_REQUEST_URL); - String getUrl = URL.encode(this.GET_REQUEST_URL); - RequestBuilder commitBuilder = new RequestBuilder(RequestBuilder.POST, commitUrl); - commitBuilder.setHeader("Content-type", "application/json"); + String getUrl = URL.encode(this.GET_REQUEST_URL + psiValue); RequestBuilder getBuilder = new RequestBuilder(RequestBuilder.GET, getUrl); - try{ - if(edited)getBuilder.sendRequest(null, new EditedHashObjectCommitRequest(null, jtm, false)); - else commitBuilder.sendRequest(jtm, new HashObjectCommitRequest(jtm)); + try{ + getBuilder.sendRequest(null, new HashObjectCheckRequest(jtm, psiValue, edited)); }catch(RequestException e){ Window.alert("could not commit the topic: " + jtm + ", because(" + e.getClass() + "): " + e.getMessage()); } @@ -217,27 +209,41 @@ }
- private class HashObjectCommitRequest implements RequestCallback { + private class HashObjectCheckRequest implements RequestCallback { private String objectToBeSend = null; + private String psi = null; + private boolean edited = false; - @SuppressWarnings("unused") - private HashObjectCommitRequest(){} - - - public HashObjectCommitRequest(String objectToBeCommitted){ - this.objectToBeSend = objectToBeCommitted; - Window.alert("test"); //TODO: remove + public HashObjectCheckRequest(String objectToBeSend, String psi, boolean edited){ + this.objectToBeSend = objectToBeSend; + this.psi = psi; + this.edited = edited; } - + @Override public void onResponseReceived(Request request, Response response) { if (200 == response.getStatusCode()) { - Window.alert("commit operation of the object " + this.objectToBeSend + " succeeded"); - // TODO: reset the web page + if(edited){ + //TODO: implement + } else { + Window.alert("could not commit the topic with the psi " + psi + " since it already exists"); + //TODO: reset page + } + } else if(404 == response.getStatusCode() && !edited) { + try{ + String commitUrl = URL.encode(CommitCallback.this.COMMIT_REQUEST_URL); + RequestBuilder commitBuilder = new RequestBuilder(RequestBuilder.POST, commitUrl); + commitBuilder.setHeader("Content-type", "application/json"); + commitBuilder.sendRequest(this.objectToBeSend, new HashObjectCommitRequest(this.objectToBeSend)); + }catch(RequestException e){ + Window.alert("could not commit the topic: " + this.objectToBeSend + ", because(" + e.getClass() + "): " + e.getMessage()); + // TODO: reset page + } } else { - Window.alert("commit operation of the object " + this.objectToBeSend + " failed: " + response.getStatusCode() + "(" + response.getStatusText() + ")\n" + response.getText()); + Window.alert("could not commit the topic with the psi " + psi + ", because: " + response.getStatusCode() + ": " + response.getStatusText()); + // TODO: reset page } } @@ -245,97 +251,39 @@ @Override public void onError(Request request, Throwable exception) { Window.alert("could not commit the data: " + this.objectToBeSend); + // TODO: reset page } } - private class EditedHashObjectCommitRequest implements RequestCallback{ - private JSONValue requestedData = null; + private class HashObjectCommitRequest implements RequestCallback { private String objectToBeSend = null; - private boolean deletedOldOccurrence = false; - private final String deleteUrl = URL.encode(CommitCallback.this.DELETE_REUQEST_URL); - private final String commitUrl = URL.encode(CommitCallback.this.COMMIT_REQUEST_URL); @SuppressWarnings("unused") - private EditedHashObjectCommitRequest(){} + private HashObjectCommitRequest(){} - public EditedHashObjectCommitRequest(JSONValue requestedData, String objectToBeSend, boolean deletedOldOccurrence){ - this.requestedData = requestedData; - this.objectToBeSend = objectToBeSend; - this.deletedOldOccurrence = deletedOldOccurrence; + public HashObjectCommitRequest(String objectToBeCommitted){ + this.objectToBeSend = objectToBeCommitted; } @Override public void onResponseReceived(Request request, Response response) { if (200 == response.getStatusCode()) { - if(this.requestedData == null){ - JSONObject occurrenceToBeDeleted = this.getOccurrenceTobeDeleted(response.getText()); - if(occurrenceToBeDeleted != null){ - RequestBuilder deleteBuilder = new RequestBuilder(RequestBuilder.DELETE, this.deleteUrl); - deleteBuilder.setHeader("Content-type", "application/json"); - try{ - deleteBuilder.sendRequest(occurrenceToBeDeleted.toString(), new EditedHashObjectCommitRequest(occurrenceToBeDeleted, this.objectToBeSend, true)); - }catch(RequestException e){ - Window.alert("could not commit the topic: " + this.objectToBeSend + ", because(" + e.getClass() + "): " + e.getMessage()); - } - } else { - Window.alert("commit operation of the object " + this.objectToBeSend + " failed, because: the occurrence http://textgrid.org/serviceregistry/model/hash-type could not be updated"); - } - } else if(deletedOldOccurrence){ - RequestBuilder commitBuilder = new RequestBuilder(RequestBuilder.POST, this.commitUrl); - commitBuilder.setHeader("Content-type", "application/json"); - try{ - commitBuilder.sendRequest(this.objectToBeSend, new HashObjectCommitRequest(this.objectToBeSend)); - }catch(RequestException e){ - Window.alert("could not commit the topic: " + this.objectToBeSend + ", because(" + e.getClass() + "): " + e.getMessage()); - } - } else { - Window.alert("commit operation of the object " + this.objectToBeSend + " succeeded"); - // TODO: reset the web page - } + Window.alert("commit operation of the object " + this.objectToBeSend + " succeeded"); + // TODO: reset the web page } else { Window.alert("commit 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 commit the data: " + this.objectToBeSend); - } - - - private JSONObject getOccurrenceTobeDeleted(String json){ - if(json == null) return null; - - JSONValue jsonValue = JSONParser.parseStrict(json); - JSONObject fragment = jsonValue.isObject(); - JSONObject topic = null; - if(fragment != null){ - JSONValue val = fragment.get("topic"); - if(val != null) topic = val.isObject(); - } - if(topic != null){ - JSONValue occs = topic.get("occurrences"); - JSONArray occsArray = null; - if(occs != null) occsArray = occs.isArray(); - if(occsArray != null) { - for(int i = 0; i != occsArray.size(); ++i){ - JSONValue val = occsArray.get(i); - JSONObject occ = val.isObject(); - if(occ == null) continue; - - JSONValue type = occ.get("type"); - JSONString typeStr = type.isString(); - if(typeStr.stringValue().endsWith("hash-type")) return occ; // fr our purpose this soft check is sufficient enough, so the prefix need not to be varified - } - } - } - - return null; + // TODO: reset page } } }