Skip to content

Commit

Permalink
refactored retrieval of license icon
Browse files Browse the repository at this point in the history
  • Loading branch information
lbownik committed Dec 9, 2024
1 parent e8d9555 commit 3d1824c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.harvard.iq.dataverse.persistence.datafile.license;

import edu.harvard.iq.dataverse.persistence.JpaEntity;
import java.io.Serializable;
import java.util.Optional;

import javax.persistence.Column;
import javax.persistence.Entity;
Expand All @@ -10,7 +11,8 @@
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import java.io.Serializable;

import edu.harvard.iq.dataverse.persistence.JpaEntity;

/**
* Entity describing on what terms
Expand Down Expand Up @@ -106,6 +108,10 @@ public TermsOfUseType getTermsOfUseType() {
}
return TermsOfUseType.TERMS_UNKNOWN;
}

public Optional<LicenseIcon> getIcon() {
return Optional.ofNullable(this.license).map(License::getIcon);
}

public FileTermsOfUse createCopy() {
FileTermsOfUse copy = new FileTermsOfUse();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package edu.harvard.iq.dataverse.persistence.datafile.license;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
Expand All @@ -10,7 +14,6 @@
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import java.io.Serializable;

/**
* Entity class representing icon (image) of the license.
Expand Down Expand Up @@ -53,6 +56,10 @@ public Long getId() {
public byte[] getContent() {
return content;
}

public InputStream getContentAsStream() {
return new ByteArrayInputStream(this.content);
}

/**
* Returns content-type of the image file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1329,22 +1329,15 @@ public boolean isMinorUpdate() {
return isDsvMinorUpdate.get();
}

public boolean isLicenseIconAvailable(FileTermsOfUse termsOfUse) {
if (termsOfUse.getTermsOfUseType() != FileTermsOfUse.TermsOfUseType.LICENSE_BASED) {
return false;
}
return termsOfUse.getLicense().getIcon() != null;
}

public Optional<StreamedContent> getLicenseIconContent(FileTermsOfUse termsOfUse) {
if (!isLicenseIconAvailable(termsOfUse)) {
return Optional.empty();
}
LicenseIcon licenseIcon = termsOfUse.getLicense().getIcon();
return Optional.of(DefaultStreamedContent.builder()
.contentType(licenseIcon.getContentType())
.stream(() -> new ByteArrayInputStream(licenseIcon.getContent()))
.build());
return termsOfUse.getIcon().map(this::toStreamedContent);
}

private DefaultStreamedContent toStreamedContent(final LicenseIcon icon) {
return DefaultStreamedContent.builder()
.contentType(icon.getContentType())
.stream(icon::getContentAsStream)
.build();
}

// -------------------- PRIVATE ---------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import edu.harvard.iq.dataverse.persistence.datafile.FileMetadata;
import edu.harvard.iq.dataverse.persistence.datafile.FileVersionDifference;
import edu.harvard.iq.dataverse.persistence.datafile.license.FileTermsOfUse;
import edu.harvard.iq.dataverse.persistence.datafile.license.FileTermsOfUse.TermsOfUseType;
import edu.harvard.iq.dataverse.persistence.datafile.license.LicenseIcon;
import edu.harvard.iq.dataverse.persistence.dataset.Dataset;
import edu.harvard.iq.dataverse.persistence.dataset.DatasetVersion;
Expand Down Expand Up @@ -60,6 +59,7 @@
* @author skraffmi
*/

@SuppressWarnings("serial")
@ViewScoped
@Named("FilePage")
public class FilePage implements java.io.Serializable {
Expand Down Expand Up @@ -352,20 +352,15 @@ public boolean isThumbnailAvailable(FileMetadata fileMetadata) {
return thumbnailAvailable;
}

public boolean isLicenseIconAvailable(FileTermsOfUse termsOfUse) {
return termsOfUse.getTermsOfUseType() == TermsOfUseType.LICENSE_BASED
&& termsOfUse.getLicense().getIcon() != null;
}

public Optional<StreamedContent> getLicenseIconContent(FileTermsOfUse termsOfUse) {
if (!isLicenseIconAvailable(termsOfUse)) {
return Optional.empty();
}
LicenseIcon licenseIcon = termsOfUse.getLicense().getIcon();
return Optional.of(DefaultStreamedContent.builder()
.contentType(licenseIcon.getContentType())
.stream(() -> new ByteArrayInputStream(licenseIcon.getContent()))
.build());
return termsOfUse.getIcon().map(this::toStreamedContent);
}

private DefaultStreamedContent toStreamedContent(final LicenseIcon icon) {
return DefaultStreamedContent.builder()
.contentType(icon.getContentType())
.stream(icon::getContentAsStream)
.build();
}

public StreamedContent getOtherTermsIconContent(FileTermsOfUse.TermsOfUseType termsOfUseType) {
Expand Down
13 changes: 8 additions & 5 deletions dataverse-webapp/src/main/webapp/license-icon-fragment.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

<!--@elvariable id="termsOfUse" type="edu.harvard.iq.dataverse.persistence.datafile.license.FileTermsOfUse"-->

<p:link href="#{termsOfUse.license.url}" target="_blank"
rendered="#{licenseIcon.present}"
title="#{bundle['file.license.link.title']} #{bundle.opensInNewTab}"
>
<p:graphicImage value="#{licenseIcon.get()}" stream="false" width="81" height="31"
<p:link rendered="#{licenseIcon.present}"
href="#{termsOfUse.license.url}"
target="_blank"
title="#{bundle['file.license.link.title']} #{bundle.opensInNewTab}">
<p:graphicImage value="#{licenseIcon.get()}"
stream="false"
width="81"
height="31"
alt="#{termsOfUse.license.getLocalizedName(dataverseSession.locale)}"/>
</p:link>
</ui:composition>

0 comments on commit 3d1824c

Please sign in to comment.