/**
   * Obtiene todas las revisiones finalizadas.
   *
   * @param context DSpace context object
   * @return an iterator over the items in the archive.
   * @throws SQLException
   */
  public static RevisionToken[] findAllRevisiones(Context context) throws SQLException {
    String myQuery = "SELECT * FROM revision_token WHERE tipo='R' and revision_id is not null";

    TableRowIterator rows = DatabaseManager.queryTable(context, "revision_token", myQuery);

    try {
      List<TableRow> revisionRows = rows.toList();

      RevisionToken[] revisionToken = new RevisionToken[revisionRows.size()];

      for (int i = 0; i < revisionRows.size(); i++) {
        TableRow row = (TableRow) revisionRows.get(i);

        // First check the cache
        RevisionToken fromCache =
            (RevisionToken)
                context.fromCache(RevisionToken.class, row.getIntColumn("revision_token_id"));

        if (fromCache != null) {
          revisionToken[i] = fromCache;
        } else {
          revisionToken[i] = new RevisionToken(row);
        }
      }

      return revisionToken;
    } finally {
      if (rows != null) {
        rows.close();
      }
    }
  }
  /**
   * Get all workflow items for a particular collection.
   *
   * @param context the context object
   * @param c the collection
   * @return array of the corresponding workflow items
   */
  public static WorkflowItem[] findByCollection(Context context, Collection c) throws SQLException {
    List wsItems = new ArrayList();

    TableRowIterator tri =
        DatabaseManager.queryTable(
            context,
            "workflowitem",
            "SELECT workflowitem.* FROM workflowitem WHERE " + "workflowitem.collection_id= ? ",
            c.getID());

    try {
      while (tri.hasNext()) {
        TableRow row = tri.next();

        // Check the cache
        WorkflowItem wi =
            (WorkflowItem) context.fromCache(WorkflowItem.class, row.getIntColumn("workflow_id"));

        // not in cache? turn row into workflowitem
        if (wi == null) {
          wi = new WorkflowItem(context, row);
        }

        wsItems.add(wi);
      }
    } finally {
      if (tri != null) tri.close();
    }

    WorkflowItem[] wsArray = new WorkflowItem[wsItems.size()];
    wsArray = (WorkflowItem[]) wsItems.toArray(wsArray);

    return wsArray;
  }
  /**
   * Get a workflow item from the database. The item, collection and submitter are loaded into
   * memory.
   *
   * @param context DSpace context object
   * @param id ID of the workspace item
   * @return the workflow item, or null if the ID is invalid.
   */
  public static WorkflowItem find(Context context, int id) throws SQLException {
    // First check the cache
    WorkflowItem fromCache = (WorkflowItem) context.fromCache(WorkflowItem.class, id);

    if (fromCache != null) {
      return fromCache;
    }

    TableRow row = DatabaseManager.find(context, "workflowitem", id);

    if (row == null) {
      if (log.isDebugEnabled()) {
        log.debug(
            LogManager.getHeader(context, "find_workflow_item", "not_found,workflow_id=" + id));
      }

      return null;
    } else {
      if (log.isDebugEnabled()) {
        log.debug(LogManager.getHeader(context, "find_workflow_item", "workflow_id=" + id));
      }

      return new WorkflowItem(context, row);
    }
  }
Пример #4
0
  /**
   * Return a List of the policies for a group
   *
   * @param c current context
   * @param g group to retrieve policies for
   * @return List of <code>ResourcePolicy</code> objects
   */
  public static List<ResourcePolicy> getPoliciesForGroup(Context c, Group g) throws SQLException {
    TableRowIterator tri =
        DatabaseManager.queryTable(
            c,
            "resourcepolicy",
            "SELECT * FROM resourcepolicy WHERE epersongroup_id= ? ",
            g.getID());

    List<ResourcePolicy> policies = new ArrayList<ResourcePolicy>();

    try {
      while (tri.hasNext()) {
        TableRow row = tri.next();

        // first check the cache (FIXME: is this right?)
        ResourcePolicy cachepolicy =
            (ResourcePolicy) c.fromCache(ResourcePolicy.class, row.getIntColumn("policy_id"));

        if (cachepolicy != null) {
          policies.add(cachepolicy);
        } else {
          policies.add(new ResourcePolicy(c, row));
        }
      }
    } finally {
      if (tri != null) {
        tri.close();
      }
    }

    return policies;
  }
