KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FixDefaultPolicies.java
3  *
4  * Version: $Revision: 1.7 $
5  *
6  * Date: $Date: 2005/04/20 14:23:21 $
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 org.dspace.browse.Browse;
43
import java.sql.SQLException JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46
47 import org.dspace.content.Collection;
48 import org.dspace.content.Community;
49 import org.dspace.content.DSpaceObject;
50 import org.dspace.core.Constants;
51 import org.dspace.core.Context;
52 import org.dspace.eperson.Group;
53
54 /**
55  * Command line tool to locate collections without default item and bitstream
56  * read policies, and assign them some. (They must be there for submitted items
57  * to inherit.)
58  *
59  * @author dstuve
60  * @version $Revision: 1.7 $
61  */

62 public class FixDefaultPolicies
63 {
64     /**
65      * Command line interface to setPolicies - run to see arguments
66      */

67     public static void main(String JavaDoc[] argv) throws Exception JavaDoc
68     {
69         Context c = new Context();
70
71         // turn off authorization
72
c.setIgnoreAuthorization(true);
73
74         //////////////////////
75
// carnage begins here
76
//////////////////////
77
Collection[] collections = Collection.findAll(c);
78
79         for (int i = 0; i < collections.length; i++)
80         {
81             Collection t = collections[i];
82
83             System.out.println("Collection " + t + " " + t.getMetadata("name"));
84
85             // check for READ
86
if (checkForPolicy(c, t, Constants.READ))
87             {
88                 System.out.println("\tFound READ policies!");
89             }
90             else
91             {
92                 System.out.println("\tNo READ policy found, adding anonymous.");
93                 addAnonymousPolicy(c, t, Constants.READ);
94             }
95
96             if (checkForPolicy(c, t, Constants.DEFAULT_ITEM_READ))
97             {
98                 System.out.println("\tFound DEFAULT_ITEM_READ policies!");
99             }
100             else
101             {
102                 System.out
103                         .println("\tNo DEFAULT_ITEM_READ policy found, adding anonymous.");
104                 addAnonymousPolicy(c, t, Constants.DEFAULT_ITEM_READ);
105             }
106
107             if (checkForPolicy(c, t, Constants.DEFAULT_BITSTREAM_READ))
108             {
109                 System.out.println("\tFound DEFAULT_BITSTREAM_READ policies!");
110             }
111             else
112             {
113                 System.out
114                         .println("\tNo DEFAULT_BITSTREAM_READ policy found, adding anonymous.");
115                 addAnonymousPolicy(c, t, Constants.DEFAULT_BITSTREAM_READ);
116             }
117         }
118
119         // now ensure communities have READ policies
120
Community[] communities = Community.findAll(c);
121
122         for (int i = 0; i < communities.length; i++)
123         {
124             Community t = communities[i];
125
126             System.out.println("Community " + t + " " + t.getMetadata("name"));
127
128             // check for READ
129
if (checkForPolicy(c, t, Constants.READ))
130             {
131                 System.out.println("\tFound READ policies!");
132             }
133             else
134             {
135                 System.out.println("\tNo READ policy found, adding anonymous.");
136                 addAnonymousPolicy(c, t, Constants.READ);
137             }
138         }
139
140         c.complete();
141     }
142
143     /**
144      * check to see if a collection has any policies for a given action
145      */

146     private static boolean checkForPolicy(Context c, DSpaceObject t,
147             int myaction) throws SQLException JavaDoc
148     {
149         // check to see if any policies exist for this action
150
List JavaDoc policies = AuthorizeManager
151                 .getPoliciesActionFilter(c, t, myaction);
152         Iterator JavaDoc i = policies.iterator();
153
154         return i.hasNext();
155     }
156
157     /**
158      * add an anonymous group permission policy to the collection for this
159      * action
160      */

161     private static void addAnonymousPolicy(Context c, DSpaceObject t,
162             int myaction) throws SQLException JavaDoc, AuthorizeException
163     {
164         // group 0 is the anonymous group!
165
Group anonymousGroup = Group.find(c, 0);
166
167         // now create the default policies for submitted items
168
ResourcePolicy myPolicy = ResourcePolicy.create(c);
169         myPolicy.setResource(t);
170         myPolicy.setAction(myaction);
171         myPolicy.setGroup(anonymousGroup);
172         myPolicy.update();
173     }
174 }
175
Popular Tags