1 40 package org.dspace.app.itemexport; 41 42 import java.io.BufferedOutputStream ; 43 import java.io.File ; 44 import java.io.FileOutputStream ; 45 import java.io.FileWriter ; 46 import java.io.InputStream ; 47 import java.io.PrintWriter ; 48 import java.util.HashMap ; 49 import java.util.Iterator ; 50 51 import org.apache.commons.cli.CommandLine; 52 import org.apache.commons.cli.CommandLineParser; 53 import org.apache.commons.cli.HelpFormatter; 54 import org.apache.commons.cli.Options; 55 import org.apache.commons.cli.PosixParser; 56 import org.dspace.content.Bitstream; 57 import org.dspace.content.Bundle; 58 import org.dspace.content.Collection; 59 import org.dspace.content.DCValue; 60 import org.dspace.content.Item; 61 import org.dspace.content.ItemIterator; 62 import org.dspace.content.MetadataSchema; 63 import org.dspace.core.Constants; 64 import org.dspace.core.Context; 65 import org.dspace.core.Utils; 66 import org.dspace.handle.HandleManager; 67 68 86 public class ItemExport 87 { 88 91 public static void main(String [] argv) throws Exception 92 { 93 CommandLineParser parser = new PosixParser(); 95 96 Options options = new Options(); 97 98 options.addOption("t", "type", true, "type: COLLECTION or ITEM"); 99 options.addOption("i", "id", true, "ID or handle of thing to export"); 100 options.addOption("d", "dest", true, 101 "destination where you want items to go"); 102 options.addOption("n", "number", true, 103 "sequence number to begin exporting items with"); 104 options.addOption("h", "help", false, "help"); 105 106 CommandLine line = parser.parse(options, argv); 107 108 String typeString = null; 109 String destDirName = null; 110 String myIDString = null; 111 int seqStart = -1; 112 int myType = -1; 113 114 Item myItem = null; 115 Collection mycollection = null; 116 117 if (line.hasOption('h')) 118 { 119 HelpFormatter myhelp = new HelpFormatter(); 120 myhelp.printHelp("ItemExport\n", options); 121 System.out 122 .println("\nfull collection: ItemExport -t COLLECTION -i ID -d dest -n number"); 123 System.out 124 .println("singleitem: ItemExport -t ITEM -i ID -d dest -n number"); 125 126 System.exit(0); 127 } 128 129 if (line.hasOption('t')) { 131 typeString = line.getOptionValue('t'); 132 133 if (typeString.equals("ITEM")) 134 { 135 myType = Constants.ITEM; 136 } 137 else if (typeString.equals("COLLECTION")) 138 { 139 myType = Constants.COLLECTION; 140 } 141 } 142 143 if (line.hasOption('i')) { 145 myIDString = line.getOptionValue('i'); 146 } 147 148 if (line.hasOption('d')) { 150 destDirName = line.getOptionValue('d'); 151 } 152 153 if (line.hasOption('n')) { 155 seqStart = Integer.parseInt(line.getOptionValue('n')); 156 } 157 158 if (myType == -1) 160 { 161 System.out 162 .println("type must be either COLLECTION or ITEM (-h for help)"); 163 System.exit(1); 164 } 165 166 if (destDirName == null) 167 { 168 System.out 169 .println("destination directory must be set (-h for help)"); 170 System.exit(1); 171 } 172 173 if (seqStart == -1) 174 { 175 System.out 176 .println("sequence start number must be set (-h for help)"); 177 System.exit(1); 178 } 179 180 if (myIDString == null) 181 { 182 System.out 183 .println("ID must be set to either a database ID or a handle (-h for help)"); 184 System.exit(1); 185 } 186 187 Context c = new Context(); 188 c.setIgnoreAuthorization(true); 189 190 if (myType == Constants.ITEM) 191 { 192 if (myIDString.indexOf('/') != -1) 194 { 195 myItem = (Item) HandleManager.resolveToObject(c, myIDString); 196 197 if ((myItem == null) || (myItem.getType() != Constants.ITEM)) 198 { 199 myItem = null; 200 } 201 } 202 else 203 { 204 myItem = Item.find(c, Integer.parseInt(myIDString)); 205 } 206 207 if (myItem == null) 208 { 209 System.out 210 .println("Error, item cannot be found: " + myIDString); 211 } 212 } 213 else 214 { 215 if (myIDString.indexOf('/') != -1) 216 { 217 mycollection = (Collection) HandleManager.resolveToObject(c, 219 myIDString); 220 221 if ((mycollection == null) 223 || (mycollection.getType() != Constants.COLLECTION)) 224 { 225 mycollection = null; 226 } 227 } 228 else if (myIDString != null) 229 { 230 mycollection = Collection.find(c, Integer.parseInt(myIDString)); 231 } 232 233 if (mycollection == null) 234 { 235 System.out.println("Error, collection cannot be found: " 236 + myIDString); 237 System.exit(1); 238 } 239 } 240 241 if (myItem != null) 242 { 243 exportItem(c, myItem, destDirName, seqStart); 245 } 246 else 247 { 248 System.out.println("Exporting from collection: " + myIDString); 249 250 ItemIterator i = mycollection.getItems(); 252 253 exportItem(c, i, destDirName, seqStart); 254 } 255 256 c.complete(); 257 } 258 259 private static void exportItem(Context c, ItemIterator i, 260 String destDirName, int seqStart) throws Exception 261 { 262 int mySequenceNumber = seqStart; 263 264 System.out.println("Beginning export"); 265 266 while (i.hasNext()) 267 { 268 System.out.println("Exporting item to " + mySequenceNumber); 269 exportItem(c, i.next(), destDirName, mySequenceNumber); 270 mySequenceNumber++; 271 } 272 } 273 274 private static void exportItem(Context c, Item myItem, String destDirName, 275 int seqStart) throws Exception 276 { 277 File destDir = new File (destDirName); 278 279 if (destDir.exists()) 280 { 281 File itemDir = new File (destDir + "/" + seqStart); 283 284 System.out.println("Exporting Item " + myItem.getID() + " to " 285 + itemDir); 286 287 if (itemDir.exists()) 288 { 289 throw new Exception ("Directory " + destDir + "/" + seqStart 290 + " already exists!"); 291 } 292 293 if (itemDir.mkdir()) 294 { 295 writeMetadata(c, myItem, itemDir); 297 writeBitstreams(c, myItem, itemDir); 298 writeHandle(c, myItem, itemDir); 299 } 300 else 301 { 302 throw new Exception ("Error, can't make dir " + itemDir); 303 } 304 } 305 else 306 { 307 throw new Exception ("Error, directory " + destDirName 308 + " doesn't exist!"); 309 } 310 } 311 312 321 private static void writeMetadata(Context c, Item i, File destDir) 322 throws Exception 323 { 324 HashMap map = new HashMap (); 326 DCValue[] dcorevalues = i.getMetadata(Item.ANY, Item.ANY, Item.ANY, Item.ANY); 327 for (int ii = 0; ii < dcorevalues.length; ii++) 328 { 329 map.put(dcorevalues[ii].schema, null); 330 } 331 332 Iterator iterator = map.keySet().iterator(); 334 while (iterator.hasNext()) 335 { 336 String schema = (String ) iterator.next(); 337 writeMetadata(c, schema, i, destDir); 338 } 339 } 340 341 private static void writeMetadata(Context c, String schema, Item i, File destDir) 343 throws Exception 344 { 345 String filename; 346 if (schema.equals(MetadataSchema.DC_SCHEMA)) { 347 filename = "dublin_core.xml"; 348 } else { 349 filename = "metadata_" + schema + ".xml"; 350 } 351 352 File outFile = new File (destDir, filename); 353 354 System.out.println("Attempting to create file " + outFile); 355 356 if (outFile.createNewFile()) 357 { 358 BufferedOutputStream out = new BufferedOutputStream ( 359 new FileOutputStream (outFile)); 360 361 DCValue[] dcorevalues = i.getMetadata(schema, Item.ANY, Item.ANY, Item.ANY); 362 363 byte[] utf8 = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n" 365 .getBytes("UTF-8"); 366 out.write(utf8, 0, utf8.length); 367 368 String dcTag = "<dublin_core schema=\""+schema+"\">\n"; 369 utf8 = dcTag.getBytes("UTF-8"); 370 out.write(utf8, 0, utf8.length); 371 372 for (int j = 0; j < dcorevalues.length; j++) 373 { 374 DCValue dcv = dcorevalues[j]; 375 String qualifier = dcv.qualifier; 376 377 if (qualifier == null) 378 { 379 qualifier = "none"; 380 } 381 382 utf8 = (" <dcvalue element=\"" + dcv.element + "\" " 383 + "qualifier=\"" + qualifier + "\">" 384 + Utils.addEntities(dcv.value) + "</dcvalue>\n").getBytes("UTF-8"); 385 386 out.write(utf8, 0, utf8.length); 387 } 388 389 utf8 = "</dublin_core>\n".getBytes("UTF-8"); 390 out.write(utf8, 0, utf8.length); 391 392 out.close(); 393 } 394 else 395 { 396 throw new Exception ("Cannot create dublin_core.xml in " + destDir); 397 } 398 } 399 400 private static void writeHandle(Context c, Item i, File destDir) 402 throws Exception 403 { 404 String filename = "handle"; 405 406 File outFile = new File (destDir, filename); 407 408 if (outFile.createNewFile()) 409 { 410 PrintWriter out = new PrintWriter (new FileWriter (outFile)); 411 412 out.println(i.getHandle()); 413 414 out.close(); 416 } 417 else 418 { 419 throw new Exception ("Cannot create file " + filename + " in " 420 + destDir); 421 } 422 } 423 424 436 private static void writeBitstreams(Context c, Item i, File destDir) 437 throws Exception 438 { 439 File outFile = new File (destDir, "contents"); 440 441 if (outFile.createNewFile()) 442 { 443 PrintWriter out = new PrintWriter (new FileWriter (outFile)); 444 445 Bundle[] bundles = i.getBundles(); 446 447 for (int j = 0; j < bundles.length; j++) 448 { 449 Bitstream[] bitstreams = bundles[j].getBitstreams(); 451 452 String bundleName = bundles[j].getName(); 453 454 for (int k = 0; k < bitstreams.length; k++) 455 { 456 Bitstream b = bitstreams[k]; 457 458 String myName = b.getName(); 459 String oldName = myName; 460 int myPrefix = 1; 462 InputStream is = b.retrieve(); 463 464 boolean isDone = false; 467 while (!isDone) 468 { 469 File fout = new File (destDir, myName); 470 471 if (fout.createNewFile()) 472 { 473 FileOutputStream fos = new FileOutputStream (fout); 474 Utils.bufferedCopy(is, fos); 475 is.close(); 477 fos.close(); 478 479 if (b.isRegisteredBitstream()) { 481 out.println("-r -s " + b.getStoreNumber() 482 + " -f " + myName 483 + "\tbundle:" + bundleName); 484 } else { 485 out.println(myName + "\tbundle:" + bundleName); 486 } 487 488 isDone = true; 489 } 490 else 491 { 492 myName = myPrefix + "_" + oldName; myPrefix++; 497 } 498 } 499 } 500 } 501 502 out.close(); 504 } 505 else 506 { 507 throw new Exception ("Cannot create contents in " + destDir); 508 } 509 } 510 } 511 | Popular Tags |