Search in sources :

Example 16 with FileMetadata

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

the class FileRecordJobListener method beforeJob.

@Override
public void beforeJob() throws Exception {
    Logger jobLogger;
    // initialize logger
    // (the beforeJob() method gets executed before anything else; so we
    // initialize the logger here. everywhere else will be retrieving
    // it with Logger.getLogger(byname) - that should be giving us the
    // same instance, created here - and not creating a new logger)
    jobLogger = LoggingUtil.getJobLogger(Long.toString(jobContext.getInstanceId()));
    // update job properties to be used elsewhere to determine dataset, user and mode
    JobOperator jobOperator = BatchRuntime.getJobOperator();
    jobParams = jobOperator.getParameters(jobContext.getInstanceId());
    // log job info
    jobLogger.log(Level.INFO, "Job ID = " + jobContext.getExecutionId());
    jobLogger.log(Level.INFO, "Job Name = " + jobContext.getJobName());
    jobLogger.log(Level.INFO, "Job Status = " + jobContext.getBatchStatus());
    jobParams.setProperty("datasetGlobalId", getDatasetGlobalId());
    jobParams.setProperty("userId", getUserId());
    jobParams.setProperty("mode", getMode());
    uploadFolder = jobParams.getProperty("uploadFolder");
    // check constraints for running the job
    if (canRunJob()) {
        // if mode = REPLACE, remove all filemetadata from the dataset version and start fresh
        if (mode.equalsIgnoreCase(ImportMode.REPLACE.name())) {
            try {
                DatasetVersion workingVersion = dataset.getEditVersion();
                List<FileMetadata> fileMetadataList = workingVersion.getFileMetadatas();
                jobLogger.log(Level.INFO, "Removing any existing file metadata since mode = REPLACE");
                for (FileMetadata fmd : fileMetadataList) {
                    dataFileServiceBean.deleteFromVersion(workingVersion, fmd.getDataFile());
                }
            } catch (Exception e) {
                jobLogger.log(Level.SEVERE, "Removing existing file metadata in REPLACE mode: " + e.getMessage());
            }
        }
        // load the checksum manifest
        loadChecksumManifest();
    } else {
        jobContext.setExitStatus("FAILED");
    }
}
Also used : FileMetadata(edu.harvard.iq.dataverse.FileMetadata) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) JobOperator(javax.batch.operations.JobOperator) Logger(java.util.logging.Logger) JobSecurityException(javax.batch.operations.JobSecurityException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) NoSuchJobExecutionException(javax.batch.operations.NoSuchJobExecutionException)

Example 17 with FileMetadata

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

the class AddReplaceFileHelper method setNewlyAddedFiles.

/**
 * We want the version of the newly added file that has an id set
 *
 * TODO: This is inefficient/expensive.  Need to redo it in a sane way
 *      - e.g. Query to find
 *          (1) latest dataset version in draft
 *          (2) pick off files that are NOT released
 *          (3) iterate through only those files
 *      - or an alternate/better version
 *
 * @param df
 */
private void setNewlyAddedFiles(List<DataFile> datafiles) {
    if (hasError()) {
        return;
    }
    // Init. newly added file list
    newlyAddedFiles = new ArrayList<>();
    newlyAddedFileMetadatas = new ArrayList<>();
    // Loop of uglinesss...but expect 1 to 4 files in final file list
    List<FileMetadata> latestFileMetadatas = dataset.getEditVersion().getFileMetadatas();
    for (DataFile newlyAddedFile : finalFileList) {
        for (FileMetadata fm : latestFileMetadatas) {
            if (newlyAddedFile.getChecksumValue().equals(fm.getDataFile().getChecksumValue())) {
                if (newlyAddedFile.getStorageIdentifier().equals(fm.getDataFile().getStorageIdentifier())) {
                    newlyAddedFiles.add(fm.getDataFile());
                    newlyAddedFileMetadatas.add(fm);
                }
            }
        }
    }
/*
       
        newlyAddedFile = df;
        
        for (FileMetadata fm : dataset.getEditVersion().getFileMetadatas()){
            
            // Find a file where the checksum value and identifiers are the same..
            //
            if (newlyAddedFile.getChecksumValue().equals(fm.getDataFile().getChecksumValue())){
                if (newlyAddedFile.getStorageIdentifier().equals(fm.getDataFile().getStorageIdentifier())){
                    newlyAddedFile = fm.getDataFile();
                    break;
                }
            }
        }
        */
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) FileMetadata(edu.harvard.iq.dataverse.FileMetadata)

Example 18 with FileMetadata

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

