Example #1
0
  /**
   * doImport called when "eventSubmit_doBatch_Archive_PreProcess" is in the request parameters to
   * do the prep work for archiving a bunch of sites that match the criteria
   */
  public void doBatch_Archive_PreProcess(RunData data, Context context) {

    SessionState state =
        ((JetspeedRunData) data).getPortletSessionState(((JetspeedRunData) data).getJs_peid());

    if (!securityService.isSuperUser()) {
      addAlert(state, rb.getString("archive.batch.auth"));
      return;
    }

    // if we have a selected term then use that as the batch export param
    String selectedTerm = data.getParameters().getString("archive-term");

    log.debug("selectedTerm: " + selectedTerm);

    if (StringUtils.isBlank(selectedTerm)) {
      addAlert(state, rb.getString("archive.batch.term.text.missingterm"));
      state.setAttribute(STATE_MODE, BATCH_MODE);
      return;
    }

    // set the message
    state.setAttribute(
        "confirmString",
        rb.getFormattedMessage("archive.batch.term.text.confirm.1", new Object[] {selectedTerm}));

    // get the sites that match the criteria
    Map<String, String> propertyCriteria = new HashMap<String, String>();
    propertyCriteria.put("term_eid", selectedTerm);
    List<Site> sites =
        siteService.getSites(
            SelectionType.ANY, null, null, propertyCriteria, SortType.TITLE_ASC, null);

    if (sites.isEmpty()) {
      addAlert(
          state,
          rb.getFormattedMessage("archive.batch.term.text.nosites", new Object[] {selectedTerm}));
      state.setAttribute(STATE_MODE, BATCH_MODE);
      return;
    }

    // convert to new list so that we dont load entire sites into context
    List<SparseSite> ssites = new ArrayList<SparseSite>();
    for (Site s : sites) {
      ssites.add(new SparseSite(s.getId(), s.getTitle()));
    }

    state.setAttribute("sites", ssites);

    // put into state for next pass
    state.setAttribute("selectedTerm", selectedTerm);

    // set mode so we go to next template
    state.setAttribute(STATE_MODE, BATCH_ARCHIVE_CONFIRM_MODE);
  }
Example #2
0
  /**
   * Process that archives the sites
   *
   * @param sites list of SparseSite
   * @throws InterruptedException
   */
  private void archiveSites(
      List<SparseSite> sites, String selectedTerm, Session currentSession, User currentUser)
      throws InterruptedException {

    if (isLocked()) {
      throw new IllegalStateException(
          "Cannot run batch archive, an archive job is already in progress");
    }
    batchArchiveStarted = System.currentTimeMillis();
    batchArchiveMessage =
        rb.getFormattedMessage(
            "archive.batch.term.text.statusmessage.start",
            new Object[] {sites.size(), selectedTerm, 0});
    batchArchiveStatus = "RUNNING";

    log.info(
        "Batch archive started for term: "
            + selectedTerm
            + ". Archiving "
            + sites.size()
            + " sites.");

    Session threadSession = sessionManager.getCurrentSession();
    if (threadSession == null) {
      threadSession = sessionManager.startSession();
    }
    threadSession.setUserId(currentUser.getId());
    threadSession.setActive();
    sessionManager.setCurrentSession(threadSession);
    authzGroupService.refreshUser(currentUser.getId());

    // counters so we can run this in batches if we have a number of sites to process
    int archiveCount = 0;

    try {

      for (SparseSite s : sites) {

        log.info("Processing site: " + s.getTitle());

        // archive the site
        archiveService.archive(s.getId());

        // compress it
        // TODO check return value? do we care?
        try {
          archiveService.archiveAndZip(s.getId());
        } catch (IOException e) {
          e.printStackTrace();
        }

        archiveCount++;

        // update message
        if (archiveCount % 1 == 0) {
          int percentComplete = (int) (archiveCount * 100) / sites.size();
          batchArchiveMessage =
              rb.getFormattedMessage(
                  "archive.batch.term.text.statusmessage.update",
                  new Object[] {sites.size(), selectedTerm, archiveCount, percentComplete});
        }

        // sleep if we need to and keep sessions alive
        if (archiveCount > 0 && archiveCount % NUM_SITES_PER_BATCH == 0) {
          log.info("Sleeping for " + PAUSE_TIME_MS + "ms");
          Thread.sleep(PAUSE_TIME_MS);
          threadSession.setActive();
          currentSession.setActive();
        }

        // check timeout
        if (!isLocked()) {
          throw new RuntimeException("Timeout occurred while running batch archive");
        }
      }

      // complete
      batchArchiveMessage =
          rb.getFormattedMessage(
              "archive.batch.term.text.statusmessage.complete",
              new Object[] {sites.size(), selectedTerm});

      log.info("Batch archive complete.");

    } finally {
      // reset
      batchArchiveStatus = STATUS_COMPLETE;
      batchArchiveStarted = 0;
      threadSession.clear();
      threadSession.invalidate();
    }
  }
Example #3
0
  /** build the context for batch archive confirm */
  public String buildDownloadContext(
      VelocityPortlet portlet, Context context, RunData rundata, SessionState state) {
    context.put("tlang", rb);
    buildMenu(context);

    // get list of existing archives
    Collection<File> files = Collections.<File>emptySet();
    File archiveBaseDir =
        new File(serverConfigurationService.getString("archive.storage.path", "sakai/archive"));

    if (archiveBaseDir.exists() && archiveBaseDir.isDirectory()) {
      files = FileUtils.listFiles(archiveBaseDir, new SuffixFileFilter(".zip"), null);
    }

    List<SparseFile> zips = new ArrayList<SparseFile>();

    SimpleDateFormat dateFormatIn = new SimpleDateFormat("yyyyMMddHHmmss");
    SimpleDateFormat dateFormatOut = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    Calendar calendar = Calendar.getInstance();

    // porcess the list. also get the hash for the file if it exists
    for (File f : files) {

      String absolutePath = f.getAbsolutePath();

      SparseFile sf = new SparseFile();
      sf.setFilename(f.getName());
      sf.setAbsolutePath(absolutePath);
      sf.setSize(FileUtils.byteCountToDisplaySize(f.length()));

      // get the datetime string, its the last part of the file name, convert back to a date that we
      // can display
      String dateTimeStr =
          StringUtils.substringAfterLast(StringUtils.removeEnd(f.getName(), ".zip"), "-");

      try {
        Date date = dateFormatIn.parse(dateTimeStr);
        sf.setDateCreated(dateFormatOut.format(date));
      } catch (ParseException pe) {
        // ignore, just don't set the date
      }

      // get siteId, first part of name
      String siteId = StringUtils.substringBeforeLast(f.getName(), "-");
      sf.setSiteId(siteId);

      // try to get site title if the site still exists
      try {
        Site site = siteService.getSite(siteId);
        sf.setSiteTitle(site.getTitle());
      } catch (IdUnusedException e) {
        // ignore, no site available
      }

      // get the hash. need to read it from the file. Same filename but diff extension
      String hashFilePath = StringUtils.removeEnd(absolutePath, ".zip");
      hashFilePath = hashFilePath + ".sha1";

      File hashFile = new File(hashFilePath);
      try {
        String hash = FileUtils.readFileToString(hashFile);
        sf.setHash(hash);
      } catch (IOException e) {
        // ignore, dont use the hash
      }

      zips.add(sf);
    }

    context.put("archives", zips);

    return "-download";
  }