Search in sources :

Example 86 with DataFile

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

the class DatasetUtil method getThumbnail.

/**
 * Note "datasetVersionId" can be null. If needed, it helps the "efficiency"
 * of "attemptToAutomaticallySelectThumbnailFromDataFiles"
 *
 * @param dataset
 * @param datasetVersion
 * @return
 */
public static DatasetThumbnail getThumbnail(Dataset dataset, DatasetVersion datasetVersion) {
    if (dataset == null) {
        return null;
    }
    StorageIO<Dataset> dataAccess = null;
    try {
        dataAccess = DataAccess.getStorageIO(dataset);
    } catch (IOException ioex) {
        logger.warning("Failed to initialize dataset for thumbnail " + dataset.getStorageIdentifier() + " (" + ioex.getMessage() + ")");
    }
    InputStream in = null;
    try {
        if (dataAccess == null) {
            logger.info("Cannot retrieve thumbnail file.");
        } else if (dataAccess.getAuxFileAsInputStream(datasetLogoThumbnail + thumb48addedByImageThumbConverter) != null) {
            in = dataAccess.getAuxFileAsInputStream(datasetLogoThumbnail + thumb48addedByImageThumbConverter);
        }
    } catch (IOException ex) {
        logger.info("Cannot retrieve dataset thumbnail file, will try to get thumbnail from file.");
    }
    if (in != null) {
        try {
            byte[] bytes = IOUtils.toByteArray(in);
            String base64image = Base64.getEncoder().encodeToString(bytes);
            DatasetThumbnail datasetThumbnail = new DatasetThumbnail(FileUtil.DATA_URI_SCHEME + base64image, null);
            logger.fine("will get thumbnail from dataset logo");
            return datasetThumbnail;
        } catch (IOException ex) {
            logger.fine("Unable to read thumbnail image from file: " + ex);
            return null;
        }
    } else {
        DataFile thumbnailFile = dataset.getThumbnailFile();
        if (thumbnailFile == null) {
            if (dataset.isUseGenericThumbnail()) {
                logger.fine("Dataset (id :" + dataset.getId() + ") does not have a thumbnail and is 'Use Generic'.");
                return null;
            } else {
                thumbnailFile = attemptToAutomaticallySelectThumbnailFromDataFiles(dataset, datasetVersion);
                if (thumbnailFile == null) {
                    logger.fine("Dataset (id :" + dataset.getId() + ") does not have a thumbnail available that could be selected automatically.");
                    return null;
                } else {
                    String imageSourceBase64 = ImageThumbConverter.getImageThumbnailAsBase64(thumbnailFile, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE);
                    DatasetThumbnail defaultDatasetThumbnail = new DatasetThumbnail(imageSourceBase64, thumbnailFile);
                    logger.fine("thumbnailFile (id :" + thumbnailFile.getId() + ") will get thumbnail through automatic selection from DataFile id " + thumbnailFile.getId());
                    return defaultDatasetThumbnail;
                }
            }
        } else if (thumbnailFile.isRestricted()) {
            logger.fine("Dataset (id :" + dataset.getId() + ") has a thumbnail the user selected but the file must have later been restricted. Returning null.");
            return null;
        } else {
            String imageSourceBase64 = ImageThumbConverter.getImageThumbnailAsBase64(thumbnailFile, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE);
            DatasetThumbnail userSpecifiedDatasetThumbnail = new DatasetThumbnail(imageSourceBase64, thumbnailFile);
            logger.fine("Dataset (id :" + dataset.getId() + ")  will get thumbnail the user specified from DataFile id " + thumbnailFile.getId());
            return userSpecifiedDatasetThumbnail;
        }
    }
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) Dataset(edu.harvard.iq.dataverse.Dataset) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 87 with DataFile

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

the class DataverseUserPage method displayNotification.

