Search in sources :

Example 1 with SavedSearchFilterQuery

use of edu.harvard.iq.dataverse.search.savedsearch.SavedSearchFilterQuery in project dataverse by IQSS.

the class SavedSearches method add.

@POST
public Response add(JsonObject body) {
    if (body == null) {
        return error(BAD_REQUEST, "JSON is expected.");
    }
    String keyForAuthenticatedUserId = "creatorId";
    long creatorIdToLookUp;
    try {
        creatorIdToLookUp = body.getInt(keyForAuthenticatedUserId);
    } catch (NullPointerException ex) {
        return error(BAD_REQUEST, "Required field missing: " + keyForAuthenticatedUserId);
    } catch (ClassCastException ex) {
        return error(BAD_REQUEST, "A number is required for " + keyForAuthenticatedUserId);
    } catch (Exception ex) {
        return error(BAD_REQUEST, "Problem with " + keyForAuthenticatedUserId + ": " + ex);
    }
    AuthenticatedUser creator = authSvc.findByID(creatorIdToLookUp);
    if (creator == null) {
        return error(Response.Status.NOT_FOUND, "Could not find user based on " + keyForAuthenticatedUserId + ": " + creatorIdToLookUp);
    }
    String keyForQuery = "query";
    String query;
    try {
        query = body.getString(keyForQuery);
    } catch (NullPointerException ex) {
        return error(BAD_REQUEST, "Required field missing: " + keyForQuery);
    }
    String keyForDefinitionPointId = "definitionPointId";
    long dataverseIdToLookup;
    try {
        dataverseIdToLookup = body.getInt(keyForDefinitionPointId);
    } catch (NullPointerException ex) {
        return error(BAD_REQUEST, "Required field missing: " + keyForDefinitionPointId);
    } catch (ClassCastException ex) {
        return error(BAD_REQUEST, "A number is required for " + keyForDefinitionPointId);
    } catch (Exception ex) {
        return error(BAD_REQUEST, "Problem with " + keyForDefinitionPointId + ": " + ex);
    }
    Dataverse definitionPoint = dataverseSvc.find(dataverseIdToLookup);
    if (definitionPoint == null) {
        return error(NOT_FOUND, "Could not find a dataverse based on id " + dataverseIdToLookup);
    }
    SavedSearch toPersist = new SavedSearch(query, definitionPoint, creator);
    String keyForFilterQueries = "filterQueries";
    List<SavedSearchFilterQuery> savedSearchFilterQuerys = new ArrayList<>();
    try {
        JsonArray filterQueries = body.getJsonArray(keyForFilterQueries);
        for (int i = 0; i < filterQueries.size(); i++) {
            String fq = filterQueries.getString(i);
            SavedSearchFilterQuery filterQuery = new SavedSearchFilterQuery(fq, toPersist);
            savedSearchFilterQuerys.add(filterQuery);
        }
    } catch (NullPointerException ex) {
    // filter queries are not required, keep going
    } catch (Exception ex) {
        return error(BAD_REQUEST, "Problem getting filter queries: " + ex);
    }
    if (!savedSearchFilterQuerys.isEmpty()) {
        toPersist.setSavedSearchFilterQueries(savedSearchFilterQuerys);
    }
    try {
        SavedSearch persistedSavedSearch = savedSearchSvc.add(toPersist);
        return ok("Added: " + persistedSavedSearch);
    } catch (EJBException ex) {
        StringBuilder errors = new StringBuilder();
        Throwable throwable = ex.getCause();
        while (throwable != null) {
            errors.append(throwable).append(" ");
            throwable = throwable.getCause();
        }
        return error(BAD_REQUEST, "Problem adding saved search: " + errors);
    }
}
Also used : SavedSearch(edu.harvard.iq.dataverse.search.savedsearch.SavedSearch) ArrayList(java.util.ArrayList) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Dataverse(edu.harvard.iq.dataverse.Dataverse) SearchException(edu.harvard.iq.dataverse.search.SearchException) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) EJBException(javax.ejb.EJBException) JsonArray(javax.json.JsonArray) SavedSearchFilterQuery(edu.harvard.iq.dataverse.search.savedsearch.SavedSearchFilterQuery) EJBException(javax.ejb.EJBException) POST(javax.ws.rs.POST)

Example 2 with SavedSearchFilterQuery

use of edu.harvard.iq.dataverse.search.savedsearch.SavedSearchFilterQuery in project dataverse by IQSS.

the class DataversePage method saveSavedSearch.

