Skip to content

Commit

Permalink
prevent filename duplicates from "edit metadata for files" page #6574
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed May 1, 2020
1 parent c3e9275 commit 35d07a4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/EditDatafilesPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import org.apache.commons.httpclient.methods.GetMethod;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Set;
import java.util.logging.Level;
import javax.faces.event.AjaxBehaviorEvent;
Expand Down Expand Up @@ -1094,6 +1095,11 @@ public String saveReplacementFile() throws FileReplaceException{
}

public String save() {
Collection<String> duplicates = IngestUtil.findDuplicateFilenames(workingVersion);
if (!duplicates.isEmpty()) {
JH.addMessage(FacesMessage.SEVERITY_ERROR, BundleUtil.getStringFromBundle("dataset.message.filesFailure"), BundleUtil.getStringFromBundle("dataset.message.editMetadata.duplicateFilenames", new ArrayList<>(duplicates)));
return null;
}
if (!saveEnabled) {
return "";
}
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/ingest/IngestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -141,6 +142,39 @@ public static boolean conflictsWithExistingFilenames(String label, String direct
return false;
}

/**
* Given a DatasetVersion, iterate across all the files (including their
* paths) and return any duplicates.
*
* @param datasetVersion
* @return A Collection of Strings in the form of path/to/file.txt
*/
public static Collection<String> findDuplicateFilenames(DatasetVersion datasetVersion) {
List<String> allFileNamesWithPaths = new ArrayList<>();
for (FileMetadata fileMetadata : datasetVersion.getFileMetadatas()) {
String directoryLabel = fileMetadata.getDirectoryLabel();
String path = "";
if (directoryLabel != null) {
path = directoryLabel + "/";
}
String pathAndfileName = path + fileMetadata.getLabel();
allFileNamesWithPaths.add(pathAndfileName);
}
return findDuplicates(allFileNamesWithPaths);
}

// https://stackoverflow.com/questions/7414667/identify-duplicates-in-a-list
private static <T> Set<T> findDuplicates(Collection<T> collection) {
Set<T> duplicates = new HashSet<>();
Set<T> uniques = new HashSet<>();
for (T t : collection) {
if (!uniques.add(t)) {
duplicates.add(t);
}
}
return duplicates;
}

// This method is called on a single file, when we need to modify the name
// of an already ingested/persisted datafile. For ex., when we have converted
// a file to tabular data, and want to update the extension accordingly.
Expand Down
1 change: 1 addition & 0 deletions src/main/java/propertyFiles/Bundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,7 @@ dataset.message.uploadFilesSingle.message=For more information about supported f
dataset.message.uploadFilesMultiple.message=Multiple file upload/download methods are available for this dataset. Once you upload a file using one of these methods, your choice will be locked in for this dataset.
dataset.message.editMetadata.label=Edit Dataset Metadata
dataset.message.editMetadata.message=Add more metadata about this dataset to help others easily find it.
dataset.message.editMetadata.duplicateFilenames=Duplicate filenames: {0}
dataset.message.editTerms.label=Edit Dataset Terms
dataset.message.editTerms.message=Add the terms of use for this dataset to explain how to access and use your data.
dataset.message.locked.editNotAllowedInReview=Dataset cannot be edited due to In Review dataset lock.
Expand Down

0 comments on commit 35d07a4

Please sign in to comment.