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);
}
}
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();
}
}
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;
}
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;
}
Aggregations