Fix dspace-api module per new code style

This commit is contained in:
Tim Donohue
2018-02-14 10:53:46 -06:00
parent 8ffc97f7f9
commit 8a48f782ea
1051 changed files with 53347 additions and 63373 deletions

View File

@@ -7,20 +7,37 @@
*/
package org.dspace.disseminate;
import java.awt.Color;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.log4j.Logger;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.service.AuthorizeService;
import org.dspace.content.*;
import org.dspace.content.Bitstream;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.content.MetadataValue;
import org.dspace.content.service.BitstreamService;
import org.dspace.content.service.CommunityService;
import org.dspace.content.service.ItemService;
@@ -31,12 +48,6 @@ import org.dspace.services.ConfigurationService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import java.awt.*;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
import java.util.List;
/**
* The Citation Document produces a dissemination package (DIP) that is different that the archival package (AIP).
* In this case we append the descriptive metadata to the end (configurable) of the document. i.e. last page of PDF.
@@ -97,7 +108,6 @@ public class CitationDocumentServiceImpl implements CitationDocumentService, Ini
@Autowired(required = true)
protected HandleService handleService;
@Override
public void afterPropertiesSet() throws Exception {
// Add valid format MIME types to set. This could be put in the Schema
@@ -123,64 +133,63 @@ public class CitationDocumentServiceImpl implements CitationDocumentService, Ini
citationEnabledGlobally = configurationService.getBooleanProperty("citation-page.enable_globally", false);
//Load enabled collections
String[] citationEnabledCollections = configurationService.getArrayProperty("citation-page.enabled_collections");
String[] citationEnabledCollections = configurationService
.getArrayProperty("citation-page.enabled_collections");
citationEnabledCollectionsList = Arrays.asList(citationEnabledCollections);
//Load enabled communities, and add to collection-list
String[] citationEnabledCommunities = configurationService.getArrayProperty("citation-page.enabled_communities");
if(citationEnabledCollectionsList == null) {
String[] citationEnabledCommunities = configurationService
.getArrayProperty("citation-page.enabled_communities");
if (citationEnabledCollectionsList == null) {
citationEnabledCollectionsList = new ArrayList<String>();
}
if(citationEnabledCommunities != null && citationEnabledCommunities.length > 0)
{
if (citationEnabledCommunities != null && citationEnabledCommunities.length > 0) {
Context context = null;
try
{
try {
context = new Context();
for(String communityString : citationEnabledCommunities) {
for (String communityString : citationEnabledCommunities) {
DSpaceObject dsoCommunity = handleService.resolveToObject(context, communityString.trim());
if(dsoCommunity instanceof Community) {
Community community = (Community)dsoCommunity;
if (dsoCommunity instanceof Community) {
Community community = (Community) dsoCommunity;
List<Collection> collections = communityService.getAllCollections(context, community);
for(Collection collection : collections) {
for (Collection collection : collections) {
citationEnabledCollectionsList.add(collection.getHandle());
}
} else {
log.error("Invalid community for citation.enabled_communities, value:" + communityString.trim());
log.error(
"Invalid community for citation.enabled_communities, value:" + communityString.trim());
}
}
} catch (SQLException e) {
log.error(e.getMessage());
}
finally {
if (context!=null)
} finally {
if (context != null) {
context.abort();
}
}
}
// Configurable text/fields, we'll set sane defaults
header1 = configurationService.getArrayProperty("citation-page.header1");
if (header1==null || header1.length==0)
{
header1 = new String[]{"DSpace Institution", ""};
if (header1 == null || header1.length == 0) {
header1 = new String[] {"DSpace Institution", ""};
}
header2 = configurationService.getArrayProperty("citation-page.header2");
if (header2==null || header2.length==0)
{
header2 = new String[]{"DSpace Repository", "http://dspace.org"};
if (header2 == null || header2.length == 0) {
header2 = new String[] {"DSpace Repository", "http://dspace.org"};
}
fields = configurationService.getArrayProperty("citation-page.fields");
if (fields==null || fields.length==0)
{
fields = new String[]{"dc.date.issued", "dc.title", "dc.creator", "dc.contributor.author", "dc.publisher", "_line_", "dc.identifier.citation", "dc.identifier.uri"};
if (fields == null || fields.length == 0) {
fields = new String[] {"dc.date.issued", "dc.title", "dc.creator", "dc.contributor.author",
"dc.publisher", "_line_", "dc.identifier.citation", "dc.identifier.uri"};
}
String footerConfig = configurationService.getProperty("citation-page.footer");
if(StringUtils.isNotBlank(footerConfig)) {
if (StringUtils.isNotBlank(footerConfig)) {
footer = footerConfig;
} else {
footer = "Downloaded from DSpace Repository, DSpace Institution's institutional repository";
@@ -189,9 +198,9 @@ public class CitationDocumentServiceImpl implements CitationDocumentService, Ini
//Ensure a temp directory is available
String tempDirString = configurationService.getProperty("dspace.dir") + File.separator + "temp";
tempDir = new File(tempDirString);
if(!tempDir.exists()) {
if (!tempDir.exists()) {
boolean success = tempDir.mkdir();
if(success) {
if (success) {
log.info("Created temp directory at: " + tempDirString);
} else {
log.info("Unable to create temp directory at: " + tempDirString);
@@ -200,7 +209,8 @@ public class CitationDocumentServiceImpl implements CitationDocumentService, Ini
}
protected CitationDocumentServiceImpl() {}
protected CitationDocumentServiceImpl() {
}
/**
* Boolean to determine is citation-functionality is enabled globally for entire site.
@@ -214,18 +224,18 @@ public class CitationDocumentServiceImpl implements CitationDocumentService, Ini
protected boolean isCitationEnabledThroughCollection(Context context, Bitstream bitstream) throws SQLException {
//Reject quickly if no-enabled collections
if(citationEnabledCollectionsList.size() == 0) {
if (citationEnabledCollectionsList.size() == 0) {
return false;
}
DSpaceObject owningDSO = bitstreamService.getParentObject(context, bitstream);
if(owningDSO instanceof Item) {
Item item = (Item)owningDSO;
if (owningDSO instanceof Item) {
Item item = (Item) owningDSO;
List<Collection> collections = item.getCollections();
for(Collection collection : collections) {
if(citationEnabledCollectionsList.contains(collection.getHandle())) {
for (Collection collection : collections) {
if (citationEnabledCollectionsList.contains(collection.getHandle())) {
return true;
}
}
@@ -237,11 +247,11 @@ public class CitationDocumentServiceImpl implements CitationDocumentService, Ini
@Override
public Boolean isCitationEnabledForBitstream(Bitstream bitstream, Context context) throws SQLException {
if(isCitationEnabledGlobally() || isCitationEnabledThroughCollection(context, bitstream)) {
if (isCitationEnabledGlobally() || isCitationEnabledThroughCollection(context, bitstream)) {
boolean adminUser = authorizeService.isAdmin(context);
if(!adminUser && canGenerateCitationVersion(context, bitstream)) {
if (!adminUser && canGenerateCitationVersion(context, bitstream)) {
return true;
}
}
@@ -258,7 +268,7 @@ public class CitationDocumentServiceImpl implements CitationDocumentService, Ini
protected Boolean citationAsFirstPage = null;
protected Boolean isCitationFirstPage() {
if(citationAsFirstPage == null) {
if (citationAsFirstPage == null) {
citationAsFirstPage = configurationService.getBooleanProperty("citation-page.citation_as_first_page", true);
}
@@ -266,8 +276,7 @@ public class CitationDocumentServiceImpl implements CitationDocumentService, Ini
}
@Override
public boolean canGenerateCitationVersion(Context context, Bitstream bitstream) throws SQLException
{
public boolean canGenerateCitationVersion(Context context, Bitstream bitstream) throws SQLException {
return VALID_TYPES.contains(bitstream.getFormat(context).getMIMEType());
}
@@ -284,7 +293,7 @@ public class CitationDocumentServiceImpl implements CitationDocumentService, Ini
addCoverPageToDocument(document, sourceDocument, coverPage);
//We already have the full PDF in memory, so keep it there
try(ByteArrayOutputStream out = new ByteArrayOutputStream()) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
document.save(out);
byte[] data = out.toByteArray();
@@ -297,7 +306,8 @@ public class CitationDocumentServiceImpl implements CitationDocumentService, Ini
}
}
protected void generateCoverPage(Context context, PDDocument document, PDPage coverPage, Item item) throws IOException {
protected void generateCoverPage(Context context, PDDocument document, PDPage coverPage, Item item)
throws IOException {
PDPageContentStream contentStream = new PDPageContentStream(document, coverPage);
try {
int ypos = 760;
@@ -312,45 +322,46 @@ public class CitationDocumentServiceImpl implements CitationDocumentService, Ini
String[][] content = {header1};
drawTable(coverPage, contentStream, ypos, xpos, content, fontHelveticaBold, 11, false);
ypos -=(ygap);
ypos -= (ygap);
String[][] content2 = {header2};
drawTable(coverPage, contentStream, ypos, xpos, content2, fontHelveticaBold, 11, false);
ypos -=ygap;
ypos -= ygap;
contentStream.fillRect(xpos, ypos, xwidth, 1);
contentStream.closeAndStroke();
String[][] content3 = {{getOwningCommunity(context, item), getOwningCollection(item)}};
drawTable(coverPage, contentStream, ypos, xpos, content3, fontHelvetica, 9, false);
ypos -=ygap;
ypos -= ygap;
contentStream.fillRect(xpos, ypos, xwidth, 1);
contentStream.closeAndStroke();
ypos -=(ygap*2);
ypos -= (ygap * 2);
for(String field : fields) {
for (String field : fields) {
field = field.trim();
PDFont font = fontHelvetica;
int fontSize = 11;
if(field.contains("title")) {
if (field.contains("title")) {
fontSize = 26;
ypos -= ygap;
} else if(field.contains("creator") || field.contains("contributor")) {
} else if (field.contains("creator") || field.contains("contributor")) {
fontSize = 16;
}
if(field.equals("_line_")) {
if (field.equals("_line_")) {
contentStream.fillRect(xpos, ypos, xwidth, 1);
contentStream.closeAndStroke();
ypos -=(ygap);
ypos -= (ygap);
} else if(StringUtils.isNotEmpty(itemService.getMetadata(item, field))) {
ypos = drawStringWordWrap(coverPage, contentStream, itemService.getMetadata(item, field), xpos, ypos, font, fontSize);
} else if (StringUtils.isNotEmpty(itemService.getMetadata(item, field))) {
ypos = drawStringWordWrap(coverPage, contentStream, itemService.getMetadata(item, field), xpos,
ypos, font, fontSize);
}
if(field.contains("title")) {
ypos -=ygap;
if (field.contains("title")) {
ypos -= ygap;
}
}
@@ -384,38 +395,33 @@ public class CitationDocumentServiceImpl implements CitationDocumentService, Ini
@Override
public int drawStringWordWrap(PDPage page, PDPageContentStream contentStream, String text,
int startX, int startY, PDFont pdfFont, float fontSize) throws IOException {
int startX, int startY, PDFont pdfFont, float fontSize) throws IOException {
float leading = 1.5f * fontSize;
PDRectangle mediabox = page.getMediaBox();
float margin = 72;
float width = mediabox.getWidth() - 2*margin;
float width = mediabox.getWidth() - 2 * margin;
List<String> lines = new ArrayList<>();
int lastSpace = -1;
while (text.length() > 0)
{
while (text.length() > 0) {
int spaceIndex = text.indexOf(' ', lastSpace + 1);
if (spaceIndex < 0)
{
if (spaceIndex < 0) {
lines.add(text);
text = "";
}
else
{
} else {
String subString = text.substring(0, spaceIndex);
float size = fontSize * pdfFont.getStringWidth(subString) / 1000;
if (size > width)
{
if (lastSpace < 0) // So we have a word longer than the line... draw it anyways
if (size > width) {
// So we have a word longer than the line... draw it anyways
if (lastSpace < 0) {
lastSpace = spaceIndex;
}
subString = text.substring(0, lastSpace);
lines.add(subString);
text = text.substring(lastSpace).trim();
lastSpace = -1;
}
else
{
} else {
lastSpace = spaceIndex;
}
}
@@ -425,8 +431,7 @@ public class CitationDocumentServiceImpl implements CitationDocumentService, Ini
contentStream.setFont(pdfFont, fontSize);
contentStream.moveTextPositionByAmount(startX, startY);
int currentY = startY;
for (String line: lines)
{
for (String line : lines) {
contentStream.drawString(line);
currentY -= leading;
contentStream.moveTextPositionByAmount(0, -leading);
@@ -439,7 +444,7 @@ public class CitationDocumentServiceImpl implements CitationDocumentService, Ini
public String getOwningCommunity(Context context, Item item) {
try {
List<Community> comms = itemService.getCommunities(context, item);
if(comms.size() > 0) {
if (comms.size() > 0) {
return comms.get(0).getName();
} else {
return " ";
@@ -462,8 +467,8 @@ public class CitationDocumentServiceImpl implements CitationDocumentService, Ini
ArrayList<String> valueArray = new ArrayList<String>();
for(MetadataValue dcValue : dcValues) {
if(StringUtils.isNotBlank(dcValue.getValue())) {
for (MetadataValue dcValue : dcValues) {
if (StringUtils.isNotBlank(dcValue.getValue())) {
valueArray.add(dcValue.getValue());
}
}
@@ -473,28 +478,28 @@ public class CitationDocumentServiceImpl implements CitationDocumentService, Ini
@Override
public void drawTable(PDPage page, PDPageContentStream contentStream,
float y, float margin,
String[][] content, PDFont font, int fontSize, boolean cellBorders) throws IOException {
float y, float margin,
String[][] content, PDFont font, int fontSize, boolean cellBorders) throws IOException {
final int rows = content.length;
final int cols = content[0].length;
final float rowHeight = 20f;
final float tableWidth = page.getMediaBox().getWidth()-(2*margin);
final float tableWidth = page.getMediaBox().getWidth() - (2 * margin);
final float tableHeight = rowHeight * rows;
final float colWidth = tableWidth/(float)cols;
final float cellMargin=5f;
final float colWidth = tableWidth / (float) cols;
final float cellMargin = 5f;
if(cellBorders) {
if (cellBorders) {
//draw the rows
float nexty = y ;
float nexty = y;
for (int i = 0; i <= rows; i++) {
contentStream.drawLine(margin,nexty,margin+tableWidth,nexty);
nexty-= rowHeight;
contentStream.drawLine(margin, nexty, margin + tableWidth, nexty);
nexty -= rowHeight;
}
//draw the columns
float nextx = margin;
for (int i = 0; i <= cols; i++) {
contentStream.drawLine(nextx,y,nextx,y-tableHeight);
contentStream.drawLine(nextx, y, nextx, y - tableHeight);
nextx += colWidth;
}
}
@@ -502,19 +507,19 @@ public class CitationDocumentServiceImpl implements CitationDocumentService, Ini
//now add the text
contentStream.setFont(font, fontSize);
float textx = margin+cellMargin;
float texty = y-15;
for(int i = 0; i < content.length; i++){
for(int j = 0 ; j < content[i].length; j++){
float textx = margin + cellMargin;
float texty = y - 15;
for (int i = 0; i < content.length; i++) {
for (int j = 0; j < content[i].length; j++) {
String text = content[i][j];
contentStream.beginText();
contentStream.moveTextPositionByAmount(textx,texty);
contentStream.moveTextPositionByAmount(textx, texty);
contentStream.drawString(text);
contentStream.endText();
textx += colWidth;
}
texty-=rowHeight;
textx = margin+cellMargin;
texty -= rowHeight;
textx = margin + cellMargin;
}
}
}