Skip to content

Commit

Permalink
#7714 refactor download all validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sekmiller committed Apr 5, 2021
1 parent 33f43f1 commit fe9d014
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 142 deletions.
116 changes: 11 additions & 105 deletions src/main/java/edu/harvard/iq/dataverse/DatasetPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import edu.harvard.iq.dataverse.datacapturemodule.ScriptRequestResponse;
import edu.harvard.iq.dataverse.dataset.DatasetThumbnail;
import edu.harvard.iq.dataverse.dataset.DatasetUtil;
import edu.harvard.iq.dataverse.datasetutility.FileSizeChecker;
import edu.harvard.iq.dataverse.datavariable.VariableServiceBean;
import edu.harvard.iq.dataverse.engine.command.Command;
import edu.harvard.iq.dataverse.engine.command.CommandContext;
Expand Down Expand Up @@ -2846,16 +2847,6 @@ public void setSelectedNonDownloadableFiles(List<FileMetadata> selectedNonDownlo
this.selectedNonDownloadableFiles = selectedNonDownloadableFiles;
}

private List<FileMetadata> selectedNonDownloadallableFiles;

public List<FileMetadata> getSelectedNonDownloadallableFiles() {
return selectedNonDownloadallableFiles;
}

public void setSelectedNonDownloadallableFiles(List<FileMetadata> selectedNonDownloadallableFiles) {
this.selectedNonDownloadallableFiles = selectedNonDownloadallableFiles;
}

