Search in sources :

Example 36 with JsonArrayBuilder

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

the class Index method searchDebug.

/**
 * This method is for integration tests of search.
 */
@GET
@Path("test")
public Response searchDebug(@QueryParam("key") String apiToken, @QueryParam("q") String query, @QueryParam("fq") final List<String> filterQueries) {
    User user = findUserByApiToken(apiToken);
    if (user == null) {
        return error(Response.Status.UNAUTHORIZED, "Invalid apikey '" + apiToken + "'");
    }
    Dataverse subtreeScope = dataverseService.findRootDataverse();
    String sortField = SearchFields.ID;
    String sortOrder = SortBy.ASCENDING;
    int paginationStart = 0;
    boolean dataRelatedToMe = false;
    int numResultsPerPage = Integer.MAX_VALUE;
    SolrQueryResponse solrQueryResponse;
    try {
        solrQueryResponse = searchService.search(createDataverseRequest(user), subtreeScope, query, filterQueries, sortField, sortOrder, paginationStart, dataRelatedToMe, numResultsPerPage);
    } catch (SearchException ex) {
        return error(Response.Status.INTERNAL_SERVER_ERROR, ex.getLocalizedMessage() + ": " + ex.getCause().getLocalizedMessage());
    }
    JsonArrayBuilder itemsArrayBuilder = Json.createArrayBuilder();
    List<SolrSearchResult> solrSearchResults = solrQueryResponse.getSolrSearchResults();
    for (SolrSearchResult solrSearchResult : solrSearchResults) {
        itemsArrayBuilder.add(solrSearchResult.getType() + ":" + solrSearchResult.getNameSort());
    }
    return ok(itemsArrayBuilder);
}
Also used : SolrSearchResult(edu.harvard.iq.dataverse.search.SolrSearchResult) SolrQueryResponse(edu.harvard.iq.dataverse.search.SolrQueryResponse) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) User(edu.harvard.iq.dataverse.authorization.users.User) GuestUser(edu.harvard.iq.dataverse.authorization.users.GuestUser) SearchException(edu.harvard.iq.dataverse.search.SearchException) JsonArrayBuilder(javax.json.JsonArrayBuilder) Dataverse(edu.harvard.iq.dataverse.Dataverse) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 37 with JsonArrayBuilder

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

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

the class Index method filesearch.

@GET
@Path("filesearch")
public Response filesearch(@QueryParam("persistentId") String persistentId, @QueryParam("semanticVersion") String semanticVersion, @QueryParam("q") String userSuppliedQuery) {
    Dataset dataset = datasetService.findByGlobalId(persistentId);
    if (dataset == null) {
        return error(Status.BAD_REQUEST, "Could not find dataset with persistent id " + persistentId);
    }
    User user = GuestUser.get();
    try {
        AuthenticatedUser authenticatedUser = findAuthenticatedUserOrDie();
        if (authenticatedUser != null) {
            user = authenticatedUser;
        }
    } catch (WrappedResponse ex) {
    }
    RetrieveDatasetVersionResponse datasetVersionResponse = datasetVersionService.retrieveDatasetVersionByPersistentId(persistentId, semanticVersion);
    if (datasetVersionResponse == null) {
        return error(Status.BAD_REQUEST, "Problem searching for files. Could not find dataset version based on " + persistentId + " and " + semanticVersion);
    }
    DatasetVersion datasetVersion = datasetVersionResponse.getDatasetVersion();
    FileView fileView = searchFilesService.getFileView(datasetVersion, user, userSuppliedQuery);
    if (fileView == null) {
        return error(Status.BAD_REQUEST, "Problem searching for files. Null returned from getFileView.");
    }
    JsonArrayBuilder filesFound = Json.createArrayBuilder();
    JsonArrayBuilder cards = Json.createArrayBuilder();
    JsonArrayBuilder fileIds = Json.createArrayBuilder();
    for (SolrSearchResult result : fileView.getSolrSearchResults()) {
        cards.add(result.getNameSort());
        fileIds.add(result.getEntityId());
        JsonObjectBuilder fileFound = Json.createObjectBuilder();
        fileFound.add("name", result.getNameSort());
        fileFound.add("entityId", result.getEntityId().toString());
        fileFound.add("datasetVersionId", result.getDatasetVersionId());
        fileFound.add("datasetId", result.getParent().get(SearchFields.ID));
        filesFound.add(fileFound);
    }
    JsonArrayBuilder facets = Json.createArrayBuilder();
    for (FacetCategory facetCategory : fileView.getFacetCategoryList()) {
        facets.add(facetCategory.getFriendlyName());
    }
    JsonArrayBuilder filterQueries = Json.createArrayBuilder();
    for (String filterQuery : fileView.getFilterQueries()) {
        filterQueries.add(filterQuery);
    }
    JsonArrayBuilder allDatasetVersionIds = Json.createArrayBuilder();
    for (DatasetVersion dsVersion : dataset.getVersions()) {
        allDatasetVersionIds.add(dsVersion.getId());
    }
    JsonObjectBuilder data = Json.createObjectBuilder();
    data.add("filesFound", filesFound);
    data.add("cards", cards);
    data.add("fileIds", fileIds);
    data.add("facets", facets);
    data.add("user", user.getIdentifier());
    data.add("persistentID", persistentId);
    data.add("query", fileView.getQuery());
    data.add("filterQueries", filterQueries);
    data.add("allDataverVersionIds", allDatasetVersionIds);
    data.add("semanticVersion", datasetVersion.getSemanticVersion());
    return ok(data);
}
Also used : SolrSearchResult(edu.harvard.iq.dataverse.search.SolrSearchResult) RetrieveDatasetVersionResponse(edu.harvard.iq.dataverse.DatasetVersionServiceBean.RetrieveDatasetVersionResponse) FileView(edu.harvard.iq.dataverse.search.FileView) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) User(edu.harvard.iq.dataverse.authorization.users.User) GuestUser(edu.harvard.iq.dataverse.authorization.users.GuestUser) Dataset(edu.harvard.iq.dataverse.Dataset) FacetCategory(edu.harvard.iq.dataverse.search.FacetCategory) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 39 with JsonArrayBuilder

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

the class Index method getPermissionsInSolrButNotDatabase.

private JsonObjectBuilder getPermissionsInSolrButNotDatabase() {
    List<Long> staleOrMissingPermissions = solrIndexService.findPermissionsInSolrNoLongerInDatabase();
    JsonArrayBuilder stalePermissionList = Json.createArrayBuilder();
    for (Long dvObjectId : staleOrMissingPermissions) {
        stalePermissionList.add(dvObjectId);
    }
    return Json.createObjectBuilder().add("dvobjects", stalePermissionList.build().size());
}
Also used : JsonArrayBuilder(javax.json.JsonArrayBuilder)

Example 40 with JsonArrayBuilder

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

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