KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > authorize > PolicySet


1 /*
2  * PolicySet.java
3  *
4  * Version: $Revision: 1.12 $
5  *
6  * Date: $Date: 2005/07/29 15:56:08 $
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 package org.dspace.authorize;
41
42 import java.sql.SQLException JavaDoc;
43
44 import org.dspace.content.Bitstream;
45 import org.dspace.content.Bundle;
46 import org.dspace.content.Collection;
47 import org.dspace.content.Item;
48 import org.dspace.content.ItemIterator;
49 import org.dspace.core.Constants;
50 import org.dspace.core.Context;
51 import org.dspace.eperson.Group;
52
53 /**
54  * Was Hack/Tool to set policies for items, bundles, and bitstreams. Now has
55  * helpful method, setPolicies();
56  *
57  * @author dstuve
58  * @version $Revision: 1.12 $
59  */

60 public class PolicySet
61 {
62     /**
63      * Command line interface to setPolicies - run to see arguments
64      */

65     public static void main(String JavaDoc[] argv) throws Exception JavaDoc
66     {
67         if (argv.length < 6)
68         {
69             System.out
70                     .println("Args: containerType containerID contentType actionID groupID command");
71             System.out.println("container=COLLECTION command = ADD|REPLACE");
72
73             return;
74         }
75
76         int containertype = Integer.parseInt(argv[0]);
77         int containerID = Integer.parseInt(argv[1]);
78         int contenttype = Integer.parseInt(argv[2]);
79         int actionID = Integer.parseInt(argv[3]);
80         int groupID = Integer.parseInt(argv[4]);
81
82         boolean isReplace = false;
83         String JavaDoc command = argv[5];
84
85         if (command.equals("REPLACE"))
86         {
87             isReplace = true;
88         }
89
90         Context c = new Context();
91
92         // turn off authorization
93
c.setIgnoreAuthorization(true);
94
95         //////////////////////
96
// carnage begins here
97
//////////////////////
98
setPolicies(c, containertype, containerID, contenttype, actionID,
99                 groupID, isReplace, false);
100
101         c.complete();
102     }
103
104     /**
105      * Useful policy wildcard tool. Can set entire collections' contents'
106      * policies
107      *
108      * @param c
109      * current context
110      * @param containerType
111      * type, Constants.ITEM or Constants.COLLECTION
112      * @param containerID
113      * ID of container (DB primary key)
114      * @param contentType
115      * type (BUNDLE, ITEM, or BITSTREAM)
116      * @param actionID
117      * action ID
118      * @param groupID
119      * group ID (database key)
120      * @param isReplace
121      * if <code>true</code>, existing policies are removed first,
122      * otherwise add to existing policies
123      * @param clearOnly
124      * if <code>true</code>, just delete policies for matching
125      * objects
126      * @throws SQLException
127      * if database problem
128      * @throws AuthorizeException
129      * if current user is not authorized to change these policies
130      */

131     public static void setPolicies(Context c, int containerType,
132             int containerID, int contentType, int actionID, int groupID,
133             boolean isReplace, boolean clearOnly) throws SQLException JavaDoc,
134             AuthorizeException
135     {
136         if (containerType == Constants.COLLECTION)
137         {
138             Collection collection = Collection.find(c, containerID);
139             Group group = Group.find(c, groupID);
140
141             ItemIterator i = collection.getItems();
142
143             if (contentType == Constants.ITEM)
144             {
145                 // build list of all items in a collection
146
while (i.hasNext())
147                 {
148                     Item myitem = i.next();
149
150                     // is this a replace? delete policies first
151
if (isReplace || clearOnly)
152                     {
153                         AuthorizeManager.removeAllPolicies(c, myitem);
154                     }
155
156                     if (!clearOnly)
157                     {
158                         // now add the policy
159
ResourcePolicy rp = ResourcePolicy.create(c);
160
161                         rp.setResource(myitem);
162                         rp.setAction(actionID);
163                         rp.setGroup(group);
164
165                         rp.update();
166                     }
167                 }
168             }
169             else if (contentType == Constants.BUNDLE)
170             {
171                 // build list of all items in a collection
172
// build list of all bundles in those items
173
while (i.hasNext())
174                 {
175                     Item myitem = i.next();
176
177                     Bundle[] bundles = myitem.getBundles();
178
179                     for (int j = 0; j < bundles.length; j++)
180                     {
181                         Bundle t = bundles[j]; // t for target
182

183                         // is this a replace? delete policies first
184
if (isReplace || clearOnly)
185                         {
186                             AuthorizeManager.removeAllPolicies(c, t);
187                         }
188
189                         if (!clearOnly)
190                         {
191                             // now add the policy
192
ResourcePolicy rp = ResourcePolicy.create(c);
193
194                             rp.setResource(t);
195                             rp.setAction(actionID);
196                             rp.setGroup(group);
197
198                             rp.update();
199                         }
200                     }
201                 }
202             }
203             else if (contentType == Constants.BITSTREAM)
204             {
205                 // build list of all bitstreams in a collection
206
// iterate over items, bundles, get bitstreams
207
while (i.hasNext())
208                 {
209                     Item myitem = i.next();
210                     System.out.println("Item " + myitem.getID());
211
212                     Bundle[] bundles = myitem.getBundles();
213
214                     for (int j = 0; j < bundles.length; j++)
215                     {
216                         System.out.println("Bundle " + bundles[j].getID());
217
218                         Bitstream[] bitstreams = bundles[j].getBitstreams();
219
220                         for (int k = 0; k < bitstreams.length; k++)
221                         {
222                             Bitstream t = bitstreams[k]; // t for target
223

224                             // is this a replace? delete policies first
225
if (isReplace || clearOnly)
226                             {
227                                 AuthorizeManager.removeAllPolicies(c, t);
228                             }
229
230                             if (!clearOnly)
231                             {
232                                 // now add the policy
233
ResourcePolicy rp = ResourcePolicy.create(c);
234
235                                 rp.setResource(t);
236                                 rp.setAction(actionID);
237                                 rp.setGroup(group);
238
239                                 rp.update();
240                             }
241                         }
242                     }
243                 }
244             }
245         }
246     }
247 }
248
Popular Tags