Search in sources :

Example 16 with Dataverse

use of edu.harvard.iq.dataverse.Dataverse in project dataverse by IQSS.

the class Datasets method receiveChecksumValidationResults.

@POST
@Path("{identifier}/dataCaptureModule/checksumValidation")
public Response receiveChecksumValidationResults(@PathParam("identifier") String id, JsonObject jsonFromDcm) {
    logger.log(Level.FINE, "jsonFromDcm: {0}", jsonFromDcm);
    AuthenticatedUser authenticatedUser = null;
    try {
        authenticatedUser = findAuthenticatedUserOrDie();
    } catch (WrappedResponse ex) {
        return error(Response.Status.BAD_REQUEST, "Authentication is required.");
    }
    if (!authenticatedUser.isSuperuser()) {
        return error(Response.Status.FORBIDDEN, "Superusers only.");
    }
    String statusMessageFromDcm = jsonFromDcm.getString("status");
    try {
        Dataset dataset = findDatasetOrDie(id);
        if ("validation passed".equals(statusMessageFromDcm)) {
            String uploadFolder = jsonFromDcm.getString("uploadFolder");
            int totalSize = jsonFromDcm.getInt("totalSize");
            ImportMode importMode = ImportMode.MERGE;
            try {
                JsonObject jsonFromImportJobKickoff = execCommand(new ImportFromFileSystemCommand(createDataverseRequest(findUserOrDie()), dataset, uploadFolder, new Long(totalSize), importMode));
                long jobId = jsonFromImportJobKickoff.getInt("executionId");
                String message = jsonFromImportJobKickoff.getString("message");
                JsonObjectBuilder job = Json.createObjectBuilder();
                job.add("jobId", jobId);
                job.add("message", message);
                return ok(job);
            } catch (WrappedResponse wr) {
                String message = wr.getMessage();
                return error(Response.Status.INTERNAL_SERVER_ERROR, "Uploaded files have passed checksum validation but something went wrong while attempting to put the files into Dataverse. Message was '" + message + "'.");
            }
        } else if ("validation failed".equals(statusMessageFromDcm)) {
            Map<String, AuthenticatedUser> distinctAuthors = permissionService.getDistinctUsersWithPermissionOn(Permission.EditDataset, dataset);
            distinctAuthors.values().forEach((value) -> {
                userNotificationService.sendNotification((AuthenticatedUser) value, new Timestamp(new Date().getTime()), UserNotification.Type.CHECKSUMFAIL, dataset.getId());
            });
            List<AuthenticatedUser> superUsers = authenticationServiceBean.findSuperUsers();
            if (superUsers != null && !superUsers.isEmpty()) {
                superUsers.forEach((au) -> {
                    userNotificationService.sendNotification(au, new Timestamp(new Date().getTime()), UserNotification.Type.CHECKSUMFAIL, dataset.getId());
                });
            }
            return ok("User notified about checksum validation failure.");
        } else {
            return error(Response.Status.BAD_REQUEST, "Unexpected status cannot be processed: " + statusMessageFromDcm);
        }
    } catch (WrappedResponse ex) {
        return ex.getResponse();
    }
}
Also used : AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Produces(javax.ws.rs.Produces) FormDataContentDisposition(org.glassfish.jersey.media.multipart.FormDataContentDisposition) UpdateDatasetVersionCommand(edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetVersionCommand) DataFileTagException(edu.harvard.iq.dataverse.datasetutility.DataFileTagException) User(edu.harvard.iq.dataverse.authorization.users.User) MediaType(javax.ws.rs.core.MediaType) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) MoveDatasetCommand(edu.harvard.iq.dataverse.engine.command.impl.MoveDatasetCommand) Map(java.util.Map) GetPrivateUrlCommand(edu.harvard.iq.dataverse.engine.command.impl.GetPrivateUrlCommand) DatasetField(edu.harvard.iq.dataverse.DatasetField) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) JsonObject(javax.json.JsonObject) MetadataBlockServiceBean(edu.harvard.iq.dataverse.MetadataBlockServiceBean) DataverseServiceBean(edu.harvard.iq.dataverse.DataverseServiceBean) WorkflowContext(edu.harvard.iq.dataverse.workflow.WorkflowContext) AbstractApiBean.error(edu.harvard.iq.dataverse.api.AbstractApiBean.error) DataFile(edu.harvard.iq.dataverse.DataFile) DatasetServiceBean(edu.harvard.iq.dataverse.DatasetServiceBean) EJBException(javax.ejb.EJBException) ImportMode(edu.harvard.iq.dataverse.batch.jobs.importer.ImportMode) FormDataParam(org.glassfish.jersey.media.multipart.FormDataParam) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) GetSpecificPublishedDatasetVersionCommand(edu.harvard.iq.dataverse.engine.command.impl.GetSpecificPublishedDatasetVersionCommand) CreatePrivateUrlCommand(edu.harvard.iq.dataverse.engine.command.impl.CreatePrivateUrlCommand) DataCaptureModuleUtil(edu.harvard.iq.dataverse.datacapturemodule.DataCaptureModuleUtil) JsonArrayBuilder(javax.json.JsonArrayBuilder) GET(javax.ws.rs.GET) PermissionServiceBean(edu.harvard.iq.dataverse.PermissionServiceBean) EjbDataverseEngine(edu.harvard.iq.dataverse.EjbDataverseEngine) DataverseSession(edu.harvard.iq.dataverse.DataverseSession) ResourceBundle(java.util.ResourceBundle) DatasetUtil(edu.harvard.iq.dataverse.dataset.DatasetUtil) Workflow(edu.harvard.iq.dataverse.workflow.Workflow) WorkflowServiceBean(edu.harvard.iq.dataverse.workflow.WorkflowServiceBean) Json(javax.json.Json) PublishDatasetCommand(edu.harvard.iq.dataverse.engine.command.impl.PublishDatasetCommand) EJB(javax.ejb.EJB) ExportService(edu.harvard.iq.dataverse.export.ExportService) UserNotificationServiceBean(edu.harvard.iq.dataverse.UserNotificationServiceBean) AuthenticationServiceBean(edu.harvard.iq.dataverse.authorization.AuthenticationServiceBean) DeletePrivateUrlCommand(edu.harvard.iq.dataverse.engine.command.impl.DeletePrivateUrlCommand) UserNotification(edu.harvard.iq.dataverse.UserNotification) DeleteDatasetVersionCommand(edu.harvard.iq.dataverse.engine.command.impl.DeleteDatasetVersionCommand) StringReader(java.io.StringReader) UpdateDatasetTargetURLCommand(edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetTargetURLCommand) ListVersionsCommand(edu.harvard.iq.dataverse.engine.command.impl.ListVersionsCommand) Date(java.util.Date) DataFileServiceBean(edu.harvard.iq.dataverse.DataFileServiceBean) DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) Path(javax.ws.rs.Path) RoleAssignee(edu.harvard.iq.dataverse.authorization.RoleAssignee) CreateDatasetVersionCommand(edu.harvard.iq.dataverse.engine.command.impl.CreateDatasetVersionCommand) DataCaptureModuleException(edu.harvard.iq.dataverse.datacapturemodule.DataCaptureModuleException) SettingsServiceBean(edu.harvard.iq.dataverse.settings.SettingsServiceBean) DDIExportServiceBean(edu.harvard.iq.dataverse.export.DDIExportServiceBean) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) Permission(edu.harvard.iq.dataverse.authorization.Permission) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) ScriptRequestResponse(edu.harvard.iq.dataverse.datacapturemodule.ScriptRequestResponse) GetDatasetCommand(edu.harvard.iq.dataverse.engine.command.impl.GetDatasetCommand) DestroyDatasetCommand(edu.harvard.iq.dataverse.engine.command.impl.DestroyDatasetCommand) DELETE(javax.ws.rs.DELETE) AddReplaceFileHelper(edu.harvard.iq.dataverse.datasetutility.AddReplaceFileHelper) DeleteDatasetCommand(edu.harvard.iq.dataverse.engine.command.impl.DeleteDatasetCommand) Timestamp(java.sql.Timestamp) ListRoleAssignments(edu.harvard.iq.dataverse.engine.command.impl.ListRoleAssignments) Logger(java.util.logging.Logger) MetadataBlock(edu.harvard.iq.dataverse.MetadataBlock) DatasetThumbnail(edu.harvard.iq.dataverse.dataset.DatasetThumbnail) List(java.util.List) IngestServiceBean(edu.harvard.iq.dataverse.ingest.IngestServiceBean) Response(javax.ws.rs.core.Response) JsonPrinter(edu.harvard.iq.dataverse.util.json.JsonPrinter) ImportFromFileSystemCommand(edu.harvard.iq.dataverse.engine.command.impl.ImportFromFileSystemCommand) ReturnDatasetToAuthorCommand(edu.harvard.iq.dataverse.engine.command.impl.ReturnDatasetToAuthorCommand) SubmitDatasetForReviewCommand(edu.harvard.iq.dataverse.engine.command.impl.SubmitDatasetForReviewCommand) JsonObjectBuilder(javax.json.JsonObjectBuilder) OptionalFileParams(edu.harvard.iq.dataverse.datasetutility.OptionalFileParams) GetDraftDatasetVersionCommand(edu.harvard.iq.dataverse.engine.command.impl.GetDraftDatasetVersionCommand) DatasetLock(edu.harvard.iq.dataverse.DatasetLock) PathParam(javax.ws.rs.PathParam) SystemConfig(edu.harvard.iq.dataverse.util.SystemConfig) HashMap(java.util.HashMap) GetLatestPublishedDatasetVersionCommand(edu.harvard.iq.dataverse.engine.command.impl.GetLatestPublishedDatasetVersionCommand) EjbUtil(edu.harvard.iq.dataverse.util.EjbUtil) Level(java.util.logging.Level) Inject(javax.inject.Inject) DataverseRole(edu.harvard.iq.dataverse.authorization.DataverseRole) NoFilesException(edu.harvard.iq.dataverse.datasetutility.NoFilesException) Dataverse(edu.harvard.iq.dataverse.Dataverse) Command(edu.harvard.iq.dataverse.engine.command.Command) UpdateDatasetThumbnailCommand(edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetThumbnailCommand) POST(javax.ws.rs.POST) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) Dataset(edu.harvard.iq.dataverse.Dataset) GetLatestAccessibleDatasetVersionCommand(edu.harvard.iq.dataverse.engine.command.impl.GetLatestAccessibleDatasetVersionCommand) AssignRoleCommand(edu.harvard.iq.dataverse.engine.command.impl.AssignRoleCommand) DatasetFieldServiceBean(edu.harvard.iq.dataverse.DatasetFieldServiceBean) PrivateUrl(edu.harvard.iq.dataverse.privateurl.PrivateUrl) RequestRsyncScriptCommand(edu.harvard.iq.dataverse.engine.command.impl.RequestRsyncScriptCommand) PublishDatasetResult(edu.harvard.iq.dataverse.engine.command.impl.PublishDatasetResult) SetDatasetCitationDateCommand(edu.harvard.iq.dataverse.engine.command.impl.SetDatasetCitationDateCommand) PUT(javax.ws.rs.PUT) InputStream(java.io.InputStream) ImportFromFileSystemCommand(edu.harvard.iq.dataverse.engine.command.impl.ImportFromFileSystemCommand) Dataset(edu.harvard.iq.dataverse.Dataset) JsonObject(javax.json.JsonObject) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Timestamp(java.sql.Timestamp) Date(java.util.Date) ImportMode(edu.harvard.iq.dataverse.batch.jobs.importer.ImportMode) List(java.util.List) JsonObjectBuilder(javax.json.JsonObjectBuilder) Map(java.util.Map) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 17 with Dataverse