public String saveSavedSearch() {
    if (linkingDataverseId == null) {
        JsfHelper.addErrorMessage("You must select a linking dataverse.");
        return "";
    }
    linkingDataverse = dataverseService.find(linkingDataverseId);
    AuthenticatedUser savedSearchCreator = getAuthenticatedUser();
    if (savedSearchCreator == null) {
        String msg = "Only authenticated users can save a search.";
        logger.severe(msg);
        JsfHelper.addErrorMessage(msg);
        return returnRedirect();
    }
    SavedSearch savedSearch = new SavedSearch(searchIncludeFragment.getQuery(), linkingDataverse, savedSearchCreator);
    savedSearch.setSavedSearchFilterQueries(new ArrayList<>());
    for (String filterQuery : searchIncludeFragment.getFilterQueriesDebug()) {
        /**
         * @todo Why are there null's here anyway? Turn on debug and figure
         * this out.
         */
        if (filterQuery != null && !filterQuery.isEmpty()) {
            SavedSearchFilterQuery ssfq = new SavedSearchFilterQuery(filterQuery, savedSearch);
            savedSearch.getSavedSearchFilterQueries().add(ssfq);
        }
    }
    CreateSavedSearchCommand cmd = new CreateSavedSearchCommand(dvRequestService.getDataverseRequest(), linkingDataverse, savedSearch);
    try {
        commandEngine.submit(cmd);
        List<String> arguments = new ArrayList<>();
        String linkString = "<a href=\"/dataverse/" + linkingDataverse.getAlias() + "\">" + StringEscapeUtils.escapeHtml(linkingDataverse.getDisplayName()) + "</a>";
        arguments.add(linkString);
        String successMessageString = BundleUtil.getStringFromBundle("dataverse.saved.search.success", arguments);
        JsfHelper.addSuccessMessage(successMessageString);
        return returnRedirect();
    } catch (CommandException ex) {
        String msg = "There was a problem linking this search to yours: " + ex;
        logger.severe(msg);
        JsfHelper.addErrorMessage(BundleUtil.getStringFromBundle("dataverse.saved.search.failure") + " " + ex);
        return returnRedirect();
    }
}
Also used : CreateSavedSearchCommand(edu.harvard.iq.dataverse.engine.command.impl.CreateSavedSearchCommand) SavedSearch(edu.harvard.iq.dataverse.search.savedsearch.SavedSearch) SavedSearchFilterQuery(edu.harvard.iq.dataverse.search.savedsearch.SavedSearchFilterQuery) ArrayList(java.util.ArrayList) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)

Example 3 with SavedSearchFilterQuery

use of edu.harvard.iq.dataverse.search.savedsearch.SavedSearchFilterQuery in project dataverse by IQSS.

the class DataversePage method createSavedSearchForChildren.

private SavedSearch createSavedSearchForChildren(AuthenticatedUser savedSearchCreator) {
    String wildcardQuery = "*";
    SavedSearch savedSearchToPersist = new SavedSearch(wildcardQuery, linkingDataverse, savedSearchCreator);
    String dataversePath = dataverseService.determineDataversePath(dataverse);
    String filterDownToSubtree = SearchFields.SUBTREE + ":\"" + dataversePath + "\"";
    SavedSearchFilterQuery filterDownToSubtreeFilterQuery = new SavedSearchFilterQuery(filterDownToSubtree, savedSearchToPersist);
    savedSearchToPersist.setSavedSearchFilterQueries(Arrays.asList(filterDownToSubtreeFilterQuery));
    SavedSearch savedSearchCreated = savedSearchService.add(savedSearchToPersist);
    return savedSearchCreated;
}
Also used : SavedSearch(edu.harvard.iq.dataverse.search.savedsearch.SavedSearch) SavedSearchFilterQuery(edu.harvard.iq.dataverse.search.savedsearch.SavedSearchFilterQuery)

Example 4 with SavedSearchFilterQuery

use of edu.harvard.iq.dataverse.search.savedsearch.SavedSearchFilterQuery in project dataverse by IQSS.

the class SavedSearches method toJson.

private JsonObjectBuilder toJson(SavedSearch savedSearch) {
    JsonObjectBuilder savedSearchJson = Json.createObjectBuilder();
    long savedSearchId = savedSearch.getId();
    Dataverse definitionPoint = savedSearch.getDefinitionPoint();
    JsonArrayBuilder fqBuilder = Json.createArrayBuilder();
    for (SavedSearchFilterQuery fq : savedSearch.getSavedSearchFilterQueries()) {
        fqBuilder.add(fq.getFilterQuery());
    }
    savedSearchJson.add("query", savedSearch.getQuery());
    savedSearchJson.add("filterQueries", fqBuilder);
    savedSearchJson.add("id", savedSearchId);
    savedSearchJson.add("definitionPointId", definitionPoint.getId());
    savedSearchJson.add("definitionPointAlias", definitionPoint.getAlias());
    savedSearchJson.add("creatorId", savedSearch.getCreator().getId());
    return savedSearchJson;
}
Also used : SavedSearchFilterQuery(edu.harvard.iq.dataverse.search.savedsearch.SavedSearchFilterQuery) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) Dataverse(edu.harvard.iq.dataverse.Dataverse)

Aggregations

SavedSearchFilterQuery (edu.harvard.iq.dataverse.search.savedsearch.SavedSearchFilterQuery)4 SavedSearch (edu.harvard.iq.dataverse.search.savedsearch.SavedSearch)3 Dataverse (edu.harvard.iq.dataverse.Dataverse)2 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)2 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)2 ArrayList (java.util.ArrayList)2 CreateSavedSearchCommand (edu.harvard.iq.dataverse.engine.command.impl.CreateSavedSearchCommand)1 SearchException (edu.harvard.iq.dataverse.search.SearchException)1 EJBException (javax.ejb.EJBException)1 JsonArray (javax.json.JsonArray)1 JsonArrayBuilder (javax.json.JsonArrayBuilder)1 JsonObjectBuilder (javax.json.JsonObjectBuilder)1 POST (javax.ws.rs.POST)1