Permalink
Browse files

added BagIt ZIP Package export wizard

  • Loading branch information...
1 parent bb3e8e5 commit 6d0b3706043d26f103753c6f2d4486151ce25e99 @gregjan gregjan committed Aug 10, 2012
@@ -992,6 +992,16 @@
class="org.eclipse.core.resources.IProject">
</selection>
</wizard>
+ <wizard
+ category="org.eclipse.ui.Basic/workbench_plugin.categorySIP"
+ class="unc.lib.cdr.workbench.project.BagItZipExportWizard"
+ icon="icons/table.gif"
+ id="workbench_plugin.wizardExportBagIt"
+ name="BagIt ZIP Package">
+ <selection
+ class="org.eclipse.core.resources.IProject">
+ </selection>
+ </wizard>
</extension>
<extension
point="org.eclipse.core.runtime.adapters">
@@ -0,0 +1,168 @@
+package unc.lib.cdr.workbench.project;
+
+import gov.loc.mets.DivType;
+import gov.loc.mets.FileGrpType;
+import gov.loc.mets.FileType;
+import gov.loc.mets.FptrType;
+import gov.loc.mets.util.METSUtils;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Deque;
+import java.util.List;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+import org.eclipse.core.filesystem.EFS;
+import org.eclipse.core.filesystem.IFileStore;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.emf.common.util.TreeIterator;
+import org.eclipse.emf.ecore.EObject;
+
+import unc.lib.cdr.workbench.originals.OriginalFileStore;
+import unc.lib.cdr.workbench.rcp.Activator;
+
+public class BagItZipExportJob extends Job {
+
+ IProject project = null;
+ String destination = null;
+
+ public BagItZipExportJob(IProject project, String destination) {
+ super("Export BagIt ZIP Package");
+ this.project = project;
+ this.destination = destination;
+ }
+
+
+
+ @Override
+ protected IStatus run(IProgressMonitor monitor) {
+ MetsProjectNature nature = MetsProjectNature.get(project);
+ if(nature == null) {
+ return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Project is not a Curator's Workbench project.");
+ }
+ DivType bag = METSUtils.findBagDiv(nature.getMets());
+ FileGrpType fileGrp = METSUtils.getObjectsFileGroup(nature.getMets());
+ byte[] buffer = new byte[1024];
+
+ // create a ZIP file writer
+ FileOutputStream fout = null;
+ try {
+ fout = new FileOutputStream(destination);
+ } catch(IOException e) {
+ return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Cannot create export ZIP file", e);
+ }
+
+ ZipOutputStream zout = new ZipOutputStream(fout);
+ PrintWriter out = new PrintWriter(zout);
+ zout.setComment("BagIt ZIP serialization by Curator's Workbench");
+
+ try {
+
+ // add bagit.txt
+ zout.putNextEntry(new ZipEntry(project.getName()+"/bagit.txt"));
+ out.println("BagIt-version: 0.96");
+ out.println("Tag-File-Character-Encoding: UTF-8");
+ out.flush();
+ zout.closeEntry();
+
+ // build manifest-md5 and packaged files list
+ List<String> files = new ArrayList<String>();
+ zout.putNextEntry(new ZipEntry(project.getName()+"/manifest-md5.txt"));
+ for(TreeIterator<EObject> iter = bag.eAllContents(); iter.hasNext();) {
+ EObject eo = iter.next();
+ if(eo instanceof FptrType) {
+ FptrType fptr = (FptrType)eo;
+ String fid = fptr.getFILEID();
+ for(FileType ftype : fileGrp.getFile()) {
+ if(fid.equals(ftype.getID())) {
+ out.print(ftype.getCHECKSUM());
+ out.print(' ');
+ out.print(getLocation((DivType)fptr.eContainer(), bag));
+ }
+ }
+ }
+ }
+ out.flush();
+ zout.closeEntry();
+
+ // send files to ZIP
+ for(TreeIterator<EObject> iter = bag.eAllContents(); iter.hasNext();) {
+ EObject eo = iter.next();
+ if(eo instanceof FptrType) {
+ FptrType fptr = (FptrType)eo;
+ String fid = fptr.getFILEID();
+ for(FileType ftype : fileGrp.getFile()) {
+ if(fid.equals(ftype.getID())) {
+ // found file element, process this file..
+ zout.putNextEntry(new ZipEntry(this.project.getName()+"/"+getLocation((DivType)fptr.eContainer(), bag)));
+ OriginalFileStore orig = MetsProjectNature.getOriginal((DivType)fptr.eContainer());
+ if(orig.getStageLocation().fetchInfo().exists()) {
+ stream(zout, orig.getStageLocation(), buffer);
+ } else {
+ stream(zout, orig, buffer);
+ }
+ zout.closeEntry();
+ }
+ }
+ }
+ }
+ } catch (IOException e) {
+ return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Cannot create export ZIP file", e);
+ } catch (CoreException e) {
+ return e.getStatus();
+ } finally {
+ if(zout != null) {
+ try { zout.close(); } catch(IOException e) {}
+ }
+ if(out != null) out.close();
+ }
+ // write all data files in main manifest
+ // close the zip file
+ return Status.OK_STATUS;
+ }
+
+
+
+ private void stream(ZipOutputStream zout, IFileStore store, byte[] buffer) throws IOException, CoreException {
+ InputStream in = store.openInputStream(EFS.NONE, new NullProgressMonitor());
+ int length;
+ while((length = in.read(buffer)) > 0) {
+ zout.write(buffer, 0, length);
+ }
+ in.close();
+ }
+
+
+
+ /**
+ * Generate a path to this file, based on labels in the div tree.
+ * @param div the div for the File
+ * @return BagIt path to the file
+ */
+ private String getLocation(DivType div, DivType bagDiv) {
+ Deque<String> pathSegments = new ArrayDeque<String>();
+ for(DivType d = div; !bagDiv.equals(d); d = (DivType)d.eContainer()) {
+ pathSegments.push(d.getLABEL1());
+ }
+ pathSegments.push("data");
+
+ StringBuilder sb = new StringBuilder(pathSegments.poll());
+ for(String seg = pathSegments.poll(); seg !=null; seg = pathSegments.poll()) {
+ sb.append("/");
+ sb.append(seg);
+ }
+ return sb.toString();
+ }
+
+}
@@ -0,0 +1,90 @@
+package unc.lib.cdr.workbench.project;
+
+import org.eclipse.core.filesystem.EFS;
+import org.eclipse.core.filesystem.IFileStore;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.wizard.Wizard;
+import org.eclipse.ui.IExportWizard;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.ide.IDE;
+
+public class BagItZipExportWizard extends Wizard implements IExportWizard {
+
+ public BagItZipExportWizard() {
+ this.setWindowTitle("Export BagIt ZIP Package");
+ }
+ IWorkbench workbench = null;
+ IStructuredSelection selection = null;
+ PickExportFilePage page = null;
+ IProject project = null;
+
+ @Override
+ public void init(IWorkbench workbench, IStructuredSelection selection) {
+ this.workbench = workbench;
+ this.selection = selection;
+ if (IProject.class.isInstance(selection.getFirstElement())) {
+ this.project = (IProject) selection.getFirstElement();
+ }
+ }
+
+ @Override
+ public boolean performFinish() {
+ BagItZipExportJob job = new BagItZipExportJob(project, this.page.getDestinationValue());
+ job.schedule();
+ try {
+ job.join();
+ } catch (InterruptedException e) {
+ }
+ if (this.page.openFile) {
+ openFile();
+ }
+ return true;
+ }
+
+ /**
+ *
+ */
+ private void openFile() {
+ IFileStore store = null;
+ try {
+ Path p = new Path(page.getDestinationValue());
+ store = EFS.getStore(p.toFile().toURI());
+ } catch (CoreException e) {
+ e.printStackTrace();
+ return;
+ }
+
+ IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+ System.out.print(page);
+ try {
+ IDE.openEditorOnFileStore(page, store);
+ } catch (PartInitException e) {
+ e.printStackTrace();
+ return;
+ }
+ }
+
+ @Override
+ public void addPages() {
+ super.addPages();
+ page = new PickExportFilePage("Choose export destination", project, "Export BagIt ZIP Package", "zip");
+ addPage(page);
+ }
+
+ @Override
+ public boolean canFinish() {
+ if (project == null)
+ return false;
+ if (!page.isPageComplete())
+ return false;
+ // TODO add warning if everything is not staged?
+ return super.canFinish();
+ }
+
+}
@@ -63,6 +63,7 @@
public static int countUnstaged(IProject project) throws CoreException {
+ // TODO update this code to check against METS instead
int result = 0;
IMarker[] captured = project.findMarkers(IResourceConstants.MARKER_CAPTURED, false, IResource.DEPTH_INFINITE);
for (IMarker m : captured) {

0 comments on commit 6d0b370

Please sign in to comment.