use of edu.harvard.iq.dataverse.Dataverse in project dataverse by IQSS.

the class Datasets method getLinks.

@GET
@Path("{id}/links")
public Response getLinks(@PathParam("id") String idSupplied) {
    try {
        User u = findUserOrDie();
        if (!u.isSuperuser()) {
            return error(Response.Status.FORBIDDEN, "Not a superuser");
        }
        Dataset dataset = findDatasetOrDie(idSupplied);
        long datasetId = dataset.getId();
        List<Dataverse> dvsThatLinkToThisDatasetId = dataverseSvc.findDataversesThatLinkToThisDatasetId(datasetId);
        JsonArrayBuilder dataversesThatLinkToThisDatasetIdBuilder = Json.createArrayBuilder();
        for (Dataverse dataverse : dvsThatLinkToThisDatasetId) {
            dataversesThatLinkToThisDatasetIdBuilder.add(dataverse.getAlias() + " (id " + dataverse.getId() + ")");
        }
        JsonObjectBuilder response = Json.createObjectBuilder();
        response.add("dataverses that link to dataset id " + datasetId, dataversesThatLinkToThisDatasetIdBuilder);
        return ok(response);
    } catch (WrappedResponse wr) {
        return wr.getResponse();
    }
}
Also used : AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) User(edu.harvard.iq.dataverse.authorization.users.User) Dataset(edu.harvard.iq.dataverse.Dataset) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) Dataverse(edu.harvard.iq.dataverse.Dataverse) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 18 with Dataverse

