1 40 41 package org.dspace.app.packager; 42 43 import java.io.FileInputStream ; 44 import java.io.FileOutputStream ; 45 import java.io.InputStream ; 46 import java.io.OutputStream ; 47 48 import org.apache.commons.cli.CommandLine; 49 import org.apache.commons.cli.CommandLineParser; 50 import org.apache.commons.cli.HelpFormatter; 51 import org.apache.commons.cli.Options; 52 import org.apache.commons.cli.PosixParser; 53 import org.dspace.content.Collection; 54 import org.dspace.content.DSpaceObject; 55 import org.dspace.content.Item; 56 import org.dspace.content.InstallItem; 57 import org.dspace.content.WorkspaceItem; 58 import org.dspace.content.packager.PackageDisseminator; 59 import org.dspace.content.packager.PackageParameters; 60 import org.dspace.content.packager.PackageIngester; 61 import org.dspace.core.Constants; 62 import org.dspace.core.Context; 63 import org.dspace.core.PluginManager; 64 import org.dspace.eperson.EPerson; 65 import org.dspace.handle.HandleManager; 66 import org.dspace.workflow.WorkflowManager; 67 import org.dspace.workflow.WorkflowItem; 68 69 120 public class Packager 121 { 122 private static void usageError(String msg) 124 { 125 System.out.println(msg); 126 System.out.println(" (run with -h flag for details)"); 127 System.exit(1); 128 } 129 130 public static void main(String [] argv) throws Exception 131 { 132 Options options = new Options(); 133 options.addOption("c", "collection", true, 134 "destination collection(s) Handle (repeatable)"); 135 options.addOption("e", "eperson", true, 136 "email address of eperson doing importing"); 137 options 138 .addOption( 139 "w", 140 "install", 141 false, 142 "disable workflow; install immediately without going through collection's workflow"); 143 options.addOption("t", "type", true, "package type or MIMEtype"); 144 options 145 .addOption("o", "option", true, 146 "Packager option to pass to plugin, \"name=value\" (repeatable)"); 147 options.addOption("d", "disseminate", false, 148 "Disseminate package (output); default is to submit."); 149 options.addOption("i", "item", true, "Handle of item to disseminate."); 150 options.addOption("h", "help", false, "help"); 151 152 CommandLineParser parser = new PosixParser(); 153 CommandLine line = parser.parse(options, argv); 154 155 String sourceFile = null; 156 String eperson = null; 157 String [] collections = null; 158 boolean useWorkflow = true; 159 String packageType = null; 160 boolean submit = true; 161 String itemHandle = null; 162 PackageParameters pkgParams = new PackageParameters(); 163 164 if (line.hasOption('h')) 165 { 166 HelpFormatter myhelp = new HelpFormatter(); 167 myhelp.printHelp("Packager [options] package-file|-\n", 168 options); 169 System.out.println("\nAvailable Submission Package (SIP) types:"); 170 String pn[] = PluginManager 171 .getAllPluginNames(PackageIngester.class); 172 for (int i = 0; i < pn.length; ++i) 173 System.out.println(" " + pn[i]); 174 System.out 175 .println("\nAvailable Dissemination Package (DIP) types:"); 176 pn = PluginManager.getAllPluginNames(PackageDisseminator.class); 177 for (int i = 0; i < pn.length; ++i) 178 System.out.println(" " + pn[i]); 179 System.exit(0); 180 } 181 if (line.hasOption('w')) 182 useWorkflow = false; 183 if (line.hasOption('e')) 184 eperson = line.getOptionValue('e'); 185 if (line.hasOption('c')) 186 collections = line.getOptionValues('c'); 187 if (line.hasOption('t')) 188 packageType = line.getOptionValue('t'); 189 if (line.hasOption('i')) 190 itemHandle = line.getOptionValue('i'); 191 String files[] = line.getArgs(); 192 if (files.length > 0) 193 sourceFile = files[0]; 194 if (line.hasOption('d')) 195 submit = false; 196 if (line.hasOption('o')) 197 { 198 String popt[] = line.getOptionValues('o'); 199 for (int i = 0; i < popt.length; ++i) 200 { 201 String pair[] = popt[i].split("\\=", 2); 202 if (pair.length == 2) 203 pkgParams.addProperty(pair[0].trim(), pair[1].trim()); 204 else if (pair.length == 1) 205 pkgParams.addProperty(pair[0].trim(), ""); 206 else 207 System.err 208 .println("Warning: Illegal package option format: \"" 209 + popt[i] + "\""); 210 } 211 } 212 213 if (sourceFile == null || eperson == null || packageType == null 215 || (submit && collections == null)) 216 { 217 System.err 218 .println("Error - missing a REQUIRED argument or option.\n"); 219 HelpFormatter myhelp = new HelpFormatter(); 220 myhelp.printHelp("PackageManager [options] package-file|-\n", 221 options); 222 System.exit(0); 223 } 224 225 Context context = new Context(); 227 EPerson myEPerson = null; 228 myEPerson = EPerson.findByEmail(context, eperson); 229 if (myEPerson == null) 230 usageError("Error, eperson cannot be found: " + eperson); 231 context.setCurrentUser(myEPerson); 232 233 if (submit) 234 { 235 InputStream source = (sourceFile.equals("-")) ? System.in 237 : new FileInputStream (sourceFile); 238 239 PackageIngester sip = (PackageIngester) PluginManager 240 .getNamedPlugin(PackageIngester.class, packageType); 241 if (sip == null) 242 usageError("Error, Unknown package type: " + packageType); 243 244 Collection[] mycollections = null; 246 247 System.out.println("Destination collections:"); 248 249 mycollections = new Collection[collections.length]; 251 for (int i = 0; i < collections.length; i++) 252 { 253 DSpaceObject dso = HandleManager.resolveToObject(context, 255 collections[i]); 256 if (dso == null) 257 throw new IllegalArgumentException ( 258 "Bad collection list -- " 259 + "Cannot resolve collection handle \"" 260 + collections[i] + "\""); 261 else if (dso.getType() != Constants.COLLECTION) 262 throw new IllegalArgumentException ( 263 "Bad collection list -- " + "Object at handle \"" 264 + collections[i] 265 + "\" is not a collection!"); 266 mycollections[i] = (Collection) dso; 267 System.out.println((i == 0 ? " Owning " : " ") 268 + " Collection: " 269 + mycollections[i].getMetadata("name")); 270 } 271 272 try 273 { 274 WorkspaceItem wi = sip.ingest(context, mycollections[0], 275 source, pkgParams, null); 276 if (useWorkflow) 277 { 278 String handle = null; 279 280 WorkflowItem wfi = WorkflowManager.startWithoutNotify(context, wi); 283 284 if (wfi.getState() == WorkflowManager.WFSTATE_ARCHIVE) 285 { 286 Item ni = wfi.getItem(); 287 handle = HandleManager.findHandle(context, ni); 288 } 289 if (handle == null) 290 System.out.println("Created Workflow item, ID=" 291 + String.valueOf(wfi.getID())); 292 else 293 System.out.println("Created and installed item, handle="+handle); 294 } 295 else 296 { 297 InstallItem.installItem(context, wi); 298 System.out.println("Created and installed item, handle=" 299 + HandleManager.findHandle(context, wi.getItem())); 300 } 301 context.complete(); 302 System.exit(0); 303 } 304 catch (Exception e) 305 { 306 context.abort(); 308 e.printStackTrace(); 309 System.out.println(e); 310 System.exit(1); 311 } 312 } 313 else 314 { 315 OutputStream dest = (sourceFile.equals("-")) ? (OutputStream ) System.out 316 : (OutputStream ) (new FileOutputStream (sourceFile)); 317 318 PackageDisseminator dip = (PackageDisseminator) PluginManager 319 .getNamedPlugin(PackageDisseminator.class, packageType); 320 if (dip == null) 321 usageError("Error, Unknown package type: " + packageType); 322 323 DSpaceObject dso = HandleManager.resolveToObject(context, 324 itemHandle); 325 if (dso == null) 326 throw new IllegalArgumentException ("Bad Item handle -- " 327 + "Cannot resolve handle \"" + itemHandle); 328 dip.disseminate(context, dso, pkgParams, dest); 329 } 330 } 331 } 332 | Popular Tags |