Search in sources :

Example 1 with GetUserTracesCommand

use of edu.harvard.iq.dataverse.engine.command.impl.GetUserTracesCommand in project dataverse by IQSS.

the class Users method getTraces.

@GET
@Path("{identifier}/traces")
public Response getTraces(@PathParam("identifier") String identifier) {
    try {
        AuthenticatedUser userToQuery = authSvc.getAuthenticatedUser(identifier);
        JsonObjectBuilder jsonObj = execCommand(new GetUserTracesCommand(createDataverseRequest(findUserOrDie()), userToQuery, null));
        return ok(jsonObj);
    } catch (WrappedResponse ex) {
        return ex.getResponse();
    }
}
Also used : GetUserTracesCommand(edu.harvard.iq.dataverse.engine.command.impl.GetUserTracesCommand) JsonObjectBuilder(javax.json.JsonObjectBuilder) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 2 with GetUserTracesCommand

use of edu.harvard.iq.dataverse.engine.command.impl.GetUserTracesCommand in project dataverse by IQSS.

the class Users method getTraces.

@GET
@Path("{identifier}/traces/{element}")
@Produces("text/csv, application/json")
public Response getTraces(@Context Request req, @PathParam("identifier") String identifier, @PathParam("element") String element) {
    try {
        AuthenticatedUser userToQuery = authSvc.getAuthenticatedUser(identifier);
        if (!elements.contains(element)) {
            throw new BadRequestException("Not a valid element");
        }
        JsonObjectBuilder jsonObj = execCommand(new GetUserTracesCommand(createDataverseRequest(findUserOrDie()), userToQuery, element));
        List<Variant> vars = Variant.mediaTypes(MediaType.valueOf(FileUtil.MIME_TYPE_CSV), MediaType.APPLICATION_JSON_TYPE).add().build();
        MediaType requestedType = req.selectVariant(vars).getMediaType();
        if ((requestedType != null) && (requestedType.equals(MediaType.APPLICATION_JSON_TYPE))) {
            return ok(jsonObj);
        }
        JsonArray items = null;
        try {
            items = jsonObj.build().getJsonObject("traces").getJsonObject(element).getJsonArray("items");
        } catch (Exception e) {
            return ok(jsonObj);
        }
        return ok(FileUtil.jsonArrayOfObjectsToCSV(items, items.getJsonObject(0).keySet().toArray(new String[0])), MediaType.valueOf(FileUtil.MIME_TYPE_CSV), element + ".csv");
    } catch (WrappedResponse ex) {
        return ex.getResponse();
    }
}
Also used : Variant(javax.ws.rs.core.Variant) JsonArray(javax.json.JsonArray) BadRequestException(javax.ws.rs.BadRequestException) GetUserTracesCommand(edu.harvard.iq.dataverse.engine.command.impl.GetUserTracesCommand) MediaType(javax.ws.rs.core.MediaType) JsonObjectBuilder(javax.json.JsonObjectBuilder) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) BadRequestException(javax.ws.rs.BadRequestException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)2 GetUserTracesCommand (edu.harvard.iq.dataverse.engine.command.impl.GetUserTracesCommand)2 JsonObjectBuilder (javax.json.JsonObjectBuilder)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 JsonArray (javax.json.JsonArray)1 BadRequestException (javax.ws.rs.BadRequestException)1 Produces (javax.ws.rs.Produces)1 MediaType (javax.ws.rs.core.MediaType)1 Variant (javax.ws.rs.core.Variant)1