Example #1
0
  /*
   * private method to invoke the xbel network exporter
   */
  private void exportNetwork() throws Exception {
    this.taskStatus = Status.PROCESSING;
    this.startTask();
    String exportFilename =
        this.resolveFilename(
            Configuration.getInstance().getNdexRoot() + "/exported-networks/",
            this.XBEL_FILE_EXTENSION);

    ODatabaseDocumentTx db = null;
    try {
      db = NdexDatabase.getInstance().getAConnection();
      NdexTaskModelService modelService = new NdexJVMDataModelService(db);
      XbelNetworkExporter exporter =
          new XbelNetworkExporter(
              this.getTask().getTaskOwnerId().toString(),
              this.networkId,
              modelService,
              exportFilename);
      exporter.exportNetwork();
      this.taskStatus = Status.COMPLETED;
      this.updateTaskStatus(this.taskStatus);
    } finally {
      if (db != null) db.close();
    }
  }
  private void createNetworkCache() throws NdexException {

    String networkIdStr = this.getTask().getResource();

    try (NetworkDAO dao = new NetworkDAO(NdexDatabase.getInstance().getAConnection())) {
      Long taskCommitId = (Long) getTask().getAttribute(TaskAttribute.readOnlyCommitId);

      String fullpath =
          Configuration.getInstance().getNdexNetworkCachePath() + taskCommitId + ".gz";

      ODocument d = dao.getNetworkDocByUUIDString(networkIdStr);
      d.reload();
      Long actId = d.field(NdexClasses.Network_P_readOnlyCommitId);

      if (!actId.equals(taskCommitId)) {
        // stop task
        getTask()
            .setMessage(
                "Network cache not created. readOnlyCommitId="
                    + actId
                    + " in db, but in task we have commitId="
                    + taskCommitId);
        return;
      }

      // create cache.

      Network n = dao.getNetworkById(UUID.fromString(networkIdStr));

      try (GZIPOutputStream w = new GZIPOutputStream(new FileOutputStream(fullpath), 16384)) {
        //  String s = mapper.writeValueAsString( original);
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(w, n);
      } catch (FileNotFoundException e) {
        throw new NdexException("Can't create network cache file in server: " + fullpath);
      } catch (IOException e) {
        throw new NdexException(
            "IO Error when writing cache file: " + fullpath + ". Cause: " + e.getMessage());
      }

      // check again.
      d.reload();
      actId = d.field(NdexClasses.Network_P_readOnlyCommitId);
      if (!actId.equals(taskCommitId)) {
        // stop task
        getTask()
            .setMessage(
                "Network cache not created. Second check found network readOnlyCommitId is"
                    + actId
                    + ", task has commitId "
                    + taskCommitId);
        return;
      }

      d.field(NdexClasses.Network_P_cacheId, taskCommitId).save();
      logger.info("Cache " + actId + " created.");
      dao.commit();
    }
  }