CitationStyle.java
/*
* Copyright 2024 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.server.service.worker.bib;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.genesys.server.api.v2.model.bib.CitationDTO;
import org.genesys.server.model.bib.CitationType;
import org.springframework.stereotype.Component;
/**
* Export citations in style!
*/
@Component
public class CitationStyle {
/**
* Export in APA style.
*
* <code>
* Babiker, S. A., Khair, M. A., Ali, A. A., Abdallah, M. A., Hagelhassan, A. M., Mohamed, E. I., Kamal, N. M., Tsujimoto, H., & Tahir, I. S. (2024). Multi-locational Evaluation of Forage-suited Selected Sudan Pearl Millet (Pennisetum glaucum LR Br.) Accessions Identified High-Yielding and Stable Genotypes in Irrigated, Arid Environments. https://www.preprints.org/manuscript/202403.1662/download/final_file
* </code>
*
* @param citation Reference to cite
* @return APA-style citation
*/
public String toAPA(CitationDTO citation) {
StringBuffer sb = new StringBuffer();
if (citation.getAuthors() != null) {
int pos = 0;
for (var author : citation.getAuthors()) {
pos++;
sb.append(author.getLastName());
if (author.getOtherNames() != null) {
sb.append(", ").append(abbreviateNames(author.getOtherNames()));
}
if (pos == citation.getAuthors().size() - 1) {
sb.append(", & ");
} else if (pos < citation.getAuthors().size()) {
sb.append(", ");
} else {
sb.append(" ");
}
}
}
if (citation.getPublicationYear() != null) {
sb.append("(").append(citation.getPublicationYear()).append(")").append(". ");
}
if (citation.getTitle() != null) {
sb.append(citation.getTitle()).append(". ");
}
if (citation.getPublication() != null) {
if (citation.getType() == CitationType.BOOK_SECTION) {
sb.append("In ");
sb.append(citation.getPublication());
if (citation.getPages() != null) {
sb.append(" (pp. ").append(citation.getPages()).append(")");
}
sb.append(". ");
} else {
sb.append(citation.getPublication());
if (citation.getVolume() != null) {
sb.append(", ").append(citation.getVolume());
if (citation.getIssue() != null) {
sb.append("(").append(citation.getIssue()).append(")");
}
}
if (citation.getPages() != null) {
sb.append(", ").append(citation.getPages());
}
sb.append(". ");
}
}
if (citation.getPublisher() != null) {
sb.append(citation.getPublisher()).append(". ");
}
if (citation.getDoi() != null) {
sb.append("https://doi.org/").append(citation.getDoi());
} else {
if (citation.getUrls() != null && citation.getUrls().size() > 0) {
sb.append(citation.getUrls().get(0));
}
}
return StringUtils.trimToNull(sb.toString());
}
private String abbreviateNames(String otherNames) {
var names = otherNames.split("\\s+");
return Arrays.stream(names).map(StringUtils::trimToNull).filter(Objects::nonNull).map(n -> n.substring(0, 1) + ".").collect(Collectors.joining(" "));
}
}