/**
   * Look up the id of a group authorized for one of the given roles. If no group is currently
   * authorized to perform this role then a new group will be created and assigned the role.
   *
   * @param context The current DSpace context.
   * @param collectionID The collection id.
   * @return The id of the group associated with that particular role or -1
   */
  public static int getCollectionDefaultRead(Context context, int collectionID)
      throws SQLException, AuthorizeException {
    Collection collection = Collection.find(context, collectionID);

    Group[] itemGroups =
        AuthorizeManager.getAuthorizedGroups(context, collection, Constants.DEFAULT_ITEM_READ);
    Group[] bitstreamGroups =
        AuthorizeManager.getAuthorizedGroups(context, collection, Constants.DEFAULT_BITSTREAM_READ);

    int itemGroupID = -1;

    // If there are more than one groups assigned either of these privileges then this role based
    // method will not work.
    // The user will need to go to the authorization section to manually straighten this out.
    if (itemGroups.length != 1 || bitstreamGroups.length != 1) {
      // do nothing the itemGroupID is already set to -1
    } else {
      Group itemGroup = itemGroups[0];
      Group bitstreamGroup = bitstreamGroups[0];

      // If the same group is not assigned both of these privileges then this role based method will
      // not work. The user
      // will need to go to the authorization section to manually straighten this out.
      if (itemGroup.getID() != bitstreamGroup.getID()) {
        // do nothing the itemGroupID is already set to -1
      } else {
        itemGroupID = itemGroup.getID();
      }
    }

    return itemGroupID;
  }
  /**
   * Use the collection's harvest settings to immediately perform a harvest cycle.
   *
   * @param context The current DSpace context.
   * @param collectionID The collection id.
   * @param request the Cocoon request object
   * @return A process result's object.
   * @throws TransformerException
   * @throws SAXException
   * @throws ParserConfigurationException
   * @throws CrosswalkException
   */
  public static FlowResult processRunCollectionHarvest(
      Context context, int collectionID, Request request)
      throws SQLException, IOException, AuthorizeException, CrosswalkException,
          ParserConfigurationException, SAXException, TransformerException {
    FlowResult result = new FlowResult();
    OAIHarvester harvester;
    List<String> testErrors = new ArrayList<String>();
    Collection collection = Collection.find(context, collectionID);
    HarvestedCollection hc = HarvestedCollection.find(context, collectionID);

    // TODO: is there a cleaner way to do this?
    try {
      if (!HarvestScheduler.hasStatus(HarvestScheduler.HARVESTER_STATUS_STOPPED)) {
        synchronized (HarvestScheduler.lock) {
          HarvestScheduler.setInterrupt(
              HarvestScheduler.HARVESTER_INTERRUPT_INSERT_THREAD, collectionID);
          HarvestScheduler.lock.notify();
        }
      } else {
        harvester = new OAIHarvester(context, collection, hc);
        harvester.runHarvest();
      }
    } catch (Exception e) {
      testErrors.add(e.getMessage());
      result.setErrors(testErrors);
      result.setContinue(false);
      return result;
    }

    result.setContinue(true);

    return result;
  }
  /**
   * Change the default read privileges to the anonymous group.
   *
   * <p>If getCollectionDefaultRead() returns -1 or the anonymous group then nothing is done.
   *
   * @param context The current DSpace context.
   * @param collectionID The collection id.
   * @return A process result's object.
   */
  public static FlowResult changeCollectionDefaultReadToAnonymous(Context context, int collectionID)
      throws SQLException, AuthorizeException, UIException {
    FlowResult result = new FlowResult();

    int roleID = getCollectionDefaultRead(context, collectionID);

    if (roleID < 1) {
      throw new UIException(
          "Unable to delete the default read role because the role is either already assigned to the anonymous group or multiple groups are assigned the default privileges.");
    }

    Collection collection = Collection.find(context, collectionID);
    Group role = Group.find(context, roleID);
    Group anonymous = Group.find(context, 0);

    // Delete the old role, this will remove the default privileges.
    role.delete();

    // Set anonymous as the default read group.
    AuthorizeManager.addPolicy(context, collection, Constants.DEFAULT_ITEM_READ, anonymous);
    AuthorizeManager.addPolicy(context, collection, Constants.DEFAULT_BITSTREAM_READ, anonymous);

    // Commit the changes
    context.commit();

    result.setContinue(true);
    result.setOutcome(true);
    result.setMessage(
        new Message(
            "default",
            "All new items submitted to this collection will default to anonymous read."));
    return result;
  }
  /**
   * Look up the id of a group authorized for one of the given roles. If no group is currently
   * authorized to perform this role then a new group will be created and assigned the role.
   *
   * @param context The current DSpace context.
   * @param collectionID The collection id.
   * @param roleName ADMIN, WF_STEP1, WF_STEP2, WF_STEP3, SUBMIT, DEFAULT_READ.
   * @return The id of the group associated with that particular role, or -1 if the role was not
   *     found.
   */
  public static int getCollectionRole(Context context, int collectionID, String roleName)
      throws SQLException, AuthorizeException, IOException, TransformerException, SAXException,
          WorkflowConfigurationException, ParserConfigurationException {
    Collection collection = Collection.find(context, collectionID);

    // Determine the group based upon wich role we are looking for.
    Group roleGroup = null;
    if (ROLE_ADMIN.equals(roleName)) {
      roleGroup = collection.getAdministrators();
      if (roleGroup == null) {
        roleGroup = collection.createAdministrators();
      }
    } else if (ROLE_SUBMIT.equals(roleName)) {
      roleGroup = collection.getSubmitters();
      if (roleGroup == null) roleGroup = collection.createSubmitters();
    } else {
      if (ConfigurationManager.getProperty("workflow", "workflow.framework")
          .equals("xmlworkflow")) { // Resolve our id to a role
        roleGroup = getXMLWorkflowRole(context, collectionID, roleName, collection, roleGroup);
      } else {
        roleGroup = getOriginalWorkflowRole(roleName, collection, roleGroup);
      }
    }

    // In case we needed to create a group, save our changes
    collection.update();
    context.commit();

    // If the role name was valid then role should be non null,
    if (roleGroup != null) return roleGroup.getID();

    return -1;
  }
  /**
   * Change default privileges from the anonymous group to a new group that will be created and
   * appropriate privileges assigned. The id of this new group will be returned.
   *
   * @param context The current DSpace context.
   * @param collectionID The collection id.
   * @return The group ID of the new group.
   */
  public static int createCollectionDefaultReadGroup(Context context, int collectionID)
      throws SQLException, AuthorizeException, UIException {
    int roleID = getCollectionDefaultRead(context, collectionID);

    if (roleID != 0) {
      throw new UIException(
          "Unable to create a new default read group because either the group already exists or multiple groups are assigned the default privileges.");
    }

    Collection collection = Collection.find(context, collectionID);
    Group role = Group.create(context);
    role.setName("COLLECTION_" + collection.getID() + "_DEFAULT_READ");

    // Remove existing privileges from the anonymous group.
    AuthorizeManager.removePoliciesActionFilter(context, collection, Constants.DEFAULT_ITEM_READ);
    AuthorizeManager.removePoliciesActionFilter(
        context, collection, Constants.DEFAULT_BITSTREAM_READ);

    // Grant our new role the default privileges.
    AuthorizeManager.addPolicy(context, collection, Constants.DEFAULT_ITEM_READ, role);
    AuthorizeManager.addPolicy(context, collection, Constants.DEFAULT_BITSTREAM_READ, role);

    // Commit the changes
    role.update();
    context.commit();

    return role.getID();
  }
  /**
   * Delete one of collection's roles
   *
   * @param context The current DSpace context.
   * @param collectionID The collection id.
   * @param roleName ADMIN, WF_STEP1, WF_STEP2, WF_STEP3, SUBMIT, DEFAULT_READ.
   * @param groupID The id of the group associated with this role.
   * @return A process result's object.
   */
  public static FlowResult processDeleteCollectionRole(
      Context context, int collectionID, String roleName, int groupID)
      throws SQLException, UIException, IOException, AuthorizeException,
          WorkflowConfigurationException {
    FlowResult result = new FlowResult();

    Collection collection = Collection.find(context, collectionID);
    Group role = Group.find(context, groupID);

    // First, Unregister the role
    if (ROLE_ADMIN.equals(roleName)) {
      collection.removeAdministrators();
    } else if (ROLE_SUBMIT.equals(roleName)) {
      collection.removeSubmitters();
    } else {
      WorkflowUtils.deleteRoleGroup(context, collection, roleName);
    }
    //		else if (ROLE_WF_STEP1.equals(roleName))
    //		{
    //			collection.setWorkflowGroup(1, null);
    //		}
    //		else if (ROLE_WF_STEP2.equals(roleName))
    //		{
    //			collection.setWorkflowGroup(2, null);
    //		}
    //		else if (ROLE_WF_STEP3.equals(roleName))
    //		{
    //			collection.setWorkflowGroup(3, null);
    //
    //		}

    // Second, remove all authorizations for this role by searching for all policies that this
    // group has on the collection and remove them otherwise the delete will fail because
    // there are dependencies.
    @SuppressWarnings("unchecked") // the cast is correct
    List<ResourcePolicy> policies = AuthorizeManager.getPolicies(context, collection);
    for (ResourcePolicy policy : policies) {
      if (policy.getGroupID() == groupID) {
        policy.delete();
      }
    }

    // Finally, Delete the role's actual group.
    collection.update();
    role.delete();
    context.commit();

    result.setContinue(true);
    result.setOutcome(true);
    result.setMessage(new Message("default", "The role was successfully deleted."));
    return result;
  }
  /**
   * Delete a collection's template item (which is not a member of the collection).
   *
   * @param context
   * @param collectionID
   * @throws SQLException
   * @throws AuthorizeException
   * @throws IOException
   */
  public static FlowResult processDeleteTemplateItem(Context context, int collectionID)
      throws SQLException, AuthorizeException, IOException {
    FlowResult result = new FlowResult();

    Collection collection = Collection.find(context, collectionID);

    collection.removeTemplateItem();
    context.commit();

    result.setContinue(true);
    result.setOutcome(true);
    return result;
  }
  /**
   * Look up the id of the template item for a given collection.
   *
   * @param context The current DSpace context.
   * @param collectionID The collection id.
   * @return The id of the template item.
   * @throws IOException
   */
  public static int getTemplateItemID(Context context, int collectionID)
      throws SQLException, AuthorizeException, IOException {
    Collection collection = Collection.find(context, collectionID);
    Item template = collection.getTemplateItem();

    if (template == null) {
      collection.createTemplateItem();
      template = collection.getTemplateItem();

      collection.update();
      template.update();
      context.commit();
    }

    return template.getID();
  }
  /**
   * Construct a workspace item corresponding to the given database row
   *
   * @param context the context this object exists in
   * @param row the database row
   */
  WorkflowItem(Context context, TableRow row) throws SQLException {
    ourContext = context;
    wfRow = row;

    item = Item.find(context, wfRow.getIntColumn("item_id"));
    collection = Collection.find(context, wfRow.getIntColumn("collection_id"));

    if (wfRow.isColumnNull("owner")) {
      owner = null;
    } else {
      owner = EPerson.find(context, wfRow.getIntColumn("owner"));
    }

    // Cache ourselves
    context.cache(this, row.getIntColumn("workflow_id"));
  }