the class AddReplaceFileHelper method step_007_auto_isReplacementInLatestVersion.

/**
 * Make sure the file to replace is in the workingVersion
 *  -- e.g. that it wasn't deleted from a previous Version
 *
 * @return
 */
private boolean step_007_auto_isReplacementInLatestVersion(DataFile existingFile) {
    if (existingFile == null) {
        throw new NullPointerException("existingFile cannot be null!");
    }
    if (this.hasError()) {
        return false;
    }
    DatasetVersion latestVersion = existingFile.getOwner().getLatestVersion();
    boolean fileInLatestVersion = false;
    for (FileMetadata fm : latestVersion.getFileMetadatas()) {
        if (fm.getDataFile().getId() != null) {
            if (Objects.equals(existingFile.getId(), fm.getDataFile().getId())) {
                fileInLatestVersion = true;
            }
        }
    }
    if (!fileInLatestVersion) {
        addError(getBundleErr("existing_file_not_in_latest_published_version"));
        return false;
    }
    return true;
}
Also used : FileMetadata(edu.harvard.iq.dataverse.FileMetadata) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion)

Example 19 with FileMetadata

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

the class DuplicateFileChecker method getDatasetHashesFromDatabase.

/**
 * From dataset version:
 *  - Get the checksum of all the files
 *  - Load them into a hash
 *
 * Loads checksums from unsaved datasetversion--checks more
 */
public Map<String, Integer> getDatasetHashesFromDatabase(DatasetVersion datasetVersion) {
    if (datasetVersion == null) {
        throw new NullPointerException("datasetVersion cannot be null");
    }
    Map<String, Integer> checksumHashCounts = new HashMap<>();
    List<FileMetadata> fileMetadatas = new ArrayList<>(datasetVersion.getFileMetadatas());
    for (FileMetadata fm : fileMetadatas) {
        String checkSum = fm.getDataFile().getChecksumValue();
        if (checksumHashCounts.get(checkSum) != null) {
            checksumHashCounts.put(checkSum, checksumHashCounts.get(checkSum).intValue() + 1);
        } else {
            checksumHashCounts.put(checkSum, 1);
        }
    }
    return checksumHashCounts;
}
Also used : HashMap(java.util.HashMap) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) ArrayList(java.util.ArrayList)

Example 20 with FileMetadata

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

the class DuplicateFileChecker method isDuplicateOriginalWay.

/**
 * Original isDuplicate method from the DatasetPage and EditDatafilesPage
 *
 * Note: this has efficiency issues in that the hash is re-created for every fileMetadata checked
 * Note: the only remaining component that uses this method is AddReplaceFileHelper;
 * Currently it is only being used for file replace calls from the File page - which always
 * operates on one file at a time; so performance is not much of an issue. -- L.A. 4.6.1
 *
 * @param workingVersion
 * @param fileMetadata
 * @return
 */
public static boolean isDuplicateOriginalWay(DatasetVersion workingVersion, FileMetadata fileMetadata) {
    if (workingVersion == null) {
        throw new NullPointerException("datasetVersion cannot be null");
    }
    String selectedCheckSum = fileMetadata.getDataFile().getChecksumValue();
    if (selectedCheckSum == null) {
        return false;
    }
    Map<String, Integer> checkSumMap = new HashMap<String, Integer>();
    // TODO:
    // think of a way to do this that doesn't involve populating this
    // map for every file on the page?
    // man not be that much of a problem, if we paginate and never display
    // more than a certain number of files... Still, needs to be revisited
    // before the final 4.0.
    // -- L.A. 4.0
    // make a "defensive copy" to avoid java.util.ConcurrentModificationException from being thrown
    // when uploading 100+ files
    List<FileMetadata> wvCopy = new ArrayList<>(workingVersion.getFileMetadatas());
    Iterator<FileMetadata> fmIt = wvCopy.iterator();
    while (fmIt.hasNext()) {
        FileMetadata fm = fmIt.next();
        String currentCheckSum = fm.getDataFile().getChecksumValue();
        if (currentCheckSum != null) {
            if (checkSumMap.get(currentCheckSum) != null) {
                checkSumMap.put(currentCheckSum, checkSumMap.get(currentCheckSum).intValue() + 1);
            } else {
                checkSumMap.put(currentCheckSum, 1);
            }
        }
    }
    // && checkSumMap.get(selectedCheckSum).intValue() > 1;
    return checkSumMap.get(selectedCheckSum) != null;
}
Also used : HashMap(java.util.HashMap) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) ArrayList(java.util.ArrayList)

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