From 69db8ba6f66a36ee6143f82d45d07af8b9d398fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Mas=C3=A1r?= Date: Mon, 21 Oct 2013 23:27:54 +0200 Subject: [PATCH] make policy import also work for bundles --- .../crosswalk/METSRightsCrosswalk.java | 6 ++ .../packager/AbstractMETSIngester.java | 32 +++++++ .../dspace/content/packager/METSManifest.java | 83 ++++++++++++++++++- 3 files changed, 120 insertions(+), 1 deletion(-) diff --git a/dspace-api/src/main/java/org/dspace/content/crosswalk/METSRightsCrosswalk.java b/dspace-api/src/main/java/org/dspace/content/crosswalk/METSRightsCrosswalk.java index 770646bfa6..fa1cba00b1 100644 --- a/dspace-api/src/main/java/org/dspace/content/crosswalk/METSRightsCrosswalk.java +++ b/dspace-api/src/main/java/org/dspace/content/crosswalk/METSRightsCrosswalk.java @@ -520,6 +520,12 @@ public class METSRightsCrosswalk if (el.getAttributeValue("MODIFY").equalsIgnoreCase("true")) { rp.setAction(Constants.DELETE); + if ((el.getAttributeValue("COPY").equalsIgnoreCase("true")) + &&(el.getAttributeValue("DUPLICATE").equalsIgnoreCase("true")) + &&(el.getAttributeValue("PRINT").equalsIgnoreCase("true"))) + { + rp.setAction(Constants.ADMIN); + } } } } diff --git a/dspace-api/src/main/java/org/dspace/content/packager/AbstractMETSIngester.java b/dspace-api/src/main/java/org/dspace/content/packager/AbstractMETSIngester.java index 1f6b637ea9..b1d1ccc289 100644 --- a/dspace-api/src/main/java/org/dspace/content/packager/AbstractMETSIngester.java +++ b/dspace-api/src/main/java/org/dspace/content/packager/AbstractMETSIngester.java @@ -749,6 +749,8 @@ public abstract class AbstractMETSIngester extends AbstractPackageIngester // Loop through these files, and add them one by one to Item List manifestContentFiles = manifest .getContentFiles(); + List manifestBundleFiles = manifest + .getBundleFiles(); boolean setPrimaryBitstream = false; BitstreamFormat unknownFormat = BitstreamFormat.findUnknown(context); @@ -837,6 +839,36 @@ public abstract class AbstractMETSIngester extends AbstractPackageIngester bitstream.update(); }// end for each manifest file + for (Iterator mi = manifestBundleFiles.iterator(); mi + .hasNext();) + { + Element mfile = mi.next(); + + String mfileGrp = mfile.getAttributeValue("ADMID"); + if (mfileGrp == null) + { + throw new PackageValidationException( + "Invalid METS Manifest: file element without ID attribute."); + } + + String bundleName = METSManifest.getBundleName(mfile, false); + + Bundle bundle; + Bundle bns[] = item.getBundles(bundleName); + if (bns != null && bns.length > 0) + { + bundle = bns[0]; + } + else + { + bundle = item.createBundle(bundleName); + } + + manifest.crosswalkBundle(context, params, bundle, mfileGrp,mdRefCallback); + + bundle.update(); + }// end for each manifest file + // Step 3 -- Sanity checks // sanity check for primary bitstream if (primaryID != null && !setPrimaryBitstream) diff --git a/dspace-api/src/main/java/org/dspace/content/packager/METSManifest.java b/dspace-api/src/main/java/org/dspace/content/packager/METSManifest.java index dbd05931f5..2ec62373bd 100644 --- a/dspace-api/src/main/java/org/dspace/content/packager/METSManifest.java +++ b/dspace-api/src/main/java/org/dspace/content/packager/METSManifest.java @@ -21,6 +21,7 @@ import org.apache.commons.codec.binary.Base64; import org.apache.log4j.Logger; import org.dspace.authorize.AuthorizeException; import org.dspace.content.Bitstream; +import org.dspace.content.Bundle; import org.dspace.content.DSpaceObject; import org.dspace.content.crosswalk.AbstractPackagerWrappingCrosswalk; import org.dspace.content.crosswalk.CrosswalkException; @@ -155,6 +156,7 @@ public class METSManifest /** elements in "original" file group (bundle) */ private List contentFiles = null; + private List bundleFiles = null; /** builder to use for mdRef streams, inherited from create() */ private SAXBuilder parser = null; @@ -319,6 +321,29 @@ public class METSManifest * the item's content. * @return a List of Elements. */ + public List getBundleFiles() + throws MetadataValidationException + { + if (bundleFiles != null) + { + return bundleFiles; + } + + bundleFiles = new ArrayList(); + Element fileSec = mets.getChild("fileSec", metsNS); + + if (fileSec != null) + { + Iterator fgi = fileSec.getChildren("fileGrp", metsNS).iterator(); + while (fgi.hasNext()) + { + Element fg = (Element)fgi.next(); + bundleFiles.add(fg); + } + } + return bundleFiles; + } + public List getContentFiles() throws MetadataValidationException { @@ -446,7 +471,24 @@ public class METSManifest public static String getBundleName(Element file) throws MetadataValidationException { - Element fg = file.getParentElement(); + return getBundleName(file, true); + } + + /** + * Get the DSpace bundle name corresponding to the USE + * attribute of the file group enclosing this file element. + * + * @return DSpace bundle name + * @throws MetadataValidationException when there is no USE attribute on the enclosing fileGrp. + */ + public static String getBundleName(Element file, boolean getParent) + throws MetadataValidationException + { + Element fg = file; + if (getParent) + { + fg = file.getParentElement(); + } String fgUse = fg.getAttributeValue("USE"); if (fgUse == null) { @@ -1278,6 +1320,45 @@ public class METSManifest } } + + public void crosswalkBundle(Context context, PackageParameters params, + Bundle bundle, + String fileId, Mdref callback) + throws MetadataValidationException, PackageValidationException, + CrosswalkException, IOException, SQLException, AuthorizeException + { + Element file = getElementByXPath("descendant::mets:fileGrp[@ADMID=\""+fileId+"\"]", false); + if (file == null) + { + throw new MetadataValidationException("Failed in Bitstream crosswalk, Could not find file element with ID=" + fileId); + } + + // In DSpace METS SIP spec, admin metadata is only "highly + // recommended", not "required", so it is OK if there is no ADMID. + String amds = file.getAttributeValue("ADMID"); + if (amds == null) + { + log.warn("Got no bitstream ADMID, file@ID="+fileId); + return; + } + String amdID[] = amds.split("\\s+"); + for (int i = 0; i < amdID.length; ++i) + { + Element amdSec = getElementByXPath("mets:amdSec[@ID=\""+amdID[i]+"\"]", false); + for (Iterator ti = amdSec.getChildren("techMD", metsNS).iterator(); ti.hasNext();) + { + crosswalkXmd(context, params, bundle, (Element)ti.next(), callback); + } + for (Iterator ti = amdSec.getChildren("sourceMD", metsNS).iterator(); ti.hasNext();) + { + crosswalkXmd(context, params, bundle, (Element)ti.next(), callback); + } + for (Iterator ti = amdSec.getChildren("rightsMD", metsNS).iterator(); ti.hasNext();) + { + crosswalkXmd(context, params, bundle, (Element)ti.next(), callback); + } + } + } /** * @return root element of METS document. */