Search in sources :

Example 1 with LinkDataverseCommand

use of edu.harvard.iq.dataverse.engine.command.impl.LinkDataverseCommand 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)

Example 2 with LinkDataverseCommand

use of edu.harvard.iq.dataverse.engine.command.impl.LinkDataverseCommand in project dataverse by IQSS.

the class SavedSearchServiceBean method makeLinksForSingleSavedSearch.

/**
 * The "Saved Search" and highly related "Linked Dataverses and Linked
 * Datasets" features can be thought of as periodic execution of the
 * LinkDataverseCommand and LinkDatasetCommand. As of this writing that
 * periodic execution can be triggered via a cron job but we'd like to put
 * it on an EJB timer as part of
 * https://github.com/IQSS/dataverse/issues/2543 .
 *
 * The commands are executed by the creator of the SavedSearch. What happens
 * if the users loses the permission that the command requires? Should the
 * commands continue to be executed periodically as some "system" user?
 *
 * @return Debug information in the form of a JSON object, which is much
 * more structured that a simple String.
 */
public JsonObjectBuilder makeLinksForSingleSavedSearch(DataverseRequest dvReq, SavedSearch savedSearch, boolean debugFlag) throws SearchException, CommandException {
    JsonObjectBuilder response = Json.createObjectBuilder();
    JsonArrayBuilder savedSearchArrayBuilder = Json.createArrayBuilder();
    JsonArrayBuilder infoPerHit = Json.createArrayBuilder();
    SolrQueryResponse queryResponse = findHits(savedSearch);
    for (SolrSearchResult solrSearchResult : queryResponse.getSolrSearchResults()) {
        JsonObjectBuilder hitInfo = Json.createObjectBuilder();
        hitInfo.add("name", solrSearchResult.getNameSort());
        hitInfo.add("dvObjectId", solrSearchResult.getEntityId());
        DvObject dvObjectThatDefinitionPointWillLinkTo = dvObjectService.findDvObject(solrSearchResult.getEntityId());
        if (dvObjectThatDefinitionPointWillLinkTo == null) {
            hitInfo.add(resultString, "Could not find DvObject with id " + solrSearchResult.getEntityId());
            infoPerHit.add(hitInfo);
            break;
        }
        if (dvObjectThatDefinitionPointWillLinkTo.isInstanceofDataverse()) {
            Dataverse dataverseToLinkTo = (Dataverse) dvObjectThatDefinitionPointWillLinkTo;
            if (wouldResultInLinkingToItself(savedSearch.getDefinitionPoint(), dataverseToLinkTo)) {
                hitInfo.add(resultString, "Skipping because dataverse id " + dataverseToLinkTo.getId() + " would link to itself.");
            } else if (alreadyLinkedToTheDataverse(savedSearch.getDefinitionPoint(), dataverseToLinkTo)) {
                hitInfo.add(resultString, "Skipping because dataverse " + savedSearch.getDefinitionPoint().getId() + " already links to dataverse " + dataverseToLinkTo.getId() + ".");
            } else if (dataverseToLinkToIsAlreadyPartOfTheSubtree(savedSearch.getDefinitionPoint(), dataverseToLinkTo)) {
                hitInfo.add(resultString, "Skipping because " + dataverseToLinkTo + " is already part of the subtree for " + savedSearch.getDefinitionPoint());
            } else {
                DataverseLinkingDataverse link = commandEngine.submitInNewTransaction(new LinkDataverseCommand(dvReq, savedSearch.getDefinitionPoint(), dataverseToLinkTo));
                hitInfo.add(resultString, "Persisted DataverseLinkingDataverse id " + link.getId() + " link of " + dataverseToLinkTo + " to " + savedSearch.getDefinitionPoint());
            }
        } else if (dvObjectThatDefinitionPointWillLinkTo.isInstanceofDataset()) {
            Dataset datasetToLinkTo = (Dataset) dvObjectThatDefinitionPointWillLinkTo;
            if (alreadyLinkedToTheDataset(savedSearch.getDefinitionPoint(), datasetToLinkTo)) {
                hitInfo.add(resultString, "Skipping because dataverse " + savedSearch.getDefinitionPoint() + " already links to dataset " + datasetToLinkTo + ".");
            } else if (datasetToLinkToIsAlreadyPartOfTheSubtree(savedSearch.getDefinitionPoint(), datasetToLinkTo)) {
                // already there from normal search/browse
                hitInfo.add(resultString, "Skipping because dataset " + datasetToLinkTo.getId() + " is already part of the subtree for " + savedSearch.getDefinitionPoint().getAlias());
            } else if (datasetAncestorAlreadyLinked(savedSearch.getDefinitionPoint(), datasetToLinkTo)) {
                hitInfo.add(resultString, "FIXME: implement this?");
            } else {
                DatasetLinkingDataverse link = commandEngine.submitInNewTransaction(new LinkDatasetCommand(dvReq, savedSearch.getDefinitionPoint(), datasetToLinkTo));
                hitInfo.add(resultString, "Persisted DatasetLinkingDataverse id " + link.getId() + " link of " + link.getDataset() + " to " + link.getLinkingDataverse());
            }
        } else if (dvObjectThatDefinitionPointWillLinkTo.isInstanceofDataFile()) {
            hitInfo.add(resultString, "Skipping because the search matched a file. The matched file id was " + dvObjectThatDefinitionPointWillLinkTo.getId() + ".");
        } else {
            hitInfo.add(resultString, "Unexpected DvObject type.");
        }
        infoPerHit.add(hitInfo);
    }
    JsonObjectBuilder info = getInfo(savedSearch, infoPerHit);
    if (debugFlag) {
        info.add("debug", getDebugInfo(savedSearch));
    }
    savedSearchArrayBuilder.add(info);
    response.add("hits for saved search id " + savedSearch.getId(), savedSearchArrayBuilder);
    return response;
}
Also used : SolrSearchResult(edu.harvard.iq.dataverse.search.SolrSearchResult) SolrQueryResponse(edu.harvard.iq.dataverse.search.SolrQueryResponse) DataverseLinkingDataverse(edu.harvard.iq.dataverse.DataverseLinkingDataverse) DvObject(edu.harvard.iq.dataverse.DvObject) LinkDatasetCommand(edu.harvard.iq.dataverse.engine.command.impl.LinkDatasetCommand) Dataset(edu.harvard.iq.dataverse.Dataset) DatasetLinkingDataverse(edu.harvard.iq.dataverse.DatasetLinkingDataverse) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) DataverseLinkingDataverse(edu.harvard.iq.dataverse.DataverseLinkingDataverse) DatasetLinkingDataverse(edu.harvard.iq.dataverse.DatasetLinkingDataverse) Dataverse(edu.harvard.iq.dataverse.Dataverse) LinkDataverseCommand(edu.harvard.iq.dataverse.engine.command.impl.LinkDataverseCommand)

Aggregations

LinkDataverseCommand (edu.harvard.iq.dataverse.engine.command.impl.LinkDataverseCommand)2 Dataset (edu.harvard.iq.dataverse.Dataset)1 DatasetLinkingDataverse (edu.harvard.iq.dataverse.DatasetLinkingDataverse)1 Dataverse (edu.harvard.iq.dataverse.Dataverse)1 DataverseLinkingDataverse (edu.harvard.iq.dataverse.DataverseLinkingDataverse)1 DvObject (edu.harvard.iq.dataverse.DvObject)1 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)1 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)1 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)1 LinkDatasetCommand (edu.harvard.iq.dataverse.engine.command.impl.LinkDatasetCommand)1 SearchException (edu.harvard.iq.dataverse.search.SearchException)1 SolrQueryResponse (edu.harvard.iq.dataverse.search.SolrQueryResponse)1 SolrSearchResult (edu.harvard.iq.dataverse.search.SolrSearchResult)1 SavedSearch (edu.harvard.iq.dataverse.search.savedsearch.SavedSearch)1 JsonArrayBuilder (javax.json.JsonArrayBuilder)1 JsonObjectBuilder (javax.json.JsonObjectBuilder)1