use of edu.harvard.iq.dataverse.Dataverse 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 19 with Dataverse

use of edu.harvard.iq.dataverse.Dataverse in project dataverse by IQSS.

the class Search method search.

@GET
public Response search(@QueryParam("q") String query, @QueryParam("type") final List<String> types, @QueryParam("subtree") String subtreeRequested, @QueryParam("sort") String sortField, @QueryParam("order") String sortOrder, @QueryParam("per_page") final int numResultsPerPageRequested, @QueryParam("start") final int paginationStart, @QueryParam("show_relevance") boolean showRelevance, @QueryParam("show_facets") boolean showFacets, @QueryParam("fq") final List<String> filterQueries, @QueryParam("show_entity_ids") boolean showEntityIds, @QueryParam("show_api_urls") boolean showApiUrls, @QueryParam("show_my_data") boolean showMyData, @Context HttpServletResponse response) {
    User user;
    try {
        user = getUser();
    } catch (WrappedResponse ex) {
        return ex.getResponse();
    }
    if (query != null) {
        // sanity checking on user-supplied arguments
        SortBy sortBy;
        int numResultsPerPage;
        Dataverse subtree;
        try {
            if (!types.isEmpty()) {
                filterQueries.add(getFilterQueryFromTypes(types));
            }
            sortBy = SearchUtil.getSortBy(sortField, sortOrder);
            numResultsPerPage = getNumberOfResultsPerPage(numResultsPerPageRequested);
            subtree = getSubtree(subtreeRequested);
            if (!subtree.equals(dataverseService.findRootDataverse())) {
                String dataversePath = dataverseService.determineDataversePath(subtree);
                String filterDownToSubtree = SearchFields.SUBTREE + ":\"" + dataversePath + "\"";
                /**
                 * @todo Should filterDownToSubtree logic be centralized in
                 * SearchServiceBean?
                 */
                filterQueries.add(filterDownToSubtree);
            }
        } catch (Exception ex) {
            return error(Response.Status.BAD_REQUEST, ex.getLocalizedMessage());
        }
        // users can't change these (yet anyway)
        // getDataRelatedToMe();
        boolean dataRelatedToMe = showMyData;
        SolrQueryResponse solrQueryResponse;
        try {
            solrQueryResponse = searchService.search(createDataverseRequest(user), subtree, query, filterQueries, sortBy.getField(), sortBy.getOrder(), paginationStart, dataRelatedToMe, numResultsPerPage);
        } catch (SearchException ex) {
            Throwable cause = ex;
            StringBuilder sb = new StringBuilder();
            sb.append(cause + " ");
            while (cause.getCause() != null) {
                cause = cause.getCause();
                sb.append(cause.getClass().getCanonicalName() + " ");
                sb.append(cause + " ");
            // if you search for a colon you see RemoteSolrException: org.apache.solr.search.SyntaxError: Cannot parse ':'
            }
            String message = "Exception running search for [" + query + "] with filterQueries " + filterQueries + " and paginationStart [" + paginationStart + "]: " + sb.toString();
            logger.info(message);
            return error(Response.Status.INTERNAL_SERVER_ERROR, message);
        }
        JsonArrayBuilder itemsArrayBuilder = Json.createArrayBuilder();
        List<SolrSearchResult> solrSearchResults = solrQueryResponse.getSolrSearchResults();
        for (SolrSearchResult solrSearchResult : solrSearchResults) {
            itemsArrayBuilder.add(solrSearchResult.toJsonObject(showRelevance, showEntityIds, showApiUrls));
        }
        JsonObjectBuilder spelling_alternatives = Json.createObjectBuilder();
        for (Map.Entry<String, List<String>> entry : solrQueryResponse.getSpellingSuggestionsByToken().entrySet()) {
            spelling_alternatives.add(entry.getKey(), entry.getValue().toString());
        }
        JsonArrayBuilder facets = Json.createArrayBuilder();
        JsonObjectBuilder facetCategoryBuilder = Json.createObjectBuilder();
        for (FacetCategory facetCategory : solrQueryResponse.getFacetCategoryList()) {
            JsonObjectBuilder facetCategoryBuilderFriendlyPlusData = Json.createObjectBuilder();
            JsonArrayBuilder facetLabelBuilderData = Json.createArrayBuilder();
            for (FacetLabel facetLabel : facetCategory.getFacetLabel()) {
                JsonObjectBuilder countBuilder = Json.createObjectBuilder();
                countBuilder.add(facetLabel.getName(), facetLabel.getCount());
                facetLabelBuilderData.add(countBuilder);
            }
            facetCategoryBuilderFriendlyPlusData.add("friendly", facetCategory.getFriendlyName());
            facetCategoryBuilderFriendlyPlusData.add("labels", facetLabelBuilderData);
            facetCategoryBuilder.add(facetCategory.getName(), facetCategoryBuilderFriendlyPlusData);
        }
        facets.add(facetCategoryBuilder);
        JsonObjectBuilder value = Json.createObjectBuilder().add("q", query).add("total_count", solrQueryResponse.getNumResultsFound()).add("start", solrQueryResponse.getResultsStart()).add("spelling_alternatives", spelling_alternatives).add("items", itemsArrayBuilder.build());
        if (showFacets) {
            value.add("facets", facets);
        }
        value.add("count_in_response", solrSearchResults.size());
        // value.add("fq_provided", filterQueries.toString());
        if (solrQueryResponse.getError() != null) {
            /**
             * @todo You get here if you pass only ":" as a query, for
             * example. Should we return more or better information?
             */
            return error(Response.Status.BAD_REQUEST, solrQueryResponse.getError());
        }
        response.setHeader("Access-Control-Allow-Origin", "*");
        return allowCors(ok(value));
    } else {
        return allowCors(error(Response.Status.BAD_REQUEST, "q parameter is missing"));
    }
}
Also used : SolrSearchResult(edu.harvard.iq.dataverse.search.SolrSearchResult) SolrQueryResponse(edu.harvard.iq.dataverse.search.SolrQueryResponse) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) User(edu.harvard.iq.dataverse.authorization.users.User) GuestUser(edu.harvard.iq.dataverse.authorization.users.GuestUser) SortBy(edu.harvard.iq.dataverse.search.SortBy) FacetLabel(edu.harvard.iq.dataverse.search.FacetLabel) SearchException(edu.harvard.iq.dataverse.search.SearchException) Dataverse(edu.harvard.iq.dataverse.Dataverse) SearchException(edu.harvard.iq.dataverse.search.SearchException) FacetCategory(edu.harvard.iq.dataverse.search.FacetCategory) ArrayList(java.util.ArrayList) List(java.util.List) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) Map(java.util.Map) GET(javax.ws.rs.GET)