Пример #5
0
  /**
   * Finds all groups in the site
   *
   * @param context DSpace context
   * @param sortField field to sort by -- Group.ID or Group.NAME
   * @return array of all groups in the site
   */
  public static Group[] findAll(Context context, int sortField) throws SQLException {
    String s;

    switch (sortField) {
      case ID:
        s = "eperson_group_id";

        break;

      case NAME:
        s = "name";

        break;

      default:
        s = "name";
    }

    // NOTE: The use of 's' in the order by clause can not cause an SQL
    // injection because the string is derived from constant values above.
    TableRowIterator rows =
        DatabaseManager.queryTable(
            context, "epersongroup", "SELECT * FROM epersongroup ORDER BY " + s);

    try {
      List<TableRow> gRows = rows.toList();

      Group[] groups = new Group[gRows.size()];

      for (int i = 0; i < gRows.size(); i++) {
        TableRow row = gRows.get(i);

        // First check the cache
        Group fromCache =
            (Group) context.fromCache(Group.class, row.getIntColumn("eperson_group_id"));

        if (fromCache != null) {
          groups[i] = fromCache;
        } else {
          groups[i] = new Group(context, row);
        }
      }

      return groups;
    } finally {
      if (rows != null) {
        rows.close();
      }
    }
  }
Пример #6
0
  /**
   * Returns all groups authorized to perform an action on an object. Returns empty array if no
   * matches.
   *
   * @param c current context
   * @param o object
   * @param actionID ID of action frm <code>org.dspace.core.Constants</code>
   * @return array of <code>Group</code>s that can perform the specified action on the specified
   *     object
   * @throws java.sql.SQLException if there's a database problem
   */
  public static Group[] getAuthorizedGroups(Context c, DSpaceObject o, int actionID)
      throws java.sql.SQLException {
    // do query matching groups, actions, and objects
    TableRowIterator tri =
        DatabaseManager.queryTable(
            c,
            "resourcepolicy",
            "SELECT * FROM resourcepolicy WHERE resource_type_id= ? "
                + "AND resource_id= ? AND action_id= ? ",
            o.getType(),
            o.getID(),
            actionID);

    List<Group> groups = new ArrayList<Group>();
    try {

      while (tri.hasNext()) {
        TableRow row = tri.next();

        // first check the cache (FIXME: is this right?)
        ResourcePolicy cachepolicy =
            (ResourcePolicy) c.fromCache(ResourcePolicy.class, row.getIntColumn("policy_id"));

        ResourcePolicy myPolicy = null;

        if (cachepolicy != null) {
          myPolicy = cachepolicy;
        } else {
          myPolicy = new ResourcePolicy(c, row);
        }

        // now do we have a group?
        Group myGroup = myPolicy.getGroup();

        if (myGroup != null) {
          groups.add(myGroup);
        }
      }
    } finally {
      if (tri != null) {
        tri.close();
      }
    }

    Group[] groupArray = new Group[groups.size()];
    groupArray = groups.toArray(groupArray);

    return groupArray;
  }
Пример #7
0
  /**
   * find the group by its ID
   *
   * @param context
   * @param id
   */
  public static Group find(Context context, int id) throws SQLException {
    // First check the cache
    Group fromCache = (Group) context.fromCache(Group.class, id);

    if (fromCache != null) {
      return fromCache;
    }

    TableRow row = DatabaseManager.find(context, "epersongroup", id);

    if (row == null) {
      return null;
    } else {
      return new Group(context, row);
    }
  }
Пример #8
0
  /**
   * Find the group by its name - assumes name is unique
   *
   * @param context
   * @param name
   * @return the named Group, or null if not found
   */
  public static Group findByName(Context context, String name) throws SQLException {
    TableRow row = DatabaseManager.findByUnique(context, "epersongroup", "name", name);

    if (row == null) {
      return null;
    } else {
      // First check the cache
      Group fromCache =
          (Group) context.fromCache(Group.class, row.getIntColumn("eperson_group_id"));

      if (fromCache != null) {
        return fromCache;
      } else {
        return new Group(context, row);
      }
    }
  }
