Skip to content

Commit

Permalink
start populating popup with images #3559
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Feb 9, 2017
1 parent 05ea430 commit 99efe69
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/main/java/Bundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1490,6 +1490,7 @@ dataset.thumbnailsAndWidget.thumbnailImage.title=The logo or image file you wish
dataset.thumbnailsAndWidget.thumbnailImage.tip=Supported image types are JPEG, TIFF, or PNG and should be no larger than 500 KB. The maximum display size for an image file as a dataset thumbnail is 48 pixels wide by 48 pixels high.
dataset.thumbnailsAndWidget.thumbnailImage.upload=Upload Image
dataset.thumbnailsAndWidget.thumbnailImage.upload.invalidMsg=The image could not be uploaded. Please try again with a jpeg, tiff, or png file.
dataset.thumbnailsAndWidget.thumbnailImage.nonDatasetFile=[not a dataset file...]

# file.xhtml
file.share.fileShare=Share File
Expand Down
44 changes: 34 additions & 10 deletions src/main/java/edu/harvard/iq/dataverse/DatasetWidgetsPage.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package edu.harvard.iq.dataverse;

import edu.harvard.iq.dataverse.dataaccess.ImageThumbConverter;
import edu.harvard.iq.dataverse.dataset.DatasetThumbnail;
import edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetCommand;
import edu.harvard.iq.dataverse.util.BundleUtil;
import edu.harvard.iq.dataverse.util.FileUtil;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.view.ViewScoped;
Expand All @@ -18,10 +18,6 @@
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

