Search in sources :

Example 1 with FileView

use of edu.harvard.iq.dataverse.search.FileView 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)

Aggregations

Dataset (edu.harvard.iq.dataverse.Dataset)1 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)1 RetrieveDatasetVersionResponse (edu.harvard.iq.dataverse.DatasetVersionServiceBean.RetrieveDatasetVersionResponse)1 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)1 GuestUser (edu.harvard.iq.dataverse.authorization.users.GuestUser)1 User (edu.harvard.iq.dataverse.authorization.users.User)1 FacetCategory (edu.harvard.iq.dataverse.search.FacetCategory)1 FileView (edu.harvard.iq.dataverse.search.FileView)1 SolrSearchResult (edu.harvard.iq.dataverse.search.SolrSearchResult)1 JsonArrayBuilder (javax.json.JsonArrayBuilder)1 JsonObjectBuilder (javax.json.JsonObjectBuilder)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1