Пример #9
0
  /**
   * Return the next item instantiated from the supplied TableRow
   *
   * @return the item or null if none
   * @throws SQLException
   */
  private Item nextByRow() throws SQLException {
    if (itemRows.hasNext()) {
      // Convert the row into an Item object
      TableRow row = itemRows.next();

      // Check cache
      Item fromCache = (Item) ourContext.fromCache(Item.class, row.getIntColumn("item_id"));

      if (fromCache != null) {
        return fromCache;
      } else {
        return new Item(ourContext, row);
      }
    } else {
      return null;
    }
  }
Пример #10
0
  /**
   * This private method knows how to get the next result out of the item id iterator
   *
   * @return the next item instantiated from the id
   * @throws SQLException
   */
  private Item nextByID() throws SQLException {
    if (iditr.hasNext()) {
      // get the id
      int id = ((Integer) iditr.next()).intValue();

      // Check cache
      Item fromCache = (Item) ourContext.fromCache(Item.class, id);

      if (fromCache != null) {
        return fromCache;
      } else {
        return Item.find(ourContext, id);
      }
    } else {
      return null;
    }
  }
  /**
   * Get all workflow items that were original submissions by a particular e-person. These are
   * ordered by workflow ID, since this should likely keep them in the order in which they were
   * created.
   *
   * @param context the context object
   * @param ep the eperson
   * @return the corresponding workflow items
   */
  public static WorkflowItem[] findByEPerson(Context context, EPerson ep) throws SQLException {
    List wfItems = new ArrayList();

    TableRowIterator tri =
        DatabaseManager.queryTable(
            context,
            "workflowitem",
            "SELECT workflowitem.* FROM workflowitem, item WHERE "
                + "workflowitem.item_id=item.item_id AND "
                + "item.submitter_id= ? "
                + "ORDER BY workflowitem.workflow_id",
            ep.getID());

    try {
      while (tri.hasNext()) {
        TableRow row = tri.next();

        // Check the cache
        WorkflowItem wi =
            (WorkflowItem) context.fromCache(WorkflowItem.class, row.getIntColumn("workflow_id"));

        if (wi == null) {
          wi = new WorkflowItem(context, row);
        }

        wfItems.add(wi);
      }
    } finally {
      if (tri != null) tri.close();
    }

    WorkflowItem[] wfArray = new WorkflowItem[wfItems.size()];
    wfArray = (WorkflowItem[]) wfItems.toArray(wfArray);

    return wfArray;
  }
Пример #12
0
  /**
   * Return a list of policies for an object that match the action
   *
   * @param c context
   * @param o DSpaceObject policies relate to
   * @param actionID action (defined in class Constants)
   * @throws SQLException if there's a database problem
   */
  public static List<ResourcePolicy> getPoliciesActionFilter(
      Context c, DSpaceObject o, int actionID) throws SQLException {
    TableRowIterator tri =
        DatabaseManager.queryTable(
            c,
            "resourcepolicy",
            "SELECT * FROM resourcepolicy WHERE resource_type_id= ? "
                + "AND resource_id= ? AND action_id= ? ",
            o.getType(),
            o.getID(),
            actionID);

    List<ResourcePolicy> policies = new ArrayList<ResourcePolicy>();

    try {
      while (tri.hasNext()) {
        TableRow row = tri.next();

        // first check the cache (FIXME: is this right?)
        ResourcePolicy cachepolicy =
            (ResourcePolicy) c.fromCache(ResourcePolicy.class, row.getIntColumn("policy_id"));

        if (cachepolicy != null) {
          policies.add(cachepolicy);
        } else {
          policies.add(new ResourcePolicy(c, row));
        }
      }
    } finally {
      if (tri != null) {
        tri.close();
      }
    }

    return policies;
  }
