KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > eperson > Supervisor


1 /*
2  * Supervisor.java
3  *
4  * Version: $Revision: 1.5 $
5  *
6  * Date: $Date: 2006/05/26 14:18:16 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40
41 package org.dspace.eperson;
42
43 import java.lang.StringBuffer JavaDoc;
44
45 import java.sql.SQLException JavaDoc;
46
47 import org.apache.log4j.Logger;
48
49 import org.dspace.authorize.AuthorizeException;
50 import org.dspace.authorize.ResourcePolicy;
51 import org.dspace.content.Bitstream;
52 import org.dspace.content.Bundle;
53 import org.dspace.content.Item;
54 import org.dspace.content.WorkspaceItem;
55 import org.dspace.core.Constants;
56 import org.dspace.core.Context;
57 import org.dspace.core.LogManager;
58 import org.dspace.eperson.Group;
59 import org.dspace.storage.rdbms.TableRow;
60 import org.dspace.storage.rdbms.TableRowIterator;
61 import org.dspace.storage.rdbms.DatabaseManager;
62
63 /**
64  * Class to represent the supervisor, primarily for use in applying supervisor
65  * activities to the database, such as setting and unsetting supervision
66  * orders and so forth.
67  *
68  * @author Richard Jones
69  * @version $Revision: 1.5 $
70  */

71 public class Supervisor {
72     
73     /** log4j category */
74     private static Logger log = Logger.getLogger(Supervisor.class);
75     
76     /** value to use for no policy set */
77     public static int POLICY_NONE = 0;
78     
79     /** value to use for editor policies */
80     public static int POLICY_EDITOR = 1;
81     
82     /** value to use for observer policies */
83     public static int POLICY_OBSERVER = 2;
84     
85     /** Creates a new instance of Supervisor */
86     private Supervisor()
87     {
88     }
89     
90     /**
91      * finds out if there is a supervision order that matches this set
92      * of values
93      *
94      * @param context the context this object exists in
95      * @param wsItemID the workspace item to be supervised
96      * @param groupID the group to be doing the supervising
97      *
98      * @return boolean true if there is an order that matches, false if not
99      */

100     public static boolean isOrder(Context context, int wsItemID, int groupID)
101         throws SQLException JavaDoc
102     {
103         String JavaDoc query = "SELECT epersongroup2workspaceitem.* " +
104                        "FROM epersongroup2workspaceitem " +
105                        "WHERE epersongroup2workspaceitem.eperson_group_id = ? " +
106                        "AND epersongroup2workspaceitem.workspace_item_id = ? ";
107         
108         TableRowIterator tri = DatabaseManager.queryTable(context,
109                                     "epersongroup2workspaceitem",
110                                     query,groupID,wsItemID);
111         
112         boolean result = tri.hasNext();
113         tri.close();
114         return result;
115     }
116     
117     /**
118      * removes the requested group from the requested workspace item in terms
119      * of supervision. This also removes all the policies that group has
120      * associated with the item
121      *
122      * @param context the context this object exists in
123      * @param wsItemID the ID of the workspace item
124      * @param groupID the ID of the group to be removed from the item
125      */

126     public static void remove(Context context, int wsItemID, int groupID)
127         throws SQLException JavaDoc, AuthorizeException
128     {
129         // get the workspace item and the group from the request values
130
WorkspaceItem wsItem = WorkspaceItem.find(context, wsItemID);
131         Group group = Group.find(context, groupID);
132         
133         // remove the link from the supervisory database
134
String JavaDoc query = "DELETE FROM epersongroup2workspaceitem " +
135                        "WHERE workspace_item_id = ? "+
136                        "AND eperson_group_id = ? ";
137         
138         DatabaseManager.updateQuery(context, query, wsItemID, groupID);
139         
140         // get the item and have it remove the policies for the group
141
Item item = wsItem.getItem();
142         item.removeGroupPolicies(group);
143     }
144     
145     /**
146      * removes redundant entries in the supervision orders database
147      *
148      * @param context the context this object exists in
149      */

150     public static void removeRedundant(Context context)
151         throws SQLException JavaDoc
152     {
153         // this horrid looking query tests to see if there are any groups or
154
// workspace items which match up to the ones in the linking database
155
// table. If there aren't, we know that the link is out of date, and
156
// it can be deleted.
157
String JavaDoc query = "DELETE FROM epersongroup2workspaceitem " +
158                        "WHERE NOT EXISTS ( " +
159                        "SELECT 1 FROM workspaceitem WHERE workspace_item_id " +
160                        "= epersongroup2workspaceitem.workspace_item_id " +
161                        ") OR NOT EXISTS ( " +
162                        "SELECT 1 FROM epersongroup WHERE eperson_group_id " +
163                        "= epersongroup2workspaceitem.eperson_group_id " +
164                        ")";
165         
166         DatabaseManager.updateQuery(context, query);
167     }
168     
169     /**
170      * adds a supervision order to the database
171      *
172      * @param context the context this object exists in
173      * @param groupID the ID of the group which will supervise
174      * @param wsItemID the ID of the workspace item to be supervised
175      * @param policy String containing the policy type to be used
176      */

177     public static void add(Context context, int groupID, int wsItemID, int policy)
178         throws SQLException JavaDoc, AuthorizeException
179     {
180         // make a table row in the database table, and update with the relevant
181
// details
182
TableRow row = DatabaseManager.create(context,
183                             "epersongroup2workspaceitem");
184         row.setColumn("workspace_item_id", wsItemID);
185         row.setColumn("eperson_group_id", groupID);
186         DatabaseManager.update(context,row);
187         
188         // If a default policy type has been requested, apply the policies using
189
// the DSpace API for doing so
190
if (policy != POLICY_NONE)
191         {
192             WorkspaceItem wsItem = WorkspaceItem.find(context, wsItemID);
193             Item item = wsItem.getItem();
194             Group group = Group.find(context, groupID);
195             
196             // "Editor" implies READ, WRITE, ADD permissions
197
// "Observer" implies READ permissions
198
if (policy == POLICY_EDITOR)
199             {
200                 ResourcePolicy r = ResourcePolicy.create(context);
201                 r.setResource(item);
202                 r.setGroup(group);
203                 r.setAction(Constants.READ);
204                 r.update();
205                 
206                 r = ResourcePolicy.create(context);
207                 r.setResource(item);
208                 r.setGroup(group);
209                 r.setAction(Constants.WRITE);
210                 r.update();
211                 
212                 r = ResourcePolicy.create(context);
213                 r.setResource(item);
214                 r.setGroup(group);
215                 r.setAction(Constants.ADD);
216                 r.update();
217                 
218             }
219             else if (policy == POLICY_OBSERVER)
220             {
221                 ResourcePolicy r = ResourcePolicy.create(context);
222                 r.setResource(item);
223                 r.setGroup(group);
224                 r.setAction(Constants.READ);
225                 r.update();
226             }
227         }
228     }
229     
230 }
231
Popular Tags