diff --git a/ubo-common/src/main/java/org/mycore/mods/enrichment/EnrichmentCommands.java b/ubo-common/src/main/java/org/mycore/mods/enrichment/EnrichmentCommands.java new file mode 100644 index 000000000..fcf140588 --- /dev/null +++ b/ubo-common/src/main/java/org/mycore/mods/enrichment/EnrichmentCommands.java @@ -0,0 +1,38 @@ +package org.mycore.mods.enrichment; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.jdom2.Element; +import org.mycore.access.MCRAccessException; +import org.mycore.common.MCRException; +import org.mycore.datamodel.metadata.MCRMetadataManager; +import org.mycore.datamodel.metadata.MCRObject; +import org.mycore.datamodel.metadata.MCRObjectID; +import org.mycore.frontend.cli.annotation.MCRCommand; +import org.mycore.frontend.cli.annotation.MCRCommandGroup; +import org.mycore.mods.MCRMODSWrapper; + +/** + * Enriches a MODS file by its ID and a given enrichment configuration, compare + * MCR.MODS.EnrichmentResolver.DataSources.<configname>. + */ +@MCRCommandGroup(name = "Enrichment Commands") +public class EnrichmentCommands { + + private final static Logger LOGGER = LogManager.getLogger(); + + @MCRCommand(syntax = "enrich {0} with config {1}", + help = "Enriches existing MODS metadata {0} with a given enrichment configuration {1}", order = 40) + public static void enrichMods(String modsId, String configID) { + try { + MCRObject obj = MCRMetadataManager.retrieveMCRObject(MCRObjectID.getInstance(modsId)); + Element mods = new MCRMODSWrapper(obj).getMODS(); + MCREnricher enricher = new MCREnricher(configID); + enricher.enrich(mods); + MCRMetadataManager.update(obj); + } catch (MCRException | MCRAccessException e) { + LOGGER.error("Error while trying to enrich {} with configuration {}: ", modsId, configID, e); + } + } + +} diff --git a/ubo-common/src/main/java/org/mycore/ubo/importer/DozBibImportServlet.java b/ubo-common/src/main/java/org/mycore/ubo/importer/DozBibImportServlet.java index 52447452b..61fdf4db3 100644 --- a/ubo-common/src/main/java/org/mycore/ubo/importer/DozBibImportServlet.java +++ b/ubo-common/src/main/java/org/mycore/ubo/importer/DozBibImportServlet.java @@ -9,25 +9,29 @@ package org.mycore.ubo.importer; -import java.io.IOException; -import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; - -import javax.xml.transform.TransformerException; - import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.jdom2.Document; import org.jdom2.Element; +import org.jdom2.output.XMLOutputter; import org.mycore.access.MCRAccessException; import org.mycore.common.content.MCRJDOMContent; +import org.mycore.frontend.MCRFrontendUtil; import org.mycore.frontend.servlets.MCRServlet; import org.mycore.frontend.servlets.MCRServletJob; +import org.mycore.services.queuedjob.MCRJob; +import org.mycore.services.queuedjob.MCRJobQueue; import org.mycore.ubo.AccessControl; import org.mycore.ubo.DozBibEntryServlet; import org.mycore.ubo.importer.evaluna.EvalunaImportJob; +import org.mycore.user2.MCRUserManager; import org.xml.sax.SAXException; +import javax.xml.transform.TransformerException; +import java.io.IOException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; + @SuppressWarnings("serial") public class DozBibImportServlet extends MCRServlet { @@ -47,8 +51,23 @@ public void doGetPost(MCRServletJob job) throws Exception { private void handleImportJob(HttpServletRequest req, HttpServletResponse res) throws Exception { Document doc = (Document) (req.getAttribute("MCRXEditorSubmission")); - Element formInput = doc.detachRootElement(); + String doAsync = doc.getRootElement().getAttributeValue("async"); + if ("true".equals(doAsync)) { + MCRJob job = new MCRJob(ImportListJobAction.class); + job.setParameter(ImportListJobAction.EDITOR_SUBMISSION_PARAMETER, new XMLOutputter().outputString(doc)); + job.setParameter(ImportListJobAction.USER_ID_PARAMETER, MCRUserManager.getCurrentUser().getUserName()); + MCRJobQueue.getInstance(ImportListJobAction.class).offer(job); + String referer = req.getHeader("Referer"); + if (referer != null) { + res.sendRedirect(referer+"&list-submitted=true"); + } else { + res.sendRedirect(MCRFrontendUtil.getBaseURL()); + } + return; + } + + Element formInput = doc.detachRootElement(); ImportJob importJob = buildImportJob(formInput); importJob.transform(formInput); diff --git a/ubo-common/src/main/java/org/mycore/ubo/importer/ImportListJobAction.java b/ubo-common/src/main/java/org/mycore/ubo/importer/ImportListJobAction.java new file mode 100644 index 000000000..c17166b0c --- /dev/null +++ b/ubo-common/src/main/java/org/mycore/ubo/importer/ImportListJobAction.java @@ -0,0 +1,115 @@ +package org.mycore.ubo.importer; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.input.SAXBuilder; +import org.mycore.common.MCRMailer; +import org.mycore.common.config.MCRConfiguration2; +import org.mycore.frontend.MCRFrontendUtil; +import org.mycore.services.i18n.MCRTranslation; +import org.mycore.services.queuedjob.MCRJob; +import org.mycore.services.queuedjob.MCRJobAction; +import org.mycore.ubo.importer.evaluna.EvalunaImportJob; +import org.mycore.user2.MCRUser; +import org.mycore.user2.MCRUserManager; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.Optional; +import java.util.concurrent.ExecutionException; + +/** + * Allows to execute a list import asynchronously. It executes the same tasks as in {@link DozBibImportServlet}. + * + * @author shermann (Silvio Hermann) + * */ +public class ImportListJobAction extends MCRJobAction { + + public static String EDITOR_SUBMISSION_PARAMETER = "xEditorSubmission"; + public static String USER_ID_PARAMETER = "userName"; + public static String IMPORT_JOB_ID_PARAMETER = "importJobId"; + + protected static final Logger LOGGER = LogManager.getLogger(ImportListJobAction.class); + + protected static final Optional DEFAULT_EMAIL_FROM = MCRConfiguration2.getString("UBO.Mail.From"); + + public ImportListJobAction() { + } + + public ImportListJobAction(MCRJob mcrJob) { + super(mcrJob); + } + + @Override + public void execute() throws ExecutionException { + String xEditorSubmission = job.getParameter(ImportListJobAction.EDITOR_SUBMISSION_PARAMETER); + + if (xEditorSubmission == null) { + LOGGER.error("No {} parameter provided", ImportListJobAction.EDITOR_SUBMISSION_PARAMETER); + return; + } + + try (InputStream inputStream = new ByteArrayInputStream(xEditorSubmission.getBytes(StandardCharsets.UTF_8))) { + Document doc = new SAXBuilder().build(inputStream); + Element formInput = doc.detachRootElement(); + + String sourceType = formInput.getAttributeValue("sourceType"); + ImportJob importJob = "Evaluna".equals(sourceType) ? new EvalunaImportJob() : new ListImportJob(sourceType); + importJob.transform(formInput); + job.setParameter(IMPORT_JOB_ID_PARAMETER, importJob.getID()); + + if ("true".equals(formInput.getAttributeValue("enrich"))) { + importJob.enrich(); + } + + importJob.saveAndIndex(); + sendMail(importJob); + } catch (Exception e) { + LOGGER.error("Could not transform form input", e); + } + } + + private void sendMail(ImportJob importJob) { + String userName = job.getParameter(ImportListJobAction.USER_ID_PARAMETER); + MCRUser mcrUser = MCRUserManager.getUser(userName); + String eMailAddress = mcrUser.getEMailAddress(); + + if (eMailAddress == null) { + LOGGER.warn("Cannot send e-mail to user {} as user has no e-mail address", userName); + return; + } + + if (ImportListJobAction.DEFAULT_EMAIL_FROM.isEmpty()) { + LOGGER.warn("Cannot send e-mail to user {} as property UBO.Mail.From is not set", userName); + return; + } + + String subject = MCRTranslation.translate("ubo.import.list.email.subject", importJob.getID()); + MCRMailer.send(DEFAULT_EMAIL_FROM.get(), eMailAddress, subject, getBody(importJob)); + } + + private String getBody(ImportJob importJob) { + String url = MCRFrontendUtil.getBaseURL() + "servlets/solr/select?fq=%2BobjectType%3Amods&q=importID%3A%22" + + URLEncoder.encode(importJob.getID(), StandardCharsets.UTF_8) + "%22"; + return MCRTranslation.translate("ubo.import.list.email.body", url); + } + + @Override + public boolean isActivated() { + return true; + } + + @Override + public String name() { + return getClass().getSimpleName(); + } + + @Override + public void rollback() { + LOGGER.warn("Rolling back in {} is not implemented", ImportListJobAction.class.getName()); + } +} diff --git a/ubo-common/src/main/java/org/mycore/ubo/publication/PublicationEventHandler.java b/ubo-common/src/main/java/org/mycore/ubo/publication/PublicationEventHandler.java index 0b0089c99..92f80d945 100644 --- a/ubo-common/src/main/java/org/mycore/ubo/publication/PublicationEventHandler.java +++ b/ubo-common/src/main/java/org/mycore/ubo/publication/PublicationEventHandler.java @@ -175,9 +175,9 @@ private void handleName(Element modsNameElement) { .ifPresent(trueValue -> { List elementsToRemove = modsNameElement.getChildren("nameIdentifier", MCRConstants.MODS_NAMESPACE) - .stream() - .filter(element -> element.getAttributeValue("type").equals(leadIDName)) - .collect(Collectors.toList()); + .stream() + .filter(element -> leadIDName.equals(element.getAttributeValue("type"))) + .collect(Collectors.toList()); elementsToRemove.forEach(modsNameElement::removeContent); }); } diff --git a/ubo-common/src/main/resources/META-INF/resources/import-list.xed b/ubo-common/src/main/resources/META-INF/resources/import-list.xed index 2a4ea4c94..e2869a64c 100644 --- a/ubo-common/src/main/resources/META-INF/resources/import-list.xed +++ b/ubo-common/src/main/resources/META-INF/resources/import-list.xed @@ -1,15 +1,40 @@ - - Literaturliste importieren + + + <i18n:de> + Literaturliste importieren + </i18n:de> + <i18n:en> + Import bibliography + </i18n:en> + + + + +
+
+ +
+
+
+

- Über dieses Formular können Sie eine Literaturliste im - BibTeX Format, - alle Publikationen einer gegebenen ORCID oder eine CSV-Datei importieren. + + Über dieses Formular können Sie eine Literaturliste im + BibTeX Format, + alle Publikationen einer gegebenen ORCID oder eine CSV-Datei importieren. + + + Using this form you can import a bibliography in + BibTeX format, + all publications of a given ORCID or a CSV file. +

+ @@ -25,11 +50,13 @@
- Literaturliste importieren + + : +
-