Skip to content

Commit

Permalink
simply conflict-checking code #6962
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Jun 15, 2020
1 parent a34d7c1 commit 2debed2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 50 deletions.
37 changes: 13 additions & 24 deletions src/main/java/edu/harvard/iq/dataverse/api/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -390,31 +390,20 @@ public Response updateFileMetadata(@FormDataParam("jsonData") String jsonData,

JsonReader jsonReader = Json.createReader(new StringReader(jsonData));
javax.json.JsonObject jsonObject = jsonReader.readObject();
String label = jsonObject.getString("label", null);
String directoryLabel = jsonObject.getString("directoryLabel", null);
// If the user is trying to change the label/directoryLabel or not.
boolean labelChange = true;
String oldLabel = df.getFileMetadata().getLabel();
String oldDirectoryLabel = df.getFileMetadata().getDirectoryLabel();
String oldPathPlusName = oldDirectoryLabel + "/" + oldLabel;
if (directoryLabel == null) {
directoryLabel = oldDirectoryLabel;
}
if (label == null) {
label = oldLabel;
}
String incomingPathPlusName = directoryLabel + "/" + label;
if (oldPathPlusName.equals(incomingPathPlusName)) {
labelChange = false;
}
logger.fine("For file id " + df.getId() + " user is trying to change the label: " + labelChange);
if (labelChange && IngestUtil.conflictsWithExistingFilenames(label, directoryLabel, fmdList, df)) {
String pathPlusFilename = "";
if (directoryLabel != null) {
pathPlusFilename = directoryLabel + "/" + label;
} else {
pathPlusFilename = label;
String incomingLabel = jsonObject.getString("label", null);
String incomingDirectoryLabel = jsonObject.getString("directoryLabel", null);
String existingLabel = df.getFileMetadata().getLabel();
String existingDirectoryLabel = df.getFileMetadata().getDirectoryLabel();
String pathPlusFilename = IngestUtil.getPathAndFileNameToCheck(incomingLabel, incomingDirectoryLabel, existingLabel, existingDirectoryLabel);
// We remove the current file from the list we'll check for duplicates.
// Instead, the current file is passed in as pathPlusFilename.
List<FileMetadata> fmdListMinusCurrentFile = new ArrayList<>();
for (FileMetadata fileMetadata : fmdList) {
if (!fileMetadata.equals(df.getFileMetadata())) {
fmdListMinusCurrentFile.add(fileMetadata);
}
}
if (IngestUtil.conflictsWithExistingFilenames(pathPlusFilename, fmdListMinusCurrentFile)) {
return error(BAD_REQUEST, BundleUtil.getStringFromBundle("files.api.metadata.update.duplicateFile", Arrays.asList(pathPlusFilename)));
}

Expand Down
49 changes: 23 additions & 26 deletions src/main/java/edu/harvard/iq/dataverse/ingest/IngestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,38 +101,35 @@ public static String duplicateFilenameCheck(FileMetadata fileMetadata, Set<Strin
return fileName;
}

/**
* Given an existing file that may or may not have a directoryLabel, take
* the incoming label and/or directory label and combine it with what's in
* the existing file, overwriting and filling in as necessary.
*/
public static String getPathAndFileNameToCheck(String incomingLabel, String incomingDirectoryLabel, String existingLabel, String existingDirectoryLabel) {
String labelToReturn = existingLabel;
String directoryLabelToReturn = existingDirectoryLabel;
if (incomingLabel != null) {
labelToReturn = incomingLabel;
}
if (incomingDirectoryLabel != null) {
directoryLabelToReturn = incomingDirectoryLabel;
}
if (directoryLabelToReturn != null) {
return directoryLabelToReturn + "/" + labelToReturn;
} else {
return labelToReturn;
}
}

/**
* Given a new proposed label or directoryLabel for a file, check against
* existing files if a duplicate directoryLabel/label combination would be
* created.
*
* @param label The new label (filename) that is being proposed. Can be
* null.
* @param directoryLabel The new directoryLabel (file path) that is being
* proposed. Can be null.
* @param fileMetadatas The list fileMetadatas to be compared against,
* probably from a draft.
* @param dataFile The file that is being updated with a new name or path.
* @return true if there is a conflict, false otherwise.
*/
public static boolean conflictsWithExistingFilenames(String label, String directoryLabel, List<FileMetadata> fileMetadatas, DataFile dataFile) {
public static boolean conflictsWithExistingFilenames(String pathPlusFilename, List<FileMetadata> fileMetadatas) {
List<String> filePathsAndNames = getPathsAndFileNames(fileMetadatas);
if (label != null || directoryLabel != null) {
String path = "";
if (directoryLabel != null) {
path = directoryLabel + "/";
}
if (label == null) {
label = dataFile.getFileMetadata().getLabel();
}
String incomingPathPlusFileName = path + label;
logger.fine(filePathsAndNames.toString());
logger.fine("incomingPathName: " + incomingPathPlusFileName);
if (filePathsAndNames.contains(incomingPathPlusFileName)) {
return true;
}
}
return false;
return filePathsAndNames.contains(pathPlusFilename);
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/ingest/IngestUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -611,4 +612,25 @@ public void testUnfUtil() {
assertEquals("UNF:6:FWBO/a1GcxDnM3fNLdzrHw==", datasetUnfValue);
}

@Test
public void testPathPlusFilename() {
String incomingLabel = "incomingLabel";
String incomingDirectoryLabel = "incomingDirectoryLabel";
String existingLabel = "existingLabel";
String existingDirectoryLabel = "existingDirectoryLabel";
String pathPlusFilename = IngestUtil.getPathAndFileNameToCheck(incomingLabel, incomingDirectoryLabel, existingLabel, existingDirectoryLabel);
assertEquals("incomingDirectoryLabel/incomingLabel", pathPlusFilename);
}

@Test
public void renameFileToSameName() {
String pathPlusFilename = "README.md";
FileMetadata file1 = new FileMetadata();
file1.setLabel("README.md");
FileMetadata file2 = new FileMetadata();
file2.setLabel("README2.md");
List<FileMetadata> fileMetadatas = Arrays.asList(file1, file2);
assertTrue(IngestUtil.conflictsWithExistingFilenames(pathPlusFilename, fileMetadatas));
}

}

0 comments on commit 2debed2

Please sign in to comment.