Skip to content

Commit 029adbc

Browse files
eskander17steph-ieffam
authored andcommitted
Merged in DSC-1386 (pull request DSpace#1422)
[DSC-1386] Added support to create template item via struct builder Approved-by: Stefano Maffei
2 parents 064e1e4 + dea151e commit 029adbc

File tree

3 files changed

+200
-1
lines changed

3 files changed

+200
-1
lines changed

dspace-api/src/main/java/org/dspace/administer/StructBuilder.java

+101-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import static org.dspace.content.Item.ANY;
1111
import static org.dspace.content.MetadataSchemaEnum.CRIS;
12+
import static org.dspace.content.authority.Choices.CF_UNSET;
1213
import static org.dspace.content.service.DSpaceObjectService.MD_COPYRIGHT_TEXT;
1314
import static org.dspace.content.service.DSpaceObjectService.MD_INTRODUCTORY_TEXT;
1415
import static org.dspace.content.service.DSpaceObjectService.MD_LICENSE;
@@ -49,12 +50,14 @@
4950
import org.dspace.content.Collection;
5051
import org.dspace.content.Community;
5152
import org.dspace.content.Item;
53+
import org.dspace.content.MetadataField;
5254
import org.dspace.content.MetadataFieldName;
5355
import org.dspace.content.MetadataSchemaEnum;
5456
import org.dspace.content.MetadataValue;
5557
import org.dspace.content.factory.ContentServiceFactory;
5658
import org.dspace.content.service.CollectionService;
5759
import org.dspace.content.service.CommunityService;
60+
import org.dspace.content.service.ItemService;
5861
import org.dspace.core.Context;
5962
import org.dspace.core.CrisConstants;
6063
import org.dspace.eperson.factory.EPersonServiceFactory;
@@ -122,7 +125,8 @@ public class StructBuilder {
122125
= EPersonServiceFactory.getInstance().getEPersonService();
123126
protected static final HandleService handleService
124127
= HandleServiceFactory.getInstance().getHandleService();
125-
128+
protected static final ItemService itemService
129+
= ContentServiceFactory.getInstance().getItemService();
126130
/**
127131
* Default constructor
128132
*/
@@ -407,6 +411,9 @@ private static Element exportACollection(Collection collection) {
407411
Element element = new Element("collection");
408412
element.setAttribute("identifier", collection.getHandle());
409413
element.addContent(new Element("name").setText(collection.getName()));
414+
415+
buildTemplateItem(collection, element);
416+
410417
element.addContent(new Element("description")
411418
.setText(collectionService.getMetadataFirstValue(collection,
412419
MetadataSchemaEnum.DC.getName(), "description", "abstract", Item.ANY)));
@@ -833,6 +840,8 @@ private static Element[] handleCollections(Context context,
833840
collectionService.setMetadataSingleValue(context, collection,
834841
MD_SHORT_DESCRIPTION, Item.ANY, " ");
835842

843+
handleTemplateItem(context, collection, tn);
844+
836845
// import the rest of the metadata
837846
for (Map.Entry<String, MetadataFieldName> entry : collectionMap.entrySet()) {
838847
NodeList nl = (NodeList) xPath.compile(entry.getKey()).evaluate(tn, XPathConstants.NODESET);
@@ -854,6 +863,8 @@ private static Element[] handleCollections(Context context,
854863

855864
String fieldValue;
856865

866+
buildTemplateItem(collection, element);
867+
857868
fieldValue = collectionService.getMetadataFirstValue(collection,
858869
CollectionService.MD_SHORT_DESCRIPTION, Item.ANY);
859870
if (fieldValue != null) {
@@ -930,4 +941,93 @@ private static Element[] handleCollections(Context context,
930941

931942
return elements;
932943
}
944+
945+
private static void handleTemplateItem(Context context, Collection collection, Node tn)
946+
throws XPathExpressionException, SQLException, AuthorizeException {
947+
948+
XPath xPath = XPathFactory.newInstance().newXPath();
949+
Node node = (Node) xPath.compile("templateItem").evaluate(tn, XPathConstants.NODE);
950+
951+
if (node == null) {
952+
return;
953+
}
954+
955+
Item templateItem = itemService.createTemplateItem(context, collection);
956+
957+
NodeList metadataNodes = (NodeList) xPath.compile("metadata").evaluate(node, XPathConstants.NODESET);
958+
959+
for (int i = 0; i < metadataNodes.getLength(); i++) {
960+
Node metadataNode = metadataNodes.item(i);
961+
MetadataFieldName metadataFieldName = buildMetadataFieldName(metadataNode);
962+
963+
Node valueAttribute = (Node) xPath.compile("value").evaluate(metadataNode, XPathConstants.NODE);
964+
Node authorityAttribute = (Node) xPath.compile("authority").evaluate(metadataNode, XPathConstants.NODE);
965+
Node confidenceAttribute = (Node) xPath.compile("confidence").evaluate(metadataNode, XPathConstants.NODE);
966+
967+
String authority = null;
968+
int confidence = CF_UNSET;
969+
970+
if (authorityAttribute != null) {
971+
authority = authorityAttribute.getTextContent();
972+
confidence = confidenceAttribute != null ? Integer.parseInt(confidenceAttribute.getTextContent()) : 600;
973+
}
974+
975+
itemService.addMetadata(context, templateItem, metadataFieldName.schema, metadataFieldName.element,
976+
metadataFieldName.qualifier, ANY, valueAttribute.getTextContent(), authority, confidence);
977+
itemService.update(context, templateItem);
978+
}
979+
}
980+
981+
private static MetadataFieldName buildMetadataFieldName(Node node) {
982+
Node schemaAttribute = node.getAttributes().getNamedItem("schema");
983+
Node elementAttribute = node.getAttributes().getNamedItem("element");
984+
Node qualifierAttribute = node.getAttributes().getNamedItem("qualifier");
985+
986+
if (qualifierAttribute == null) {
987+
return new MetadataFieldName(schemaAttribute.getTextContent(), elementAttribute.getTextContent());
988+
} else {
989+
return new MetadataFieldName(schemaAttribute.getTextContent(),
990+
elementAttribute.getTextContent(), qualifierAttribute.getTextContent());
991+
}
992+
}
993+
994+
private static void buildTemplateItem(Collection collection, Element element) {
995+
996+
try {
997+
Item templateItem = collection.getTemplateItem();
998+
999+
if (templateItem == null) {
1000+
return;
1001+
}
1002+
1003+
Element templateItemElement = new Element("templateItem");
1004+
1005+
for (MetadataValue metadataValue : templateItem.getMetadata()) {
1006+
MetadataField metadataField = metadataValue.getMetadataField();
1007+
Element metadata = new Element("metadata");
1008+
metadata.setAttribute("schema", metadataField.getMetadataSchema().getName());
1009+
metadata.setAttribute("element", metadataField.getElement());
1010+
1011+
if (metadataField.getQualifier() != null) {
1012+
metadata.setAttribute("qualifier", metadataField.getQualifier());
1013+
}
1014+
1015+
metadata.addContent(new Element("value").setText(metadataValue.getValue()));
1016+
1017+
if (metadataValue.getAuthority() != null) {
1018+
metadata.addContent(new Element("authority").setText(metadataValue.getAuthority()));
1019+
metadata.addContent(new Element("confidence").setText(
1020+
String.valueOf(metadataValue.getConfidence())
1021+
));
1022+
}
1023+
1024+
templateItemElement.addContent(metadata);
1025+
}
1026+
1027+
element.addContent(templateItemElement);
1028+
} catch (SQLException e) {
1029+
throw new RuntimeException(e);
1030+
}
1031+
}
1032+
9331033
}

dspace-api/src/test/java/org/dspace/administer/StructBuilderIT.java

+29
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.nio.charset.StandardCharsets;
1818
import java.sql.SQLException;
1919
import java.util.Iterator;
20+
import java.util.UUID;
2021
import javax.xml.parsers.ParserConfigurationException;
2122
import javax.xml.transform.Source;
2223
import javax.xml.transform.stream.StreamSource;
@@ -27,10 +28,12 @@
2728
import org.dspace.authorize.AuthorizeException;
2829
import org.dspace.content.Collection;
2930
import org.dspace.content.Community;
31+
import org.dspace.content.Item;
3032
import org.dspace.content.MetadataSchemaEnum;
3133
import org.dspace.content.factory.ContentServiceFactory;
3234
import org.dspace.content.service.CollectionService;
3335
import org.dspace.content.service.CommunityService;
36+
import org.dspace.content.service.ItemService;
3437
import org.dspace.handle.Handle;
3538
import org.junit.AfterClass;
3639
import org.junit.Before;
@@ -61,6 +64,8 @@ public class StructBuilderIT
6164
= ContentServiceFactory.getInstance().getCommunityService();
6265
private static final CollectionService collectionService
6366
= ContentServiceFactory.getInstance().getCollectionService();
67+
private static final ItemService itemService
68+
= ContentServiceFactory.getInstance().getItemService();
6469

6570
public StructBuilderIT() {
6671
}
@@ -114,6 +119,21 @@ public void setUp() throws SQLException, AuthorizeException, IOException {
114119
" <sidebar>Another sidebar</sidebar>\n" +
115120
" <collection identifier='" + COLLECTION_0_0_0_HANDLE + "'>\n" +
116121
" <name>Collection 0.0.0</name>\n" +
122+
" <templateItem>\n" +
123+
" <metadata schema='dc' element='title'>\n" +
124+
" <value>template item</value>\n" +
125+
" </metadata>\n" +
126+
" <metadata schema='dc' element='contributor' qualifier='author'>\n" +
127+
" <value>Walter White</value>\n" +
128+
" <authority>" + UUID.randomUUID() + "</authority>\n" +
129+
" <confidence>600</confidence>\n" +
130+
" </metadata>\n" +
131+
" <metadata schema='dc' element='contributor' qualifier='author'>\n" +
132+
" <value>Donald, Smith</value>\n" +
133+
" <authority>" + UUID.randomUUID() + "</authority>\n" +
134+
" <confidence>400</confidence>\n" +
135+
" </metadata>\n" +
136+
" </templateItem>\n" +
117137
" <description>A collection</description>\n" +
118138
" <intro>Our next guest needs no introduction</intro>\n" +
119139
" <copyright>1776</copyright>\n" +
@@ -149,6 +169,11 @@ public void setUp() throws SQLException, AuthorizeException, IOException {
149169
" <description/><intro/><copyright/><sidebar/>\n" +
150170
" <collection>\n" +
151171
" <name>Collection 0.0</name>\n" +
172+
" <templateItem>\n" +
173+
" <metadata schema='dc' element='title'>\n" +
174+
" <value>template item</value>\n" +
175+
" </metadata>\n" +
176+
" </templateItem>\n" +
152177
" <description/><intro/><copyright/><sidebar/><license/>\n" +
153178
" </collection>\n" +
154179
" </community>\n" +
@@ -301,6 +326,10 @@ public void testExportStructure()
301326
MetadataSchemaEnum.DC.getName(), "title", null,
302327
null, "Collection 0.0");
303328

329+
Item item = itemService.createTemplateItem(context, collection0_0);
330+
itemService.addMetadata(context, item, MetadataSchemaEnum.DC.getName(), "title", null,
331+
Item.ANY, "template item", null, -1);
332+
304333
// Export the current structure.
305334
System.out.println("exportStructure");
306335
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

dspace/config/sample-structure.xml

+70
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
<name>Person</name>
1111
<entity-type>Person</entity-type>
1212
<submission-type>person</submission-type>
13+
<!-- <templateItem>-->
14+
<!-- <metadata schema="schema" element="element" qualifier="qualifier">-->
15+
<!-- <value></value>-->
16+
<!-- <authority></authority>-->
17+
<!-- <confidence></confidence>-->
18+
<!-- </metadata>-->
19+
<!-- </templateItem>-->
1320
<description />
1421
<intro />
1522
<copyright />
@@ -21,6 +28,13 @@
2128
<name>Project</name>
2229
<entity-type>Project</entity-type>
2330
<submission-type>project</submission-type>
31+
<!-- <templateItem>-->
32+
<!-- <metadata schema="schema" element="element" qualifier="qualifier">-->
33+
<!-- <value></value>-->
34+
<!-- <authority></authority>-->
35+
<!-- <confidence></confidence>-->
36+
<!-- </metadata>-->
37+
<!-- </templateItem>-->
2438
<description />
2539
<intro />
2640
<copyright />
@@ -32,6 +46,13 @@
3246
<name>Funding</name>
3347
<entity-type>Funding</entity-type>
3448
<submission-type>funding</submission-type>
49+
<!-- <templateItem>-->
50+
<!-- <metadata schema="schema" element="element" qualifier="qualifier">-->
51+
<!-- <value></value>-->
52+
<!-- <authority></authority>-->
53+
<!-- <confidence></confidence>-->
54+
<!-- </metadata>-->
55+
<!-- </templateItem>-->
3556
<description />
3657
<intro />
3758
<copyright />
@@ -43,6 +64,13 @@
4364
<name>OrgUnit</name>
4465
<entity-type>OrgUnit</entity-type>
4566
<submission-type>orgunit</submission-type>
67+
<!-- <templateItem>-->
68+
<!-- <metadata schema="schema" element="element" qualifier="qualifier">-->
69+
<!-- <value></value>-->
70+
<!-- <authority></authority>-->
71+
<!-- <confidence></confidence>-->
72+
<!-- </metadata>-->
73+
<!-- </templateItem>-->
4674
<description />
4775
<intro />
4876
<copyright />
@@ -54,6 +82,13 @@
5482
<name>Journal</name>
5583
<entity-type>Journal</entity-type>
5684
<submission-type>journal</submission-type>
85+
<!-- <templateItem>-->
86+
<!-- <metadata schema="schema" element="element" qualifier="qualifier">-->
87+
<!-- <value></value>-->
88+
<!-- <authority></authority>-->
89+
<!-- <confidence></confidence>-->
90+
<!-- </metadata>-->
91+
<!-- </templateItem>-->
5792
<description />
5893
<intro />
5994
<copyright />
@@ -65,6 +100,13 @@
65100
<name>Publication</name>
66101
<entity-type>Publication</entity-type>
67102
<submission-type>publication</submission-type>
103+
<!-- <templateItem>-->
104+
<!-- <metadata schema="schema" element="element" qualifier="qualifier">-->
105+
<!-- <value></value>-->
106+
<!-- <authority></authority>-->
107+
<!-- <confidence></confidence>-->
108+
<!-- </metadata>-->
109+
<!-- </templateItem>-->
68110
<description />
69111
<intro />
70112
<copyright />
@@ -76,6 +118,13 @@
76118
<name>Patent</name>
77119
<entity-type>Patent</entity-type>
78120
<submission-type>patent</submission-type>
121+
<!-- <templateItem>-->
122+
<!-- <metadata schema="schema" element="element" qualifier="qualifier">-->
123+
<!-- <value></value>-->
124+
<!-- <authority></authority>-->
125+
<!-- <confidence></confidence>-->
126+
<!-- </metadata>-->
127+
<!-- </templateItem>-->
79128
<description />
80129
<intro />
81130
<copyright />
@@ -86,6 +135,13 @@
86135
<collection>
87136
<name>Dataset or other products</name>
88137
<entity-type>Product</entity-type>
138+
<!-- <templateItem>-->
139+
<!-- <metadata schema="schema" element="element" qualifier="qualifier">-->
140+
<!-- <value></value>-->
141+
<!-- <authority></authority>-->
142+
<!-- <confidence></confidence>-->
143+
<!-- </metadata>-->
144+
<!-- </templateItem>-->
89145
<description />
90146
<intro />
91147
<copyright />
@@ -97,6 +153,13 @@
97153
<name>Event</name>
98154
<entity-type>Event</entity-type>
99155
<submission-type>event</submission-type>
156+
<!-- <templateItem>-->
157+
<!-- <metadata schema="schema" element="element" qualifier="qualifier">-->
158+
<!-- <value></value>-->
159+
<!-- <authority></authority>-->
160+
<!-- <confidence></confidence>-->
161+
<!-- </metadata>-->
162+
<!-- </templateItem>-->
100163
<description />
101164
<intro />
102165
<copyright />
@@ -108,6 +171,13 @@
108171
<name>Equipment</name>
109172
<entity-type>Equipment</entity-type>
110173
<submission-type>equipment</submission-type>
174+
<!-- <templateItem>-->
175+
<!-- <metadata schema="schema" element="element" qualifier="qualifier">-->
176+
<!-- <value></value>-->
177+
<!-- <authority></authority>-->
178+
<!-- <confidence></confidence>-->
179+
<!-- </metadata>-->
180+
<!-- </templateItem>-->
111181
<description />
112182
<intro />
113183
<copyright />

0 commit comments

Comments
 (0)