/**
*
* @author skraffmi
*/
@ViewScoped
@Named("DatasetWidgetsPage")
public class DatasetWidgetsPage implements java.io.Serializable {
Expand All @@ -33,6 +29,8 @@ public class DatasetWidgetsPage implements java.io.Serializable {

private Long datasetId;
private Dataset dataset;
private List<DatasetThumbnail> datasetThumbnails;
private DatasetThumbnail datasetThumbnail;

@Inject
PermissionsWrapper permissionsWrapper;
Expand All @@ -48,6 +46,20 @@ public String init() {
if (!permissionsWrapper.canIssueCommand(dataset, UpdateDatasetCommand.class)) {
return permissionsWrapper.notAuthorized();
}
datasetThumbnails = new ArrayList<>();
String altThumbnail = dataset.getAltThumbnail();
if (altThumbnail != null) {
DatasetThumbnail datasetThumbnail = new DatasetThumbnail(BundleUtil.getStringFromBundle("dataset.thumbnailsAndWidget.thumbnailImage.nonDatasetFile"), altThumbnail);
datasetThumbnails.add(datasetThumbnail);
}
for (FileMetadata fileMetadata : dataset.getLatestVersion().getFileMetadatas()) {
DataFile dataFile = fileMetadata.getDataFile();
if (dataFile != null && dataFile.isImage()) {
String imageSourceBase64 = ImageThumbConverter.getImageThumbAsBase64(dataFile, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE);
DatasetThumbnail datasetThumbnail = new DatasetThumbnail(fileMetadata.getLabel(), imageSourceBase64);
datasetThumbnails.add(datasetThumbnail);
}
}
return null;
}

Expand All @@ -67,6 +79,18 @@ public void setDataset(Dataset dataset) {
this.dataset = dataset;
}

public List<DatasetThumbnail> getDatasetThumbnails() {
return datasetThumbnails;
}

public DatasetThumbnail getDatasetThumbnail() {
return datasetThumbnail;
}

public void setDatasetThumbnail(DatasetThumbnail datasetThumbnail) {
this.datasetThumbnail = datasetThumbnail;
}

public void handleImageFileUpload(FileUploadEvent event) {
logger.fine("handleImageFileUpload clicked");
UploadedFile uploadedFile = event.getFile();
Expand All @@ -93,7 +117,7 @@ public void save() {

public String cancel() {
logger.fine("cancel clicked");
return "/dataset.xhtml?persistentId=" + dataset.getGlobalId() + "&faces-redirect=true";
return "/dataset.xhtml?persistentId=" + dataset.getGlobalId() + "&faces-redirect=true";
}

}
14 changes: 8 additions & 6 deletions src/main/java/edu/harvard/iq/dataverse/api/Datasets.java
Original file line number Diff line number Diff line change
Expand Up @@ -567,10 +567,12 @@ public Response deletePrivateUrl(@PathParam("id") String idSupplied) {

/**
* @todo Comment out or delete if not needed. Enforce permissions.
*
* @todo Should we call this a logo or a thumbnail?
*/
@GET
@Path("{id}/thumbnail")
public Response getDatasetThumbnail(@PathParam("id") String idSupplied) {
@Path("{id}/logo")
public Response getDatasetLogo(@PathParam("id") String idSupplied) {
try {
Dataset dataset = findDatasetOrDie(idSupplied);
return ok("All good, got dataset id: " + dataset.getId());
Expand All @@ -583,9 +585,9 @@ public Response getDatasetThumbnail(@PathParam("id") String idSupplied) {
* @todo Enforce permissions. Make into a Command?
*/
@POST
@Path("{id}/thumbnail")
@Path("{id}/logo")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response overrideDatasetThumbnail(@PathParam("id") String idSupplied, @FormDataParam("file") InputStream fileInputStream
public Response uploadDatasetLogo(@PathParam("id") String idSupplied, @FormDataParam("file") InputStream fileInputStream
) {
try {
Dataset dataset = findDatasetOrDie(idSupplied);
Expand All @@ -595,8 +597,8 @@ public Response overrideDatasetThumbnail(@PathParam("id") String idSupplied, @Fo
} catch (IOException ex) {
return error(Response.Status.BAD_REQUEST, "Problem uploading file: " + ex);
}
String thumbnail = ImageThumbConverter.getImageAsBase64FromFile(file);
dataset.setAltThumbnail(thumbnail);
String datasetLogo = ImageThumbConverter.getImageAsBase64FromFile(file);
dataset.setAltThumbnail(datasetLogo);
Dataset merged = datasetService.merge(dataset);
return ok("Thumbnail overridden for dataset id: " + merged.getId());
} catch (WrappedResponse ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package edu.harvard.iq.dataverse.dataset;

public class DatasetThumbnail {

private String filename;
private String base64image;

public DatasetThumbnail(String filename, String base64image) {
this.filename = filename;
this.base64image = base64image;
}

public String getFilename() {
return filename;
}

public void setFilename(String filename) {
this.filename = filename;
}

public String getBase64image() {
return base64image;
}

public void setBase64image(String base64image) {
this.base64image = base64image;
}

}
23 changes: 17 additions & 6 deletions src/main/webapp/dataset-widgets.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@
<p:commandButton styleClass="btn btn-default" update="@all" value="#{bundle.saveChanges}" action="#{DatasetWidgetsPage.save()}"/>
<p:commandButton styleClass="btn btn-default" update="@all" value="#{bundle.cancel}" action="#{DatasetWidgetsPage.cancel()}"/>
</div>


<p:dialog id="selectFileThumbnail" header="Select File Thumbnail" widgetVar="selectFileThumbnail" modal="true">
<div class="form-horizontal col-sm-12">
<div class="form-group">
Expand All @@ -91,9 +91,20 @@
<h:graphicImage value="#{DatasetWidgetsPage.dataset.altThumbnail}"/>
</h:panelGrid>
</ui:remove>

<p:selectOneRadio id="selectOneRadioDatasetThumbnail" value="#{DatasetWidgetsPage.datasetThumbnail}" layout="responsive" columns="3">
<f:selectItems value="#{DatasetWidgetsPage.datasetThumbnails}" var="thumb" itemLabel="#{thumb.filename}" itemValue="#{thumb.filename}"/>
</p:selectOneRadio>
<p:dataTable value="#{DatasetWidgetsPage.datasetThumbnails}" var="thumbnail">
<p:column>
<h:outputText value="#{thumbnail.filename}"/>
</p:column>
<p:column>
<h:graphicImage value="#{thumbnail.base64image}"/>
</p:column>
</p:dataTable>

<!-- TODO REMOVE THIS PLACEHOLDER TABLE -->
<table id="j_idt88:city" role="presentation" class="ui-selectoneradio ui-widget">
<table id="j_idt88:city" role="presentation" class="ui-selectoneradio ui-widget" jsf:rendered="#{false}">
<tbody>
<tr>
<td>
Expand All @@ -118,7 +129,7 @@
</tbody>
</table>
<!-- TODO REMOVE THIS PLACEHOLDER TABLE -->

</div>
</div>
<div class="button-block">
Expand All @@ -132,7 +143,7 @@
</button>
</div>
</p:dialog>

</h:form>
</p:tab>
<p:tab id="widgetsTab" title="#{bundle['dataset.thumbnailsAndWidget.widgets.title']}">
Expand Down
8 changes: 6 additions & 2 deletions src/test/java/edu/harvard/iq/dataverse/api/SearchIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -1337,12 +1337,16 @@ public void testDatasetThumbnail() {
Response uploadFile = UtilIT.uploadFile(datasetPersistentId, "trees.zip", apiToken);
uploadFile.prettyPrint();

String imageFile = "src/main/webapp/resources/images/cc0.png";
Response overrideThumbnail = UtilIT.overrideDatasetThumbnail(datasetPersistentId, imageFile, apiToken);
String datasetLogo = "src/main/webapp/resources/images/cc0.png";
Response overrideThumbnail = UtilIT.uploadDatasetLogo(datasetPersistentId, datasetLogo, apiToken);
overrideThumbnail.prettyPrint();
overrideThumbnail.then().assertThat()
.statusCode(200);

if (true) {
return;
}

Response publishDataverse = UtilIT.publishDataverseViaSword(dataverseAlias, apiToken);
publishDataverse.prettyPrint();
Response publishDataset = UtilIT.publishDatasetViaNativeApi(datasetId, "major", apiToken);
Expand Down
8 changes: 2 additions & 6 deletions src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -641,18 +641,14 @@ static Response privateUrlDelete(Integer datasetId, String apiToken) {
static Response downloadDatasetThumbnail(String datasetPersistentId, String imageFile, String apiToken) {
return given()
.header(API_TOKEN_HTTP_HEADER, apiToken)
// .post("/api/datasets/" + datasetPersistentId + "/thumbnail");
// .post("/api/datasets/:persistentId/thumbnail" + "?persistentId=" + datasetPersistentId);
.get("/api/datasets/:persistentId/thumbnail" + "?persistentId=" + datasetPersistentId);
}

static Response overrideDatasetThumbnail(String datasetPersistentId, String pathToImageFile, String apiToken) {
static Response uploadDatasetLogo(String datasetPersistentId, String pathToImageFile, String apiToken) {
return given()
.header(API_TOKEN_HTTP_HEADER, apiToken)
// .post("/api/datasets/" + datasetPersistentId + "/thumbnail");
.multiPart("file", new File(pathToImageFile))
.post("/api/datasets/:persistentId/thumbnail" + "?persistentId=" + datasetPersistentId);
// .get("/api/datasets/:persistentId/thumbnail" + "?persistentId=" + datasetPersistentId);
.post("/api/datasets/:persistentId/logo" + "?persistentId=" + datasetPersistentId);
}

static Response search(String query, String apiToken) {
Expand Down

0 comments on commit 99efe69

Please sign in to comment.