Skip to content

Commit

Permalink
Merged dspace-cris-2023_02_x into DSC-1460
Browse files Browse the repository at this point in the history
  • Loading branch information
abollini committed Mar 25, 2024
2 parents 03fc08b + 05d06eb commit 1082406
Show file tree
Hide file tree
Showing 152 changed files with 7,857 additions and 2,017 deletions.
2 changes: 1 addition & 1 deletion dspace-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>org.dspace</groupId>
<artifactId>dspace-parent</artifactId>
<version>cris-2023.02.02-SNAPSHOT</version>
<version>cris-2023.02.03-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
*/
package org.dspace.administer;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPath;
Expand All @@ -30,6 +34,8 @@
import org.dspace.content.service.MetadataFieldService;
import org.dspace.content.service.MetadataSchemaService;
import org.dspace.core.Context;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
Expand Down Expand Up @@ -61,10 +67,18 @@
* }
*/
public class MetadataImporter {
public static final String BASE = DSpaceServicesFactory.getInstance()
.getConfigurationService().getProperty("dspace.dir") + File.separator + "config" + File.separator
+ "registries" + File.separator;
public static final String REGISTRY_METADATA_PROPERTY = "registry.metadata.load";
public static final String REGISTRY_BITSTREAM_FORMAT_PROPERTY = "registry.bitstream-formats.load";

protected static MetadataSchemaService metadataSchemaService = ContentServiceFactory.getInstance()
.getMetadataSchemaService();
protected static MetadataFieldService metadataFieldService = ContentServiceFactory.getInstance()
.getMetadataFieldService();
protected static ConfigurationService configurationService = DSpaceServicesFactory.getInstance()
.getConfigurationService();

/**
* logging category
Expand Down Expand Up @@ -100,18 +114,35 @@ public static void main(String[] args)
Options options = new Options();
options.addOption("f", "file", true, "source xml file for DC fields");
options.addOption("u", "update", false, "update an existing schema");
options.addOption("h", "help", false, "help message");
CommandLine line = parser.parse(options, args);

if (line.hasOption('f')) {
if (line.hasOption('h')) {
usage();
System.exit(1);
} else if (line.hasOption('f')) {
String file = line.getOptionValue('f');
boolean forceUpdate = line.hasOption('u');
loadRegistry(file, forceUpdate);
} else {
usage();
System.exit(1);
boolean forceUpdate = line.hasOption('u');
for (String file : getAllRegistryFiles(REGISTRY_METADATA_PROPERTY)) {
loadRegistry(file, forceUpdate);
}
}
}

/**
* Load all registry file names from config
*
* @param propertyName name of the property that used in config
* @return list of all registry files
*/
public static List<String> getAllRegistryFiles(String propertyName) {
List<String> files = Arrays.asList(configurationService.getArrayProperty(propertyName));
return files.stream().map(file -> BASE + file).collect(Collectors.toList());
}

/**
* Load the data from the specified file path into the database
*
Expand Down Expand Up @@ -285,7 +316,10 @@ private static void loadType(Context context, Node node)
public static void usage() {
String usage = "Use this class with the following option:\n" +
" -f <xml source file> : specify which xml source file " +
"contains the DC fields to import.\n";
"contains the DC fields to import.\n" +
"If you use the script without the -f parameter, then all" +
" registries will be loaded from the config/registries folder\n";

System.out.println(usage);
}
}
43 changes: 40 additions & 3 deletions dspace-api/src/main/java/org/dspace/administer/RegistryLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;



/**
* Loads the bitstream format and Dublin Core type registries into the database.
* Intended for use as a command-line tool.
Expand Down Expand Up @@ -68,7 +70,7 @@ private RegistryLoader() { }
*/
public static void main(String[] argv) throws Exception {
String usage = "Usage: " + RegistryLoader.class.getName()
+ " (-bitstream | -metadata) registry-file.xml";
+ " (-bitstream | -metadata | -all) registry-file.xml";

Context context = null;

Expand All @@ -81,10 +83,21 @@ public static void main(String[] argv) throws Exception {

// Work out what we're loading
if (argv[0].equalsIgnoreCase("-bitstream")) {
RegistryLoader.loadBitstreamFormats(context, argv[1]);
if (argv.length == 1) {
loadAllBitstreamFormats(context);
} else {
RegistryLoader.loadBitstreamFormats(context, argv[1]);
}
} else if (argv[0].equalsIgnoreCase("-metadata")) {
// Call MetadataImporter, as it handles Metadata schema updates
MetadataImporter.loadRegistry(argv[1], true);
if (argv.length == 1) {
loadAllRegistry();
} else {
MetadataImporter.loadRegistry(argv[1], true);
}
} else if (argv[0].equalsIgnoreCase("-all")) {
loadAllBitstreamFormats(context);
loadAllRegistry();
} else {
System.err.println(usage);
}
Expand All @@ -111,6 +124,30 @@ public static void main(String[] argv) throws Exception {
}
}


/**
* Load all bitstream formats from configuration properties
*
* @param context DSpace context object
* @throws Exception
*/
private static void loadAllBitstreamFormats(Context context) throws Exception {
for (String file : MetadataImporter.getAllRegistryFiles(MetadataImporter.REGISTRY_BITSTREAM_FORMAT_PROPERTY)) {
RegistryLoader.loadBitstreamFormats(context, file);
}
}

/**
* Load all metadata registry from configuration properties
*
* @throws Exception
*/
private static void loadAllRegistry() throws Exception {
for (String file : MetadataImporter.getAllRegistryFiles(MetadataImporter.REGISTRY_METADATA_PROPERTY)) {
MetadataImporter.loadRegistry(file, true);
}
}

/**
* Load Bitstream Format metadata
*
Expand Down
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 @@ -43,8 +43,8 @@
import org.dspace.app.deduplication.service.DedupService;
import org.dspace.app.deduplication.service.SearchDeduplication;
import org.dspace.app.deduplication.service.SolrDedupServiceIndexPlugin;
import org.dspace.app.deduplication.utils.DedupUtils;
import org.dspace.app.deduplication.utils.DuplicateItemInfo;
import org.dspace.app.deduplication.utils.IDedupUtils;
import org.dspace.app.deduplication.utils.Signature;
import org.dspace.app.util.Util;
import org.dspace.authorize.AuthorizeException;
Expand Down Expand Up @@ -174,7 +174,7 @@ public class SolrDedupServiceImpl implements DedupService {
protected VersioningService versioningService;

@Autowired(required = true)
protected DedupUtils dedupUtils;
protected IDedupUtils dedupUtils;

/***
* Deduplication status
Expand Down Expand Up @@ -750,8 +750,8 @@ private void setDuplicateDecision(Context context, Item item, UUID duplicatedIte
private List<DuplicateItemInfo> findDuplicationWithDecisions(Context context, Item item) {
try {
return dedupUtils.getAdminDuplicateByIdAndType(context, item.getID(), item.getType()).stream()
.filter(duplication -> isNotEmpty(duplication.getDecisionTypes()))
.collect(Collectors.toList());
.filter(duplication -> isNotEmpty(duplication.getDecisionTypes()))
.collect(Collectors.toList());
} catch (SQLException | SearchServiceException e) {
throw new RuntimeException(e);
}
Expand Down
Loading

0 comments on commit 1082406

Please sign in to comment.