Example 20 with Dataverse

use of edu.harvard.iq.dataverse.Dataverse in project dataverse by IQSS.

the class WorldMapRelatedData method getWorldMapDatafileInfo.

/**
 * Retrieve FileMetadata for Use by WorldMap.
 *  This includes information about the DataFile, Dataset, DatasetVersion, and Dataverse
 *
 * @param jsonTokenData
 * @param request
 * @return
 */
@POST
// + "{worldmap_token}")
@Path(GET_WORLDMAP_DATAFILE_API_PATH_FRAGMENT)
public Response getWorldMapDatafileInfo(String jsonTokenData, @Context HttpServletRequest request) {
    // , @PathParam("worldmap_token") String worldmapTokenParam) {
    if (true) {
    // return okResponse("Currently deactivated");
    // return okResponse("remote server: " + request.getRemoteAddr());
    }
    logger.info("API call: getWorldMapDatafileInfo");
    // ----------------------------------
    // Auth check: Parse the json message and check for a valid GEOCONNECT_TOKEN_KEY and GEOCONNECT_TOKEN_VALUE
    // -- For testing, the GEOCONNECT_TOKEN_VALUE will be dynamic, found in the db
    // ----------------------------------
    logger.info("(1) jsonTokenData: " + jsonTokenData);
    // Parse JSON
    JsonObject jsonTokenInfo;
    try (StringReader rdr = new StringReader(jsonTokenData)) {
        jsonTokenInfo = Json.createReader(rdr).readObject();
    } catch (JsonParsingException jpe) {
        logger.log(Level.SEVERE, "Json: " + jsonTokenData);
        return error(Response.Status.BAD_REQUEST, "Error parsing Json: " + jpe.getMessage());
    }
    logger.info("(1a) jsonTokenInfo: " + jsonTokenInfo);
    // Retrieve token string
    String worldmapTokenParam = this.retrieveTokenValueFromJson(jsonTokenInfo);
    logger.info("(1b) token from JSON: " + worldmapTokenParam);
    if (worldmapTokenParam == null) {
        return error(Response.Status.BAD_REQUEST, "Token not found in JSON request.");
    }
    // Retrieve WorldMapToken and make sure it is valid
    // 
    WorldMapToken wmToken = tokenServiceBean.retrieveAndRefreshValidToken(worldmapTokenParam);
    logger.info("(2) token retrieved from db: " + wmToken);
    if (wmToken == null) {
        return error(Response.Status.UNAUTHORIZED, "No access. Invalid token.");
    }
    // Make sure the token's User still has permissions to access the file
    // 
    logger.info("(3) check permissions");
    if (!(tokenServiceBean.canTokenUserEditFile(wmToken))) {
        tokenServiceBean.expireToken(wmToken);
        return error(Response.Status.UNAUTHORIZED, "No access. Invalid token.");
    }
    // (1) Retrieve token connected data: DataverseUser, DataFile
    // 
    // Make sure token user and file are still available
    // 
    AuthenticatedUser dvUser = wmToken.getDataverseUser();
    if (dvUser == null) {
        return error(Response.Status.NOT_FOUND, "DataverseUser not found for token");
    }
    DataFile dfile = wmToken.getDatafile();
    if (dfile == null) {
        return error(Response.Status.NOT_FOUND, "DataFile not found for token");
    }
    // (1a) Retrieve FileMetadata
    FileMetadata dfile_meta = dfile.getFileMetadata();
    if (dfile_meta == null) {
        return error(Response.Status.NOT_FOUND, "FileMetadata not found");
    }
    // (2) Now get the dataset and the latest DatasetVersion
    Dataset dset = dfile.getOwner();
    if (dset == null) {
        return error(Response.Status.NOT_FOUND, "Owning Dataset for this DataFile not found");
    }
    // (2a) latest DatasetVersion
    // !! How do you check if the lastest version has this specific file?
    // 
    DatasetVersion dset_version = dset.getLatestVersion();
    if (dset_version == null) {
        return error(Response.Status.NOT_FOUND, "Latest DatasetVersion for this DataFile not found");
    }
    // (3) get Dataverse
    Dataverse dverse = dset.getOwner();
    if (dverse == null) {
        return error(Response.Status.NOT_FOUND, "Dataverse for this DataFile's Dataset not found");
    }
    // (4) Roll it all up in a JSON response
    final JsonObjectBuilder jsonData = Json.createObjectBuilder();
    // ------------------------------------
    if (dfile.isShapefileType()) {
        jsonData.add("mapping_type", "shapefile");
    } else if (dfile.isTabularData()) {
        jsonData.add("mapping_type", "tabular");
    } else {
        logger.log(Level.SEVERE, "This was neither a Shapefile nor a Tabular data file.  DataFile id: " + dfile.getId());
        return error(Response.Status.BAD_REQUEST, "Sorry! This file does not have mapping data. Please contact the Dataverse administrator. DataFile id: " + dfile.getId());
    }
    // ------------------------------------
    // DataverseUser Info
    // ------------------------------------
    jsonData.add("dv_user_id", dvUser.getId());
    jsonData.add("dv_username", dvUser.getUserIdentifier());
    jsonData.add("dv_user_email", dvUser.getEmail());
    // ------------------------------------
    // Dataverse URLs to this server
    // ------------------------------------
    String serverName = systemConfig.getDataverseSiteUrl();
    jsonData.add("return_to_dataverse_url", dset_version.getReturnToFilePageURL(serverName, dset, dfile));
    jsonData.add("datafile_download_url", dfile.getMapItFileDownloadURL(serverName));
    // ------------------------------------
    // Dataverse
    // ------------------------------------
    // jsonData.add("dataverse_installation_name", "Harvard Dataverse"); // todo / fix
    // is this enough to distinguish a dataverse installation?
    jsonData.add("dataverse_installation_name", systemConfig.getDataverseSiteUrl());
    jsonData.add("dataverse_id", dverse.getId());
    jsonData.add("dataverse_name", dverse.getName());
    String dataverseDesc = dverse.getDescription();
    if (dataverseDesc == null || dataverseDesc.equalsIgnoreCase("")) {
        dataverseDesc = "";
    }
    jsonData.add("dataverse_description", dataverseDesc);
    // ------------------------------------
    // Dataset Info
    // ------------------------------------
    jsonData.add("dataset_id", dset.getId());
    // ------------------------------------
    // DatasetVersion Info
    // ------------------------------------
    // database id
    jsonData.add("dataset_version_id", dset_version.getId());
    // major/minor version number, e.g. 3.1
    jsonData.add("dataset_semantic_version", dset_version.getSemanticVersion());
    jsonData.add("dataset_name", dset_version.getTitle());
    jsonData.add("dataset_citation", dset_version.getCitation(true));
    // Need to fix to/do
    jsonData.add("dataset_description", "");
    jsonData.add("dataset_is_public", dset_version.isReleased());
    // ------------------------------------
    // DataFile/FileMetaData Info
    // ------------------------------------
    jsonData.add("datafile_id", dfile.getId());
    jsonData.add("datafile_label", dfile_meta.getLabel());
    // jsonData.add("filename", dfile_meta.getLabel());
    jsonData.add("datafile_expected_md5_checksum", dfile.getChecksumValue());
    Long fsize = dfile.getFilesize();
    if (fsize == null) {
        fsize = new Long(-1);
    }
    jsonData.add("datafile_filesize", fsize);
    jsonData.add("datafile_content_type", dfile.getContentType());
    jsonData.add("datafile_create_datetime", dfile.getCreateDate().toString());
    // restriction status of the DataFile
    jsonData.add("datafile_is_restricted", dfile.isRestricted());
    return ok(jsonData);
}
Also used : Dataset(edu.harvard.iq.dataverse.Dataset) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) JsonObject(javax.json.JsonObject) WorldMapToken(edu.harvard.iq.dataverse.worldmapauth.WorldMapToken) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Dataverse(edu.harvard.iq.dataverse.Dataverse) DataFile(edu.harvard.iq.dataverse.DataFile) StringReader(java.io.StringReader) JsonObjectBuilder(javax.json.JsonObjectBuilder) JsonParsingException(javax.json.stream.JsonParsingException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

Dataverse (edu.harvard.iq.dataverse.Dataverse)94 Dataset (edu.harvard.iq.dataverse.Dataset)34 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)31 Test (org.junit.Test)27 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)22 DataFile (edu.harvard.iq.dataverse.DataFile)18 IOException (java.io.IOException)18 Path (javax.ws.rs.Path)16 JsonObject (javax.json.JsonObject)15 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)11 ArrayList (java.util.ArrayList)11 EJBException (javax.ejb.EJBException)11 JsonObjectBuilder (javax.json.JsonObjectBuilder)11 InputStream (java.io.InputStream)10 Date (java.util.Date)10 JsonArrayBuilder (javax.json.JsonArrayBuilder)10 POST (javax.ws.rs.POST)10 DataverseRole (edu.harvard.iq.dataverse.authorization.DataverseRole)9 User (edu.harvard.iq.dataverse.authorization.users.User)9 SwordError (org.swordapp.server.SwordError)9