1 40 package org.dspace.authorize; 41 42 import java.sql.SQLException ; 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 60 public class PolicySet 61 { 62 65 public static void main(String [] argv) throws Exception 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 command = argv[5]; 84 85 if (command.equals("REPLACE")) 86 { 87 isReplace = true; 88 } 89 90 Context c = new Context(); 91 92 c.setIgnoreAuthorization(true); 94 95 setPolicies(c, containertype, containerID, contenttype, actionID, 99 groupID, isReplace, false); 100 101 c.complete(); 102 } 103 104 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 , 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 while (i.hasNext()) 147 { 148 Item myitem = i.next(); 149 150 if (isReplace || clearOnly) 152 { 153 AuthorizeManager.removeAllPolicies(c, myitem); 154 } 155 156 if (!clearOnly) 157 { 158 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 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]; 183 if (isReplace || clearOnly) 185 { 186 AuthorizeManager.removeAllPolicies(c, t); 187 } 188 189 if (!clearOnly) 190 { 191 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 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]; 224 if (isReplace || clearOnly) 226 { 227 AuthorizeManager.removeAllPolicies(c, t); 228 } 229 230 if (!clearOnly) 231 { 232 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 |