Search in sources :

Example 6 with FileAttribute

use of org.openforis.idm.model.FileAttribute in project collect by openforis.

the class XMLDataImportProcess method importRecordFiles.

private void importRecordFiles(CollectRecord record) throws IOException, RecordPersistenceException {
    sessionRecordFileManager.resetTempInfo();
    List<FileAttribute> fileAttributes = record.getFileAttributes();
    String sessionId = "admindataimport";
    for (FileAttribute fileAttribute : fileAttributes) {
        String recordFileEntryName = XMLDataExportProcess.calculateRecordFileEntryName(fileAttribute);
        InputStream is = backupFileExtractor.findEntryInputStream(recordFileEntryName);
        if (is != null) {
            sessionRecordFileManager.saveToTempFile(is, fileAttribute.getFilename(), record, fileAttribute.getInternalId());
        }
    }
    if (sessionRecordFileManager.commitChanges(record)) {
        if (record.getStep() == Step.ANALYSIS) {
            record.setStep(Step.CLEANSING);
            recordManager.save(record, sessionId);
            record.setStep(Step.ANALYSIS);
        }
        recordManager.save(record, sessionId);
    }
}
Also used : InputStream(java.io.InputStream) FileAttribute(org.openforis.idm.model.FileAttribute)

Example 7 with FileAttribute

use of org.openforis.idm.model.FileAttribute in project collect by openforis.

the class RecordFileBackupTask method backup.

private void backup(CollectRecordSummary summary) throws RecordFileException, IOException {
    Integer id = summary.getId();
    CollectRecord record = recordManager.load(survey, id, summary.getStep(), false);
    List<FileAttribute> fileAttributes = record.getFileAttributes();
    for (FileAttribute fileAttribute : fileAttributes) {
        if (StringUtils.isNotBlank(fileAttribute.getFilename())) {
            File file = recordFileManager.getRepositoryFile(fileAttribute);
            if (file != null && file.exists()) {
                String entryName = calculateRecordFileEntryName(fileAttribute);
                writeFile(file, entryName);
            } else {
                addSkippedFileError(summary, fileAttribute.getPath(), recordFileManager.getRepositoryFileAbsolutePath(fileAttribute));
                log.error(String.format("Record file not found for record %s (%d) attribute %s (%d)", StringUtils.join(record.getRootEntityKeyValues(), ','), record.getId(), fileAttribute.getPath(), fileAttribute.getInternalId()));
            // throw new RecordFileException(message);
            }
        }
    }
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) File(java.io.File) FileAttribute(org.openforis.idm.model.FileAttribute)

Example 8 with FileAttribute

use of org.openforis.idm.model.FileAttribute in project collect by openforis.

the class SpecifiedValidator method evaluate.

@Override
public ValidationResultFlag evaluate(Attribute<?, ?> attribute) {
    CollectRecord record = (CollectRecord) attribute.getRecord();
    Step step = record.getStep();
    if (Step.ENTRY == step) {
        if (attribute.isRelevant() && attribute.isEmpty() && !(attribute instanceof FileAttribute)) {
            if (isReasonBlankAlwaysSpecified(attribute)) {
                if (attribute.isRequired()) {
                    return WARNING;
                } else {
                    return OK;
                }
            } else {
                return ERROR;
            }
        } else {
            return OK;
        }
    }
    return OK;
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) Step(org.openforis.collect.model.CollectRecord.Step) FileAttribute(org.openforis.idm.model.FileAttribute)

Example 9 with FileAttribute

use of org.openforis.idm.model.FileAttribute in project collect by openforis.

the class RecordFileManager method moveFileIntoRepository.

/**
 * Moves a file into the repository and associates the file name to the corresponding file attribute node
 * Returns true if the record is modified (file name or size different from the old one).
 */
public boolean moveFileIntoRepository(CollectRecord record, int nodeId, java.io.File newFile) throws IOException {
    boolean recordUpdated = false;
    FileAttribute fileAttribute = (FileAttribute) record.getNodeByInternalId(nodeId);
    FileAttributeDefinition defn = fileAttribute.getDefinition();
    String repositoryFileName = generateUniqueRepositoryFileName(fileAttribute, newFile);
    File repositoryFile = new java.io.File(getRepositoryDir(defn), repositoryFileName);
    long repositoryFileSize = newFile.length();
    if (!repositoryFileName.equals(fileAttribute.getFilename()) || !Long.valueOf(repositoryFileSize).equals(fileAttribute.getSize())) {
        recordUpdated = true;
        fileAttribute.setFilename(repositoryFileName);
        fileAttribute.setSize(repositoryFileSize);
    }
    FileUtils.moveFile(newFile, repositoryFile);
    return recordUpdated;
}
Also used : FileAttributeDefinition(org.openforis.idm.metamodel.FileAttributeDefinition) File(java.io.File) FileAttribute(org.openforis.idm.model.FileAttribute)

Example 10 with FileAttribute

use of org.openforis.idm.model.FileAttribute in project collect by openforis.

the class SessionRecordFileManager method performFilesDelete.

protected boolean performFilesDelete(CollectRecord record) {
    boolean result = false;
    Set<Entry<Integer, String>> entrySet = filesToDelete.entrySet();
    for (Entry<Integer, String> entry : entrySet) {
        int nodeId = entry.getKey();
        String fileName = entry.getValue();
        FileAttribute fileAttr = (FileAttribute) record.getNodeByInternalId(nodeId);
        FileAttributeDefinition defn = fileAttr.getDefinition();
        java.io.File repositoryFile = recordFileManager.getRepositoryFile(defn, fileName);
        repositoryFile.delete();
        result = true;
    }
    filesToDelete.clear();
    return result;
}
Also used : Entry(java.util.Map.Entry) FileAttributeDefinition(org.openforis.idm.metamodel.FileAttributeDefinition) FileAttribute(org.openforis.idm.model.FileAttribute)

Aggregations

FileAttribute (org.openforis.idm.model.FileAttribute)10 File (java.io.File)3 InputStream (java.io.InputStream)2 CollectRecord (org.openforis.collect.model.CollectRecord)2 FileAttributeDefinition (org.openforis.idm.metamodel.FileAttributeDefinition)2 Entity (org.openforis.idm.model.Entity)2 ArrayList (java.util.ArrayList)1 Entry (java.util.Map.Entry)1 RecordFileException (org.openforis.collect.manager.exception.RecordFileException)1 Step (org.openforis.collect.model.CollectRecord.Step)1 AttributeUpdateRequest (org.openforis.collect.remoting.service.NodeUpdateRequest.AttributeUpdateRequest)1 Attribute (org.openforis.idm.model.Attribute)1 File (org.openforis.idm.model.File)1 NodeVisitor (org.openforis.idm.model.NodeVisitor)1 Value (org.openforis.idm.model.Value)1