Ignore:
Timestamp:
09/13/11 09:07:39 (14 years ago)
Author:
lgiessmann
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/isidorus/CommitCallback.java

    r881 r882  
    1212import com.google.gwt.http.client.Response;
    1313import com.google.gwt.http.client.URL;
    14 import com.google.gwt.json.client.JSONArray;
    15 import com.google.gwt.json.client.JSONObject;
    16 import com.google.gwt.json.client.JSONParser;
    17 import com.google.gwt.json.client.JSONString;
    18 import com.google.gwt.json.client.JSONValue;
    1914import com.google.gwt.user.client.Window;
    2015import us.isidor.gdl.anaToMia.TopicMaps.TmEngineModel.ExporterException;
     
    161156                        Name env = assoc.getRoles(TmHelper.getTopicByPsi("http://textgrid.org/serviceregistry/model/environment", tm)).get(0).getPlayer().getNames().get(0);
    162157                        Occurrence key = top.getOccurrences(TmHelper.getTopicByPsi("http://textgrid.org/serviceregistry/model/key", tm)).get(0);
    163                         Locator psi = tm.createLocator("http://textgrid.org/serviceregistry/hash-object/" + env.getValue() + "/" + key.getValue());
     158                        String psiValue = "http://textgrid.org/serviceregistry/hash-object/" + env.getValue() + "/" + key.getValue();
     159                        Locator psi = tm.createLocator(psiValue);
    164160                        this.removeSubjectIdentifiers(top);
    165161                        top.addSubjectIdentifier(psi);
     
    172168                        }
    173169                       
    174                         String commitUrl = URL.encode(this.COMMIT_REQUEST_URL);
    175                         String getUrl = URL.encode(this.GET_REQUEST_URL);
    176                         RequestBuilder commitBuilder = new RequestBuilder(RequestBuilder.POST, commitUrl);
    177                         commitBuilder.setHeader("Content-type", "application/json");
     170                        String getUrl = URL.encode(this.GET_REQUEST_URL + psiValue);
    178171                        RequestBuilder getBuilder = new RequestBuilder(RequestBuilder.GET, getUrl);
    179                         try{                           
    180                                 if(edited)getBuilder.sendRequest(null, new EditedHashObjectCommitRequest(null, jtm, false));
    181                                 else commitBuilder.sendRequest(jtm, new HashObjectCommitRequest(jtm));
     172                        try{
     173                                getBuilder.sendRequest(null, new HashObjectCheckRequest(jtm, psiValue, edited));
    182174                        }catch(RequestException e){
    183175                                Window.alert("could not commit the topic: " + jtm + ", because(" + e.getClass() + "): " + e.getMessage());
     
    218210
    219211       
     212        private class HashObjectCheckRequest implements RequestCallback {
     213                private String objectToBeSend = null;
     214                private String psi = null;
     215                private boolean edited = false;
     216               
     217               
     218                public HashObjectCheckRequest(String objectToBeSend, String psi, boolean edited){
     219                        this.objectToBeSend = objectToBeSend;
     220                        this.psi = psi;
     221                        this.edited = edited;
     222                }
     223               
     224
     225                @Override
     226                public void onResponseReceived(Request request, Response response) {
     227                        if (200 == response.getStatusCode()) {
     228                                if(edited){
     229                                        //TODO: implement
     230                                } else {
     231                                        Window.alert("could not commit the topic with the psi " + psi + " since it already exists");
     232                                        //TODO: reset page
     233                                }
     234                        } else if(404 == response.getStatusCode() && !edited) {
     235                                try{
     236                                        String commitUrl = URL.encode(CommitCallback.this.COMMIT_REQUEST_URL);
     237                                        RequestBuilder commitBuilder = new RequestBuilder(RequestBuilder.POST, commitUrl);
     238                                        commitBuilder.setHeader("Content-type", "application/json");
     239                                        commitBuilder.sendRequest(this.objectToBeSend, new HashObjectCommitRequest(this.objectToBeSend));
     240                                }catch(RequestException e){
     241                                        Window.alert("could not commit the topic: " + this.objectToBeSend + ", because(" + e.getClass() + "): " + e.getMessage());
     242                                        // TODO: reset page
     243                                }
     244                        } else {
     245                                Window.alert("could not commit the topic with the psi " + psi + ", because: " + response.getStatusCode() + ": " + response.getStatusText());
     246                                // TODO: reset page
     247                        }
     248                }
     249               
     250
     251                @Override
     252                public void onError(Request request, Throwable exception) {
     253                        Window.alert("could not commit the data: " + this.objectToBeSend);
     254                        // TODO: reset page
     255                }
     256        }
     257       
     258       
    220259        private class HashObjectCommitRequest implements RequestCallback {
    221260                private String objectToBeSend = null;
     
    228267                public HashObjectCommitRequest(String objectToBeCommitted){
    229268                        this.objectToBeSend = objectToBeCommitted;
    230                         Window.alert("test"); //TODO: remove
    231269                }
    232270               
     
    246284                public void onError(Request request, Throwable exception) {
    247285                        Window.alert("could not commit the data: " + this.objectToBeSend);
    248                 }
    249         }
    250        
    251        
    252         private class EditedHashObjectCommitRequest implements RequestCallback{
    253                 private JSONValue requestedData = null;
    254                 private String objectToBeSend = null;
    255                 private boolean deletedOldOccurrence = false;
    256                 private final String deleteUrl = URL.encode(CommitCallback.this.DELETE_REUQEST_URL);
    257                 private final String commitUrl = URL.encode(CommitCallback.this.COMMIT_REQUEST_URL);
    258                
    259                
    260                 @SuppressWarnings("unused")
    261                 private EditedHashObjectCommitRequest(){}
    262                
    263                
    264                 public EditedHashObjectCommitRequest(JSONValue requestedData, String objectToBeSend, boolean deletedOldOccurrence){
    265                         this.requestedData = requestedData;
    266                         this.objectToBeSend = objectToBeSend;
    267                         this.deletedOldOccurrence = deletedOldOccurrence;
    268                 }
    269                
    270                
    271                 @Override
    272                 public void onResponseReceived(Request request, Response response) {
    273                         if (200 == response.getStatusCode()) {
    274                                 if(this.requestedData == null){
    275                                         JSONObject occurrenceToBeDeleted = this.getOccurrenceTobeDeleted(response.getText());
    276                                         if(occurrenceToBeDeleted != null){
    277                                                 RequestBuilder deleteBuilder = new RequestBuilder(RequestBuilder.DELETE, this.deleteUrl);
    278                                                 deleteBuilder.setHeader("Content-type", "application/json");
    279                                                 try{
    280                                                         deleteBuilder.sendRequest(occurrenceToBeDeleted.toString(), new EditedHashObjectCommitRequest(occurrenceToBeDeleted, this.objectToBeSend, true));
    281                                                 }catch(RequestException e){
    282                                                         Window.alert("could not commit the topic: " + this.objectToBeSend + ", because(" + e.getClass() + "): " + e.getMessage());
    283                                                 }
    284                                         } else {
    285                                                 Window.alert("commit operation of the object " + this.objectToBeSend + " failed, because: the occurrence http://textgrid.org/serviceregistry/model/hash-type could not be updated");
    286                                         }                               
    287                                 } else if(deletedOldOccurrence){
    288                                         RequestBuilder commitBuilder = new RequestBuilder(RequestBuilder.POST, this.commitUrl);
    289                                         commitBuilder.setHeader("Content-type", "application/json");
    290                                         try{
    291                                                 commitBuilder.sendRequest(this.objectToBeSend, new HashObjectCommitRequest(this.objectToBeSend));
    292                                         }catch(RequestException e){
    293                                                 Window.alert("could not commit the topic: " + this.objectToBeSend + ", because(" + e.getClass() + "): " + e.getMessage());
    294                                         }
    295                                 } else {
    296                                         Window.alert("commit operation of the object " + this.objectToBeSend + " succeeded");
    297                                         // TODO: reset the web page
    298                                 }
    299                         } else {
    300                                 Window.alert("commit operation of the object " + this.objectToBeSend + " failed: " + response.getStatusCode() + "(" + response.getStatusText() + ")\n" + response.getText());
    301                         }
    302                 }
    303 
    304                
    305                 @Override
    306                 public void onError(Request request, Throwable exception) {
    307                         Window.alert("could not commit the data: " + this.objectToBeSend);
    308                 }
    309        
    310        
    311                 private JSONObject getOccurrenceTobeDeleted(String json){
    312                         if(json == null) return null;
    313                        
    314                         JSONValue jsonValue = JSONParser.parseStrict(json);
    315                         JSONObject fragment = jsonValue.isObject();
    316                         JSONObject topic = null;
    317                         if(fragment != null){
    318                                 JSONValue val = fragment.get("topic");
    319                                 if(val != null) topic = val.isObject();
    320                         }
    321                         if(topic != null){
    322                                 JSONValue occs = topic.get("occurrences");
    323                                 JSONArray occsArray = null;
    324                                 if(occs != null) occsArray = occs.isArray();
    325                                 if(occsArray != null) {
    326                                         for(int i = 0; i != occsArray.size(); ++i){
    327                                                 JSONValue val = occsArray.get(i);
    328                                                 JSONObject occ = val.isObject();
    329                                                 if(occ == null) continue;
    330                                                
    331                                                 JSONValue type = occ.get("type");
    332                                                 JSONString typeStr = type.isString();
    333                                                 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
    334                                         }
    335                                 }
    336                         }
    337                        
    338                         return null;
     286                        // TODO: reset page
    339287                }
    340288        }
Note: See TracChangeset for help on using the changeset viewer.