Example #10
0
  /**
   * processCurateCollection
   *
   * <p>Utility method to process curation tasks submitted via the DSpace GUI
   *
   * @param context
   * @param dsoID
   * @param request
   */
  public static FlowResult processCurateCollection(Context context, int dsoID, Request request)
      throws AuthorizeException, IOException, SQLException, Exception {
    String task = request.getParameter("curate_task");
    Curator curator = FlowCurationUtils.getCurator(task);

    try {
      Collection collection = Collection.find(context, dsoID);
      if (collection != null) {
        // Call curate(context,ID) to ensure a Task Performer (Eperson) is set in Curator
        curator.curate(context, collection.getHandle());
      }
      return FlowCurationUtils.getRunFlowResult(task, curator, true);
    } catch (Exception e) {
      curator.setResult(task, e.getMessage());
      return FlowCurationUtils.getRunFlowResult(task, curator, false);
    }
  }
  /**
   * Send a collection backup file and respective children to cloud to be preserved.
   *
   * @param context context DSpace
   * @param ref ID of the collection
   * @param establishConnection true if pretend establish connection to cloud
   * @return true if file correctly sent to cloud, or false if not
   */
  public Boolean sendCollectionAndChilds(
      Context context, Integer ref, Boolean establishConnection) {
    // if first, make connection and get collection and item files preserved in cloud
    if (establishConnection == true) {
      this.makeConnection();
      this.filesInCloud.putAll(this.newCloudConnection.getInfoFilesIn(Constants.COLLECTION));
      this.filesInCloud.putAll(this.newCloudConnection.getInfoFilesIn(Constants.ITEM));
    }

    // send to cloud atual collection
    sendCollection(context, ref, false);

    Collection obj;
    ItemIterator items;

    // get the items presents in the collection
    try {
      obj = Collection.find(context, ref);
      items = obj.getAllItems();
    } catch (Exception ex) {
      Logger.getLogger(ActualContentManagement.class.getName()).log(Level.SEVERE, null, ex);
      // it means it is the first father in the order, so close connection
      if (establishConnection == true) this.closeConnection();
      return false;
    }

    // send to cloud, one by one, each item
    try {
      if (items.hasNext()) {
        Item newObj = items.next();
        sendItem(context, newObj.getID(), false);
      }
    } catch (Exception ex) {
      Logger.getLogger(ActualContentManagement.class.getName()).log(Level.SEVERE, null, ex);
      // it means it is the first father in the order, so close connection
      if (establishConnection == true) this.closeConnection();
      return false;
    }

    // it means it is the first father in the order
    if (establishConnection == true) this.closeConnection();

    return true;
  }
