Search in sources :

Example 1 with NullSafeJsonBuilder

use of edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder in project dataverse by IQSS.

the class AuthenticatedUser method toJson.

public JsonObjectBuilder toJson() {
    // JsonObjectBuilder authenicatedUserJson = Json.createObjectBuilder();
    NullSafeJsonBuilder authenicatedUserJson = NullSafeJsonBuilder.jsonObjectBuilder();
    authenicatedUserJson.add("id", this.id);
    authenicatedUserJson.add("userIdentifier", this.userIdentifier);
    authenicatedUserJson.add("lastName", this.lastName);
    authenicatedUserJson.add("firstName", this.firstName);
    authenicatedUserJson.add("email", this.email);
    authenicatedUserJson.add("affiliation", UserUtil.getStringOrNull(this.affiliation));
    authenicatedUserJson.add("position", UserUtil.getStringOrNull(this.position));
    authenicatedUserJson.add("isSuperuser", this.superuser);
    authenicatedUserJson.add("authenticationProvider", this.authProviderFactoryAlias);
    authenicatedUserJson.add("roles", UserUtil.getStringOrNull(this.roles));
    authenicatedUserJson.add("createdTime", UserUtil.getTimestampStringOrNull(this.createdTime));
    authenicatedUserJson.add("lastLoginTime", UserUtil.getTimestampStringOrNull(this.lastLoginTime));
    authenicatedUserJson.add("lastApiUseTime", UserUtil.getTimestampStringOrNull(this.lastApiUseTime));
    return authenicatedUserJson;
}
Also used : NullSafeJsonBuilder(edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder)

Example 2 with NullSafeJsonBuilder

use of edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder in project dataverse by IQSS.

the class Index method searchPermsDebug.

/**
 * This method is for integration tests of search.
 */