public String getSizeOfDataset() {
boolean original = false;
return DatasetUtil.getDownloadSize(workingVersion, original);
Expand All @@ -2870,7 +2861,10 @@ public Long getSizeOfDatasetOrigNumeric() {
boolean original = true;
return DatasetUtil.getDownloadSizeNumeric(workingVersion, original);
}


public String getSizeOfSelectedAsString(){
return FileSizeChecker.bytesToHumanReadable(getSizeOfSelectedOrigNumeric());
}

public Long getSizeOfSelectedOrigNumeric(){
boolean original = true;
Expand All @@ -2890,19 +2884,21 @@ public String getSizeOfDatasetOrig() {
}

public void validateAllFilesForDownloadArchival() {
this.setSelectedFiles(workingVersion.getFileMetadatas());
boolean guestbookRequired = isDownloadPopupRequired();
boolean downloadOriginal = false;
validateFilesForDownloadAll(guestbookRequired, downloadOriginal);
validateFilesForDownload(guestbookRequired, downloadOriginal);
}

/**
* Can result in "requested optional service" error. For non-tabular files
* it's safer to use validateAllFilesForDownloadArchival.
*/
public void validateAllFilesForDownloadOriginal() {
this.setSelectedFiles(workingVersion.getFileMetadatas());
boolean guestbookRequired = isDownloadPopupRequired();
boolean downloadOriginal = true;
validateFilesForDownloadAll(guestbookRequired, downloadOriginal);
validateFilesForDownload(guestbookRequired, downloadOriginal);
}

public void validateFilesForDownload(boolean guestbookRequired, boolean downloadOriginal){
Expand Down Expand Up @@ -2969,75 +2965,7 @@ public void validateFilesForDownload(boolean guestbookRequired, boolean download

}

/**
* This method borrows heavily from validateFilesForDownload but does not
* use the selectedFiles field.
*/
public void validateFilesForDownloadAll(boolean guestbookRequired, boolean downloadOriginal) {
setSelectedNonDownloadallableFiles(new ArrayList<>());
List<FileMetadata> downloadableFiles = new ArrayList<>();
for (FileMetadata fmd : workingVersion.getFileMetadatas()) {
if (this.fileDownloadHelper.canDownloadFile(fmd)) {
downloadableFiles.add(fmd);
} else {
getSelectedNonDownloadallableFiles().add(fmd);
}
}

// If some of the files were restricted and we had to drop them off the
// list, and NONE of the files are left on the downloadable list
// - we show them a "you're out of luck" popup:
if (downloadableFiles.isEmpty() && !getSelectedNonDownloadallableFiles().isEmpty()) {
//RequestContext requestContext = RequestContext.getCurrentInstance();
PrimeFaces.current().executeScript("PF('downloadInvalid').show()");
return;
}

// Note that the GuestbookResponse object may still have information from
// the last download action performed by the user. For example, it may
// still have the non-null Datafile in it, if the user has just downloaded
// a single file; or it may still have the format set to "original" -
// even if that's not what they are trying to do now.
// So make sure to reset these values:
guestbookResponse.setDataFile(null);
// Inline getSelectedDownloadableFilesIdsString() that doesn't use selectedDownloadableFiles
String downloadIdString = "";
for (FileMetadata fmd : downloadableFiles) {
if (!StringUtil.isEmpty(downloadIdString)) {
downloadIdString += ",";
}
downloadIdString += fmd.getDataFile().getId();
}
guestbookResponse.setSelectedFileIds(downloadIdString);
if (downloadOriginal) {
guestbookResponse.setFileFormat("original");
} else {
guestbookResponse.setFileFormat("");
}
guestbookResponse.setDownloadtype("Download");

// If we have a bunch of files that we can download, AND there were no files
// that we had to take off the list, because of permissions - we can
// either send the user directly to the download API (if no guestbook/terms
// popup is required), or send them to the download popup:
if (!downloadableFiles.isEmpty() && getSelectedNonDownloadallableFiles().isEmpty()) {
if (guestbookRequired) {
openDownloadPopupForDownloadAll();
} else {
startMultipleFileDownload();
}
return;
}

// ... and if some files were restricted, but some are downloadable,
// we are showing them this "you are somewhat in luck" popup; that will
// then direct them to the download, or popup, as needed:
if (!downloadableFiles.isEmpty() && !getSelectedNonDownloadallableFiles().isEmpty()) {
//RequestContext requestContext = RequestContext.getCurrentInstance();
PrimeFaces.current().executeScript("PF('downloadAllMixed').show()");
}

}


private boolean selectAllFiles;

Expand Down Expand Up @@ -4087,7 +4015,7 @@ public void setDownloadType(String downloadType) {
}


public void openDownloadPopupForMultipleFileDownload() {
public void openDownloadPopupForMultipleFileDownload() {
if (this.selectedFiles.isEmpty()) {
//RequestContext requestContext = RequestContext.getCurrentInstance();
PrimeFaces.current().executeScript("PF('selectFilesForDownload').show()");
Expand All @@ -4103,28 +4031,6 @@ public void openDownloadPopupForMultipleFileDownload() {
//RequestContext requestContext = RequestContext.getCurrentInstance();
PrimeFaces.current().executeScript("PF('downloadPopup').show();handleResizeDialog('downloadPopup');");
}

/**
* This method borrows heavily from
* openDownloadPopupForMultipleFileDownload. It does not use the
* selectedFiles field.
*/
public void openDownloadPopupForDownloadAll() {
// This is commented out because "download all" doesn't use selectedFiles.
// if (this.selectedFiles.isEmpty()) {
// //RequestContext requestContext = RequestContext.getCurrentInstance();
// PrimeFaces.current().executeScript("PF('selectFilesForDownload').show()");
// return;
// }

// There's a chance that this is not really a batch download - i.e.,
// there may only be one file on the downloadable list. But the fileDownloadService
// method below will check for that, and will redirect to the single download, if
// that's the case. -- L.A.
this.guestbookResponse.setDownloadtype("Download");
//RequestContext requestContext = RequestContext.getCurrentInstance();
PrimeFaces.current().executeScript("PF('downloadPopup').show();handleResizeDialog('downloadPopup');");
}

public void initGuestbookMultipleResponse(String selectedFileIds){
initGuestbookResponse(null, "download", selectedFileIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void writeGuestbookAndStartBatchDownload(GuestbookResponse guestbookRespo
}

public void writeGuestbookAndStartBatchDownload(GuestbookResponse guestbookResponse, Boolean doNotSaveGuestbookRecord){
System.out.print(guestbookResponse.getSelectedFileIds());
if (guestbookResponse == null || guestbookResponse.getSelectedFileIds() == null) {
return;
}
Expand Down
11 changes: 1 addition & 10 deletions src/main/java/edu/harvard/iq/dataverse/dataset/DatasetUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -466,16 +466,7 @@ public static boolean isAppropriateStorageDriver(Dataset dataset){
* size for tabular files.
*/
public static String getDownloadSize(DatasetVersion dsv, boolean original) {
long bytes = 0l;
for (FileMetadata fileMetadata : dsv.getFileMetadatas()) {
DataFile dataFile = fileMetadata.getDataFile();
if (original && dataFile.isTabularData()) {
bytes += dataFile.getOriginalFileSize() == null ? 0 : dataFile.getOriginalFileSize();
} else {
bytes += dataFile.getFilesize();
}
}
return FileSizeChecker.bytesToHumanReadable(bytes);
return FileSizeChecker.bytesToHumanReadable(getDownloadSizeNumeric(dsv, original));
}

public static Long getDownloadSizeNumeric(DatasetVersion dsv, boolean original) {
Expand Down
34 changes: 8 additions & 26 deletions src/main/webapp/dataset.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,23 @@
<li class="dropdown-header">#{bundle['dataset.accessBtn.header.download']} <span class="glyphicon glyphicon-download-alt"/></li>
<!-- NORMAL DOWNLOAD BUTTON (NO TABULAR FILES) -->
<li jsf:rendered="#{!DatasetPage.versionHasTabular and !(DatasetPage.sizeOfDatasetNumeric > settingsWrapper.zipDownloadLimit)}">
<p:commandLink update="@form" actionListener="#{DatasetPage.validateAllFilesForDownloadArchival()}" styleClass="btn-download">
<p:commandLink update="@form, @([id$=downloadMixed]) " actionListener="#{DatasetPage.validateAllFilesForDownloadArchival()}" styleClass="btn-download">
#{bundle.download}
<h:outputFormat value="#{bundle['dataset.accessBtn.download.size']}">
<f:param value="#{DatasetPage.sizeOfDataset}" />
</h:outputFormat>
</p:commandLink>
</li>
<ui:fragment rendered="#{DatasetPage.canDownloadFiles() and (DatasetPage.getSizeOfDatasetOrigNumeric > settingsWrapper.zipDownloadLimit) }">
<ui:fragment rendered="#{DatasetPage.canDownloadFiles() and (DatasetPage.sizeOfDatasetOrigNumeric > settingsWrapper.zipDownloadLimit) }">
<h:outputText value="#{bundle['dataset.accessBtn.too.big']}">
</h:outputText>
</ui:fragment>

<!-- DOWNLOAD ORIGINAL BUTTON (TABULAR FILES PRESENT) -->
<li jsf:rendered="#{DatasetPage.versionHasTabular and DatasetPage.sizeOfDatasetOrig != '0 B' }">
<p:commandLink update="@form" actionListener="#{DatasetPage.validateAllFilesForDownloadOriginal()}" styleClass="btn-download">
<p:commandLink update="@form, @([id$=downloadMixed])" actionListener="#{DatasetPage.validateAllFilesForDownloadOriginal()}"
oncomplete="bind_bsui_components();"
styleClass="btn-download">
#{bundle.downloadOriginal}
<h:outputFormat value="#{bundle['dataset.accessBtn.download.size']}">
<f:param value="#{DatasetPage.sizeOfDatasetOrig}" />
Expand All @@ -174,7 +176,8 @@
</li>
<!-- DOWNLOAD ARCHIVAL FILES (TABULAR FILES PRESENT) -->
<li jsf:rendered="#{DatasetPage.versionHasTabular}">
<p:commandLink update="@form" actionListener="#{DatasetPage.validateAllFilesForDownloadArchival()}" styleClass="btn-download">
<p:commandLink update="@form, @([id$=downloadMixed])" oncomplete="bind_bsui_components();"
actionListener="#{DatasetPage.validateAllFilesForDownloadArchival()}" styleClass="btn-download">
#{bundle.downloadArchival}
<h:outputFormat value="#{bundle['dataset.accessBtn.download.size']}">
<f:param value="#{DatasetPage.sizeOfDataset}" />
Expand Down Expand Up @@ -900,6 +903,7 @@
</p:dialog>
<p:dialog id="downloadMixed" styleClass="smallPopUp" header="#{bundle['dataset.inValidSelectedFilesForDownload']}" widgetVar="downloadMixed" modal="true">
<p class="text-danger"><span class="glyphicon glyphicon-exclamation-sign"/> #{bundle['dataset.mixedSelectedFilesForDownload']}</p>
#{DatasetPage.selectedNonDownloadableFiles.size()}
<table>
<ui:repeat var="resFile" value="#{DatasetPage.selectedNonDownloadableFiles}" >
<tr>
Expand All @@ -920,28 +924,6 @@
</button>
</div>
</p:dialog>
<p:dialog id="downloadAllMixed" styleClass="smallPopUp" header="#{bundle['dataset.inValidSelectedFilesForDownload']}" widgetVar="downloadAllMixed" modal="true">
<p class="text-danger"><span class="glyphicon glyphicon-exclamation-sign"/> #{bundle['dataset.mixedSelectedFilesForDownload']}</p>
<table>
<ui:repeat var="resFile" value="#{DatasetPage.selectedNonDownloadallableFiles}" >
<tr>
<td>#{resFile.label}</td>
</tr>
</ui:repeat>
</table>
<div class="button-block">
<p class="help-block">#{bundle['dataset.downloadUnrestricted']}</p>
<p:commandButton styleClass="btn btn-default" value="#{bundle.continue}" onclick="PF('downloadAllMixed').hide()"
rendered="#{!DatasetPage.downloadPopupRequired}"
action="#{DatasetPage.startMultipleFileDownload()}"/>
<p:commandButton styleClass="btn btn-default" value="#{bundle.continue}" onclick="PF('downloadAllMixed').hide()"
rendered="#{DatasetPage.downloadPopupRequired and !settingsWrapper.rsyncDownload}"
action="#{DatasetPage.openDownloadPopupForDownloadAll()}" update="@form"/>
<button class="btn btn-link" onclick="PF('downloadAllMixed').hide();" type="button">
#{bundle.cancel}
</button>
</div>
</p:dialog>
<p:dialog id="deleteConfirmation" styleClass="smallPopUp" header="#{bundle['file.deleteDialog.header']}" widgetVar="deleteConfirmation" modal="true">
<p class="text-warning"><span class="glyphicon glyphicon-warning-sign"/> #{bundle['file.deleteDialog.tip']}</p>
<div class="button-block">
Expand Down
12 changes: 12 additions & 0 deletions src/main/webapp/filesFragment.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,18 @@
</p>
</div>
</div>
<div class="row" jsf:rendered="#{DatasetPage.sizeOfDatasetNumeric > settingsWrapper.zipDownloadLimit and (DatasetPage.sizeOfSelectedOrigNumeric gt 0)
and DatasetPage.selectedFiles.size() > 1}">
<div class="col-xs-12 bg-warning text-left" style="font-weight:normal;padding-top:.5em;">
<!-- SELECTION MESSAGE -->
<p>
<h:outputText value="#{DatasetPage.sizeOfSelectedAsString}"/>
</p>
<p>
<h:outputText value="#{settingsWrapper.zipDownloadLimit}"/>
</p>
</div>
</div>
</f:facet>

<p:column selectionMode="multiple" class="text-center" ariaHeaderText="#{bundle['file.select.tooltip']}"/>
Expand Down

0 comments on commit fe9d014

Please sign in to comment.