Example #12
0
 /** queues curation tasks */
 public static FlowResult processQueueCollection(Context context, int dsoID, Request request)
     throws AuthorizeException, IOException, SQLException, Exception {
   String task = request.getParameter("curate_task");
   Curator curator = FlowCurationUtils.getCurator(task);
   String objId = String.valueOf(dsoID);
   String taskQueueName = ConfigurationManager.getProperty("curate", "ui.queuename");
   boolean status = false;
   Collection collection = Collection.find(context, dsoID);
   if (collection != null) {
     objId = collection.getHandle();
     try {
       curator.queue(context, objId, taskQueueName);
       status = true;
     } catch (IOException ioe) {
       // no-op
     }
   }
   return FlowCurationUtils.getQueueFlowResult(task, status, objId, taskQueueName);
 }
Example #13
0
  /**
   * Delete collection itself
   *
   * @param context The current DSpace context.
   * @param collectionID The collection id.
   * @return A process result's object.
   */
  public static FlowResult processDeleteCollection(Context context, int collectionID)
      throws SQLException, AuthorizeException, IOException {
    FlowResult result = new FlowResult();

    Collection collection = Collection.find(context, collectionID);

    Community[] parents = collection.getCommunities();

    for (Community parent : parents) {
      parent.removeCollection(collection);
      parent.update();
    }

    context.commit();

    result.setContinue(true);
    result.setOutcome(true);
    result.setMessage(new Message("default", "The collection was successfully deleted."));

    return result;
  }
