Search in sources :

Example 31 with RecordFilter

use of org.openforis.collect.model.RecordFilter in project collect by openforis.

the class CSVDataImportProcess method validateRecordKey.

private boolean validateRecordKey(DataLine line) {
    long currentRowNumber = line.getLineNumber();
    EntityDefinition parentEntityDefn = getParentEntityDefinition();
    EntityDefinition rootEntityDefn = parentEntityDefn.getRootEntity();
    Value[] recordKeyValues = line.getRecordKeyValues(rootEntityDefn);
    String[] recordKeyStringValues = Values.toStringValues(recordKeyValues);
    RecordFilter filter = new RecordFilter(survey);
    filter.setRootEntityId(rootEntityDefn.getId());
    filter.setKeyValues(recordKeyStringValues);
    List<CollectRecordSummary> recordSummaries = recordManager.loadSummaries(filter);
    String[] recordKeyColumnNames = DataCSVReader.getKeyAttributeColumnNames(parentEntityDefn, rootEntityDefn.getKeyAttributeDefinitions());
    String errorMessageKey = null;
    if (settings.isInsertNewRecords()) {
        if (!recordSummaries.isEmpty()) {
            errorMessageKey = ONLY_NEW_RECORDS_ALLOWED_MESSAGE_KEY;
        }
    } else if (recordSummaries.isEmpty() && settings.isReportNoRecordFoundErrors()) {
        errorMessageKey = NO_RECORD_FOUND_ERROR_MESSAGE_KEY;
    } else if (recordSummaries.size() > 1) {
        errorMessageKey = MULTIPLE_RECORDS_FOUND_ERROR_MESSAGE_KEY;
    }
    if (errorMessageKey == null) {
        return true;
    } else {
        ParsingError parsingError = new ParsingError(ErrorType.INVALID_VALUE, currentRowNumber, recordKeyColumnNames, errorMessageKey);
        parsingError.setMessageArgs(new String[] { StringUtils.join(recordKeyStringValues) });
        status.addParsingError(currentRowNumber, parsingError);
        return false;
    }
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) ParsingError(org.openforis.collect.io.metadata.parsing.ParsingError) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) AbstractValue(org.openforis.idm.model.AbstractValue) Value(org.openforis.idm.model.Value) RecordFilter(org.openforis.collect.model.RecordFilter)

Example 32 with RecordFilter

use of org.openforis.collect.model.RecordFilter in project collect by openforis.

the class XMLDataImportProcess method findAlreadyExistingRecordSummary.

private CollectRecordSummary findAlreadyExistingRecordSummary(CollectRecord parsedRecord) {
    CollectSurvey survey = (CollectSurvey) parsedRecord.getSurvey();
    List<String> keyValues = parsedRecord.getRootEntityKeyValues();
    RecordFilter filter = new RecordFilter(survey);
    filter.setRootEntityId(parsedRecord.getRootEntityDefinitionId());
    filter.setKeyValues(keyValues);
    List<CollectRecordSummary> oldRecords = recordManager.loadFullSummaries(filter);
    if (oldRecords == null || oldRecords.isEmpty()) {
        return null;
    } else if (oldRecords.size() == 1) {
        return oldRecords.get(0);
    } else {
        throw new IllegalStateException(String.format("Multiple records found in survey %s with key(s): %s", survey.getName(), keyValues));
    }
}
Also used : CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) CollectSurvey(org.openforis.collect.model.CollectSurvey) RecordFilter(org.openforis.collect.model.RecordFilter)

Example 33 with RecordFilter

use of org.openforis.collect.model.RecordFilter in project collect by openforis.

the class DataRestoreJob method initializeTask.

