Search in sources :

Example 1 with SavedSearch

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

the class SavedSearches method delete.

@DELETE
@Path("{id}")
public Response delete(@PathParam("id") long doomedId) {
    boolean disabled = true;
    if (disabled) {
        return error(BAD_REQUEST, "Saved Searches can not safely be deleted because links can not safely be deleted. See https://github.com/IQSS/dataverse/issues/1364 for details.");
    }
    SavedSearch doomed = savedSearchSvc.find(doomedId);
    if (doomed == null) {
        return error(NOT_FOUND, "Could not find saved search id " + doomedId);
    }
    boolean wasDeleted = savedSearchSvc.delete(doomedId);
    if (wasDeleted) {
        return ok(Json.createObjectBuilder().add("Deleted", doomedId));
    } else {
        return error(INTERNAL_SERVER_ERROR, "Problem deleting id " + doomedId);
    }
}
Also used : SavedSearch(edu.harvard.iq.dataverse.search.savedsearch.SavedSearch) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 2 with SavedSearch

use of edu.harvard.iq.dataverse.search.savedsearch.SavedSearch 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 3 with SavedSearch

use of edu.harvard.iq.dataverse.search.savedsearch.SavedSearch 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 4 with SavedSearch

use of edu.harvard.iq.dataverse.search.savedsearch.SavedSearch 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 5 with SavedSearch

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

the class DataversePage method saveLinkedDataverse.

public String saveLinkedDataverse() {
    if (linkingDataverseId == null) {
        JsfHelper.addSuccessMessage("You must select a linking dataverse.");
        return "";
    }
    AuthenticatedUser savedSearchCreator = getAuthenticatedUser();
    if (savedSearchCreator == null) {
        String msg = "Only authenticated users can link a dataverse.";
        logger.severe(msg);
        JsfHelper.addErrorMessage(msg);
        return returnRedirect();
    }
    linkingDataverse = dataverseService.find(linkingDataverseId);
    LinkDataverseCommand cmd = new LinkDataverseCommand(dvRequestService.getDataverseRequest(), linkingDataverse, dataverse);
    // LinkDvObjectCommand cmd = new LinkDvObjectCommand (session.getUser(), linkingDataverse, dataverse);
    try {
        commandEngine.submit(cmd);
    } catch (CommandException ex) {
        String msg = "Unable to link " + dataverse.getDisplayName() + " to " + linkingDataverse.getDisplayName() + ". An internal error occurred.";
        logger.log(Level.SEVERE, "{0} {1}", new Object[] { msg, ex });
        JsfHelper.addErrorMessage(msg);
        return returnRedirect();
    }
    SavedSearch savedSearchOfChildren = createSavedSearchForChildren(savedSearchCreator);
    boolean createLinksAndIndexRightNow = false;
    if (createLinksAndIndexRightNow) {
        try {
            // create links (does indexing) right now (might be expensive)
            boolean debug = false;
            DataverseRequest dataverseRequest = new DataverseRequest(savedSearchCreator, SavedSearchServiceBean.getHttpServletRequest());
            savedSearchService.makeLinksForSingleSavedSearch(dataverseRequest, savedSearchOfChildren, debug);
            JsfHelper.addSuccessMessage(BundleUtil.getStringFromBundle("dataverse.linked.success", getSuccessMessageArguments()));
            return returnRedirect();
        } catch (SearchException | CommandException ex) {
            // error: solr is down, etc. can't link children right now
            JsfHelper.addErrorMessage(BundleUtil.getStringFromBundle("dataverse.linked.internalerror", getSuccessMessageArguments()));
            String msg = dataverse.getDisplayName() + " has been successfully linked to " + linkingDataverse.getDisplayName() + " but contents will not appear until an internal error has been fixed.";
            logger.log(Level.SEVERE, "{0} {1}", new Object[] { msg, ex });
            // JsfHelper.addErrorMessage(msg);
            return returnRedirect();
        }
    } else {
        // defer: please wait for the next timer/cron job
        // JsfHelper.addSuccessMessage(dataverse.getDisplayName() + " has been successfully linked to " + linkingDataverse.getDisplayName() + ". Please wait for its contents to appear.");
        JsfHelper.addSuccessMessage(BundleUtil.getStringFromBundle("dataverse.linked.success.wait", getSuccessMessageArguments()));
        return returnRedirect();
    }
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) SavedSearch(edu.harvard.iq.dataverse.search.savedsearch.SavedSearch) SearchException(edu.harvard.iq.dataverse.search.SearchException) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) LinkDataverseCommand(edu.harvard.iq.dataverse.engine.command.impl.LinkDataverseCommand)

Aggregations

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