Example #14
0
  /**
   * Purge the collection of all items, then run a fresh harvest cycle.
   *
   * @param context The current DSpace context.
   * @param collectionID The collection id.
   * @param request the Cocoon request object
   * @return A process result's object.
   * @throws TransformerException
   * @throws SAXException
   * @throws ParserConfigurationException
   * @throws CrosswalkException
   * @throws BrowseException
   */
  public static FlowResult processReimportCollection(
      Context context, int collectionID, Request request)
      throws SQLException, IOException, AuthorizeException, CrosswalkException,
          ParserConfigurationException, SAXException, TransformerException, BrowseException {
    Collection collection = Collection.find(context, collectionID);
    HarvestedCollection hc = HarvestedCollection.find(context, collectionID);

    ItemIterator it = collection.getAllItems();
    // IndexBrowse ib = new IndexBrowse(context);
    while (it.hasNext()) {
      Item item = it.next();
      // System.out.println("Deleting: " + item.getHandle());
      // ib.itemRemoved(item);
      collection.removeItem(item);
    }
    hc.setHarvestResult(null, "");
    hc.update();
    collection.update();
    context.commit();

    return processRunCollectionHarvest(context, collectionID, request);
  }
Example #15
0
  /**
   * Find collection from DSpace database. It is encapsulation of method
   * org.dspace.content.Collection.find with checking if item exist and if user logged into context
   * has permission to do passed action.
   *
   * @param context Context of actual logged user.
   * @param id Id of collection in DSpace.
   * @param action Constant from org.dspace.core.Constants.
   * @return It returns DSpace collection.
   * @throws WebApplicationException Is thrown when item with passed id is not exists and if user
   *     has no permission to do passed action.
   */
  private org.dspace.content.Collection findCollection(
      org.dspace.core.Context context, int id, int action) throws WebApplicationException {
    org.dspace.content.Collection collection = null;
    try {
      collection = org.dspace.content.Collection.find(context, id);

      if (collection == null) {
        context.abort();
        log.warn("Collection(id=" + id + ") was not found!");
        throw new WebApplicationException(Response.Status.NOT_FOUND);
      } else if (!AuthorizeManager.authorizeActionBoolean(context, collection, action)) {
        context.abort();
        if (context.getCurrentUser() != null) {
          log.error(
              "User("
                  + context.getCurrentUser().getEmail()
                  + ") has not permission to "
                  + getActionString(action)
                  + " collection!");
        } else {
          log.error(
              "User(anonymous) has not permission to " + getActionString(action) + " collection!");
        }
        throw new WebApplicationException(Response.Status.UNAUTHORIZED);
      }

    } catch (SQLException e) {
      processException(
          "Something get wrong while finding collection(id="
              + id
              + "). SQLException, Message: "
              + e,
          context);
    }
    return collection;
  }
  public void addBody(Body body) throws WingException, SQLException {
    // Get all our parameters
    String baseURL = contextPath + "/admin/groups?administrative-continue=" + knot.getId();
    String query = parameters.getParameter("query", "");
    int page = parameters.getParameterAsInteger("page", 0);
    int highlightID = parameters.getParameterAsInteger("highlightID", -1);
    // FIXME: Bad!
    //        int resultCount = Group.searchResultCount(context, query);
    int resultCount = Group.search(context, query).length;
    Group[] groups = Group.search(context, query, page * PAGE_SIZE, PAGE_SIZE);

    // DIVISION: groups-main
    Division main =
        body.addInteractiveDivision(
            "groups-main",
            contextPath + "/admin/groups",
            Division.METHOD_POST,
            "primary administrative groups");
    main.setHead(T_main_head);

    // DIVISION: group-actions
    Division actions = main.addDivision("group-actions");
    actions.setHead(T_actions_head);

    // Browse Epeople
    List actionsList = actions.addList("actions");
    actionsList.addLabel(T_actions_create);
    actionsList.addItemXref(baseURL + "&submit_add", T_actions_create_link);
    actionsList.addLabel(T_actions_browse);
    actionsList.addItemXref(baseURL + "&query&submit_search", T_actions_browse_link);

    actionsList.addLabel(T_actions_search);
    org.dspace.app.xmlui.wing.element.Item actionItem = actionsList.addItem();
    Text queryField = actionItem.addText("query");
    if (query != null) queryField.setValue(query);
    queryField.setHelp(T_search_help);
    actionItem.addButton("submit_search").setValue(T_go);

    // DIVISION: group-search
    Division search = main.addDivision("group-search");
    search.setHead(T_search_head);

    if (resultCount > PAGE_SIZE) {
      // If there are enough results then paginate the results
      int firstIndex = page * PAGE_SIZE + 1;
      int lastIndex = page * PAGE_SIZE + groups.length;

      String nextURL = null, prevURL = null;
      if (page < (resultCount / PAGE_SIZE)) nextURL = baseURL + "&page=" + (page + 1);
      if (page > 0) prevURL = baseURL + "&page=" + (page - 1);

      search.setSimplePagination(resultCount, firstIndex, lastIndex, prevURL, nextURL);
    }

    Table table = search.addTable("groups-search-table", groups.length + 1, 1);
    Row header = table.addRow(Row.ROLE_HEADER);
    header.addCell().addContent(T_search_column1);
    header.addCell().addContent(T_search_column2);
    header.addCell().addContent(T_search_column3);
    header.addCell().addContent(T_search_column4);
    header.addCell().addContent(T_search_column5);

    for (Group group : groups) {
      Row row;
      if (group.getID() == highlightID) row = table.addRow(null, null, "highlight");
      else row = table.addRow();

      if (group.getID() > 1) {
        CheckBox select = row.addCell().addCheckBox("select_group");
        select.setLabel(new Integer(group.getID()).toString());
        select.addOption(new Integer(group.getID()).toString());
      } else {
        // Don't allow the user to remove the administrative (id:1) or
        // anonymous group (id:0)
        row.addCell();
      }

      row.addCell().addContent(group.getID());
      row.addCell().addXref(baseURL + "&submit_edit&groupID=" + group.getID(), group.getName());

      int memberCount = group.getMembers().length + group.getMemberGroups().length;
      row.addCell().addContent(memberCount == 0 ? "-" : String.valueOf(memberCount));

      Cell cell = row.addCell();
      if (FlowGroupUtils.getCollectionId(group.getName()) > -1) {
        Collection collection =
            Collection.find(context, FlowGroupUtils.getCollectionId(group.getName()));
        if (collection != null) {
          String collectionName = collection.getMetadata("name");

          if (collectionName == null) collectionName = "";
          else if (collectionName.length() > MAX_COLLECTION_NAME)
            collectionName = collectionName.substring(0, MAX_COLLECTION_NAME - 3) + "...";

          cell.addContent(collectionName + " ");

          Highlight highlight = cell.addHighlight("fade");

          highlight.addContent("[");
          highlight.addXref(
              contextPath + "/handle/" + collection.getExternalIdentifier().getCanonicalForm(),
              T_collection_link);
          highlight.addContent("]");
        }
      }
    }

    if (groups.length <= 0) {
      Cell cell = table.addRow().addCell(1, 5);
      cell.addHighlight("italic").addContent(T_no_results);
    } else {
      search.addPara().addButton("submit_delete").setValue(T_submit_delete);
    }

    search.addHidden("administrative-continue").setValue(knot.getId());
  }
