1 40 package org.dspace.administer; 41 42 import java.io.IOException ; 43 import java.sql.SQLException ; 44 45 import org.apache.commons.cli.CommandLine; 46 import org.apache.commons.cli.CommandLineParser; 47 import org.apache.commons.cli.HelpFormatter; 48 import org.apache.commons.cli.Options; 49 import org.apache.commons.cli.PosixParser; 50 import org.dspace.authorize.AuthorizeException; 51 import org.dspace.content.Community; 52 import org.dspace.core.Constants; 53 import org.dspace.core.Context; 54 import org.dspace.handle.HandleManager; 55 import org.dspace.storage.rdbms.DatabaseManager; 56 57 64 65 public class CommunityFiliator 66 { 67 public static void main(String [] argv) throws Exception  68 { 69 CommandLineParser parser = new PosixParser(); 71 72 Options options = new Options(); 73 74 options.addOption("s", "set", false, "set a parent/child relationship"); 75 options.addOption("r", "remove", false, 76 "remove a parent/child relationship"); 77 options.addOption("p", "parent", true, 78 "parent community (handle or database ID)"); 79 options.addOption("c", "child", true, 80 "child community (handle or databaseID)"); 81 options.addOption("h", "help", false, "help"); 82 83 CommandLine line = parser.parse(options, argv); 84 85 String command = null; String parentID = null; 87 String childID = null; 88 89 if (line.hasOption('h')) 90 { 91 HelpFormatter myhelp = new HelpFormatter(); 92 myhelp.printHelp("CommunityFiliator\n", options); 93 System.out 94 .println("\nestablish a relationship: CommunityFiliator -s -p parentID -c childID"); 95 System.out 96 .println("remove a relationship: CommunityFiliator -r -p parentID -c childID"); 97 98 System.exit(0); 99 } 100 101 if (line.hasOption('s')) 102 { 103 command = "set"; 104 } 105 106 if (line.hasOption('r')) 107 { 108 command = "remove"; 109 } 110 111 if (line.hasOption('p')) { 113 parentID = line.getOptionValue('p'); 114 } 115 116 if (line.hasOption('c')) { 118 childID = line.getOptionValue('c'); 119 } 120 121 if (command == null) 124 { 125 System.out 126 .println("Error - must run with either set or remove (run with -h flag for details)"); 127 System.exit(1); 128 } 129 130 if (command.equals("set") || command.equals("remove")) 131 { 132 if (parentID == null) 133 { 134 System.out 135 .println("Error - a parentID must be specified (run with -h flag for details)"); 136 System.exit(1); 137 } 138 139 if (childID == null) 140 { 141 System.out 142 .println("Error - a childID must be specified (run with -h flag for details)"); 143 System.exit(1); 144 } 145 } 146 147 CommunityFiliator filiator = new CommunityFiliator(); 148 Context c = new Context(); 149 150 c.setIgnoreAuthorization(true); 152 153 try 154 { 155 Community parent = filiator.resolveCommunity(c, parentID); 157 Community child = filiator.resolveCommunity(c, childID); 158 159 if (parent == null) 160 { 161 System.out.println("Error, parent community cannot be found: " 162 + parentID); 163 System.exit(1); 164 } 165 166 if (child == null) 167 { 168 System.out.println("Error, child community cannot be found: " 169 + childID); 170 System.exit(1); 171 } 172 173 if (command.equals("set")) 174 { 175 filiator.filiate(c, parent, child); 176 } 177 else 178 { 179 filiator.defiliate(c, parent, child); 180 } 181 } 182 catch (SQLException sqlE) 183 { 184 System.out.println("Error - SQL exception: " + sqlE.toString()); 185 } 186 catch (AuthorizeException authE) 187 { 188 System.out.println("Error - Authorize exception: " 189 + authE.toString()); 190 } 191 catch (IOException ioE) 192 { 193 System.out.println("Error - IO exception: " + ioE.toString()); 194 } 195 } 196 197 public void filiate(Context c, Community parent, Community child) 198 throws SQLException , AuthorizeException, IOException  199 { 200 Community childDad = child.getParentCommunity(); 204 205 if (childDad != null) 206 { 207 System.out.println("Error, child community: " + child.getID() 208 + " already a child of: " + childDad.getID()); 209 System.exit(1); 210 } 211 212 Community[] parentDads = parent.getAllParents(); 215 216 for (int i = 0; i < parentDads.length; i++) 217 { 218 if (parentDads[i].getID() == child.getID()) 219 { 220 System.out 221 .println("Error, circular parentage - child is parent of parent"); 222 System.exit(1); 223 } 224 } 225 226 parent.addSubcommunity(child); 228 229 c.complete(); 231 System.out.println("Filiation complete. Community: '" + parent.getID() 232 + "' is parent of community: '" + child.getID() + "'"); 233 } 234 235 public void defiliate(Context c, Community parent, Community child) 236 throws SQLException , AuthorizeException, IOException  237 { 238 Community[] parentKids = parent.getSubcommunities(); 240 boolean isChild = false; 241 242 for (int i = 0; i < parentKids.length; i++) 243 { 244 if (parentKids[i].getID() == child.getID()) 245 { 246 isChild = true; 247 248 break; 249 } 250 } 251 252 if (!isChild) 253 { 254 System.out 255 .println("Error, child community not a child of parent community"); 256 System.exit(1); 257 } 258 259 DatabaseManager.updateQuery(c, 262 "DELETE FROM community2community WHERE parent_comm_id= ? "+ 263 "AND child_comm_id= ? ", parent.getID(), child.getID()); 264 265 c.complete(); 267 System.out.println("Defiliation complete. Community: '" + child.getID() 268 + "' is no longer a child of community: '" + parent.getID() 269 + "'"); 270 } 271 272 private Community resolveCommunity(Context c, String communityID) 273 throws SQLException  274 { 275 Community community = null; 276 277 if (communityID.indexOf('/') != -1) 278 { 279 community = (Community) HandleManager.resolveToObject(c, 281 communityID); 282 283 if ((community == null) 285 || (community.getType() != Constants.COMMUNITY)) 286 { 287 community = null; 288 } 289 } 290 else 291 { 292 community = Community.find(c, Integer.parseInt(communityID)); 293 } 294 295 return community; 296 } 297 } 298
| Popular Tags
|