Пример #13
0
  /**
   * Populate Group with eperson and group objects
   *
   * @throws SQLException
   */
  public void loadData() {
    // only populate if not already populated
    if (!isDataLoaded) {
      // naughty thing to do - swallowing SQL exception and throwing it as
      // a RuntimeException - a hack to avoid changing the API all over
      // the place
      try {
        // get epeople objects
        TableRowIterator tri =
            DatabaseManager.queryTable(
                myContext,
                "eperson",
                "SELECT eperson.* FROM eperson, epersongroup2eperson WHERE "
                    + "epersongroup2eperson.eperson_id=eperson.eperson_id AND "
                    + "epersongroup2eperson.eperson_group_id= ?",
                myRow.getIntColumn("eperson_group_id"));

        try {
          while (tri.hasNext()) {
            TableRow r = (TableRow) tri.next();

            // First check the cache
            EPerson fromCache =
                (EPerson) myContext.fromCache(EPerson.class, r.getIntColumn("eperson_id"));

            if (fromCache != null) {
              epeople.add(fromCache);
            } else {
              epeople.add(new EPerson(myContext, r));
            }
          }
        } finally {
          // close the TableRowIterator to free up resources
          if (tri != null) {
            tri.close();
          }
        }

        // now get Group objects
        tri =
            DatabaseManager.queryTable(
                myContext,
                "epersongroup",
                "SELECT epersongroup.* FROM epersongroup, group2group WHERE "
                    + "group2group.child_id=epersongroup.eperson_group_id AND "
                    + "group2group.parent_id= ? ",
                myRow.getIntColumn("eperson_group_id"));

        try {
          while (tri.hasNext()) {
            TableRow r = (TableRow) tri.next();

            // First check the cache
            Group fromCache =
                (Group) myContext.fromCache(Group.class, r.getIntColumn("eperson_group_id"));

            if (fromCache != null) {
              groups.add(fromCache);
            } else {
              groups.add(new Group(myContext, r));
            }
          }
        } finally {
          // close the TableRowIterator to free up resources
          if (tri != null) {
            tri.close();
          }
        }

      } catch (Exception e) {
        throw new IllegalStateException(e);
      }
      isDataLoaded = true;
    }
  }
Пример #14
0
  /**
   * Find the groups that match the search query across eperson_group_id or name
   *
   * @param context DSpace context
   * @param query The search string
   * @param offset Inclusive offset
   * @param limit Maximum number of matches returned
   * @return array of Group objects
   */
  public static Group[] search(Context context, String query, int offset, int limit)
      throws SQLException {
    String params = "%" + query.toLowerCase() + "%";
    StringBuffer queryBuf = new StringBuffer();
    queryBuf.append(
        "SELECT * FROM epersongroup WHERE LOWER(name) LIKE LOWER(?) OR eperson_group_id = ? ORDER BY name ASC ");

    // Add offset and limit restrictions - Oracle requires special code
    if ("oracle".equals(ConfigurationManager.getProperty("db.name"))) {
      // First prepare the query to generate row numbers
      if (limit > 0 || offset > 0) {
        queryBuf.insert(0, "SELECT /*+ FIRST_ROWS(n) */ rec.*, ROWNUM rnum  FROM (");
        queryBuf.append(") ");
      }

      // Restrict the number of rows returned based on the limit
      if (limit > 0) {
        queryBuf.append("rec WHERE rownum<=? ");
        // If we also have an offset, then convert the limit into the maximum row number
        if (offset > 0) {
          limit += offset;
        }
      }

      // Return only the records after the specified offset (row number)
      if (offset > 0) {
        queryBuf.insert(0, "SELECT * FROM (");
        queryBuf.append(") WHERE rnum>?");
      }
    } else {
      if (limit > 0) {
        queryBuf.append(" LIMIT ? ");
      }

      if (offset > 0) {
        queryBuf.append(" OFFSET ? ");
      }
    }

    String dbquery = queryBuf.toString();

    // When checking against the eperson-id, make sure the query can be made into a number
    Integer int_param;
    try {
      int_param = Integer.valueOf(query);
    } catch (NumberFormatException e) {
      int_param = Integer.valueOf(-1);
    }

    // Create the parameter array, including limit and offset if part of the query
    Object[] paramArr = new Object[] {params, int_param};
    if (limit > 0 && offset > 0) {
      paramArr = new Object[] {params, int_param, limit, offset};
    } else if (limit > 0) {
      paramArr = new Object[] {params, int_param, limit};
    } else if (offset > 0) {
      paramArr = new Object[] {params, int_param, offset};
    }

    TableRowIterator rows = DatabaseManager.query(context, dbquery, paramArr);

    try {
      List<TableRow> groupRows = rows.toList();
      Group[] groups = new Group[groupRows.size()];

      for (int i = 0; i < groupRows.size(); i++) {
        TableRow row = groupRows.get(i);

        // First check the cache
        Group fromCache =
            (Group) context.fromCache(Group.class, row.getIntColumn("eperson_group_id"));

        if (fromCache != null) {
          groups[i] = fromCache;
        } else {
          groups[i] = new Group(context, row);
        }
      }
      return groups;
    } finally {
      if (rows != null) {
        rows.close();
      }
    }
  }