Skip to content

Commit 5efd557

Browse files
committed
Add usage and response counts to get guestbook
1 parent 6a144ac commit 5efd557

4 files changed

Lines changed: 37 additions & 3 deletions

File tree

src/main/java/edu/harvard/iq/dataverse/api/Guestbooks.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import edu.harvard.iq.dataverse.Dataverse;
44
import edu.harvard.iq.dataverse.Guestbook;
5+
import edu.harvard.iq.dataverse.GuestbookResponseServiceBean;
56
import edu.harvard.iq.dataverse.GuestbookServiceBean;
67
import edu.harvard.iq.dataverse.api.auth.AuthRequired;
78
import edu.harvard.iq.dataverse.authorization.Permission;
@@ -38,6 +39,8 @@ public class Guestbooks extends AbstractApiBean {
3839

3940
@EJB
4041
GuestbookServiceBean guestbookService;
42+
@EJB
43+
GuestbookResponseServiceBean guestbookResponseService;
4144

4245
@GET
4346
@AuthRequired
@@ -57,16 +60,22 @@ public Response getGuestbook(@Context ContainerRequestContext crc, @PathParam("i
5760
@GET
5861
@AuthRequired
5962
@Path("{identifier}/list")
60-
public Response getGuestbooks(@Context ContainerRequestContext crc, @PathParam("identifier") String identifier) {
63+
public Response getGuestbooks(@Context ContainerRequestContext crc, @PathParam("identifier") String identifier, @QueryParam("ignoreStats") boolean ignoreStats) {
6164
return response( req -> {
6265
Dataverse dataverse = findDataverseOrDie(identifier);
66+
final Long dataverseId = dataverse.getId();
6367
if (!permissionSvc.request(req).on(dataverse).has(Permission.EditDataverse)) {
6468
return error(Response.Status.FORBIDDEN, "Not authorized");
6569
}
6670
List<Guestbook> guestbooks = guestbookService.findGuestbooksForGivenDataverse(dataverse);
6771
JsonArrayBuilder guestbookArray = Json.createArrayBuilder();
6872
JsonPrinter jsonPrinter = new JsonPrinter();
6973
for (Guestbook gb : guestbooks) {
74+
// default is to include the stats. Ignore the stats for a faster reply if they are not needed
75+
if (!ignoreStats) {
76+
gb.setUsageCount(guestbookService.findCountUsages(gb.getId(), dataverseId));
77+
gb.setResponseCount(guestbookResponseService.findCountByGuestbookId(gb.getId(), dataverseId));
78+
}
7079
guestbookArray.add(jsonPrinter.json(gb));
7180
}
7281
return ok(guestbookArray);

src/main/java/edu/harvard/iq/dataverse/util/json/JsonPrinter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,12 @@ public static JsonObjectBuilder json(Guestbook guestbook) {
402402
guestbookObject.add("nameRequired", guestbook.isNameRequired());
403403
guestbookObject.add("institutionRequired", guestbook.isInstitutionRequired());
404404
guestbookObject.add("positionRequired", guestbook.isPositionRequired());
405+
if (guestbook.getUsageCount() != null) {
406+
guestbookObject.add("usageCount", guestbook.getUsageCount());
407+
}
408+
if (guestbook.getResponseCount() != null) {
409+
guestbookObject.add("responseCount", guestbook.getResponseCount());
410+
}
405411
JsonArrayBuilder customQuestions = Json.createArrayBuilder();
406412
if (guestbook.getCustomQuestions() != null) {
407413
for (CustomQuestion cq : guestbook.getCustomQuestions()) {

src/test/java/edu/harvard/iq/dataverse/api/FilesIT.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3983,6 +3983,7 @@ public void testDownloadFileWithGuestbookResponse() throws IOException, JsonPars
39833983
.statusCode(BAD_REQUEST.getStatusCode());
39843984
String guestbookResponseForGuest = guestbookResponse.replace("\"guestbookResponse\": {",
39853985
"\"guestbookResponse\": { \"name\":\"My Name\", \"email\":\"myemail@example.com\", \"position\":\"My Position\", \"institution\":\"My Institution\",");
3986+
39863987
// With GuestbookResponse. Guest user doesn't have the required Name, etc. So we will add those to the Guestbook Response
39873988
downloadResponse = UtilIT.postDownloadFile(fileId4, guestbookResponseForGuest);
39883989
downloadResponse.prettyPrint();
@@ -4008,6 +4009,12 @@ public void testDownloadFileWithGuestbookResponse() throws IOException, JsonPars
40084009
.statusCode(OK.getStatusCode());
40094010
signedUrl = UtilIT.getSignedUrlFromResponse(downloadResponse);
40104011

4012+
// Verify that the Guestbook Response is persisted
4013+
Response guestbookResponseResponse = UtilIT.getGuestbookResponses(dataverseAlias, guestbook.getId(), ownerApiToken);
4014+
guestbookResponseResponse.then().assertThat()
4015+
.statusCode(OK.getStatusCode());
4016+
assertTrue(guestbookResponseResponse.prettyPrint().contains("What color car do you drive,Yellow"));
4017+
40114018
// Download the file using the signed url
40124019
signedUrlResponse = get(signedUrl);
40134020
assertEquals(OK.getStatusCode(), signedUrlResponse.getStatusCode());
@@ -4053,6 +4060,14 @@ public void testDownloadFileWithGuestbookResponse() throws IOException, JsonPars
40534060
signedUrl = UtilIT.getSignedUrlFromResponse(downloadResponse);
40544061
signedUrlResponse = get(signedUrl);
40554062
assertEquals(OK.getStatusCode(), signedUrlResponse.getStatusCode());
4063+
4064+
// Verify that the guestbook has proper stats
4065+
Response guestbookListResponse = UtilIT.getGuestbooks(dataverseAlias, ownerApiToken);
4066+
guestbookListResponse.prettyPrint();
4067+
guestbookListResponse.then().assertThat()
4068+
.statusCode(OK.getStatusCode())
4069+
.body("data[0].usageCount", is(1))
4070+
.body("data[0].responseCount", is(16));
40564071
}
40574072

40584073
// This test is disabled because it is only compatible with the containerized development environment and would cause the Jenkins job to fail.

src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,9 @@ static Response downloadFileOriginal(Integer fileId, String apiToken) {
12671267

12681268
static Response getDownloadFileUrlWithGuestbookResponse(Integer fileId, String apiToken, String body) {
12691269
RequestSpecification requestSpecification = given();
1270-
requestSpecification.header(API_TOKEN_HTTP_HEADER, apiToken);
1270+
if (apiToken != null) {
1271+
requestSpecification.header(API_TOKEN_HTTP_HEADER, apiToken);
1272+
}
12711273
if (body != null) {
12721274
requestSpecification.body(body);
12731275
}
@@ -1276,7 +1278,9 @@ static Response getDownloadFileUrlWithGuestbookResponse(Integer fileId, String a
12761278

12771279
static Response downloadFilesUrlWithGuestbookResponse(Integer[] fileIds, String apiToken, String body) {
12781280
RequestSpecification requestSpecification = given();
1279-
requestSpecification.header(API_TOKEN_HTTP_HEADER, apiToken);
1281+
if (apiToken != null) {
1282+
requestSpecification.header(API_TOKEN_HTTP_HEADER, apiToken);
1283+
}
12801284
if (body != null) {
12811285
requestSpecification.body(body);
12821286
}

0 commit comments

Comments
 (0)