public void displayNotification() {
    for (UserNotification userNotification : notificationsList) {
        switch(userNotification.getType()) {
            case ASSIGNROLE:
            case REVOKEROLE:
                // Can either be a dataverse or dataset, so search both
                Dataverse dataverse = dataverseService.find(userNotification.getObjectId());
                if (dataverse != null) {
                    userNotification.setRoleString(this.getRoleStringFromUser(this.getCurrentUser(), dataverse));
                    userNotification.setTheObject(dataverse);
                } else {
                    Dataset dataset = datasetService.find(userNotification.getObjectId());
                    if (dataset != null) {
                        userNotification.setRoleString(this.getRoleStringFromUser(this.getCurrentUser(), dataset));
                        userNotification.setTheObject(dataset);
                    } else {
                        DataFile datafile = fileService.find(userNotification.getObjectId());
                        userNotification.setRoleString(this.getRoleStringFromUser(this.getCurrentUser(), datafile));
                        userNotification.setTheObject(datafile);
                    }
                }
                break;
            case CREATEDV:
                userNotification.setTheObject(dataverseService.find(userNotification.getObjectId()));
                break;
            case REQUESTFILEACCESS:
                DataFile file = fileService.find(userNotification.getObjectId());
                userNotification.setTheObject(file.getOwner());
                break;
            case GRANTFILEACCESS:
            case REJECTFILEACCESS:
                userNotification.setTheObject(datasetService.find(userNotification.getObjectId()));
                break;
            case MAPLAYERUPDATED:
            case CREATEDS:
            case SUBMITTEDDS:
            case PUBLISHEDDS:
            case RETURNEDDS:
                userNotification.setTheObject(datasetVersionService.find(userNotification.getObjectId()));
                break;
            case MAPLAYERDELETEFAILED:
                userNotification.setTheObject(fileService.findFileMetadata(userNotification.getObjectId()));
                break;
            case CREATEACC:
                userNotification.setTheObject(userNotification.getUser());
                break;
            case CHECKSUMFAIL:
                userNotification.setTheObject(datasetService.find(userNotification.getObjectId()));
                break;
            case FILESYSTEMIMPORT:
                userNotification.setTheObject(datasetVersionService.find(userNotification.getObjectId()));
                break;
            case CHECKSUMIMPORT:
                userNotification.setTheObject(datasetVersionService.find(userNotification.getObjectId()));
                break;
        }
        userNotification.setDisplayAsRead(userNotification.isReadNotification());
        if (userNotification.isReadNotification() == false) {
            userNotification.setReadNotification(true);
            userNotificationService.save(userNotification);
        }
    }
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) Dataset(edu.harvard.iq.dataverse.Dataset) UserNotification(edu.harvard.iq.dataverse.UserNotification) Dataverse(edu.harvard.iq.dataverse.Dataverse)

Example 88 with DataFile

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

the class FileRecordProcessor method processItem.

// TODO: This method may be meaningles when used in the context of a "package file"
// batch import. See if it can be modified/improved? -- L.A. 4.6.1
@Override
public Object processItem(Object object) throws Exception {
    DatasetVersion version = dataset.getLatestVersion();
    String path = object.toString();
    String gid = dataset.getAuthority() + dataset.getDoiSeparator() + dataset.getIdentifier();
    String relativePath = path.substring(path.indexOf(gid) + gid.length() + 1);
    // skip if it already exists
    DataFile datafile = dataFileServiceBean.findByStorageIdandDatasetVersion(relativePath, version);
    if (datafile == null) {
        return new File(path);
    } else {
        Logger.getLogger("job-" + jobContext.getInstanceId()).log(Level.INFO, "Skipping " + relativePath + ", DataFile already exists.");
        return null;
    }
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) DataFile(edu.harvard.iq.dataverse.DataFile) File(java.io.File)

Example 89 with DataFile

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

the class FileRecordWriter method writeItems.

@Override
public void writeItems(List list) {
    if (!list.isEmpty()) {
        if (FILE_MODE_INDIVIDUAL_FILES.equals(fileMode)) {
            List<DataFile> datafiles = dataset.getFiles();
            for (Object file : list) {
                DataFile df = createDataFile((File) file);
                if (df != null) {
                    // log success if the dataset isn't huge
                    if (fileCount < 20000) {
                        getJobLogger().log(Level.INFO, "Creating DataFile for: " + ((File) file).getAbsolutePath());
                    }
                    datafiles.add(df);
                } else {
                    getJobLogger().log(Level.SEVERE, "Unable to create DataFile for: " + ((File) file).getAbsolutePath());
                }
            }
            dataset.getLatestVersion().getDataset().setFiles(datafiles);
        } else if (FILE_MODE_PACKAGE_FILE.equals(fileMode)) {
            DataFile packageFile = createPackageDataFile(list);
            if (packageFile == null) {
                getJobLogger().log(Level.SEVERE, "File package import failed.");
                jobContext.setExitStatus("FAILED");
                return;
            }
            updateDatasetVersion(dataset.getLatestVersion());
        } else {
            getJobLogger().log(Level.SEVERE, "File mode " + fileMode + " is not supported.");
            jobContext.setExitStatus("FAILED");
        }
    } else {
        getJobLogger().log(Level.SEVERE, "No items in the writeItems list.");
    }
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) DataFile(edu.harvard.iq.dataverse.DataFile) File(java.io.File)

