diff --git a/dspace-api/src/main/java/org/dspace/administer/StructBuilder.java b/dspace-api/src/main/java/org/dspace/administer/StructBuilder.java index cb65604a2626..c08566dc5b0a 100644 --- a/dspace-api/src/main/java/org/dspace/administer/StructBuilder.java +++ b/dspace-api/src/main/java/org/dspace/administer/StructBuilder.java @@ -9,6 +9,7 @@ import static org.dspace.content.Item.ANY; import static org.dspace.content.MetadataSchemaEnum.CRIS; +import static org.dspace.content.authority.Choices.CF_UNSET; import static org.dspace.content.service.DSpaceObjectService.MD_COPYRIGHT_TEXT; import static org.dspace.content.service.DSpaceObjectService.MD_INTRODUCTORY_TEXT; import static org.dspace.content.service.DSpaceObjectService.MD_LICENSE; @@ -49,12 +50,14 @@ import org.dspace.content.Collection; import org.dspace.content.Community; import org.dspace.content.Item; +import org.dspace.content.MetadataField; import org.dspace.content.MetadataFieldName; import org.dspace.content.MetadataSchemaEnum; import org.dspace.content.MetadataValue; import org.dspace.content.factory.ContentServiceFactory; import org.dspace.content.service.CollectionService; import org.dspace.content.service.CommunityService; +import org.dspace.content.service.ItemService; import org.dspace.core.Context; import org.dspace.core.CrisConstants; import org.dspace.eperson.factory.EPersonServiceFactory; @@ -122,7 +125,8 @@ public class StructBuilder { = EPersonServiceFactory.getInstance().getEPersonService(); protected static final HandleService handleService = HandleServiceFactory.getInstance().getHandleService(); - + protected static final ItemService itemService + = ContentServiceFactory.getInstance().getItemService(); /** * Default constructor */ @@ -407,6 +411,9 @@ private static Element exportACollection(Collection collection) { Element element = new Element("collection"); element.setAttribute("identifier", collection.getHandle()); element.addContent(new Element("name").setText(collection.getName())); + + buildTemplateItem(collection, element); + element.addContent(new Element("description") .setText(collectionService.getMetadataFirstValue(collection, MetadataSchemaEnum.DC.getName(), "description", "abstract", Item.ANY))); @@ -833,6 +840,8 @@ private static Element[] handleCollections(Context context, collectionService.setMetadataSingleValue(context, collection, MD_SHORT_DESCRIPTION, Item.ANY, " "); + handleTemplateItem(context, collection, tn); + // import the rest of the metadata for (Map.Entry entry : collectionMap.entrySet()) { NodeList nl = (NodeList) xPath.compile(entry.getKey()).evaluate(tn, XPathConstants.NODESET); @@ -854,6 +863,8 @@ private static Element[] handleCollections(Context context, String fieldValue; + buildTemplateItem(collection, element); + fieldValue = collectionService.getMetadataFirstValue(collection, CollectionService.MD_SHORT_DESCRIPTION, Item.ANY); if (fieldValue != null) { @@ -930,4 +941,93 @@ private static Element[] handleCollections(Context context, return elements; } + + private static void handleTemplateItem(Context context, Collection collection, Node tn) + throws XPathExpressionException, SQLException, AuthorizeException { + + XPath xPath = XPathFactory.newInstance().newXPath(); + Node node = (Node) xPath.compile("templateItem").evaluate(tn, XPathConstants.NODE); + + if (node == null) { + return; + } + + Item templateItem = itemService.createTemplateItem(context, collection); + + NodeList metadataNodes = (NodeList) xPath.compile("metadata").evaluate(node, XPathConstants.NODESET); + + for (int i = 0; i < metadataNodes.getLength(); i++) { + Node metadataNode = metadataNodes.item(i); + MetadataFieldName metadataFieldName = buildMetadataFieldName(metadataNode); + + Node valueAttribute = (Node) xPath.compile("value").evaluate(metadataNode, XPathConstants.NODE); + Node authorityAttribute = (Node) xPath.compile("authority").evaluate(metadataNode, XPathConstants.NODE); + Node confidenceAttribute = (Node) xPath.compile("confidence").evaluate(metadataNode, XPathConstants.NODE); + + String authority = null; + int confidence = CF_UNSET; + + if (authorityAttribute != null) { + authority = authorityAttribute.getTextContent(); + confidence = confidenceAttribute != null ? Integer.parseInt(confidenceAttribute.getTextContent()) : 600; + } + + itemService.addMetadata(context, templateItem, metadataFieldName.schema, metadataFieldName.element, + metadataFieldName.qualifier, ANY, valueAttribute.getTextContent(), authority, confidence); + itemService.update(context, templateItem); + } + } + + private static MetadataFieldName buildMetadataFieldName(Node node) { + Node schemaAttribute = node.getAttributes().getNamedItem("schema"); + Node elementAttribute = node.getAttributes().getNamedItem("element"); + Node qualifierAttribute = node.getAttributes().getNamedItem("qualifier"); + + if (qualifierAttribute == null) { + return new MetadataFieldName(schemaAttribute.getTextContent(), elementAttribute.getTextContent()); + } else { + return new MetadataFieldName(schemaAttribute.getTextContent(), + elementAttribute.getTextContent(), qualifierAttribute.getTextContent()); + } + } + + private static void buildTemplateItem(Collection collection, Element element) { + + try { + Item templateItem = collection.getTemplateItem(); + + if (templateItem == null) { + return; + } + + Element templateItemElement = new Element("templateItem"); + + for (MetadataValue metadataValue : templateItem.getMetadata()) { + MetadataField metadataField = metadataValue.getMetadataField(); + Element metadata = new Element("metadata"); + metadata.setAttribute("schema", metadataField.getMetadataSchema().getName()); + metadata.setAttribute("element", metadataField.getElement()); + + if (metadataField.getQualifier() != null) { + metadata.setAttribute("qualifier", metadataField.getQualifier()); + } + + metadata.addContent(new Element("value").setText(metadataValue.getValue())); + + if (metadataValue.getAuthority() != null) { + metadata.addContent(new Element("authority").setText(metadataValue.getAuthority())); + metadata.addContent(new Element("confidence").setText( + String.valueOf(metadataValue.getConfidence()) + )); + } + + templateItemElement.addContent(metadata); + } + + element.addContent(templateItemElement); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } diff --git a/dspace-api/src/test/java/org/dspace/administer/StructBuilderIT.java b/dspace-api/src/test/java/org/dspace/administer/StructBuilderIT.java index a113aa0438dd..e69fc85b4970 100644 --- a/dspace-api/src/test/java/org/dspace/administer/StructBuilderIT.java +++ b/dspace-api/src/test/java/org/dspace/administer/StructBuilderIT.java @@ -17,6 +17,7 @@ import java.nio.charset.StandardCharsets; import java.sql.SQLException; import java.util.Iterator; +import java.util.UUID; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; @@ -27,10 +28,12 @@ import org.dspace.authorize.AuthorizeException; import org.dspace.content.Collection; import org.dspace.content.Community; +import org.dspace.content.Item; import org.dspace.content.MetadataSchemaEnum; import org.dspace.content.factory.ContentServiceFactory; import org.dspace.content.service.CollectionService; import org.dspace.content.service.CommunityService; +import org.dspace.content.service.ItemService; import org.dspace.handle.Handle; import org.junit.AfterClass; import org.junit.Before; @@ -61,6 +64,8 @@ public class StructBuilderIT = ContentServiceFactory.getInstance().getCommunityService(); private static final CollectionService collectionService = ContentServiceFactory.getInstance().getCollectionService(); + private static final ItemService itemService + = ContentServiceFactory.getInstance().getItemService(); public StructBuilderIT() { } @@ -114,6 +119,21 @@ public void setUp() throws SQLException, AuthorizeException, IOException { " Another sidebar\n" + " \n" + " Collection 0.0.0\n" + + " \n" + + " \n" + + " template item\n" + + " \n" + + " \n" + + " Walter White\n" + + " " + UUID.randomUUID() + "\n" + + " 600\n" + + " \n" + + " \n" + + " Donald, Smith\n" + + " " + UUID.randomUUID() + "\n" + + " 400\n" + + " \n" + + " \n" + " A collection\n" + " Our next guest needs no introduction\n" + " 1776\n" + @@ -149,6 +169,11 @@ public void setUp() throws SQLException, AuthorizeException, IOException { " \n" + " \n" + " Collection 0.0\n" + + " \n" + + " \n" + + " template item\n" + + " \n" + + " \n" + " \n" + " \n" + " \n" + @@ -301,6 +326,10 @@ public void testExportStructure() MetadataSchemaEnum.DC.getName(), "title", null, null, "Collection 0.0"); + Item item = itemService.createTemplateItem(context, collection0_0); + itemService.addMetadata(context, item, MetadataSchemaEnum.DC.getName(), "title", null, + Item.ANY, "template item", null, -1); + // Export the current structure. System.out.println("exportStructure"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); diff --git a/dspace/config/sample-structure.xml b/dspace/config/sample-structure.xml index 89c577bdcfa6..bde920dea384 100644 --- a/dspace/config/sample-structure.xml +++ b/dspace/config/sample-structure.xml @@ -10,6 +10,13 @@ Person Person person + + + + + + + @@ -21,6 +28,13 @@ Project Project project + + + + + + + @@ -32,6 +46,13 @@ Funding Funding funding + + + + + + + @@ -43,6 +64,13 @@ OrgUnit OrgUnit orgunit + + + + + + + @@ -54,6 +82,13 @@ Journal Journal journal + + + + + + + @@ -65,6 +100,13 @@ Publication Publication publication + + + + + + + @@ -76,6 +118,13 @@ Patent Patent patent + + + + + + + @@ -86,6 +135,13 @@ Dataset or other products Product + + + + + + + @@ -97,6 +153,13 @@ Event Event event + + + + + + + @@ -108,6 +171,13 @@ Equipment Equipment equipment + + + + + + +