001: /*
002: * Supervisor.java
003: *
004: * Version: $Revision: 1523 $
005: *
006: * Date: $Date: 2006-05-26 09:23:17 -0500 (Fri, 26 May 2006) $
007: *
008: * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
009: * Institute of Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are
013: * met:
014: *
015: * - Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * - Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in the
020: * documentation and/or other materials provided with the distribution.
021: *
022: * - Neither the name of the Hewlett-Packard Company nor the name of the
023: * Massachusetts Institute of Technology nor the names of their
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038: * DAMAGE.
039: */
040:
041: package org.dspace.eperson;
042:
043: import java.lang.StringBuffer;
044:
045: import java.sql.SQLException;
046:
047: import org.apache.log4j.Logger;
048:
049: import org.dspace.authorize.AuthorizeException;
050: import org.dspace.authorize.ResourcePolicy;
051: import org.dspace.content.Bitstream;
052: import org.dspace.content.Bundle;
053: import org.dspace.content.Item;
054: import org.dspace.content.WorkspaceItem;
055: import org.dspace.core.Constants;
056: import org.dspace.core.Context;
057: import org.dspace.core.LogManager;
058: import org.dspace.eperson.Group;
059: import org.dspace.storage.rdbms.TableRow;
060: import org.dspace.storage.rdbms.TableRowIterator;
061: import org.dspace.storage.rdbms.DatabaseManager;
062:
063: /**
064: * Class to represent the supervisor, primarily for use in applying supervisor
065: * activities to the database, such as setting and unsetting supervision
066: * orders and so forth.
067: *
068: * @author Richard Jones
069: * @version $Revision: 1523 $
070: */
071: public class Supervisor {
072:
073: /** log4j category */
074: private static Logger log = Logger.getLogger(Supervisor.class);
075:
076: /** value to use for no policy set */
077: public static int POLICY_NONE = 0;
078:
079: /** value to use for editor policies */
080: public static int POLICY_EDITOR = 1;
081:
082: /** value to use for observer policies */
083: public static int POLICY_OBSERVER = 2;
084:
085: /** Creates a new instance of Supervisor */
086: private Supervisor() {
087: }
088:
089: /**
090: * finds out if there is a supervision order that matches this set
091: * of values
092: *
093: * @param context the context this object exists in
094: * @param wsItemID the workspace item to be supervised
095: * @param groupID the group to be doing the supervising
096: *
097: * @return boolean true if there is an order that matches, false if not
098: */
099: public static boolean isOrder(Context context, int wsItemID,
100: int groupID) throws SQLException {
101: String query = "SELECT epersongroup2workspaceitem.* "
102: + "FROM epersongroup2workspaceitem "
103: + "WHERE epersongroup2workspaceitem.eperson_group_id = ? "
104: + "AND epersongroup2workspaceitem.workspace_item_id = ? ";
105:
106: TableRowIterator tri = DatabaseManager.queryTable(context,
107: "epersongroup2workspaceitem", query, groupID, wsItemID);
108:
109: boolean result = tri.hasNext();
110: tri.close();
111: return result;
112: }
113:
114: /**
115: * removes the requested group from the requested workspace item in terms
116: * of supervision. This also removes all the policies that group has
117: * associated with the item
118: *
119: * @param context the context this object exists in
120: * @param wsItemID the ID of the workspace item
121: * @param groupID the ID of the group to be removed from the item
122: */
123: public static void remove(Context context, int wsItemID, int groupID)
124: throws SQLException, AuthorizeException {
125: // get the workspace item and the group from the request values
126: WorkspaceItem wsItem = WorkspaceItem.find(context, wsItemID);
127: Group group = Group.find(context, groupID);
128:
129: // remove the link from the supervisory database
130: String query = "DELETE FROM epersongroup2workspaceitem "
131: + "WHERE workspace_item_id = ? "
132: + "AND eperson_group_id = ? ";
133:
134: DatabaseManager.updateQuery(context, query, wsItemID, groupID);
135:
136: // get the item and have it remove the policies for the group
137: Item item = wsItem.getItem();
138: item.removeGroupPolicies(group);
139: }
140:
141: /**
142: * removes redundant entries in the supervision orders database
143: *
144: * @param context the context this object exists in
145: */
146: public static void removeRedundant(Context context)
147: throws SQLException {
148: // this horrid looking query tests to see if there are any groups or
149: // workspace items which match up to the ones in the linking database
150: // table. If there aren't, we know that the link is out of date, and
151: // it can be deleted.
152: String query = "DELETE FROM epersongroup2workspaceitem "
153: + "WHERE NOT EXISTS ( "
154: + "SELECT 1 FROM workspaceitem WHERE workspace_item_id "
155: + "= epersongroup2workspaceitem.workspace_item_id "
156: + ") OR NOT EXISTS ( "
157: + "SELECT 1 FROM epersongroup WHERE eperson_group_id "
158: + "= epersongroup2workspaceitem.eperson_group_id "
159: + ")";
160:
161: DatabaseManager.updateQuery(context, query);
162: }
163:
164: /**
165: * adds a supervision order to the database
166: *
167: * @param context the context this object exists in
168: * @param groupID the ID of the group which will supervise
169: * @param wsItemID the ID of the workspace item to be supervised
170: * @param policy String containing the policy type to be used
171: */
172: public static void add(Context context, int groupID, int wsItemID,
173: int policy) throws SQLException, AuthorizeException {
174: // make a table row in the database table, and update with the relevant
175: // details
176: TableRow row = DatabaseManager.create(context,
177: "epersongroup2workspaceitem");
178: row.setColumn("workspace_item_id", wsItemID);
179: row.setColumn("eperson_group_id", groupID);
180: DatabaseManager.update(context, row);
181:
182: // If a default policy type has been requested, apply the policies using
183: // the DSpace API for doing so
184: if (policy != POLICY_NONE) {
185: WorkspaceItem wsItem = WorkspaceItem
186: .find(context, wsItemID);
187: Item item = wsItem.getItem();
188: Group group = Group.find(context, groupID);
189:
190: // "Editor" implies READ, WRITE, ADD permissions
191: // "Observer" implies READ permissions
192: if (policy == POLICY_EDITOR) {
193: ResourcePolicy r = ResourcePolicy.create(context);
194: r.setResource(item);
195: r.setGroup(group);
196: r.setAction(Constants.READ);
197: r.update();
198:
199: r = ResourcePolicy.create(context);
200: r.setResource(item);
201: r.setGroup(group);
202: r.setAction(Constants.WRITE);
203: r.update();
204:
205: r = ResourcePolicy.create(context);
206: r.setResource(item);
207: r.setGroup(group);
208: r.setAction(Constants.ADD);
209: r.update();
210:
211: } else if (policy == POLICY_OBSERVER) {
212: ResourcePolicy r = ResourcePolicy.create(context);
213: r.setResource(item);
214: r.setGroup(group);
215: r.setAction(Constants.READ);
216: r.update();
217: }
218: }
219: }
220:
221: }
|