Skip to content

Commit

Permalink
[CST-1728][D4CRIS-562] Generate PDF license from template
Browse files Browse the repository at this point in the history
[CST-1728] added high resolution logo; configure jasper.cfg to simplify license name; design the three template"
  • Loading branch information
lap82 committed Nov 19, 2019
1 parent 3cd650e commit 1edd9d8
Show file tree
Hide file tree
Showing 14 changed files with 1,496 additions and 41 deletions.
11 changes: 11 additions & 0 deletions dspace-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -771,5 +771,16 @@
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports -->
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports-fonts -->
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports-fonts</artifactId>
</dependency>
</dependencies>
</project>
97 changes: 97 additions & 0 deletions dspace-api/src/main/java/org/dspace/content/LicenseUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,31 @@
package org.dspace.content;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.Formatter;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.AuthorizeManager;
import org.dspace.content.license.FormattableArgument;
import org.dspace.core.ConfigurationManager;
import org.dspace.core.Context;
import org.dspace.eperson.EPerson;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;

/**
* Utility class to manage generation and storing of the license text that the
* submitter has to grant/granted for archiving the item
Expand All @@ -28,6 +42,8 @@
*/
public class LicenseUtils
{
private static Logger log = Logger.getLogger(LicenseUtils.class);

/**
* Return the text of the license that the user has granted/must grant
* before for submit the item. The license text is build using the template
Expand Down Expand Up @@ -144,4 +160,85 @@ public static void grantLicense(Context context, Item item,

b.update();
}
public static void getLicensePDF(Context context, Item item, String choiceLicense) {
String reportName = ConfigurationManager.getProperty("jasper", choiceLicense);
String reportFolderPath = ConfigurationManager.getProperty("jasper","report.folder");
String reportExtension = ConfigurationManager.getProperty("jasper","report.extension");
String reportPath = reportFolderPath+reportName+reportExtension;
try {
fill(context, item, reportPath,reportName);
} catch (SQLException e) {
log.error(e.getMessage(),e);
}


}

private static void fill(Context context, Item item, String reportPath, String reportName) throws SQLException {
HashMap<String, Object> reportMap = new HashMap<String, Object>();
String key = "";
String value = "";
EPerson ep = item.getSubmitter();
List<Metadatum> listEPMetadata = ep.getMetadata();
Metadatum[] listItemMetadata = item.getMetadataWithoutPlaceholder(Item.ANY, Item.ANY, Item.ANY, Item.ANY);
for (Metadatum m: listItemMetadata) {
key = m.getField().replace('.', '_');
String oldValue = (String) reportMap.get(key);
value = m.value;
if (oldValue != null) {
value = oldValue+", "+value;
}
reportMap.put(key, value);
}
for (Metadatum m: listEPMetadata) {
key = m.getField().replace('.', '_');
String oldValue = (String) reportMap.get(key);
value = m.value;
if (oldValue != null) {
value = oldValue+", "+value;
}
reportMap.put(key, value);
}
String logo = ConfigurationManager.getProperty("jasper","parameter.logo");
String pathLogoImage = ConfigurationManager.getProperty("jasper","parameter.logo.path");
String submitterEmail = ConfigurationManager.getProperty("jasper","parameter.submitter.email");
String submitterFullName = ConfigurationManager.getProperty("jasper","parameter.submitter.fullname");
String currentDateLabel = ConfigurationManager.getProperty("jasper","parameter.current.date");

reportMap.put(logo, pathLogoImage);
String email = item.getSubmitter().getEmail();
String fullname = item.getSubmitter().getFullName();
Date date = new Date();
String currentDate = date.toString();
reportMap.put(submitterEmail, email);
reportMap.put(submitterFullName, fullname);
reportMap.put(currentDateLabel, currentDate);
try {
// Give the path of report jrxml file path for complie.
JasperReport jasperReport = JasperCompileManager.compileReport(reportPath);
//pass data HashMap with compiled jrxml file and a empty data source .
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, reportMap,new JREmptyDataSource() );

ByteArrayOutputStream baos = new ByteArrayOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
Bitstream b = item.createSingleBitstream(bais, "LICENSE");
// Now set the format and name of the bitstream
b.setName("license.pdf");
b.setSource("Written by org.dspace.content.LicenseUtils");

// Find the License format
BitstreamFormat bf = BitstreamFormat.findByShortDescription(context,
"License PDF");
b.setFormat(bf);
b.update();
context.turnOffAuthorisationSystem();
// grant access to the license to the submitter
AuthorizeManager.addPolicy(context, b, org.dspace.core.Constants.READ, context.getCurrentUser());
context.restoreAuthSystemState();
} catch (Exception e) {
log.error(e.getMessage(),e);
throw new RuntimeException(e.getMessage(), e);
}
}
}
65 changes: 39 additions & 26 deletions dspace-api/src/main/java/org/dspace/submit/step/LicenseStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -21,6 +24,7 @@
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Item;
import org.dspace.content.LicenseUtils;
import org.dspace.core.ConfigurationManager;
import org.dspace.core.Context;
import org.dspace.core.LogManager;
import org.dspace.eperson.EPerson;
Expand Down Expand Up @@ -118,24 +122,37 @@ else if (buttonPressed.equals(NEXT_BUTTON))
&& (buttonPressed.equals("submit_grant") || buttonPressed
.equals(NEXT_BUTTON)))
{
// License granted
log.info(LogManager.getHeader(context, "accept_license",
subInfo.getSubmissionLogInfo()));

// Add the license to the item
Item item = subInfo.getSubmissionItem().getItem();
EPerson submitter = context.getCurrentUser();

// remove any existing DSpace license (just in case the user
// accepted it previously)
item.removeDSpaceLicense();

String license = LicenseUtils.getLicenseText(context
.getCurrentLocale(), subInfo.getSubmissionItem()
.getCollection(), item, submitter);

LicenseUtils.grantLicense(context, item, license);


String typeMetadata = ConfigurationManager.getProperty("license.articles.metadata");
String type = subInfo.getSubmissionItem().getItem().getMetadata(typeMetadata);
String[] articleType = ConfigurationManager.getProperty("license.articles.value").split(",");
List<String> list = new ArrayList<String>();
list = Arrays.asList(articleType);
if (list.contains(type))
{
// License granted
log.info(LogManager.getHeader(context, "accept_license",
subInfo.getSubmissionLogInfo()));

// Add the license to the item
Item item = subInfo.getSubmissionItem().getItem();
EPerson submitter = context.getCurrentUser();

// remove any existing DSpace license (just in case the user
// accepted it previously)
item.removeDSpaceLicense();

String license = LicenseUtils.getLicenseText(context
.getCurrentLocale(), subInfo.getSubmissionItem()
.getCollection(), item, submitter);

LicenseUtils.grantLicense(context, item, license);
}else {
String choiceLicense = request.getParameter("license_chooser");
Item item = subInfo.getSubmissionItem().getItem();
Item item2 = item.getWrapper();
LicenseUtils.getLicensePDF(context, item2, choiceLicense);
}
// commit changes
context.commit();
}
Expand Down Expand Up @@ -173,13 +190,9 @@ public int getNumberOfPages(HttpServletRequest request,
return 1;

}


@Override
public void doClear(SubmissionInfo subInfo) throws ServletException,
IOException, SQLException, AuthorizeException
{
// NOOP
}

public void doClear(SubmissionInfo subInfo) throws SQLException, AuthorizeException, IOException {
Item item = subInfo.getSubmissionItem().getItem();
item.removeLicenses();
}
}
13 changes: 11 additions & 2 deletions dspace-api/src/main/resources/Messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1044,10 +1044,16 @@ jsp.submit.creative-commons.info1 = If you wish, y
jsp.submit.creative-commons.title = Use a Creative Commons License

jsp.submit.creative-commons.license = License Type
jsp.submit.creative-commons.select_change = Select or modify your license ...
jsp.submit.creative-commons.select_change = Select this voice for select German Copyright law
jsp.submit.creative-commons.no_license = No Creative Commons License
jsp.submit.creative-commons.license.current = Current license

jsp.submit.license.1 = Master Thesis
jsp.submit.license.2 = Gold and Green OpenAccess Publications
jsp.submit.license.3 = Dissertation Habilitation
jsp.submit.license.retrodigitalization = Retrodigitalization
jsp.submit.show-license.template-instruction = Please select the type of license appropriate for your submission according to the University definition. The system will generate a pre-filled PDF for you downloadable from the next page to be sent to the Library

jsp.submit.edit-bitstream-access.title = Edit Bitstream Access
jsp.submit.edit-bitstream-access.heading = Edit Bitstream Access
jsp.submit.edit-bitstream-access.save.button = Save
Expand Down Expand Up @@ -5117,6 +5123,8 @@ error.ec.product.sale = <span class="label label-warning">You can not select Ano
error.ec.product.price = <span class="label label-warning">You can not insert price if sale value is <strong>free</strong></span>
error.ec.product.not.price = <span class="label label-warning">You must insert price if sale value is not <strong>free</strong></span>

license.download = You can download a pre-filled <a href="{0}" target="_blank">copy of the license from here</a>. Please return it signed to the Library

jsp.layout.navbar-admin.sitestatistics= Site Statistics
jsp.layout.navbar-admin.loginstatistics= Login Statistics
jsp.layout.navbar-admin.workflowstatistics= Workflow Statistics
Expand Down Expand Up @@ -5268,4 +5276,5 @@ jsp.layout.detail.info.about.feature = If you interested in "Add Fulltext" or "I
jsp.itemlisttag.fulltext.button = Add fulltext
jsp.itemlisttag.createnewversion.button = Create new version
jsp.itemlisttag.interestedinopenaccess.button = Interested in green<br/> open access?
jsp.itemlisttag.edititem.button = Edit Item
jsp.itemlisttag.edititem.button = Edit Item

20 changes: 19 additions & 1 deletion dspace-jspui/src/main/webapp/submit/complete.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<%@ page import="org.dspace.app.util.SubmissionInfo" %>
<%@ page import="org.dspace.content.InProgressSubmission" %>
<%@ page import="org.dspace.content.Collection"%>
<%@ page import="org.dspace.content.Bundle"%>
<%@ page import="org.dspace.content.Bitstream"%>

<%@ taglib uri="http://www.dspace.org/dspace-tags.tld" prefix="dspace" %>

Expand All @@ -37,6 +39,17 @@
//get collection
Collection collection = subInfo.getSubmissionItem().getCollection();
Bundle[] licenseBnds = subInfo.getSubmissionItem().getItem().getBundles("LICENSE");
Bundle licenseBnd = null;
if (licenseBnds != null && licenseBnds.length > 0){
licenseBnd = licenseBnds[0];
}
Bitstream l = null;
if (licenseBnd != null) {
l = licenseBnd.getBitstreamByName("license.pdf");
}
%>

<dspace:layout style="submission" locbar="off" navbar="off" titlekey="jsp.submit.complete.title">
Expand All @@ -52,7 +65,12 @@
notification as soon as your submission has become a part of the collection,
or if for some reason there is a problem with your submission. You can also
check on the status of your submission by going to the My DSpace page.</p> --%>
<p class="alert alert-info"><fmt:message key="jsp.submit.complete.info"/></p>
<p class="alert alert-info"><fmt:message key="jsp.submit.complete.info"/></p>

<% if (l != null) { %>
<p><fmt:message key="license.download"><fmt:param><%= request.getContextPath() %>/retrieve/<%= l.getID() %></fmt:param></fmt:message></p>
<% } %>

<p><a href="<%= request.getContextPath() %>/mydspace"><fmt:message key="jsp.submit.complete.link"/></a></p>

<p><a href="<%= request.getContextPath() %>/community-list"><fmt:message key="jsp.community-list.title"/></a></p>
Expand Down
Loading

0 comments on commit 1edd9d8

Please sign in to comment.