Example #17
0
  public DSpaceObject getParentObject() throws SQLException {
    // could a collection/community administrator manage related groups?
    // check before the configuration options could give a performance gain
    // if all group management are disallowed
    if (AuthorizeConfiguration.canCollectionAdminManageAdminGroup()
        || AuthorizeConfiguration.canCollectionAdminManageSubmitters()
        || AuthorizeConfiguration.canCollectionAdminManageWorkflows()
        || AuthorizeConfiguration.canCommunityAdminManageAdminGroup()
        || AuthorizeConfiguration.canCommunityAdminManageCollectionAdminGroup()
        || AuthorizeConfiguration.canCommunityAdminManageCollectionSubmitters()
        || AuthorizeConfiguration.canCommunityAdminManageCollectionWorkflows()) {
      // is this a collection related group?
      TableRow qResult =
          DatabaseManager.querySingle(
              myContext,
              "SELECT collection_id, workflow_step_1, workflow_step_2, "
                  + " workflow_step_3, submitter, admin FROM collection "
                  + " WHERE workflow_step_1 = ? OR "
                  + " workflow_step_2 = ? OR "
                  + " workflow_step_3 = ? OR "
                  + " submitter =  ? OR "
                  + " admin = ?",
              getID(),
              getID(),
              getID(),
              getID(),
              getID());
      if (qResult != null) {
        Collection collection = Collection.find(myContext, qResult.getIntColumn("collection_id"));

        if ((qResult.getIntColumn("workflow_step_1") == getID()
            || qResult.getIntColumn("workflow_step_2") == getID()
            || qResult.getIntColumn("workflow_step_3") == getID())) {
          if (AuthorizeConfiguration.canCollectionAdminManageWorkflows()) {
            return collection;
          } else if (AuthorizeConfiguration.canCommunityAdminManageCollectionWorkflows()) {
            return collection.getParentObject();
          }
        }
        if (qResult.getIntColumn("submitter") == getID()) {
          if (AuthorizeConfiguration.canCollectionAdminManageSubmitters()) {
            return collection;
          } else if (AuthorizeConfiguration.canCommunityAdminManageCollectionSubmitters()) {
            return collection.getParentObject();
          }
        }
        if (qResult.getIntColumn("admin") == getID()) {
          if (AuthorizeConfiguration.canCollectionAdminManageAdminGroup()) {
            return collection;
          } else if (AuthorizeConfiguration.canCommunityAdminManageCollectionAdminGroup()) {
            return collection.getParentObject();
          }
        }
      }
      // is the group related to a community and community administrator allowed
      // to manage it?
      else if (AuthorizeConfiguration.canCommunityAdminManageAdminGroup()) {
        qResult =
            DatabaseManager.querySingle(
                myContext, "SELECT community_id FROM community " + "WHERE admin = ?", getID());

        if (qResult != null) {
          Community community = Community.find(myContext, qResult.getIntColumn("community_id"));
          return community;
        }
      }
    }
    return null;
  }
