📄 collection.java
字号:
*/ public void removeItem(Item item) throws SQLException, AuthorizeException, IOException { // Check authorisation AuthorizeManager.authorizeAction(ourContext, this, Constants.REMOVE); log.info(LogManager.getHeader(ourContext, "remove_item", "collection_id=" + getID() + ",item_id=" + item.getID())); DatabaseManager.updateQuery(ourContext, "DELETE FROM collection2item WHERE collection_id=" + getID() + " AND item_id=" + item.getID()); // Is the item an orphan? TableRowIterator tri = DatabaseManager.query(ourContext, "SELECT * FROM collection2item WHERE item_id=" + item.getID()); if (!tri.hasNext()) { //make the right to remove the item explicit because the implicit // relation //has been removed. This only has to concern the currentUser // because //he started the removal process and he will end it too. //also add right to remove from the item to remove it's bundles. AuthorizeManager.addPolicy(ourContext, item, Constants.DELETE, ourContext.getCurrentUser()); AuthorizeManager.addPolicy(ourContext, item, Constants.REMOVE, ourContext.getCurrentUser()); // Orphan; delete it item.delete(); } // close the TableRowIterator to free up resources tri.close(); } /** * Update the collection metadata (including logo, and workflow groups) to * the database. Inserts if this is a new collection. * * @throws SQLException * @throws IOException * @throws AuthorizeException */ public void update() throws SQLException, IOException, AuthorizeException { // Check authorisation canEdit(); HistoryManager.saveHistory(ourContext, this, HistoryManager.MODIFY, ourContext.getCurrentUser(), ourContext.getExtraLogInfo()); log.info(LogManager.getHeader(ourContext, "update_collection", "collection_id=" + getID())); DatabaseManager.update(ourContext, collectionRow); // reindex this collection (could be smarter, to only do when name // changes) DSIndexer.reIndexContent(ourContext, this); } public boolean canEditBoolean() throws java.sql.SQLException { try { canEdit(); return true; } catch (AuthorizeException e) { return false; } } public void canEdit() throws AuthorizeException, SQLException { Community[] parents = getCommunities(); for (int i = 0; i < parents.length; i++) { if (AuthorizeManager.authorizeActionBoolean(ourContext, parents[i], Constants.WRITE)) { return; } if (AuthorizeManager.authorizeActionBoolean(ourContext, parents[i], Constants.ADD)) { return; } } AuthorizeManager.authorizeAnyOf(ourContext, this, new int[] { Constants.WRITE, Constants.COLLECTION_ADMIN }); } /** * Delete the collection, including the metadata and logo. Items that are * then orphans are deleted. Groups associated with this collection * (workflow participants and submitters) are NOT deleted. * * @throws SQLException * @throws AuthorizeException * @throws IOException */ void delete() throws SQLException, AuthorizeException, IOException { log.info(LogManager.getHeader(ourContext, "delete_collection", "collection_id=" + getID())); // remove from index DSIndexer.unIndexContent(ourContext, this); // Remove from cache ourContext.removeCached(this, getID()); HistoryManager.saveHistory(ourContext, this, HistoryManager.REMOVE, ourContext.getCurrentUser(), ourContext.getExtraLogInfo()); // remove subscriptions - hmm, should this be in Subscription.java? DatabaseManager.updateQuery(ourContext, "DELETE FROM subscription WHERE " + "collection_id=" + getID()); // Remove items ItemIterator items = getItems(); while (items.hasNext()) { removeItem(items.next()); } // Delete bitstream logo setLogo(null); // Remove all authorization policies AuthorizeManager.removeAllPolicies(ourContext, this); // Remove any WorkflowItems WorkflowItem[] wfarray = WorkflowItem .findByCollection(ourContext, this); for (int x = 0; x < wfarray.length; x++) { // remove the workflowitem first, then the item Item myItem = wfarray[x].getItem(); wfarray[x].deleteWrapper(); myItem.delete(); } // Remove any WorkspaceItems WorkspaceItem[] wsarray = WorkspaceItem.findByCollection(ourContext, this); for (int x = 0; x < wsarray.length; x++) { wsarray[x].deleteAll(); } // Delete collection row DatabaseManager.delete(ourContext, collectionRow); // Remove any workflow groups - must happen after deleting collection Group g = null; g = getWorkflowGroup(1); if (g != null) { g.delete(); } g = getWorkflowGroup(2); if (g != null) { g.delete(); } g = getWorkflowGroup(3); if (g != null) { g.delete(); } // Remove default administrators group g = getAdministrators(); if (g != null) { g.delete(); } // Remove default submitters group g = getSubmitters(); if (g != null) { g.delete(); } } /** * Get the communities this collection appears in * * @return array of <code>Community</code> objects * @throws SQLException */ public Community[] getCommunities() throws SQLException { // Get the bundle table rows TableRowIterator tri = DatabaseManager .query( ourContext, "community", "SELECT community.* FROM community, community2collection WHERE " + "community.community_id=community2collection.community_id " + "AND community2collection.collection_id=" + getID()); // Build a list of Community objects List communities = new ArrayList(); while (tri.hasNext()) { TableRow row = tri.next(); // First check the cache Community owner = (Community) ourContext.fromCache(Community.class, row.getIntColumn("community_id")); if (owner == null) { owner = new Community(ourContext, row); } communities.add(owner); // now add any parent communities Community[] parents = owner.getAllParents(); for (int i = 0; i < parents.length; i++) { communities.add(parents[i]); } } // close the TableRowIterator to free up resources tri.close(); Community[] communityArray = new Community[communities.size()]; communityArray = (Community[]) communities.toArray(communityArray); return communityArray; } /** * Return <code>true</code> if <code>other</code> is the same Collection * as this object, <code>false</code> otherwise * * @param other * object to compare to * * @return <code>true</code> if object passed in represents the same * collection as this object */ public boolean equals(Object other) { if (!(other instanceof Collection)) { return false; } return (getID() == ((Collection) other).getID()); } /** * Utility method for reading in a group from a group ID in a column. If the * column is null, null is returned. * * @param col * the column name to read * @return the group referred to by that column, or null * @throws SQLException */ private Group groupFromColumn(String col) throws SQLException { if (collectionRow.isColumnNull(col)) { return null; } return Group.find(ourContext, collectionRow.getIntColumn(col)); } /** * return type found in Constants * * @return int Constants.COLLECTION */ public int getType() { return Constants.COLLECTION; } /** * return an array of collections that user has a given permission on * (useful for trimming 'select to collection' list) or figuring out which * collections a person is an editor for. * * @param context * @param comm * (optional) restrict search to a community, else null * @param actionID * fo the action * * @return Collection [] of collections with matching permissions * @throws SQLException */ public static Collection[] findAuthorized(Context context, Community comm, int actionID) throws java.sql.SQLException { List myResults = new ArrayList(); Collection[] myCollections = null; if (comm != null) { myCollections = comm.getCollections(); } else { myCollections = Collection.findAll(context); } // now build a list of collections you have authorization for for (int i = 0; i < myCollections.length; i++) { if (AuthorizeManager.authorizeActionBoolean(context, myCollections[i], actionID)) { myResults.add(myCollections[i]); } } myCollections = new Collection[myResults.size()]; myCollections = (Collection[]) myResults.toArray(myCollections); return myCollections; } /** * counts items in this collection * * @return total items */ public int countItems() throws SQLException { Statement statement = ourContext.getDBConnection().createStatement(); final ResultSet rs; rs = statement .executeQuery("SELECT count(*) FROM collection2item, item WHERE " + "collection2item.collection_id = " + getID() + " AND collection2item.item_id = item.item_id " + "AND in_archive ='1' AND item.withdrawn='0'"); int itemcount = 0; rs.next(); itemcount = rs.getInt(1); statement.close(); return itemcount; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -