@Override
  public void onReferencesRequired(final Long dbId) {
    String url = "/ReactomeRESTfulAPI/RESTfulWS/queryReferences/" + dbId;
    RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, url);
    requestBuilder.setHeader("Accept", "application/json");
    try {
      requestBuilder.sendRequest(
          null,
          new RequestCallback() {
            @Override
            public void onResponseReceived(Request request, Response response) {
              try {
                JSONArray list = JSONParser.parseStrict(response.getText()).isArray();
                List<LiteratureReference> references = new ArrayList<LiteratureReference>();
                for (int i = 0; i < list.size(); ++i) {
                  JSONObject object = list.get(i).isObject();
                  references.add((LiteratureReference) ModelFactory.getDatabaseObject(object));
                }
                DataRequiredListener.getDataRequiredListener()
                    .setRequiredReferences(dbId, references);
              } catch (Exception ex) {
                // ModelFactoryException, NullPointerException, IllegalArgumentException,
                // JSONException
                MessageObject msgObj =
                    new MessageObject(
                        "The received References object for 'DbId="
                            + dbId
                            + "' is empty or faulty and could not be parsed.\n"
                            + "ERROR: "
                            + ex.getMessage(),
                        getClass(),
                        MessageType.INTERNAL_ERROR);
                eventBus.fireELVEvent(ELVEventType.INTERNAL_MESSAGE, msgObj);
                Console.error(getClass() + " ERROR: " + ex.getMessage());
                DataRequiredListener.getDataRequiredListener()
                    .setRequiredReferences(dbId, new ArrayList<LiteratureReference>());
              }
            }

            @Override
            public void onError(Request request, Throwable exception) {
              if (!GWT.isScript()) {
                Console.error(getClass() + " ERROR: " + exception.getMessage());
              }

              MessageObject msgObj =
                  new MessageObject(
                      "The request for 'DbId="
                          + dbId
                          + "' received an error instead of a valid response.\n"
                          + "ERROR: "
                          + exception.getMessage(),
                      getClass(),
                      MessageType.INTERNAL_ERROR);
              eventBus.fireELVEvent(ELVEventType.INTERNAL_MESSAGE, msgObj);
              DataRequiredListener.getDataRequiredListener()
                  .setRequiredReferences(dbId, new ArrayList<LiteratureReference>());
            }
          });
    } catch (RequestException ex) {
      MessageObject msgObj =
          new MessageObject(
              "The requested references data for 'DbId="
                  + dbId
                  + "' could not be received.\n"
                  + "ERROR: "
                  + ex.getMessage(),
              getClass(),
              MessageType.INTERNAL_ERROR);
      eventBus.fireELVEvent(ELVEventType.INTERNAL_MESSAGE, msgObj);
      Console.error(getClass() + " ERROR: " + ex.getMessage());
      DataRequiredListener.getDataRequiredListener()
          .setRequiredReferences(dbId, new ArrayList<LiteratureReference>());
    }
  }
  @Override
  public void onMoleculeDataRequired(final Molecule molecule) {
    String url = "/ReactomeRESTfulAPI/RESTfulWS/queryById/ReferenceEntity/" + molecule.getDbId();
    RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, url);
    requestBuilder.setHeader("Accept", "application/json");
    try {
      requestBuilder.sendRequest(
          null,
          new RequestCallback() {
            @Override
            public void onResponseReceived(Request request, Response response) {
              try {
                JSONObject json = JSONParser.parseStrict(response.getText()).isObject();
                Molecule molecule =
                    new Molecule(ModelFactory.getDatabaseObject(json).getSchemaClass(), json);

                DataRequiredListener.getDataRequiredListener().setRequiredMoleculeData(molecule);
              } catch (Exception ex) {
                // ModelFactoryException, NullPointerException, IllegalArgumentException,
                // JSONException
                MessageObject msgObj =
                    new MessageObject(
                        "The received object for Molecule '"
                            + molecule.getDisplayName()
                            + "' is empty or faulty and could not be parsed.\n"
                            + "ERROR: "
                            + ex.getMessage(),
                        getClass(),
                        MessageType.INTERNAL_ERROR);
                eventBus.fireELVEvent(ELVEventType.INTERNAL_MESSAGE, msgObj);
                Console.error(getClass() + " ERROR: " + ex.getMessage());
                DataRequiredListener.getDataRequiredListener().setRequiredMoleculeData(molecule);
              }
            }

            @Override
            public void onError(Request request, Throwable exception) {
              if (!GWT.isScript()) {
                Console.error(getClass() + " ERROR: " + exception.getMessage());
              }

              MessageObject msgObj =
                  new MessageObject(
                      "The request for Molecule '"
                          + molecule.getDisplayName()
                          + "' received an error instead of a valid response.\n"
                          + "ERROR: "
                          + exception.getMessage(),
                      getClass(),
                      MessageType.INTERNAL_ERROR);
              eventBus.fireELVEvent(ELVEventType.INTERNAL_MESSAGE, msgObj);
              DataRequiredListener.getDataRequiredListener().setRequiredMoleculeData(molecule);
            }
          });
    } catch (RequestException ex) {
      MessageObject msgObj =
          new MessageObject(
              "The requested detailed data for Molecule \n'"
                  + molecule.getDisplayName()
                  + "' could not be received.\n"
                  + "ERROR: "
                  + ex.getMessage(),
              getClass(),
              MessageType.INTERNAL_ERROR);
      eventBus.fireELVEvent(ELVEventType.INTERNAL_MESSAGE, msgObj);
      Console.error(getClass() + " ERROR: " + ex.getMessage());
      DataRequiredListener.getDataRequiredListener().setRequiredMoleculeData(molecule);
    }
  }
  @Override
  public void onDataRequired(final DatabaseObject databaseObject) {
    final long dbId = databaseObject.getDbId();
    String url = "/ReactomeRESTfulAPI/RESTfulWS/detailedView/DatabaseObject/" + dbId;
    RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, url);
    requestBuilder.setHeader("Accept", "application/json");
    try {
      requestBuilder.sendRequest(
          null,
          new RequestCallback() {
            @Override
            public void onResponseReceived(Request request, Response response) {
              try {
                JSONObject json = JSONParser.parseStrict(response.getText()).isObject();
                DatabaseObject databaseObject = ModelFactory.getDatabaseObject(json);
                DataRequiredListener.getDataRequiredListener()
                    .setRequiredData(dbId, databaseObject);
                eventBus.fireELVEvent(ELVEventType.DETAILED_VIEW_LOADED, databaseObject);
              } catch (Exception ex) {
                // ModelFactoryException, NullPointerException, IllegalArgumentException,
                // JSONException
                MessageObject msgObj =
                    new MessageObject(
                        "The received object for 'DbId="
                            + databaseObject.getDbId()
                            + "' is empty or faulty and could not be parsed.\n"
                            + "ERROR: "
                            + ex.getMessage(),
                        getClass(),
                        MessageType.INTERNAL_ERROR);
                eventBus.fireELVEvent(ELVEventType.INTERNAL_MESSAGE, msgObj);
                Console.error(getClass() + " ERROR: " + ex.getMessage());
              }
            }

            @Override
            public void onError(Request request, Throwable exception) {
              if (!GWT.isScript()) {
                Console.error(getClass() + " ERROR: " + exception.getMessage());
              }

              MessageObject msgObj =
                  new MessageObject(
                      "The request for 'DbId="
                          + databaseObject.getDbId()
                          + "' received an error instead of a valid response.\n"
                          + "ERROR: "
                          + exception.getMessage(),
                      getClass(),
                      MessageType.INTERNAL_ERROR);
              eventBus.fireELVEvent(ELVEventType.INTERNAL_MESSAGE, msgObj);
            }
          });
    } catch (RequestException ex) {
      MessageObject msgObj =
          new MessageObject(
              "The requested data for 'DbId="
                  + databaseObject.getDbId()
                  + "' could not be received.\n"
                  + "ERROR: "
                  + ex.getMessage(),
              getClass(),
              MessageType.INTERNAL_ERROR);
      eventBus.fireELVEvent(ELVEventType.INTERNAL_MESSAGE, msgObj);
      Console.error(getClass() + " ERROR: " + ex.getMessage());
    }
  }
  @Override
  public void onPathRequired(final Event event) {
    /* TODO Testing
    but never used because DataRequiredListener is used by DetailsPanel and there ancestorsRequired is unused */
    final long dbId = event.getDbId();
    String url = "/ReactomeRESTfulAPI/RESTfulWS/queryEventAncestors/" + dbId;
    RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, url);
    requestBuilder.setHeader("Accept", "application/json");
    try {
      requestBuilder.sendRequest(
          null,
          new RequestCallback() {
            @Override
            public void onResponseReceived(Request request, Response response) {
              try {
                JSONArray list = JSONParser.parseStrict(response.getText()).isArray();
                Ancestors ancestors = new Ancestors(list);
                DataRequiredListener.getDataRequiredListener()
                    .setRequiredAncestors(dbId, ancestors);
              } catch (Exception ex) {
                // ModelFactoryException, NullPointerException, IllegalArgumentException,
                // JSONException
                MessageObject msgObj =
                    new MessageObject(
                        "The received object for 'DbId="
                            + event.getDbId()
                            + "' is empty or faulty and could not be parsed into a path or hierarchy.\n"
                            + "ERROR: "
                            + ex.getMessage(),
                        getClass(),
                        MessageType.INTERNAL_ERROR);
                eventBus.fireELVEvent(ELVEventType.INTERNAL_MESSAGE, msgObj);
                Console.error(getClass() + " ERROR: " + ex.getMessage());
              }
            }

            @Override
            public void onError(Request request, Throwable exception) {
              if (!GWT.isScript()) {
                Console.error(getClass() + " ERROR: " + exception.getMessage());
              }

              MessageObject msgObj =
                  new MessageObject(
                      "The PathRequired request for 'DbId="
                          + event.getDbId()
                          + "' received an error instead of a valid response.\n"
                          + "ERROR: "
                          + exception.getMessage(),
                      getClass(),
                      MessageType.INTERNAL_ERROR);
              eventBus.fireELVEvent(ELVEventType.INTERNAL_MESSAGE, msgObj);
            }
          });
    } catch (RequestException ex) {
      MessageObject msgObj =
          new MessageObject(
              "The requested data for 'DbId="
                  + event.getDbId()
                  + "' could not be received.\n"
                  + "ERROR: "
                  + ex.getMessage(),
              getClass(),
              MessageType.INTERNAL_ERROR);
      eventBus.fireELVEvent(ELVEventType.INTERNAL_MESSAGE, msgObj);
      Console.error(getClass() + " ERROR: " + ex.getMessage());
    }
  }
  @Override
  public void onResourceChosen(final String resource) {
    String url = AnalysisHelper.URL_PREFIX + "/token/" + token + "?page=1&resource=" + resource;
    RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, url);
    requestBuilder.setHeader("Accept", "application/json");
    try {
      requestBuilder.sendRequest(
          null,
          new RequestCallback() {
            @Override
            public void onResponseReceived(Request request, Response response) {
              try {
                AnalysisResult result =
                    AnalysisModelFactory.getModelObject(AnalysisResult.class, response.getText());
                // We have to do this here because there is no name coming from the RESTFul service
                result
                    .getSummary()
                    .setSpeciesName(speciesMap.get(result.getSummary().getSpecies()));
                view.clearSelection();
                view.showResult(result, resource);
                view.selectPathway(selected);
              } catch (AnalysisModelException e) {
                MessageObject msgObj =
                    new MessageObject(
                        "The received object for '"
                            + resource
                            + "' is empty or faulty and could not be parsed.\n"
                            + "ERROR: "
                            + e.getMessage(),
                        getClass(),
                        MessageType.INTERNAL_ERROR);
                eventBus.fireELVEvent(ELVEventType.INTERNAL_MESSAGE, msgObj);
                Console.error(getClass() + " ERROR: " + e.getMessage());
                view.setInitialState();
              }
            }

            @Override
            public void onError(Request request, Throwable exception) {
              // TODO Check before commit - not yet tested
              MessageObject msgObj =
                  new MessageObject(
                      "The request for '"
                          + resource
                          + "' received an error instead of a valid response.\n"
                          + "ERROR: "
                          + exception.getMessage(),
                      getClass(),
                      MessageType.INTERNAL_ERROR);
              eventBus.fireELVEvent(ELVEventType.INTERNAL_MESSAGE, msgObj);
              Console.error(getClass() + " ERROR: " + exception.getMessage());
              view.setInitialState();
            }
          });
    } catch (RequestException ex) {
      MessageObject msgObj =
          new MessageObject(
              "The requested detailed data for '"
                  + resource
                  + "'\nin the Analysis could not be received.\n"
                  + "ERROR: "
                  + ex.getMessage(),
              getClass(),
              MessageType.INTERNAL_ERROR);
      eventBus.fireELVEvent(ELVEventType.INTERNAL_MESSAGE, msgObj);
      Console.error(getClass() + " ERROR: " + ex.getMessage());
      view.setInitialState();
    }
  }