/**
   * Delete bitstream from item
   *
   * @param context
   * @param ItemArchive
   * @param isTest
   * @param suppressUndo
   * @throws IllegalArgumentException
   * @throws ParseException
   * @throws IOException
   * @throws AuthorizeException
   * @throws SQLException
   */
  public void execute(Context context, ItemArchive itarch, boolean isTest, boolean suppressUndo)
      throws IllegalArgumentException, IOException, SQLException, AuthorizeException,
          ParseException {
    File f = new File(itarch.getDirectory(), ItemUpdate.DELETE_CONTENTS_FILE);
    if (!f.exists()) {
      ItemUpdate.pr(
          "Warning: Delete_contents file for item " + itarch.getDirectoryName() + " not found.");
    } else {
      List<Integer> list = MetadataUtilities.readDeleteContentsFile(f);
      if (list.isEmpty()) {
        ItemUpdate.pr("Warning: empty delete_contents file for item " + itarch.getDirectoryName());
      } else {
        for (int id : list) {
          try {
            Bitstream bs = Bitstream.find(context, id);
            if (bs == null) {
              ItemUpdate.pr("Bitstream not found by id: " + id);
            } else {
              Bundle[] bundles = bs.getBundles();
              for (Bundle b : bundles) {
                if (isTest) {
                  ItemUpdate.pr("Delete bitstream with id = " + id);
                } else {
                  b.removeBitstream(bs);
                  ItemUpdate.pr("Deleted bitstream with id = " + id);
                }
              }

              if (alterProvenance) {
                DtoMetadata dtom = DtoMetadata.create("dc.description.provenance", "en", "");

                String append =
                    "Bitstream " + bs.getName() + " deleted on " + DCDate.getCurrent() + "; ";
                Item item = bundles[0].getItems()[0];
                ItemUpdate.pr("Append provenance with: " + append);

                if (!isTest) {
                  MetadataUtilities.appendMetadata(item, dtom, false, append);
                }
              }
            }
          } catch (SQLException e) {
            ItemUpdate.pr("Error finding bitstream from id: " + id + " : " + e.toString());
          }
        }
      }
    }
  }
Example #2
0
  private void processArchive(
      Context context,
      String sourceDirPath,
      String itemField,
      String metadataIndexName,
      boolean alterProvenance,
      boolean isTest)
      throws Exception {
    // open and process the source directory
    File sourceDir = new File(sourceDirPath);

    if ((sourceDir == null) || !sourceDir.exists() || !sourceDir.isDirectory()) {
      pr("Error, cannot open archive source directory " + sourceDirPath);
      throw new Exception("error with archive source directory " + sourceDirPath);
    }

    String[] dircontents = sourceDir.list(directoryFilter); // just the names, not the path
    Arrays.sort(dircontents);

    // Undo is suppressed to prevent undo of undo
    boolean suppressUndo = false;
    File fSuppressUndo = new File(sourceDir, SUPPRESS_UNDO_FILENAME);
    if (fSuppressUndo.exists()) {
      suppressUndo = true;
    }

    File undoDir = null; // sibling directory of source archive

    if (!suppressUndo && !isTest) {
      undoDir = initUndoArchive(sourceDir);
    }

    int itemCount = 0;
    int successItemCount = 0;

    for (String dirname : dircontents) {
      itemCount++;
      pr("");
      pr("processing item " + dirname);

      try {
        ItemArchive itarch = ItemArchive.create(context, new File(sourceDir, dirname), itemField);

        for (UpdateAction action : actionMgr) {
          pr("action: " + action.getClass().getName());
          action.execute(context, itarch, isTest, suppressUndo);
          if (!isTest && !suppressUndo) {
            itarch.writeUndo(undoDir);
          }
        }
        if (!isTest) {
          Item item = itarch.getItem();
          item.update(); // need to update before commit
          context.commit();
          item.decache();
        }
        ItemUpdate.pr("Item " + dirname + " completed");
        successItemCount++;
      } catch (Exception e) {
        pr("Exception processing item " + dirname + ": " + e.toString());
      }
    }

    if (!suppressUndo && !isTest) {
      StringBuilder sb = new StringBuilder("dsrun org.dspace.app.itemupdate.ItemUpdate ");
      sb.append(" -e ").append(this.eperson);
      sb.append(" -s ").append(undoDir);

      if (itemField != null) {
        sb.append(" -i ").append(itemField);
      }

      if (!alterProvenance) {
        sb.append(" -P ");
      }
      if (isTest) {
        sb.append(" -t ");
      }

      for (String actionOption : undoActionList) {
        sb.append(actionOption);
      }

      PrintWriter pw = null;
      try {
        File cmdFile = new File(undoDir.getParent(), undoDir.getName() + "_command.sh");
        pw = new PrintWriter(new BufferedWriter(new FileWriter(cmdFile)));
        pw.println(sb.toString());
      } finally {
        pw.close();
      }
    }

    pr("");
    pr(
        "Done processing.  Successful items: "
            + successItemCount
            + " of "
            + itemCount
            + " items in source archive");
    pr("");
  }