Example 90 with DataFile

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

the class FileRecordWriter method createDataFile.

/**
 * Create a DatasetFile and corresponding FileMetadata for a file on the filesystem and add it to the
 * latest dataset version (if the user has AddDataset permissions for the dataset).
 * @param file file to create dataFile from
 * @return datafile
 */
private DataFile createDataFile(File file) {
    DatasetVersion version = dataset.getLatestVersion();
    String path = file.getAbsolutePath();
    String gid = dataset.getAuthority() + dataset.getDoiSeparator() + dataset.getIdentifier();
    String relativePath = path.substring(path.indexOf(gid) + gid.length() + 1);
    // we don't determine mime type
    DataFile datafile = new DataFile("application/octet-stream");
    datafile.setStorageIdentifier(relativePath);
    datafile.setFilesize(file.length());
    datafile.setModificationTime(new Timestamp(new Date().getTime()));
    datafile.setCreateDate(new Timestamp(new Date().getTime()));
    datafile.setPermissionModificationTime(new Timestamp(new Date().getTime()));
    datafile.setOwner(dataset);
    datafile.setIngestDone();
    // check system property first, otherwise use the batch job property
    String jobChecksumType;
    if (System.getProperty("checksumType") != null) {
        jobChecksumType = System.getProperty("checksumType");
    } else {
        jobChecksumType = checksumType;
    }
    // initial default
    datafile.setChecksumType(DataFile.ChecksumType.SHA1);
    for (DataFile.ChecksumType type : DataFile.ChecksumType.values()) {
        if (jobChecksumType.equalsIgnoreCase(type.name())) {
            datafile.setChecksumType(type);
            break;
        }
    }
    // lookup the checksum value in the job's manifest hashmap
    if (jobContext.getTransientUserData() != null) {
        String checksumVal = ((Map<String, String>) jobContext.getTransientUserData()).get(relativePath);
        if (checksumVal != null) {
            datafile.setChecksumValue(checksumVal);
            // remove the key, so we can check for unused checksums when the job is complete
            ((Map<String, String>) jobContext.getTransientUserData()).remove(relativePath);
        } else {
            datafile.setChecksumValue("Unknown");
            getJobLogger().log(Level.WARNING, "Unable to find checksum in manifest for: " + file.getAbsolutePath());
        }
    } else {
        getJobLogger().log(Level.SEVERE, "No checksum hashmap found in transientUserData");
        jobContext.setExitStatus("FAILED");
        return null;
    }
    // set metadata and add to latest version
    FileMetadata fmd = new FileMetadata();
    fmd.setLabel(file.getName());
    // set the subdirectory if there is one
    if (relativePath.contains(File.separator)) {
        fmd.setDirectoryLabel(relativePath.replace(File.separator + file.getName(), ""));
    }
    fmd.setDataFile(datafile);
    datafile.getFileMetadatas().add(fmd);
    if (version.getFileMetadatas() == null)
        version.setFileMetadatas(new ArrayList<>());
    version.getFileMetadatas().add(fmd);
    fmd.setDatasetVersion(version);
    datafile = dataFileServiceBean.save(datafile);
    return datafile;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) ChecksumType(edu.harvard.iq.dataverse.DataFile.ChecksumType) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) ArrayList(java.util.ArrayList) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) Timestamp(java.sql.Timestamp) HashMap(java.util.HashMap) Map(java.util.Map) Date(java.util.Date)

Aggregations

DataFile (edu.harvard.iq.dataverse.DataFile)111 Dataset (edu.harvard.iq.dataverse.Dataset)39 IOException (java.io.IOException)39 FileMetadata (edu.harvard.iq.dataverse.FileMetadata)30 ArrayList (java.util.ArrayList)25 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)20 File (java.io.File)20 FileNotFoundException (java.io.FileNotFoundException)18 Path (javax.ws.rs.Path)18 Dataverse (edu.harvard.iq.dataverse.Dataverse)17 FileInputStream (java.io.FileInputStream)16 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)14 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)13 Date (java.util.Date)13 GET (javax.ws.rs.GET)13 Test (org.junit.Test)13 Timestamp (java.sql.Timestamp)11 InputStream (java.io.InputStream)10 DataVariable (edu.harvard.iq.dataverse.datavariable.DataVariable)8 FileOutputStream (java.io.FileOutputStream)8