@Override
protected void initializeTask(Worker task) {
    if (task instanceof SurveyBackupJob) {
        SurveyBackupJob t = (SurveyBackupJob) task;
        t.setFull(true);
        t.setIncludeData(true);
        t.setIncludeRecordFiles(true);
        t.setOutputFormat(OutputFormat.DESKTOP_FULL);
        t.setRecordFilter(new RecordFilter(publishedSurvey));
        t.setSurvey(publishedSurvey);
    } else if (task instanceof DataRestoreTask) {
        DataRestoreTask t = (DataRestoreTask) task;
        t.setRecordManager(recordManager);
        t.setUserManager(userManager);
        t.setUserGroupManager(userGroupManager);
        t.setRecordProvider(recordProvider);
        t.setTargetSurvey(publishedSurvey);
        t.setUser(user);
        t.setOverwriteStrategy(recordOverwriteStrategy);
        t.setEntryIdsToImport(entryIdsToImport);
        t.setIncludeRecordPredicate(includeRecordPredicate);
    } else if (task instanceof RecordFileRestoreTask) {
        RecordFileRestoreTask t = (RecordFileRestoreTask) task;
        t.setRecordManager(recordManager);
        t.setRecordFileManager(recordFileManager);
        t.setBackupFileExtractor(backupFileExtractor);
        t.setRecordProvider(recordProvider);
        t.setOverwriteStrategy(recordOverwriteStrategy);
        t.setEntryIdsToImport(entryIdsToImport);
        t.setSurvey(publishedSurvey);
    }
    super.initializeTask(task);
}
Also used : SurveyBackupJob(org.openforis.collect.io.SurveyBackupJob) RecordFilter(org.openforis.collect.model.RecordFilter)

Example 34 with RecordFilter

use of org.openforis.collect.model.RecordFilter in project collect by openforis.

the class DataRestoreJob method isBackupNeeded.

private boolean isBackupNeeded() {
    if (newSurvey) {
        return false;
    }
    Date lastBackupDate = backupStorageManager.getLastBackupDate(surveyName);
    RecordFilter recordFilter = new RecordFilter(publishedSurvey);
    recordFilter.setModifiedSince(lastBackupDate);
    return recordManager.countRecords(recordFilter) > 0 || (lastBackupDate != null && publishedSurvey.getModifiedDate().after(lastBackupDate));
}
Also used : Date(java.util.Date) RecordFilter(org.openforis.collect.model.RecordFilter)

Example 35 with RecordFilter

use of org.openforis.collect.model.RecordFilter in project collect by openforis.

the class RecordFileBackupTask method execute.

@Override
protected void execute() throws Throwable {
    if (!hasFileAttributeDefinitions()) {
        return;
    }
    RecordFilter filter = new RecordFilter(survey);
    filter.setRootEntityId(survey.getSchema().getRootEntityDefinition(rootEntityName).getId());
    recordManager.visitSummaries(filter, new Visitor<CollectRecordSummary>() {

        public void visit(CollectRecordSummary summary) {
            try {
                backup(summary);
                incrementProcessedItems();
            } catch (Exception e) {
                addSkippedRecord(summary);
                log.error(String.format("Error backing up record files for record with id %d and keys %s", summary.getId(), summary.getRootEntityKeyValues().toString()));
            }
        }
    }, new Predicate<CollectRecordSummary>() {

        public boolean evaluate(CollectRecordSummary s) {
            return !isRunning();
        }
    });
}
Also used : CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) RecordFilter(org.openforis.collect.model.RecordFilter) RecordFileException(org.openforis.collect.manager.exception.RecordFileException) IOException(java.io.IOException)

Aggregations

RecordFilter (org.openforis.collect.model.RecordFilter)66 CollectRecordSummary (org.openforis.collect.model.CollectRecordSummary)33 CollectSurvey (org.openforis.collect.model.CollectSurvey)27 CollectRecord (org.openforis.collect.model.CollectRecord)14 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 Date (java.util.Date)10 User (org.openforis.collect.model.User)10 SessionState (org.openforis.collect.web.session.SessionState)7 Transactional (org.springframework.transaction.annotation.Transactional)7 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)6 File (java.io.File)5 HashMap (java.util.HashMap)5 CSVDataExportParameters (org.openforis.collect.io.data.csv.CSVDataExportParameters)5 Step (org.openforis.collect.model.CollectRecord.Step)5 IOException (java.io.IOException)4 SurveyBackupJob (org.openforis.collect.io.SurveyBackupJob)4 CSVDataExportJob (org.openforis.collect.io.data.CSVDataExportJob)4 Schema (org.openforis.idm.metamodel.Schema)4 Test (org.junit.Test)3