Skip to content

Commit

Permalink
Consolidate citation creation in a utility class
Browse files Browse the repository at this point in the history
  • Loading branch information
markpatton committed Jan 3, 2025
1 parent 76aa73b commit 94a9f14
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.pass.deposit.model.DepositMetadata.Manuscript;
import org.eclipse.pass.deposit.model.DepositMetadata.Person;
import org.eclipse.pass.deposit.model.DepositSubmission;
import org.eclipse.pass.deposit.provider.util.CitationUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -80,7 +81,7 @@ public String patchWorkspaceItem(DepositSubmission submission) {
metadata.add(add_array(SECTION, "dc.description.abstract", manuscriptMd.getMsAbstract()));
}

String citation = createCitation(submission);
String citation = CitationUtil.createCitation(submission);

if (!citation.isEmpty()) {
metadata.add(add_array(SECTION, "dc.identifier.citation", citation));
Expand Down Expand Up @@ -143,64 +144,4 @@ private JSONObject array_value(String value) {
obj.put("value", value);
return obj;
}

// TODO Could we use citation from CrossRef metadata?
private String createCitation(DepositSubmission submission) {
DepositMetadata submissionMd = submission.getMetadata();
DepositMetadata.Article articleMd = submissionMd.getArticleMetadata();
DepositMetadata.Journal journalMd = submissionMd.getJournalMetadata();

StringBuilder citationBldr = new StringBuilder();

int authorIndex = 0;
for (Person p: submissionMd.getPersons()) {
if (p.getType() == DepositMetadata.PERSON_TYPE.author) {
// Citation: For author 0, add name. For authorIndex 1 and 2, add comma then name.
// For author 3, add comma and "et al". For later authorIndex, do nothing.
if (authorIndex == 0) {
citationBldr.append(p.getReversedName());
} else if (authorIndex <= 2) {
citationBldr.append(", " + p.getReversedName());
} else if (authorIndex == 3) {
citationBldr.append(", et al");
}
authorIndex++;
}
}

// Add period at end of author list in citation

if (citationBldr.length() > 0) {
citationBldr.append(".");
}

// Attach a <dc:identifier:citation> if not empty
// publication date - after a single space, in parens, followed by "."
if (journalMd != null && journalMd.getPublicationDate() != null) {
citationBldr.append(" (" + journalMd.getPublicationDate().
format(DateTimeFormatter.ISO_LOCAL_DATE) + ").");
}
// article title - after single space, in double quotes with "." inside
if (articleMd != null && articleMd.getTitle() != null && !articleMd.getTitle().isEmpty()) {
citationBldr.append(" \"" + articleMd.getTitle() + ".\"");
}
// journal title - after single space, followed by "."
if (journalMd != null && journalMd.getJournalTitle() != null && !journalMd.getJournalTitle().isEmpty()) {
citationBldr.append(" " + journalMd.getJournalTitle() + ".");
}
// volume - after single space
if (articleMd != null && articleMd.getVolume() != null && !articleMd.getVolume().isEmpty()) {
citationBldr.append(" " + articleMd.getVolume());
}
// issue - after single space, inside parens, followed by "."
if (articleMd != null && articleMd.getIssue() != null && !articleMd.getIssue().isEmpty()) {
citationBldr.append(" (" + articleMd.getIssue() + ").");
}
// DOI - after single space, followed by "."
if (articleMd != null && articleMd.getDoi() != null) {
citationBldr.append(" " + articleMd.getDoi().toString() + ".");
}

return citationBldr.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import au.edu.apsr.mtk.base.AmdSec;
import au.edu.apsr.mtk.base.Constants;
import au.edu.apsr.mtk.base.Div;
import au.edu.apsr.mtk.base.DmdSec;
Expand All @@ -70,11 +68,11 @@
import au.edu.apsr.mtk.base.METSWrapper;
import au.edu.apsr.mtk.base.MdSec;
import au.edu.apsr.mtk.base.MdWrap;
import au.edu.apsr.mtk.base.SourceMD;
import au.edu.apsr.mtk.base.StructMap;
import org.eclipse.pass.deposit.assembler.PackageStream;
import org.eclipse.pass.deposit.model.DepositMetadata;
import org.eclipse.pass.deposit.model.DepositSubmission;
import org.eclipse.pass.deposit.provider.util.CitationUtil;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
Expand Down Expand Up @@ -105,8 +103,6 @@ public class DspaceMetadataDomWriter {

private METS mets;

private int authorIndex;

DspaceMetadataDomWriter(DocumentBuilderFactory dbf) {
try {
this.dbf = dbf;
Expand Down Expand Up @@ -409,7 +405,6 @@ Element createDublinCoreMetadataDCMES(DepositSubmission submission) {
// Attach a <dc:contributor> for each Person associated with the submission to the Manuscript metadata
DepositMetadata nimsMd = submission.getMetadata();
DepositMetadata.Manuscript manuscriptMd = nimsMd.getManuscriptMetadata();
DepositMetadata.Article articleMd = nimsMd.getArticleMetadata();
DepositMetadata.Journal journalMd = nimsMd.getJournalMetadata();

// Attach a <dc:title> for the Manuscript title
Expand All @@ -435,68 +430,26 @@ Element createDublinCoreMetadataDCMES(DepositSubmission submission) {
record.appendChild(publisher);
}

// Begin building citation string
StringBuilder citationBldr = new StringBuilder();

// Attach a <dc:contributor> for each author of the manuscript and add authorIndex to citation
authorIndex = 0;
// Attach a <dc:contributor> for each author of the manuscript
nimsMd.getPersons().forEach(p -> {
// Only include authorIndex, PIs and CoPIs as contributors
if (p.getType() != DepositMetadata.PERSON_TYPE.submitter) {
Element contributor = dcDocument.createElementNS(DC_NS, asQname(DC_NS, DC_CONTRIBUTOR));
contributor.setTextContent(p.getName());
record.appendChild(contributor);
}

if (p.getType() == DepositMetadata.PERSON_TYPE.author) {
// Citation: For author 0, add name. For authorIndex 1 and 2, add comma then name.
// For author 3, add comma and "et al". For later authorIndex, do nothing.
if (authorIndex == 0) {
citationBldr.append(p.getReversedName());
} else if (authorIndex <= 2) {
citationBldr.append(", " + p.getReversedName());
} else if (authorIndex == 3) {
citationBldr.append(", et al");
}
authorIndex++;
}
});
if (authorIndex == 0) {

if (nimsMd.getPersons().stream().filter(p -> p.getType() == DepositMetadata.PERSON_TYPE.author).count() == 0) {
throw new RuntimeException("No authors found in the manuscript metadata!");
}
// Add period at end of author list in citation
citationBldr.append(".");

// Attach a <dc:identifier:citation> if not empty
// publication date - after a single space, in parens, followed by "."
if (journalMd != null && journalMd.getPublicationDate() != null) {
citationBldr.append(" (" + journalMd.getPublicationDate().format(DateTimeFormatter.ISO_LOCAL_DATE) + ").");
}
// article title - after single space, in double quotes with "." inside
if (articleMd != null && articleMd.getTitle() != null && !articleMd.getTitle().isEmpty()) {
citationBldr.append(" \"" + articleMd.getTitle() + ".\"");
}
// journal title - after single space, followed by "."
if (journalMd != null && journalMd.getJournalTitle() != null && !journalMd.getJournalTitle().isEmpty()) {
citationBldr.append(" " + journalMd.getJournalTitle() + ".");
}
// volume - after single space
if (articleMd != null && articleMd.getVolume() != null && !articleMd.getVolume().isEmpty()) {
citationBldr.append(" " + articleMd.getVolume());
}
// issue - after single space, inside parens, followed by "."
if (articleMd != null && articleMd.getIssue() != null && !articleMd.getIssue().isEmpty()) {
citationBldr.append(" (" + articleMd.getIssue() + ").");
}
// DOI - after single space, followed by "."
if (articleMd != null && articleMd.getDoi() != null) {
citationBldr.append(" " + articleMd.getDoi().toString() + ".");
}
String citation = CitationUtil.createCitation(submission);

if (!citationBldr.toString().isEmpty()) {
Element citation = dcDocument.createElementNS(DCTERMS_NS, asQname(DCTERMS_NS, DCT_BIBLIOCITATION));
citation.setTextContent(citationBldr.toString());
record.appendChild(citation);
if (!citation.isEmpty()) {
Element el = dcDocument.createElementNS(DCTERMS_NS, asQname(DCTERMS_NS, DCT_BIBLIOCITATION));
el.setTextContent(citation);
record.appendChild(el);
}

return record;
Expand Down Expand Up @@ -656,47 +609,6 @@ private FileSec createFileSec() throws METSException {
return fs;
}

private SourceMD getSourceMd(String id) throws METSException {
if (id == null) {
return createSourceMd();
}

Optional<AmdSec> amdSec = mets
.getAmdSecs()
.stream()
.filter(candidateAmdSec -> candidateAmdSec.getSourceMD(id) != null)
.findAny();

if (amdSec.isPresent()) {
return amdSec.get().getSourceMD(id);
}

throw new RuntimeException("SourceMD with id '" + id + "' not found.");
}

private SourceMD createSourceMd() throws METSException {
AmdSec amdSec = getAmdSec();
SourceMD sourceMD = amdSec.newSourceMD();
sourceMD.setID(mintId());
amdSec.addSourceMD(sourceMD);
return sourceMD;
}

private AmdSec getAmdSec() throws METSException {
if (mets.getAmdSecs() == null || mets.getAmdSecs().isEmpty()) {
return createAmdSec();
}

return mets.getAmdSecs().get(0);
}

private AmdSec createAmdSec() throws METSException {
AmdSec as = mets.newAmdSec();
mets.addAmdSec(as);
as.setID(mintId());
return as;
}

/**
* Obtains the specified {@code <dmdSec>}, or creates a new {@code <dmdSec>} if {@code id} is {@code null}.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.eclipse.pass.deposit.provider.util;

import java.time.format.DateTimeFormatter;

import org.eclipse.pass.deposit.model.DepositMetadata;
import org.eclipse.pass.deposit.model.DepositMetadata.Person;
import org.eclipse.pass.deposit.model.DepositSubmission;

public class CitationUtil {
private CitationUtil() {}

// TODO Could we use citation from CrossRef metadata?
public static String createCitation(DepositSubmission submission) {
DepositMetadata submissionMd = submission.getMetadata();
DepositMetadata.Article articleMd = submissionMd.getArticleMetadata();
DepositMetadata.Journal journalMd = submissionMd.getJournalMetadata();

StringBuilder citationBldr = new StringBuilder();

int authorIndex = 0;
for (Person p: submissionMd.getPersons()) {
if (p.getType() == DepositMetadata.PERSON_TYPE.author) {
// Citation: For author 0, add name. For authorIndex 1 and 2, add comma then name.
// For author 3, add comma and "et al". For later authorIndex, do nothing.
if (authorIndex == 0) {
citationBldr.append(p.getReversedName());
} else if (authorIndex <= 2) {
citationBldr.append(", " + p.getReversedName());
} else if (authorIndex == 3) {
citationBldr.append(", et al");
}
authorIndex++;
}
}

// Add period at end of author list in citation

if (citationBldr.length() > 0) {
citationBldr.append(".");
}

// Attach a <dc:identifier:citation> if not empty
// publication date - after a single space, in parens, followed by "."
if (journalMd != null && journalMd.getPublicationDate() != null) {
citationBldr.append(" (" + journalMd.getPublicationDate().
format(DateTimeFormatter.ISO_LOCAL_DATE) + ").");
}
// article title - after single space, in double quotes with "." inside
if (articleMd != null && articleMd.getTitle() != null && !articleMd.getTitle().isEmpty()) {
citationBldr.append(" \"" + articleMd.getTitle() + ".\"");
}
// journal title - after single space, followed by "."
if (journalMd != null && journalMd.getJournalTitle() != null && !journalMd.getJournalTitle().isEmpty()) {
citationBldr.append(" " + journalMd.getJournalTitle() + ".");
}
// volume - after single space
if (articleMd != null && articleMd.getVolume() != null && !articleMd.getVolume().isEmpty()) {
citationBldr.append(" " + articleMd.getVolume());
}
// issue - after single space, inside parens, followed by "."
if (articleMd != null && articleMd.getIssue() != null && !articleMd.getIssue().isEmpty()) {
citationBldr.append(" (" + articleMd.getIssue() + ").");
}
// DOI - after single space, followed by "."
if (articleMd != null && articleMd.getDoi() != null) {
citationBldr.append(" " + articleMd.getDoi().toString() + ".");
}

return citationBldr.toString();
}
}

0 comments on commit 94a9f14

Please sign in to comment.