Skip to content

Commit

Permalink
changed some method/variable names to be more descriptive;
Browse files Browse the repository at this point in the history
changed the name of the API method, to be more REST-like;
modified the "fix" method to only recalculate the unf for versions that don't have one.
  • Loading branch information
landreev committed Feb 2, 2017
1 parent a8fbc08 commit 41fa5fa
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import org.apache.commons.lang.StringUtils;
import org.jsoup.helper.StringUtil;


/**
Expand Down Expand Up @@ -994,6 +995,10 @@ public JsonObjectBuilder fixUnf(String datasetVersionId) {
info.add("message", "Could not find a dataset version based on datasetVersionId " + datasetVersionId + ".");
return info;
}
if (!StringUtil.isBlank(datasetVersion.getUNF())) {
info.add("message", "Dataset version (id=" + datasetVersionId + ") already has a UNF. Blank the UNF value in the database if you must change it.");
return info;
}
IngestUtil.recalculateDatasetVersionUNF(datasetVersion);
DatasetVersion saved = em.merge(datasetVersion);
info.add("message", "New UNF value saved (" + saved.getUNF() + "). Reindexing dataset.");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/edu/harvard/iq/dataverse/api/Admin.java
Original file line number Diff line number Diff line change
Expand Up @@ -868,14 +868,14 @@ public Response findRoleAssignee(@PathParam("idtf") String idtf) {
@Path("datasets/integrity")
@GET
public Response checkDatasetIntegrity() {
JsonArray datasetVersionsWithWrongUnfValue = IngestUtil.getUnfData(datasetSvc.findAll()).build();
JsonArray datasetVersionsWithWrongUnfValue = IngestUtil.getVersionsWithMissingUNFs(datasetSvc.findAll()).build();
JsonObjectBuilder info = Json.createObjectBuilder();
info.add("numProblems", datasetVersionsWithWrongUnfValue.size());
info.add("problems", datasetVersionsWithWrongUnfValue);
return ok(info);
}

@Path("datasets/integrity/unf/{datasetVersionId}")
@Path("datasets/integrity/{datasetVersionId}/fixunf")
@POST
public Response fixUnf(@PathParam("datasetVersionId") String datasetVersionId) {
JsonObjectBuilder info = datasetVersionSvc.fixUnf(datasetVersionId);
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/edu/harvard/iq/dataverse/ingest/IngestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,16 @@ public static boolean shouldHaveUnf(DatasetVersion version) {
}

}

public static JsonArrayBuilder getUnfData(List<Dataset> datasets) {
JsonArrayBuilder datasetVersionsWithWrongUnfValue = Json.createArrayBuilder();

// TODO:
// provide a similar method that would find versions with *wrong* UNFs;
// i.e. recalculate on the fly the UNFs for the versions that have them,
// compare the 2, and list the mismatches.

public static JsonArrayBuilder getVersionsWithMissingUNFs(List<Dataset> datasets) {
JsonArrayBuilder datasetVersionsWithMissingUNFs = Json.createArrayBuilder();
if (datasets == null || datasets.isEmpty()) {
return datasetVersionsWithWrongUnfValue;
return datasetVersionsWithMissingUNFs;
}
for (Dataset dataset : datasets) {
for (DatasetVersion dsv : dataset.getVersions()) {
Expand All @@ -290,18 +295,18 @@ public static JsonArrayBuilder getUnfData(List<Dataset> datasets) {
JsonObjectBuilder problem = Json.createObjectBuilder();
problem.add("datasetVersionId", dsv.getId());
problem.add("message", msg);
datasetVersionsWithWrongUnfValue.add(problem);
datasetVersionsWithMissingUNFs.add(problem);
}
} else if (existingUnf != null) {
String msg = "Dataset version " + dsv.getSemanticVersion() + " (datasetVersionId " + dsv.getId() + ") from " + dsv.getDataset().getGlobalId() + " has a UNF (" + existingUnf + ") but shouldn't!";
JsonObjectBuilder problem = Json.createObjectBuilder();
problem.add("datasetVersionId", dsv.getId());
problem.add("message", msg);
datasetVersionsWithWrongUnfValue.add(problem);
datasetVersionsWithMissingUNFs.add(problem);
}
}
}
return datasetVersionsWithWrongUnfValue;
return datasetVersionsWithMissingUNFs;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -590,9 +590,9 @@ public void testGetUnfData() {
Dataset dataset = new Dataset();
DatasetVersion dsv1 = new DatasetVersion();
datasets.add(dataset);
IngestUtil.getUnfData(datasets);
IngestUtil.getVersionsWithMissingUNFs(datasets);
assertEquals(null, dsv1.getUNF());
assertEquals(Json.createArrayBuilder().build(), IngestUtil.getUnfData(null).build());
assertEquals(Json.createArrayBuilder().build(), IngestUtil.getVersionsWithMissingUNFs(null).build());
}

@Test
Expand Down Expand Up @@ -623,7 +623,7 @@ public void testDatasetShouldNotHaveUnf() {
datasetVersions.add(dsv1);
dataset.setVersions(datasetVersions);
datasets.add(dataset);
JsonArray array = IngestUtil.getUnfData(datasets).build();
JsonArray array = IngestUtil.getVersionsWithMissingUNFs(datasets).build();
System.out.println("array: " + array);
assertEquals("pretendThisIsValidUnf", dsv1.getUNF());
assertEquals(42, array.getJsonObject(0).getInt("datasetVersionId"));
Expand Down Expand Up @@ -660,7 +660,7 @@ public void testDatasetShouldHaveUnf() {

dataset.setVersions(datasetVersions);
datasets.add(dataset);
JsonArray array = IngestUtil.getUnfData(datasets).build();
JsonArray array = IngestUtil.getVersionsWithMissingUNFs(datasets).build();
System.out.println("array: " + array);
assertEquals(42, array.getJsonObject(0).getInt("datasetVersionId"));
assertEquals("Dataset version DRAFT (datasetVersionId 42) from doi:fakeAuthority/12345 doesn't have a UNF but should!", array.getJsonObject(0).getString("message"));
Expand Down

0 comments on commit 41fa5fa

Please sign in to comment.