Esempio n. 1
0
  public static void main(String[] args) throws Exception {

    DBSessionFactory.registerDataSource(
        "target",
        "jdbc:access:////usr/data/rldbRelationshipFree.mdb",
        "com.hxtt.sql.access.AccessDriver",
        "",
        "");

    SysDebugger.getInstance().println("Connecting to target database.");
    ExecutionContext ectarget = new SystemExecutionContext("target");
    ectarget.setExecutionLevel(ExecutionContext.ADMIN);
    ectarget.setAPILevel(ExecutionContext.SQL_ALLOWED);

    try {
      ectarget.doUpdate("drop table REDLIST_PUB");
    } catch (Exception failOk) {
      SysDebugger.getInstance().println("REDLIST_PUB doesn't already exist");
    }
    ectarget.doUpdate(
        "create table REDLIST_PUB (ASSESSMENTID integer,REDLISTDATASOURCEID integer,OTHERDATASOURCEID integer)");

    BufferedReader fr =
        new BufferedReader(
            new InputStreamReader(
                new FileInputStream("/home/rob.heittman/Desktop/RedList_Pub.txt"), "UTF-8"));

    while (fr.ready()) {
      String line = fr.readLine();
      if (line == null) break;
      try {
        CSVTokenizer tok = new CSVTokenizer(line);
        String assessmentId = tok.nextToken();
        String redListDataSourceId = tok.nextToken();
        if ("".equals(redListDataSourceId)) {
          redListDataSourceId = "NULL";
        }
        tok.nextToken(); // string version discarded
        String otherListDataSourceId = tok.nextToken();
        if ("".equals(otherListDataSourceId)) {
          otherListDataSourceId = "NULL";
        }
        String s =
            "insert into REDLIST_PUB"
                + " values ("
                + assessmentId
                + ","
                + redListDataSourceId
                + ","
                + otherListDataSourceId
                + ");";
        SysDebugger.getInstance().println(s);
        ectarget.doUpdate(s);
        SysDebugger.getInstance().println("Imported citation info for " + assessmentId);
      } catch (Exception oops) {
        oops.printStackTrace();
      }
    }
  }
  public static synchronized String getNextDraftID(VFS vfs, String speciesID) {
    int nextID = 0;
    VFSPath rootURI;

    try {
      rootURI = VFSUtils.parseVFSPath(ServerPaths.getDraftAssessmentRootURL(speciesID));
    } catch (VFSPathParseException e) {
      e.printStackTrace();
      return null;
    }

    if (vfs.exists(rootURI)) { // If there's already regionals for this guy,
      // get the next
      VFSPathToken[] tokens;
      try {
        tokens = vfs.list(rootURI);
        for (VFSPathToken curToken : tokens) {
          String filename = curToken.toString();
          filename = filename.replaceAll(".xml", "");
          if (!SISContainerApp.amIOnline()) filename = filename.replaceAll("offline", "");

          System.out.println("Crawling file " + curToken + " for new regional ID.");

          try {
            int value = Integer.valueOf(filename.split("_")[1]);
            nextID = Math.max(value, nextID);
          } catch (NumberFormatException e) {
            SysDebugger.getNamedInstance(SISContainerApp.SEVERE_LOG)
                .println(
                    "Annoying file in path "
                        + curToken
                        + " non-conformant "
                        + "to standard region assessment file name pattern.");
          }
        }
      } catch (NotFoundException e) {
        SysDebugger.getNamedInstance(SISContainerApp.SEVERE_LOG)
            .println("Big WTF. " + "List failed on existing path " + rootURI.toString());
        return null;
      }

      nextID++; // Increment it one past the highest found.
    }

    String assessmentID = speciesID + "_" + nextID;
    return assessmentID;
  }
  private void deleteAssessment(
      Request request,
      Response response,
      final String assessmentID,
      final String assessmentType,
      final String username) {
    try {
      AssessmentData assessment =
          AssessmentIO.readAssessment(vfs, assessmentID, assessmentType, username);
      boolean deleted = AssessmentIO.trashAssessment(vfs, assessment, username);

      if (deleted) {
        String log = "<assessment user=\"" + username;
        log += "\" status=\"" + assessmentType.toLowerCase().replace("_status", "");
        log +=
            "\" date=\""
                + new Date().toString()
                + "\" node=\""
                + assessment.getSpeciesName()
                + "\">"
                + assessmentID
                + "</assessment>";

        trashBuffer.addEvent(DocumentUtils.createDocumentFromString(log));
        trashBuffer.flush();

        // remove from recent
        final Request req =
            new Request(
                Method.DELETE,
                "riap://host/recentAssessments/" + username + assessmentType + "/" + assessmentID);
        Response resp = getContext().getClientDispatcher().handle(req);
        if (!(resp.getStatus()).isSuccess()) {
          System.out.println("Unable to delete assessment from recent.");
        } else {
          // node.removeAssessment(pubAssessments.get(i));
          System.out.println("Assessment deleted from recent.");
        }

        response.setStatus(Status.SUCCESS_OK);
      } else response.setStatus(Status.CLIENT_ERROR_LOCKED);
    } catch (Exception e) {
      SysDebugger.getInstance().println("Could not find assessment " + assessmentID);
      e.printStackTrace();
      response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
    }
  }