Search in sources :

Example 51 with JsonArrayBuilder

use of javax.json.JsonArrayBuilder in project dataverse by IQSS.

the class JsonPrinter method getFileCategories.

private static JsonArrayBuilder getFileCategories(FileMetadata fmd) {
    if (fmd == null) {
        return null;
    }
    List<String> categories = fmd.getCategoriesByName();
    if (categories == null || categories.isEmpty()) {
        return null;
    }
    JsonArrayBuilder fileCategories = Json.createArrayBuilder();
    for (String category : categories) {
        fileCategories.add(category);
    }
    return fileCategories;
}
Also used : JsonArrayBuilder(javax.json.JsonArrayBuilder)

Example 52 with JsonArrayBuilder

use of javax.json.JsonArrayBuilder in project dataverse by IQSS.

the class JsonPrinter method json.

public static JsonObject json(DatasetField dfv) {
    if (dfv.isEmpty()) {
        return null;
    } else {
        JsonArrayBuilder fieldArray = Json.createArrayBuilder();
        DatasetFieldWalker.walk(dfv, new DatasetFieldsToJson(fieldArray));
        JsonArray out = fieldArray.build();
        return out.getJsonObject(0);
    }
}
Also used : JsonArray(javax.json.JsonArray) JsonArrayBuilder(javax.json.JsonArrayBuilder)

Example 53 with JsonArrayBuilder

use of javax.json.JsonArrayBuilder in project dataverse by IQSS.

the class SavedSearchServiceBean method makeLinksForAllSavedSearches.

public JsonObjectBuilder makeLinksForAllSavedSearches(boolean debugFlag) throws SearchException, CommandException {
    JsonObjectBuilder response = Json.createObjectBuilder();
    List<SavedSearch> allSavedSearches = findAll();
    JsonArrayBuilder savedSearchArrayBuilder = Json.createArrayBuilder();
    for (SavedSearch savedSearch : allSavedSearches) {
        DataverseRequest dataverseRequest = new DataverseRequest(savedSearch.getCreator(), getHttpServletRequest());
        JsonObjectBuilder perSavedSearchResponse = makeLinksForSingleSavedSearch(dataverseRequest, savedSearch, debugFlag);
        savedSearchArrayBuilder.add(perSavedSearchResponse);
    }
    response.add("hits by saved search", savedSearchArrayBuilder);
    return response;
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 54 with JsonArrayBuilder

use of javax.json.JsonArrayBuilder 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 55 with JsonArrayBuilder

use of javax.json.JsonArrayBuilder 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

JsonArrayBuilder (javax.json.JsonArrayBuilder)177 JsonObjectBuilder (javax.json.JsonObjectBuilder)103 JsonObject (javax.json.JsonObject)36 Map (java.util.Map)29 Path (javax.ws.rs.Path)26 GET (javax.ws.rs.GET)24 HashMap (java.util.HashMap)19 JsonArray (javax.json.JsonArray)18 ArrayList (java.util.ArrayList)15 List (java.util.List)15 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)12 IOException (java.io.IOException)12 Dataverse (edu.harvard.iq.dataverse.Dataverse)10 Dataset (edu.harvard.iq.dataverse.Dataset)9 User (edu.harvard.iq.dataverse.authorization.users.User)9 JsonValue (javax.json.JsonValue)9 StringWriter (java.io.StringWriter)8 JsonString (javax.json.JsonString)7 Date (java.util.Date)6 JsonException (javax.json.JsonException)6