Example #18
0
  public static void main(String[] argv) throws Exception {
    DSIndexer.setBatchProcessingMode(true);
    Date startTime = new Date();
    int status = 0;

    try {
      // create an options object and populate it
      CommandLineParser parser = new PosixParser();

      Options options = new Options();

      options.addOption("a", "add", false, "add items to DSpace");
      options.addOption("r", "replace", false, "replace items in mapfile");
      options.addOption("d", "delete", false, "delete items listed in mapfile");
      options.addOption("s", "source", true, "source of items (directory)");
      options.addOption("z", "zip", true, "name of zip file");
      options.addOption("c", "collection", true, "destination collection(s) Handle or database ID");
      options.addOption("m", "mapfile", true, "mapfile items in mapfile");
      options.addOption("e", "eperson", true, "email of eperson doing importing");
      options.addOption("w", "workflow", false, "send submission through collection's workflow");
      options.addOption(
          "n",
          "notify",
          false,
          "if sending submissions through the workflow, send notification emails");
      options.addOption("t", "test", false, "test run - do not actually import items");
      options.addOption("p", "template", false, "apply template");
      options.addOption("R", "resume", false, "resume a failed import (add only)");
      options.addOption("q", "quiet", false, "don't display metadata");

      options.addOption("h", "help", false, "help");

      CommandLine line = parser.parse(options, argv);

      String command = null; // add replace remove, etc
      String sourcedir = null;
      String mapfile = null;
      String eperson = null; // db ID or email
      String[] collections = null; // db ID or handles

      if (line.hasOption('h')) {
        HelpFormatter myhelp = new HelpFormatter();
        myhelp.printHelp("ItemImport\n", options);
        System.out.println(
            "\nadding items:    ItemImport -a -e eperson -c collection -s sourcedir -m mapfile");
        System.out.println(
            "\nadding items from zip file:    ItemImport -a -e eperson -c collection -s sourcedir -z filename.zip -m mapfile");
        System.out.println(
            "replacing items: ItemImport -r -e eperson -c collection -s sourcedir -m mapfile");
        System.out.println("deleting items:  ItemImport -d -e eperson -m mapfile");
        System.out.println(
            "If multiple collections are specified, the first collection will be the one that owns the item.");

        System.exit(0);
      }

      if (line.hasOption('a')) {
        command = "add";
      }

      if (line.hasOption('r')) {
        command = "replace";
      }

      if (line.hasOption('d')) {
        command = "delete";
      }

      if (line.hasOption('w')) {
        useWorkflow = true;
        if (line.hasOption('n')) {
          useWorkflowSendEmail = true;
        }
      }

      if (line.hasOption('t')) {
        isTest = true;
        System.out.println("**Test Run** - not actually importing items.");
      }

      if (line.hasOption('p')) {
        template = true;
      }

      if (line.hasOption('s')) // source
      {
        sourcedir = line.getOptionValue('s');
      }

      if (line.hasOption('m')) // mapfile
      {
        mapfile = line.getOptionValue('m');
      }

      if (line.hasOption('e')) // eperson
      {
        eperson = line.getOptionValue('e');
      }

      if (line.hasOption('c')) // collections
      {
        collections = line.getOptionValues('c');
      }

      if (line.hasOption('R')) {
        isResume = true;
        System.out.println("**Resume import** - attempting to import items not already imported");
      }

      if (line.hasOption('q')) {
        isQuiet = true;
      }

      boolean zip = false;
      String zipfilename = "";
      String ziptempdir = ConfigurationManager.getProperty("org.dspace.app.itemexport.work.dir");
      if (line.hasOption('z')) {
        zip = true;
        zipfilename = sourcedir + System.getProperty("file.separator") + line.getOptionValue('z');
      }

      // now validate
      // must have a command set
      if (command == null) {
        System.out.println(
            "Error - must run with either add, replace, or remove (run with -h flag for details)");
        System.exit(1);
      } else if ("add".equals(command) || "replace".equals(command)) {
        if (sourcedir == null) {
          System.out.println("Error - a source directory containing items must be set");
          System.out.println(" (run with -h flag for details)");
          System.exit(1);
        }

        if (mapfile == null) {
          System.out.println("Error - a map file to hold importing results must be specified");
          System.out.println(" (run with -h flag for details)");
          System.exit(1);
        }

        if (eperson == null) {
          System.out.println("Error - an eperson to do the importing must be specified");
          System.out.println(" (run with -h flag for details)");
          System.exit(1);
        }

        if (collections == null) {
          System.out.println("Error - at least one destination collection must be specified");
          System.out.println(" (run with -h flag for details)");
          System.exit(1);
        }
      } else if ("delete".equals(command)) {
        if (eperson == null) {
          System.out.println("Error - an eperson to do the importing must be specified");
          System.exit(1);
        }

        if (mapfile == null) {
          System.out.println("Error - a map file must be specified");
          System.exit(1);
        }
      }

      // can only resume for adds
      if (isResume && !"add".equals(command)) {
        System.out.println("Error - resume option only works with --add command");
        System.exit(1);
      }

      // do checks around mapfile - if mapfile exists and 'add' is selected,
      // resume must be chosen
      File myFile = new File(mapfile);

      if (!isResume && "add".equals(command) && myFile.exists()) {
        System.out.println("Error - the mapfile " + mapfile + " already exists.");
        System.out.println(
            "Either delete it or use --resume if attempting to resume an aborted import.");
        System.exit(1);
      }

      // does the zip file exist and can we write to the temp directory
      if (zip) {
        File zipfile = new File(sourcedir);
        if (!zipfile.canRead()) {
          System.out.println("Zip file '" + sourcedir + "' does not exist, or is not readable.");
          System.exit(1);
        }

        if (ziptempdir == null) {
          System.out.println(
              "Unable to unzip import file as the key 'org.dspace.app.itemexport.work.dir' is not set in dspace.cfg");
          System.exit(1);
        }
        zipfile = new File(ziptempdir);
        if (!zipfile.isDirectory()) {
          System.out.println(
              "'"
                  + ConfigurationManager.getProperty("org.dspace.app.itemexport.work.dir")
                  + "' as defined by the key 'org.dspace.app.itemexport.work.dir' in dspace.cfg "
                  + "is not a valid directory");
          System.exit(1);
        }
        File tempdir = new File(ziptempdir);
        if (!tempdir.exists() && !tempdir.mkdirs()) {
          log.error("Unable to create temporary directory");
        }
        sourcedir = ziptempdir + System.getProperty("file.separator") + line.getOptionValue("z");
        ziptempdir =
            ziptempdir
                + System.getProperty("file.separator")
                + line.getOptionValue("z")
                + System.getProperty("file.separator");
      }

      ItemImport myloader = new ItemImport();

      // create a context
      Context c = new Context();

      // find the EPerson, assign to context
      EPerson myEPerson = null;

      if (eperson.indexOf('@') != -1) {
        // @ sign, must be an email
        myEPerson = EPerson.findByEmail(c, eperson);
      } else {
        myEPerson = EPerson.find(c, Integer.parseInt(eperson));
      }

      if (myEPerson == null) {
        System.out.println("Error, eperson cannot be found: " + eperson);
        System.exit(1);
      }

      c.setCurrentUser(myEPerson);

      // find collections
      Collection[] mycollections = null;

      // don't need to validate collections set if command is "delete"
      if (!"delete".equals(command)) {
        System.out.println("Destination collections:");

        mycollections = new Collection[collections.length];

        // validate each collection arg to see if it's a real collection
        for (int i = 0; i < collections.length; i++) {
          // is the ID a handle?
          if (collections[i].indexOf('/') != -1) {
            // string has a / so it must be a handle - try and resolve
            // it
            mycollections[i] = (Collection) HandleManager.resolveToObject(c, collections[i]);

            // resolved, now make sure it's a collection
            if ((mycollections[i] == null)
                || (mycollections[i].getType() != Constants.COLLECTION)) {
              mycollections[i] = null;
            }
          }
          // not a handle, try and treat it as an integer collection
          // database ID
          else if (collections[i] != null) {
            mycollections[i] = Collection.find(c, Integer.parseInt(collections[i]));
          }

          // was the collection valid?
          if (mycollections[i] == null) {
            throw new IllegalArgumentException(
                "Cannot resolve " + collections[i] + " to collection");
          }

          // print progress info
          String owningPrefix = "";

          if (i == 0) {
            owningPrefix = "Owning ";
          }

          System.out.println(owningPrefix + " Collection: " + mycollections[i].getMetadata("name"));
        }
      } // end of validating collections

      try {
        // If this is a zip archive, unzip it first
        if (zip) {
          ZipFile zf = new ZipFile(zipfilename);
          ZipEntry entry;
          Enumeration<? extends ZipEntry> entries = zf.entries();
          while (entries.hasMoreElements()) {
            entry = entries.nextElement();
            if (entry.isDirectory()) {
              if (!new File(ziptempdir + entry.getName()).mkdir()) {
                log.error("Unable to create contents directory");
              }
            } else {
              System.out.println("Extracting file: " + entry.getName());
              int index = entry.getName().lastIndexOf('/');
              if (index == -1) {
                // Was it created on Windows instead?
                index = entry.getName().lastIndexOf('\\');
              }
              if (index > 0) {
                File dir = new File(ziptempdir + entry.getName().substring(0, index));
                if (!dir.mkdirs()) {
                  log.error("Unable to create directory");
                }
              }
              byte[] buffer = new byte[1024];
              int len;
              InputStream in = zf.getInputStream(entry);
              BufferedOutputStream out =
                  new BufferedOutputStream(new FileOutputStream(ziptempdir + entry.getName()));
              while ((len = in.read(buffer)) >= 0) {
                out.write(buffer, 0, len);
              }
              in.close();
              out.close();
            }
          }
        }

        c.turnOffAuthorisationSystem();

        if ("add".equals(command)) {
          myloader.addItems(c, mycollections, sourcedir, mapfile, template);
        } else if ("replace".equals(command)) {
          myloader.replaceItems(c, mycollections, sourcedir, mapfile, template);
        } else if ("delete".equals(command)) {
          myloader.deleteItems(c, mapfile);
        }

        // complete all transactions
        c.complete();
      } catch (Exception e) {
        // abort all operations
        if (mapOut != null) {
          mapOut.close();
        }

        mapOut = null;

        c.abort();
        e.printStackTrace();
        System.out.println(e);
        status = 1;
      }

      // Delete the unzipped file
      try {
        if (zip) {
          System.gc();
          System.out.println("Deleting temporary zip directory: " + ziptempdir);
          ItemImport.deleteDirectory(new File(ziptempdir));
        }
      } catch (Exception ex) {
        System.out.println("Unable to delete temporary zip archive location: " + ziptempdir);
      }

      if (mapOut != null) {
        mapOut.close();
      }

      if (isTest) {
        System.out.println("***End of Test Run***");
      }
    } finally {
      DSIndexer.setBatchProcessingMode(false);
      Date endTime = new Date();
      System.out.println("Started: " + startTime.getTime());
      System.out.println("Ended: " + endTime.getTime());
      System.out.println(
          "Elapsed time: "
              + ((endTime.getTime() - startTime.getTime()) / 1000)
              + " secs ("
              + (endTime.getTime() - startTime.getTime())
              + " msecs)");
    }

    System.exit(status);
  }
