Search in sources :

Example 1 with FileMetadata

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

the class MocksFactory method makeDatasetVersion.

public static DatasetVersion makeDatasetVersion(List<DataFileCategory> categories) {
    final DatasetVersion retVal = new DatasetVersion();
    final List<DataFile> files = makeFiles(10);
    final List<FileMetadata> metadatas = new ArrayList<>(10);
    Random rand = new Random();
    files.forEach(df -> {
        df.getFileMetadata().addCategory(categories.get(rand.nextInt(categories.size())));
        metadatas.add(df.getFileMetadata());
    });
    retVal.setFileMetadatas(metadatas);
    List<DatasetField> fields = new ArrayList<>();
    DatasetField field = new DatasetField();
    field.setId(nextId());
    field.setSingleValue("Sample Field Value");
    field.setDatasetFieldType(makeDatasetFieldType());
    fields.add(field);
    retVal.setDatasetFields(fields);
    return retVal;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) Random(java.util.Random) DatasetField(edu.harvard.iq.dataverse.DatasetField) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) ArrayList(java.util.ArrayList) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion)

Example 2 with FileMetadata

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

the class MocksFactory method addFileMetadata.

public static FileMetadata addFileMetadata(DataFile df) {
    FileMetadata fmd = new FileMetadata();
    fmd.setId(nextId());
    fmd.setLabel("Metadata for DataFile " + df.getId());
    fmd.setDataFile(df);
    if (df.getFileMetadatas() != null) {
        df.getFileMetadatas().add(fmd);
    } else {
        df.setFileMetadatas(new LinkedList(Arrays.asList(fmd)));
    }
    return fmd;
}
Also used : FileMetadata(edu.harvard.iq.dataverse.FileMetadata) LinkedList(java.util.LinkedList)

Example 3 with FileMetadata

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

the class MocksFactory method makeDataset.

public static Dataset makeDataset() {
    Dataset ds = new Dataset();
    ds.setId(nextId());
    ds.setIdentifier("sample-ds-" + ds.getId());
    ds.setCategoriesByName(Arrays.asList("CatOne", "CatTwo", "CatThree"));
    final List<DataFile> files = makeFiles(10);
    final List<FileMetadata> metadatas = new ArrayList<>(10);
    final List<DataFileCategory> categories = ds.getCategories();
    Random rand = new Random();
    files.forEach(df -> {
        df.getFileMetadata().addCategory(categories.get(rand.nextInt(categories.size())));
        metadatas.add(df.getFileMetadata());
    });
    ds.setFiles(files);
    final DatasetVersion initialVersion = ds.getVersions().get(0);
    initialVersion.setFileMetadatas(metadatas);
    List<DatasetField> fields = new ArrayList<>();
    DatasetField field = new DatasetField();
    field.setId(nextId());
    field.setSingleValue("Sample Field Value");
    field.setDatasetFieldType(makeDatasetFieldType());
    fields.add(field);
    initialVersion.setDatasetFields(fields);
    ds.setOwner(makeDataverse());
    return ds;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) DataFileCategory(edu.harvard.iq.dataverse.DataFileCategory) Random(java.util.Random) DatasetField(edu.harvard.iq.dataverse.DatasetField) Dataset(edu.harvard.iq.dataverse.Dataset) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) ArrayList(java.util.ArrayList) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion)

Example 4 with FileMetadata

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

the class DatasetUtilTest method testGetThumbnailCandidates.

/**
 * Test of getThumbnailCandidates method, of class DatasetUtil.
 */
@Test
public void testGetThumbnailCandidates() {
    assertEquals(new ArrayList<>(), DatasetUtil.getThumbnailCandidates(null, false));
    Dataset dataset = MocksFactory.makeDataset();
    DataFile dataFile = MocksFactory.makeDataFile();
    dataFile.setContentType("image/");
    dataFile.setOwner(dataset);
    dataFile.setStorageIdentifier("file://src/test/resources/images/coffeeshop.png");
    System.out.println(ImageThumbConverter.isThumbnailAvailable(dataFile));
    DatasetVersion version = dataset.getCreateVersion();
    List<FileMetadata> fmds = new ArrayList<>();
    fmds.add(MocksFactory.addFileMetadata(dataFile));
    version.setFileMetadatas(fmds);
    assertEquals(new ArrayList<>(), DatasetUtil.getThumbnailCandidates(dataset, false));
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) Dataset(edu.harvard.iq.dataverse.Dataset) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) ArrayList(java.util.ArrayList) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) Test(org.junit.Test)

Example 5 with FileMetadata

use of edu.harvard.iq.dataverse.FileMetadata 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

FileMetadata (edu.harvard.iq.dataverse.FileMetadata)54 DataFile (edu.harvard.iq.dataverse.DataFile)30 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)26 ArrayList (java.util.ArrayList)23 Dataset (edu.harvard.iq.dataverse.Dataset)18 Test (org.junit.Test)13 Date (java.util.Date)12 IOException (java.io.IOException)10 Timestamp (java.sql.Timestamp)10 DataTable (edu.harvard.iq.dataverse.DataTable)5 DatasetField (edu.harvard.iq.dataverse.DatasetField)5 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)5 MocksFactory.makeDataset (edu.harvard.iq.dataverse.mocks.MocksFactory.makeDataset)5 SimpleDateFormat (java.text.SimpleDateFormat)5 HashMap (java.util.HashMap)5 Dataverse (edu.harvard.iq.dataverse.Dataverse)4 File (java.io.File)4 FileNotFoundException (java.io.FileNotFoundException)4 JsonObjectBuilder (javax.json.JsonObjectBuilder)4 DataFileTag (edu.harvard.iq.dataverse.DataFileTag)3