From 4844f8385c47f6c76aa02a6e8f6a3109019a201c Mon Sep 17 00:00:00 2001 From: MajoBerger <88670521+MajoBerger@users.noreply.github.com> Date: Wed, 24 Jul 2024 16:51:13 +0200 Subject: [PATCH] Ufal/synchronising cmdi outputs (#708) * work in progress * only use first available value from demo uri * reorder funding to be correct * WIP * remove forgotten script * restore old version * restore old version for real * restore old version for real fr * restore old version for real fr2 * comment of changed order * add issues and todo comments * use old and correct version * comment change * sacrifice to the checkstyle god * reformat TODOs --- .../org/dspace/utils/SpecialItemService.java | 17 +++++-- .../functions/NodeListXslFunction.java | 44 +++++++++++++++---- .../java/org/dspace/xoai/util/ItemUtils.java | 27 ++++++++++++ .../oai/metadataFormats/metasharev2.xsl | 8 ++-- 4 files changed, 82 insertions(+), 14 deletions(-) diff --git a/dspace-oai/src/main/java/org/dspace/utils/SpecialItemService.java b/dspace-oai/src/main/java/org/dspace/utils/SpecialItemService.java index 068c8dc619d0..d62cbc481fcb 100644 --- a/dspace-oai/src/main/java/org/dspace/utils/SpecialItemService.java +++ b/dspace-oai/src/main/java/org/dspace/utils/SpecialItemService.java @@ -129,16 +129,27 @@ public static Node getFunding(String mdValue) { } String[] values = mdValue .split(DCInput.ComplexDefinitions.getSeparator(), -1); - // mind the order in input forms, org;code;projname;type - Element[] elements = {organization, code, projName, fundsType}; + // ORIGINAL order of funding was org;code;projname;type + // Element[] elements = {organization, code, projName, fundsType}; + + // TODO 2024/07 - order was changed to fundsType, code, org, projName + Element[] elements = {fundsType, code, organization, projName}; + for (int i = 0; i < elements.length; i++) { if (values.length <= i) { elements[i].appendChild(doc.createTextNode("")); } else { elements[i].appendChild(doc.createTextNode(values[i])); } - el.appendChild(elements[i]); + } + // swap to original order to display correctly + Element[] correctOrder = {organization, code, projName, fundsType}; + + for (Element e : correctOrder) { + el.appendChild(e); + } + return doc; } catch (ParserConfigurationException e) { return null; diff --git a/dspace-oai/src/main/java/org/dspace/xoai/services/impl/resources/functions/NodeListXslFunction.java b/dspace-oai/src/main/java/org/dspace/xoai/services/impl/resources/functions/NodeListXslFunction.java index 775d5c8c12be..0d0c05163016 100644 --- a/dspace-oai/src/main/java/org/dspace/xoai/services/impl/resources/functions/NodeListXslFunction.java +++ b/dspace-oai/src/main/java/org/dspace/xoai/services/impl/resources/functions/NodeListXslFunction.java @@ -11,7 +11,8 @@ import static org.dspace.xoai.services.impl.resources.functions.StringXSLFunction.BASE; import java.util.Objects; -import javax.xml.transform.dom.DOMSource; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; import net.sf.saxon.s9api.DocumentBuilder; import net.sf.saxon.s9api.ExtensionFunction; @@ -25,10 +26,12 @@ import net.sf.saxon.s9api.XdmValue; import org.apache.logging.log4j.Logger; import org.bouncycastle.util.Arrays; -import org.w3c.dom.Node; +import org.w3c.dom.Document; +import org.w3c.dom.Element; import org.w3c.dom.NodeList; + /** * Serves as proxy for call from XSL engine. * @@ -48,7 +51,7 @@ final public QName getName() { @Override final public SequenceType getResultType() { - return SequenceType.makeSequenceType(ItemType.ANY_NODE, OccurrenceIndicator.ZERO_OR_ONE); + return SequenceType.makeSequenceType(ItemType.ANY_NODE, OccurrenceIndicator.ZERO_OR_MORE); } @Override @@ -75,11 +78,36 @@ final public XdmValue call(XdmValue[] xdmValues) throws SaxonApiException { NodeList nodeList = getNodeList(val); - Node oneNode = nodeList.item(0); + // TODO: 2024/07 (mb) Below is attempted fix that does not work yet. + // see https://github.com/dataquest-dev/DSpace/issues/709 +// Node oneNode = nodeList.item(0); + + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + javax.xml.parsers.DocumentBuilder db = null; + try { + db = dbf.newDocumentBuilder(); + Document newDoc = db.newDocument(); + Element rootElement = newDoc.createElement("root"); + newDoc.appendChild(rootElement); + + for (int i = 0; i < nodeList.getLength(); i++) { + try { + rootElement.appendChild(newDoc.importNode(nodeList.item(i), true)); + } catch (Exception e) { + log.error("Error while importing node", e); + } + + } + Processor processor = new Processor(false); + DocumentBuilder saxonDb = processor.newDocumentBuilder(); + XdmValue xdmValue = saxonDb.wrap(newDoc); + + return xdmValue; + } catch (ParserConfigurationException e) { + throw new RuntimeException(e); + } + + - DocumentBuilder db = new Processor(false).newDocumentBuilder(); - DOMSource sourceObj = new DOMSource(oneNode); - var res = db.wrap(sourceObj); - return res; } } diff --git a/dspace-oai/src/main/java/org/dspace/xoai/util/ItemUtils.java b/dspace-oai/src/main/java/org/dspace/xoai/util/ItemUtils.java index b32983581321..eedc678ac349 100644 --- a/dspace-oai/src/main/java/org/dspace/xoai/util/ItemUtils.java +++ b/dspace-oai/src/main/java/org/dspace/xoai/util/ItemUtils.java @@ -36,6 +36,8 @@ import org.dspace.core.Constants; import org.dspace.core.Context; import org.dspace.core.Utils; +import org.dspace.core.factory.CoreServiceFactory; +import org.dspace.core.service.LicenseService; import org.dspace.services.ConfigurationService; import org.dspace.services.factory.DSpaceServicesFactory; import org.dspace.xoai.data.DSpaceItem; @@ -65,6 +67,9 @@ public class ItemUtils { private static final AuthorizeService authorizeService = AuthorizeServiceFactory.getInstance().getAuthorizeService(); + + private static final LicenseService licenseService = CoreServiceFactory.getInstance().getLicenseService(); + /** * Default constructor */ @@ -153,6 +158,8 @@ private static Element createBundlesElement(Context context, Item item) throws S bitstream.getField().add(createValue("checksum", cks)); bitstream.getField().add(createValue("checksumAlgorithm", cka)); bitstream.getField().add(createValue("sid", bit.getSequenceID() + "")); + bitstream.getField().add(createValue("id", bit.getID().toString())); + } } @@ -290,6 +297,26 @@ public static Metadata retrieveMetadata(Context context, Item item) { other.getField().add(createValue("handle", item.getHandle())); other.getField().add(createValue("identifier", DSpaceItem.buildIdentifier(item.getHandle()))); other.getField().add(createValue("lastModifyDate", item.getLastModified().toString())); + // TODO: 2024/07 (mb) this is taken from clarin, but restricted is never set correctly. + // see issue https://github.com/dataquest-dev/DSpace/issues/710 + boolean restricted = false; +// if(!restricted){ +// List lds = +// for(LicenseDefinition ld : lds){ +// if(ld.getRequiredInfo() != null && ld.getRequiredInfo().length() > 0){ +// restricted = true; +// } +// if(restricted){ +// break; +// } +// } +// } + + if (restricted) { + other.getField().add(createValue("restrictedAccess", "true")); + } + other.getField().add(createValue("owningCollection", item.getOwningCollection().getName())); + other.getField().add(createValue("itemId", item.getID().toString())); metadata.getElement().add(other); // Repository Info diff --git a/dspace/config/crosswalks/oai/metadataFormats/metasharev2.xsl b/dspace/config/crosswalks/oai/metadataFormats/metasharev2.xsl index e716844112e6..9ec5f893d21e 100644 --- a/dspace/config/crosswalks/oai/metadataFormats/metasharev2.xsl +++ b/dspace/config/crosswalks/oai/metadataFormats/metasharev2.xsl @@ -236,7 +236,7 @@ - + @@ -285,10 +285,12 @@ - + + - + +