Example #19
0
  /**
   * Process the collection metadata edit form.
   *
   * @param context The current DSpace context.
   * @param collectionID The collection id.
   * @param deleteLogo Determines if the logo should be deleted along with the metadata editing
   *     action.
   * @param request the Cocoon request object
   * @return A process result's object.
   */
  public static FlowResult processEditCollection(
      Context context, int collectionID, boolean deleteLogo, Request request)
      throws SQLException, IOException, AuthorizeException {
    FlowResult result = new FlowResult();

    Collection collection = Collection.find(context, collectionID);

    // Get the metadata
    String name = request.getParameter("name");
    String shortDescription = request.getParameter("short_description");
    String introductoryText = request.getParameter("introductory_text");
    String copyrightText = request.getParameter("copyright_text");
    String sideBarText = request.getParameter("side_bar_text");
    String license = request.getParameter("license");
    String provenanceDescription = request.getParameter("provenance_description");

    // If they don't have a name then make it untitled.
    if (name == null || name.length() == 0) {
      name = "Untitled";
    }

    // If empty, make it null.
    if (shortDescription != null && shortDescription.length() == 0) {
      shortDescription = null;
    }
    if (introductoryText != null && introductoryText.length() == 0) {
      introductoryText = null;
    }
    if (copyrightText != null && copyrightText.length() == 0) {
      copyrightText = null;
    }
    if (sideBarText != null && sideBarText.length() == 0) {
      sideBarText = null;
    }
    if (license != null && license.length() == 0) {
      license = null;
    }
    if (provenanceDescription != null && provenanceDescription.length() == 0) {
      provenanceDescription = null;
    }

    // Save the metadata
    collection.setMetadata("name", name);
    collection.setMetadata("short_description", shortDescription);
    collection.setMetadata("introductory_text", introductoryText);
    collection.setMetadata("copyright_text", copyrightText);
    collection.setMetadata("side_bar_text", sideBarText);
    collection.setMetadata("license", license);
    collection.setMetadata("provenance_description", provenanceDescription);

    // Change or delete the logo
    if (deleteLogo) {
      // Remove the logo
      collection.setLogo(null);
    } else {
      // Update the logo
      Object object = request.get("logo");
      Part filePart = null;
      if (object instanceof Part) {
        filePart = (Part) object;
      }

      if (filePart != null && filePart.getSize() > 0) {
        InputStream is = filePart.getInputStream();

        collection.setLogo(is);
      }
    }

    // Save everything
    collection.update();
    context.commit();

    // No notice...
    result.setContinue(true);

    return result;
  }