Skip to content

Commit

Permalink
Fix Download "wrong file" issue
Browse files Browse the repository at this point in the history
Also add Guestbook to multiple file downloads and do not create GB
responses on draft versions
  • Loading branch information
sekmiller committed Apr 15, 2015
1 parent b278c8b commit 857793e
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 36 deletions.
1 change: 1 addition & 0 deletions src/main/java/Bundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ next=Next
more=More...
less=Less...
select=Select...
selectedFiles=Selected Files
htmlAllowedTitle=Allowed HTML Tags
htmlAllowedMsg=This field supports only certain <span class="text-info popoverHTML">HTML tags</span>.
htmlAllowedTags=<a>, <b>, <blockquote>, <br>, <code>, <del>, <dd>, <dl>, <dt>, <em>, <hr>, <h1>-<h3>, <i>, <img>, <kbd>, <li>, <ol>, <p>, <pre>, <s>, <sup>, <sub>, <strong>, <strike>, <ul>
Expand Down
120 changes: 99 additions & 21 deletions src/main/java/edu/harvard/iq/dataverse/DatasetPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ public enum DisplayMode {
private Map<String, Boolean> datasetPermissionMap = new HashMap<>(); // { Permission human_name : Boolean }
private Map<Long, Boolean> fileDownloadPermissionMap = new HashMap<>(); // { FileMetadata.id : Boolean }

private DataFile selectedDownloadFile;

public DataFile getSelectedDownloadFile() {
return selectedDownloadFile;
}

public void setSelectedDownloadFile(DataFile selectedDownloadFile) {
this.selectedDownloadFile = selectedDownloadFile;
}
public Dataverse getLinkingDataverse() {
return linkingDataverse;
}
Expand Down Expand Up @@ -1104,20 +1113,36 @@ public String saveGuestbookResponse(String type) {

Command cmd;
try {
cmd = new CreateGuestbookResponseCommand(session.getUser(), this.guestbookResponse, dataset);
commandEngine.submit(cmd);
if (this.guestbookResponse != null) {
if (!type.equals("multiple")) {
cmd = new CreateGuestbookResponseCommand(session.getUser(), this.guestbookResponse, dataset);
commandEngine.submit(cmd);
} else {
for (Long dfId : this.downloadSelection) {
DataFile df = datafileService.find(dfId);
if (df != null) {
this.guestbookResponse.setDataFile(df);
cmd = new CreateGuestbookResponseCommand(session.getUser(), this.guestbookResponse, dataset);
commandEngine.submit(cmd);
}
}
}
}
} catch (CommandException ex) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Guestbook Response Save Failed", " - " + ex.toString()));
logger.severe(ex.getMessage());

}

if (guestbookResponse.getDataFile() != null && type.equals("download")) {
return callDownloadServlet(downloadFormat, guestbookResponse.getDataFile().getId());
if (type.equals("multiple")){
return callDownloadServlet(getSelectedDownloadIds());
}

if ((type.equals("download") || type.isEmpty())) {
return callDownloadServlet(downloadFormat, this.selectedDownloadFile.getId());
}

if (guestbookResponse.getDataFile() != null && type.equals("explore")) {
String retVal = getDataExploreURLComplete(guestbookResponse.getDataFile().getId());
if (type.equals("explore")) {
String retVal = getDataExploreURLComplete(this.selectedDownloadFile.getId());
try {
FacesContext.getCurrentInstance().getExternalContext().redirect(retVal);
return retVal;
Expand All @@ -1139,24 +1164,36 @@ public String exploreOutputLink(FileMetadata fm, String type){
return "";
}

private String callDownloadServlet(String multiFileString){

String fileDownloadUrl = "/api/access/datafiles/" + multiFileString;
try {
FacesContext.getCurrentInstance().getExternalContext().redirect(fileDownloadUrl);
} catch (IOException ex) {
logger.info("Failed to issue a redirect to file download url.");
}

return fileDownloadUrl;
}

private String callDownloadServlet( String downloadType, Long fileId){

String fileDownloadUrl = "/api/access/datafile/" + fileId;

if (downloadType != null && downloadType.equals("bundle")){
fileDownloadUrl = "/api/access/datafile/bundle/" + guestbookResponse.getDataFile().getId();
fileDownloadUrl = "/api/access/datafile/bundle/" + this.selectedDownloadFile.getId();
}
if (downloadType != null && downloadType.equals("original")){
fileDownloadUrl = "/api/access/datafile/" + guestbookResponse.getDataFile().getId() + "?format=original";
fileDownloadUrl = "/api/access/datafile/" + this.selectedDownloadFile.getId() + "?format=original";
}
if (downloadType != null && downloadType.equals("RData")){
fileDownloadUrl = "/api/access/datafile/" + guestbookResponse.getDataFile().getId() + "?format=RData";
fileDownloadUrl = "/api/access/datafile/" + this.selectedDownloadFile.getId() + "?format=RData";
}
if (downloadType != null && downloadType.equals("var")){
fileDownloadUrl = "/api/meta/datafile/" + guestbookResponse.getDataFile().getId();
fileDownloadUrl = "/api/meta/datafile/" + this.selectedDownloadFile.getId();
}
if (downloadType != null && downloadType.equals("tab")){
fileDownloadUrl = "/api/access/datafile/" + guestbookResponse.getDataFile().getId()+ "?format=tab";
fileDownloadUrl = "/api/access/datafile/" + this.selectedDownloadFile.getId()+ "?format=tab";
}
logger.fine("Returning file download url: " + fileDownloadUrl);
try {
Expand Down Expand Up @@ -2196,19 +2233,35 @@ public void setDatasetVersionDifference(DatasetVersionDifference datasetVersionD
this.datasetVersionDifference = datasetVersionDifference;
}

private void createSilentGuestbookEntry(FileMetadata fileMetadata, String format){
private void createSilentGuestbookEntry(FileMetadata fileMetadata, String format){
initGuestbookResponse(fileMetadata, format);
Command cmd;
try {
cmd = new CreateGuestbookResponseCommand(session.getUser(), guestbookResponse, dataset);
commandEngine.submit(cmd);
if (this.guestbookResponse != null) {
cmd = new CreateGuestbookResponseCommand(session.getUser(), guestbookResponse, dataset);
commandEngine.submit(cmd);
}
} catch (CommandException ex) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Guestbook Response Save Failed", " - " + ex.toString()));
logger.severe(ex.getMessage());
}

}

public String startMultipleFileDownload (){
String downloadIdString = getSelectedDownloadIds();
if (downloadIdString != null ){
for (Long dfId : this.downloadSelection){
DataFile df = datafileService.find(dfId);
if (df.isReleased()){
createSilentGuestbookEntry(df.getFileMetadata(), "");
}
}
return callDownloadServlet(downloadIdString);
}
return "";
}

public String startFileDownload(FileMetadata fileMetadata, String format) {
createSilentGuestbookEntry(fileMetadata, format);
return callDownloadServlet(format, fileMetadata.getDataFile().getId());
Expand All @@ -2235,14 +2288,28 @@ public void setDownloadType(String downloadType) {
}


public void initGuestbookResponse(FileMetadata fileMetadata){
public void initGuestbookResponse(FileMetadata fileMetadata){
initGuestbookResponse(fileMetadata, "");
}

public void initGuestbookMultipleResponse(){
initGuestbookResponse(null, "download");
}

public void initGuestbookResponse(FileMetadata fileMetadata, String downloadFormat) {
if (fileMetadata != null){
this.setSelectedDownloadFile(fileMetadata.getDataFile());
}
setDownloadFormat(downloadFormat);
setDownloadType("download");

if (fileMetadata == null){
setDownloadType("multiple");
} else {
setDownloadType("download");
}
if(this.workingVersion != null && this.workingVersion.isDraft()){
this.guestbookResponse = null;
return;
}
this.guestbookResponse = new GuestbookResponse();

User user = session.getUser();
Expand All @@ -2262,9 +2329,15 @@ public void initGuestbookResponse(FileMetadata fileMetadata, String downloadForm
this.guestbookResponse.setPosition(aUser.getPosition());
this.guestbookResponse.setSessionId(session.toString());
}
this.guestbookResponse.setDataFile(fileMetadata.getDataFile());
if (fileMetadata != null){
this.guestbookResponse.setDataFile(fileMetadata.getDataFile());
}
} else {
this.guestbookResponse = guestbookServiceBean.initDefaultGuestbookResponse(dataset, fileMetadata.getDataFile(), user, session);
if (fileMetadata != null){
this.guestbookResponse = guestbookServiceBean.initDefaultGuestbookResponse(dataset, fileMetadata.getDataFile(), user, session);
} else {
this.guestbookResponse = guestbookServiceBean.initDefaultGuestbookResponse(dataset, null, user, session);
}
}
if (this.dataset.getGuestbook() != null && !this.dataset.getGuestbook().getCustomQuestions().isEmpty()) {
this.guestbookResponse.setCustomQuestionResponses(new ArrayList());
Expand Down Expand Up @@ -2908,7 +2981,12 @@ public String getFileDateToDisplay(FileMetadata fileMetadata) {
public boolean isDownloadPopupRequired() {
// Each of these conditions is sufficient reason to have to
// present the user with the popup:


//0. if version is draft then Popup "not required"
if (!workingVersion.isReleased()){
return false;
}

// 1. License and Terms of Use:
if (!DatasetVersion.License.CC0.equals(workingVersion.getLicense())
&& !(workingVersion.getTermsOfUse() == null
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/DatasetVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ public void setLicense(License license) {
public List<FileMetadata> getFileMetadatas() {
return fileMetadatas;
}

public List<FileMetadata> getFileMetadatasSorted() {
Collections.sort(fileMetadatas, FileMetadata.compareByLabel);
return fileMetadatas;
}

public void setFileMetadatas(List<FileMetadata> fileMetadatas) {
this.fileMetadatas = fileMetadatas;
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/FileMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -334,4 +335,10 @@ public String toString() {
return "edu.harvard.iq.dvn.core.study.FileMetadata[id=" + id + "]";
}

public static final Comparator<FileMetadata> compareByLabel = new Comparator<FileMetadata>() {
@Override
public int compare(FileMetadata o1, FileMetadata o2) {
return o1.getLabel().compareTo(o2.getLabel());
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,9 @@ public AuthenticatedUser getAuthenticatedUser(User user) {
public GuestbookResponse initDefaultGuestbookResponse(Dataset dataset, DataFile dataFile, User user, DataverseSession session) {
GuestbookResponse guestbookResponse = new GuestbookResponse();
guestbookResponse.setGuestbook(findDefaultGuestbook());
guestbookResponse.setDataFile(dataFile);
if (dataFile != null){
guestbookResponse.setDataFile(dataFile);
}
guestbookResponse.setDataset(dataset);
guestbookResponse.setResponseTime(new Date());
guestbookResponse.setSessionId(session.toString());
Expand Down
Loading

0 comments on commit 857793e

Please sign in to comment.