Skip to content

Commit

Permalink
Merged in DSC-1386 (pull request DSpace#1422)
Browse files Browse the repository at this point in the history
[DSC-1386] Added support to create template item via struct builder

Approved-by: Stefano Maffei
  • Loading branch information
eskander17 authored and steph-ieffam committed Feb 16, 2024
2 parents 064e1e4 + dea151e commit 029adbc
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 1 deletion.
102 changes: 101 additions & 1 deletion dspace-api/src/main/java/org/dspace/administer/StructBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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<String, MetadataFieldName> entry : collectionMap.entrySet()) {
NodeList nl = (NodeList) xPath.compile(entry.getKey()).evaluate(tn, XPathConstants.NODESET);
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
}
Expand Down Expand Up @@ -114,6 +119,21 @@ public void setUp() throws SQLException, AuthorizeException, IOException {
" <sidebar>Another sidebar</sidebar>\n" +
" <collection identifier='" + COLLECTION_0_0_0_HANDLE + "'>\n" +
" <name>Collection 0.0.0</name>\n" +
" <templateItem>\n" +
" <metadata schema='dc' element='title'>\n" +
" <value>template item</value>\n" +
" </metadata>\n" +
" <metadata schema='dc' element='contributor' qualifier='author'>\n" +
" <value>Walter White</value>\n" +
" <authority>" + UUID.randomUUID() + "</authority>\n" +
" <confidence>600</confidence>\n" +
" </metadata>\n" +
" <metadata schema='dc' element='contributor' qualifier='author'>\n" +
" <value>Donald, Smith</value>\n" +
" <authority>" + UUID.randomUUID() + "</authority>\n" +
" <confidence>400</confidence>\n" +
" </metadata>\n" +
" </templateItem>\n" +
" <description>A collection</description>\n" +
" <intro>Our next guest needs no introduction</intro>\n" +
" <copyright>1776</copyright>\n" +
Expand Down Expand Up @@ -149,6 +169,11 @@ public void setUp() throws SQLException, AuthorizeException, IOException {
" <description/><intro/><copyright/><sidebar/>\n" +
" <collection>\n" +
" <name>Collection 0.0</name>\n" +
" <templateItem>\n" +
" <metadata schema='dc' element='title'>\n" +
" <value>template item</value>\n" +
" </metadata>\n" +
" </templateItem>\n" +
" <description/><intro/><copyright/><sidebar/><license/>\n" +
" </collection>\n" +
" </community>\n" +
Expand Down Expand Up @@ -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();
Expand Down
70 changes: 70 additions & 0 deletions dspace/config/sample-structure.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
<name>Person</name>
<entity-type>Person</entity-type>
<submission-type>person</submission-type>
<!-- <templateItem>-->
<!-- <metadata schema="schema" element="element" qualifier="qualifier">-->
<!-- <value></value>-->
<!-- <authority></authority>-->
<!-- <confidence></confidence>-->
<!-- </metadata>-->
<!-- </templateItem>-->
<description />
<intro />
<copyright />
Expand All @@ -21,6 +28,13 @@
<name>Project</name>
<entity-type>Project</entity-type>
<submission-type>project</submission-type>
<!-- <templateItem>-->
<!-- <metadata schema="schema" element="element" qualifier="qualifier">-->
<!-- <value></value>-->
<!-- <authority></authority>-->
<!-- <confidence></confidence>-->
<!-- </metadata>-->
<!-- </templateItem>-->
<description />
<intro />
<copyright />
Expand All @@ -32,6 +46,13 @@
<name>Funding</name>
<entity-type>Funding</entity-type>
<submission-type>funding</submission-type>
<!-- <templateItem>-->
<!-- <metadata schema="schema" element="element" qualifier="qualifier">-->
<!-- <value></value>-->
<!-- <authority></authority>-->
<!-- <confidence></confidence>-->
<!-- </metadata>-->
<!-- </templateItem>-->
<description />
<intro />
<copyright />
Expand All @@ -43,6 +64,13 @@
<name>OrgUnit</name>
<entity-type>OrgUnit</entity-type>
<submission-type>orgunit</submission-type>
<!-- <templateItem>-->
<!-- <metadata schema="schema" element="element" qualifier="qualifier">-->
<!-- <value></value>-->
<!-- <authority></authority>-->
<!-- <confidence></confidence>-->
<!-- </metadata>-->
<!-- </templateItem>-->
<description />
<intro />
<copyright />
Expand All @@ -54,6 +82,13 @@
<name>Journal</name>
<entity-type>Journal</entity-type>
<submission-type>journal</submission-type>
<!-- <templateItem>-->
<!-- <metadata schema="schema" element="element" qualifier="qualifier">-->
<!-- <value></value>-->
<!-- <authority></authority>-->
<!-- <confidence></confidence>-->
<!-- </metadata>-->
<!-- </templateItem>-->
<description />
<intro />
<copyright />
Expand All @@ -65,6 +100,13 @@
<name>Publication</name>
<entity-type>Publication</entity-type>
<submission-type>publication</submission-type>
<!-- <templateItem>-->
<!-- <metadata schema="schema" element="element" qualifier="qualifier">-->
<!-- <value></value>-->
<!-- <authority></authority>-->
<!-- <confidence></confidence>-->
<!-- </metadata>-->
<!-- </templateItem>-->
<description />
<intro />
<copyright />
Expand All @@ -76,6 +118,13 @@
<name>Patent</name>
<entity-type>Patent</entity-type>
<submission-type>patent</submission-type>
<!-- <templateItem>-->
<!-- <metadata schema="schema" element="element" qualifier="qualifier">-->
<!-- <value></value>-->
<!-- <authority></authority>-->
<!-- <confidence></confidence>-->
<!-- </metadata>-->
<!-- </templateItem>-->
<description />
<intro />
<copyright />
Expand All @@ -86,6 +135,13 @@
<collection>
<name>Dataset or other products</name>
<entity-type>Product</entity-type>
<!-- <templateItem>-->
<!-- <metadata schema="schema" element="element" qualifier="qualifier">-->
<!-- <value></value>-->
<!-- <authority></authority>-->
<!-- <confidence></confidence>-->
<!-- </metadata>-->
<!-- </templateItem>-->
<description />
<intro />
<copyright />
Expand All @@ -97,6 +153,13 @@
<name>Event</name>
<entity-type>Event</entity-type>
<submission-type>event</submission-type>
<!-- <templateItem>-->
<!-- <metadata schema="schema" element="element" qualifier="qualifier">-->
<!-- <value></value>-->
<!-- <authority></authority>-->
<!-- <confidence></confidence>-->
<!-- </metadata>-->
<!-- </templateItem>-->
<description />
<intro />
<copyright />
Expand All @@ -108,6 +171,13 @@
<name>Equipment</name>
<entity-type>Equipment</entity-type>
<submission-type>equipment</submission-type>
<!-- <templateItem>-->
<!-- <metadata schema="schema" element="element" qualifier="qualifier">-->
<!-- <value></value>-->
<!-- <authority></authority>-->
<!-- <confidence></confidence>-->
<!-- </metadata>-->
<!-- </templateItem>-->
<description />
<intro />
<copyright />
Expand Down

0 comments on commit 029adbc

Please sign in to comment.