@GET
@Path("permsDebug")
public Response searchPermsDebug(@QueryParam("key") String apiToken, @QueryParam("id") Long dvObjectId) {
    User user = findUserByApiToken(apiToken);
    if (user == null) {
        return error(Response.Status.UNAUTHORIZED, "Invalid apikey '" + apiToken + "'");
    }
    DvObject dvObjectToLookUp = dvObjectService.findDvObject(dvObjectId);
    if (dvObjectToLookUp == null) {
        return error(Status.BAD_REQUEST, "Could not find DvObject based on id " + dvObjectId);
    }
    List<DvObjectSolrDoc> solrDocs = SolrIndexService.determineSolrDocs(dvObjectToLookUp);
    JsonObjectBuilder data = Json.createObjectBuilder();
    JsonArrayBuilder permissionsData = Json.createArrayBuilder();
    for (DvObjectSolrDoc solrDoc : solrDocs) {
        JsonObjectBuilder dataDoc = Json.createObjectBuilder();
        dataDoc.add(SearchFields.ID, solrDoc.getSolrId());
        dataDoc.add(SearchFields.NAME_SORT, solrDoc.getNameOrTitle());
        JsonArrayBuilder perms = Json.createArrayBuilder();
        for (String perm : solrDoc.getPermissions()) {
            perms.add(perm);
        }
        dataDoc.add(SearchFields.DISCOVERABLE_BY, perms);
        permissionsData.add(dataDoc);
    }
    data.add("perms", permissionsData);
    DvObject dvObject = dvObjectService.findDvObject(dvObjectId);
    NullSafeJsonBuilder timestamps = jsonObjectBuilder();
    timestamps.add(contentChanged, SearchUtil.getTimestampOrNull(dvObject.getModificationTime()));
    timestamps.add(contentIndexed, SearchUtil.getTimestampOrNull(dvObject.getIndexTime()));
    timestamps.add(permsChanged, SearchUtil.getTimestampOrNull(dvObject.getPermissionModificationTime()));
    timestamps.add(permsIndexed, SearchUtil.getTimestampOrNull(dvObject.getPermissionIndexTime()));
    Set<RoleAssignment> roleAssignments = rolesSvc.rolesAssignments(dvObject);
    JsonArrayBuilder roleAssignmentsData = Json.createArrayBuilder();
    for (RoleAssignment roleAssignment : roleAssignments) {
        roleAssignmentsData.add(roleAssignment.getRole() + " has been granted to " + roleAssignment.getAssigneeIdentifier() + " on " + roleAssignment.getDefinitionPoint());
    }
    data.add("timestamps", timestamps);
    data.add("roleAssignments", roleAssignmentsData);
    return ok(data);
}
Also used : DvObjectSolrDoc(edu.harvard.iq.dataverse.search.DvObjectSolrDoc) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) User(edu.harvard.iq.dataverse.authorization.users.User) GuestUser(edu.harvard.iq.dataverse.authorization.users.GuestUser) DvObject(edu.harvard.iq.dataverse.DvObject) RoleAssignment(edu.harvard.iq.dataverse.RoleAssignment) JsonArrayBuilder(javax.json.JsonArrayBuilder) NullSafeJsonBuilder(edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 3 with NullSafeJsonBuilder

use of edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder in project dataverse by IQSS.

the class Notifications method getAllNotificationsForUser.

@GET
@Path("all")
public Response getAllNotificationsForUser() {
    User user;
    try {
        user = findUserOrDie();
    } catch (WrappedResponse ex) {
        return error(Response.Status.UNAUTHORIZED, "You must supply an API token.");
    }
    if (user == null) {
        return error(Response.Status.BAD_REQUEST, "A user could not be found based on the API token.");
    }
    if (!(user instanceof AuthenticatedUser)) {
        // It's unlikely we'll reach this error. A Guest doesn't have an API token and would have been blocked above.
        return error(Response.Status.BAD_REQUEST, "Only an AuthenticatedUser can have notifications.");
    }
    AuthenticatedUser authenticatedUser = (AuthenticatedUser) user;
    JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
    List<UserNotification> notifications = userNotificationSvc.findByUser(authenticatedUser.getId());
    for (UserNotification notification : notifications) {
        NullSafeJsonBuilder notificationObjectBuilder = jsonObjectBuilder();
        JsonArrayBuilder reasonsForReturn = Json.createArrayBuilder();
        Type type = notification.getType();
        notificationObjectBuilder.add("id", notification.getId());
        notificationObjectBuilder.add("type", type.toString());
        /* FIXME - Re-add reasons for return if/when they are added to the notifications page.
            
            if (Type.RETURNEDDS.equals(type) || Type.SUBMITTEDDS.equals(type)) {
                JsonArrayBuilder reasons = getReasonsForReturn(notification);
                for (JsonValue reason : reasons.build()) {
                    reasonsForReturn.add(reason);
                }
                notificationObjectBuilder.add("reasonsForReturn", reasonsForReturn);
            }
            */
        jsonArrayBuilder.add(notificationObjectBuilder);
    }
    JsonObjectBuilder result = Json.createObjectBuilder().add("notifications", jsonArrayBuilder);
    return ok(result);
}
Also used : Type(edu.harvard.iq.dataverse.UserNotification.Type) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) User(edu.harvard.iq.dataverse.authorization.users.User) UserNotification(edu.harvard.iq.dataverse.UserNotification) JsonArrayBuilder(javax.json.JsonArrayBuilder) NullSafeJsonBuilder(edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 4 with NullSafeJsonBuilder

use of edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder in project dataverse by IQSS.

the class SolrSearchResult method json.

// getJsonForMydata
public JsonObjectBuilder json(boolean showRelevance, boolean showEntityIds, boolean showApiUrls) {
    if (this.type == null) {
        return jsonObjectBuilder();
    }
    String displayName = null;
    String identifierLabel = null;
    String datasetCitation = null;
    String preferredUrl = null;
    String apiUrl = null;
    if (this.type.equals(SearchConstants.DATAVERSES)) {
        displayName = this.name;
        identifierLabel = "identifier";
        preferredUrl = getHtmlUrl();
    } else if (this.type.equals(SearchConstants.DATASETS)) {
        displayName = this.title;
        identifierLabel = "global_id";
        preferredUrl = getPersistentUrl();
    /**
     * @todo Should we show the name of the parent dataverse?
     */
    } else if (this.type.equals(SearchConstants.FILES)) {
        displayName = this.name;
        identifierLabel = "file_id";
        preferredUrl = getDownloadUrl();
        /**
         * @todo show more information for a file's parent, such as the
         * title of the dataset it belongs to.
         */
        datasetCitation = parent.get("citation");
    }
    // displayName = null; // testing NullSafeJsonBuilder
    // because we are using NullSafeJsonBuilder key/value pairs will be dropped if the value is null
    NullSafeJsonBuilder nullSafeJsonBuilder = jsonObjectBuilder().add("name", displayName).add("type", getDisplayType(getType())).add("url", preferredUrl).add("image_url", getImageUrl()).add(identifierLabel, this.identifier).add("description", this.descriptionNoSnippet).add("published_at", getDateTimePublished()).add("file_type", this.filetype).add("file_content_type", this.fileContentType).add("size_in_bytes", getFileSizeInBytes()).add("md5", getFileMd5()).add("checksum", JsonPrinter.getChecksumTypeAndValue(getFileChecksumType(), getFileChecksumValue())).add("unf", getUnf()).add("dataset_citation", datasetCitation).add("deaccession_reason", this.deaccessionReason).add("citationHtml", this.citationHtml).add("citation", this.citation);
    // Now that nullSafeJsonBuilder has been instatiated, check for null before adding to it!
    if (showRelevance) {
        nullSafeJsonBuilder.add("matches", getRelevance());
        nullSafeJsonBuilder.add("score", getScore());
    }
    if (showEntityIds) {
        if (this.entityId != null) {
            nullSafeJsonBuilder.add("entity_id", this.entityId);
        }
    }
    if (showApiUrls) {
        /**
         * @todo We should probably have a metadata_url or api_url concept
         * enabled by default, not hidden behind an undocumented boolean.
         * For datasets, this would be http://example.com/api/datasets/10 or
         * whatever (to get more detailed JSON), but right now this requires
         * an API token. Discuss at
         * https://docs.google.com/document/d/1d8sT2GLSavgiAuMTVX8KzTCX0lROEET1edhvHHRDZOs/edit?usp=sharing";
         */
        if (getApiUrl() != null) {
            nullSafeJsonBuilder.add("api_url", getApiUrl());
        }
    }
    // NullSafeJsonBuilder is awesome but can't build null safe arrays. :(
    if (!datasetAuthors.isEmpty()) {
        JsonArrayBuilder authors = Json.createArrayBuilder();
        for (String datasetAuthor : datasetAuthors) {
            authors.add(datasetAuthor);
        }
        nullSafeJsonBuilder.add("authors", authors);
    }
    return nullSafeJsonBuilder;
}
Also used : NullSafeJsonBuilder(edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder) JsonArrayBuilder(javax.json.JsonArrayBuilder)

Example 5 with NullSafeJsonBuilder

use of edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder in project dataverse by IQSS.

the class WorkflowUtil method getAllWorkflowComments.

public static JsonArrayBuilder getAllWorkflowComments(DatasetVersion datasetVersion) {
    JsonArrayBuilder workflowCommentsAsJson = Json.createArrayBuilder();
    List<WorkflowComment> workflowComments = datasetVersion.getWorkflowComments();
    for (WorkflowComment workflowComment : workflowComments) {
        NullSafeJsonBuilder workflowCommentAsJson = jsonObjectBuilder();
        workflowCommentAsJson.add("workflowCommentId", workflowComment.getId());
        workflowCommentAsJson.add("message", workflowComment.getMessage());
        workflowCommentAsJson.add("createTime", Util.getDateTimeFormat().format(workflowComment.getCreated()));
        workflowCommentAsJson.add("commentBy", workflowComment.getAuthenticatedUser().getIdentifier());
        workflowCommentAsJson.add("datasetId", datasetVersion.getDataset().getId());
        workflowCommentAsJson.add("datasetVersionId", datasetVersion.getId());
        workflowCommentAsJson.add("datasetTitle", datasetVersion.getTitle());
        workflowCommentsAsJson.add(workflowCommentAsJson);
    }
    return workflowCommentsAsJson;
}
Also used : JsonArrayBuilder(javax.json.JsonArrayBuilder) NullSafeJsonBuilder(edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder)

Aggregations

NullSafeJsonBuilder (edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder)5 JsonArrayBuilder (javax.json.JsonArrayBuilder)4 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)2 User (edu.harvard.iq.dataverse.authorization.users.User)2 JsonObjectBuilder (javax.json.JsonObjectBuilder)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 DvObject (edu.harvard.iq.dataverse.DvObject)1 RoleAssignment (edu.harvard.iq.dataverse.RoleAssignment)1 UserNotification (edu.harvard.iq.dataverse.UserNotification)1 Type (edu.harvard.iq.dataverse.UserNotification.Type)1 GuestUser (edu.harvard.iq.dataverse.authorization.users.GuestUser)1 DvObjectSolrDoc (edu.harvard.iq.dataverse.search.DvObjectSolrDoc)1