Search in sources :

Example 26 with CollectRecordSummary

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

the class RecordManager method determineLastStep.

private Step determineLastStep(CollectSurvey survey, int recordId) {
    RecordFilter filter = new RecordFilter(survey);
    filter.setRecordId(recordId);
    List<CollectRecordSummary> summaries = recordDao.loadSummaries(filter, null);
    if (summaries.isEmpty()) {
        return null;
    } else {
        CollectRecordSummary summary = summaries.get(0);
        return summary.getStep();
    }
}
Also used : CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) RecordFilter(org.openforis.collect.model.RecordFilter)

Example 27 with CollectRecordSummary

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

the class RecordValidationProcess method startProcessing.

@Override
@Transactional(propagation = Propagation.SUPPORTS)
public void startProcessing() throws Exception {
    super.startProcessing();
    validateParameters();
    status.setTotal(recordManager.countRecords(new RecordFilter(survey)));
    Schema schema = survey.getSchema();
    List<EntityDefinition> rootEntities = schema.getRootEntityDefinitions();
    for (EntityDefinition rootEntityDef : rootEntities) {
        RecordFilter filter = new RecordFilter(survey, rootEntityDef.getId());
        List<CollectRecordSummary> summaries = recordManager.loadSummaries(filter);
        for (CollectRecordSummary summary : summaries) {
            // print(outputStream, "Start validating record: " + recordKey);
            if (status.isRunning()) {
                Step step = summary.getStep();
                Integer recordId = summary.getId();
                recordManager.validateAndSave(survey, user, sessionId, recordId, step);
                status.incrementProcessed();
            // long elapsedMillis = System.currentTimeMillis() - start;
            // print(outputStream, "Validation of record " + recordKey + " completed in " + elapsedMillis + " millis");
            }
        }
    }
    if (status.isRunning()) {
        status.complete();
    }
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Schema(org.openforis.idm.metamodel.Schema) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) Step(org.openforis.collect.model.CollectRecord.Step) RecordFilter(org.openforis.collect.model.RecordFilter) Transactional(org.springframework.transaction.annotation.Transactional)

Example 28 with CollectRecordSummary

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

the class CSVDataExportProcess method exportData.

// private String calculateOutputFileName() {
// return "data.zip";
// /*
// StringBuilder sb = new StringBuilder();
// sb.append(survey.getName());
// sb.append("_");
// sb.append(rootEntityName);
// sb.append("_");
// sb.append("csv_data");
// SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
// String today = formatter.format(new Date());
// sb.append(today);
// sb.append(".zip");
// return sb.toString();
// */
// }
private void exportData(OutputStream outputStream, int entityDefId) throws InvalidExpressionException, IOException, RecordPersistenceException {
    Writer outputWriter = new OutputStreamWriter(outputStream, OpenForisIOUtils.UTF_8);
    CSVDataExportColumnProviderGenerator csvDataExportColumnProviderGenerator = new CSVDataExportColumnProviderGenerator(recordFilter.getSurvey(), configuration);
    DataTransformation transform = csvDataExportColumnProviderGenerator.generateDataTransformation(entityDefId);
    @SuppressWarnings("resource") ModelCsvWriter // closing modelWriter will close referenced output stream
    modelWriter = new ModelCsvWriter(outputWriter, transform);
    modelWriter.printColumnHeadings();
    CollectSurvey survey = recordFilter.getSurvey();
    Step step = recordFilter.getStepGreaterOrEqual();
    List<CollectRecordSummary> summaries = recordManager.loadSummaries(recordFilter);
    for (CollectRecordSummary s : summaries) {
        if (status.isRunning()) {
            try {
                CollectRecord record = recordManager.load(survey, s.getId(), step, false);
                modelWriter.printData(record);
            } catch (Exception e) {
                LOG.error(e.getMessage(), e);
            }
            status.incrementProcessed();
        } else {
            break;
        }
    }
    modelWriter.flush();
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) DataTransformation(org.openforis.collect.io.data.csv.DataTransformation) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) OutputStreamWriter(java.io.OutputStreamWriter) Step(org.openforis.collect.model.CollectRecord.Step) CollectSurvey(org.openforis.collect.model.CollectSurvey) ModelCsvWriter(org.openforis.collect.io.data.csv.ModelCsvWriter) ModelCsvWriter(org.openforis.collect.io.data.csv.ModelCsvWriter) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) RecordPersistenceException(org.openforis.collect.persistence.RecordPersistenceException) IOException(java.io.IOException)

Example 29 with CollectRecordSummary

use of org.openforis.collect.model.CollectRecordSummary 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 30 with CollectRecordSummary

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

the class CSVDataImportProcess method processLine.

private void processLine(DataLine line) throws RecordPersistenceException {
    if (!validateRecordKey(line)) {
        return;
    }
    if (settings.isInsertNewRecords()) {
        // create new record
        EntityDefinition rootEntityDefn = survey.getSchema().getRootEntityDefinition(parentEntityDefinitionId);
        CollectRecord record = recordManager.instantiateRecord(survey, rootEntityDefn.getName(), adminUser, settings.getNewRecordVersionName(), Step.ENTRY);
        NodeChangeSet changes = recordManager.initializeRecord(record);
        if (nodeChangeBatchProcessor != null) {
            nodeChangeBatchProcessor.add(changes, adminUser.getUsername());
        }
        setRecordKeys(line, record);
        setValuesInRecord(line, record, Step.ENTRY);
        insertRecord(record);
    } else {
        CollectRecordSummary recordSummary = loadRecordSummaryIfAny(line);
        if (recordSummary != null) {
            Step originalRecordStep = recordSummary.getStep();
            if (step == null) {
                // set values in each step data
                for (Step currentStep : Step.values()) {
                    if (currentStep.beforeEqual(originalRecordStep)) {
                        CollectRecord record = loadRecord(recordSummary.getId(), currentStep);
                        setValuesInRecord(line, record, currentStep);
                        // always save record when updating multiple record steps in the same process
                        updateRecord(record, originalRecordStep, currentStep);
                    }
                }
            } else {
                if (step.beforeEqual(originalRecordStep)) {
                    CollectRecord record;
                    boolean recordChanged = lastModifiedRecordSummary == null || !recordSummary.getId().equals(lastModifiedRecordSummary.getId());
                    if (recordChanged) {
                        // record changed
                        if (lastModifiedRecordSummary != null) {
                            saveLastModifiedRecord();
                        }
                        record = loadRecord(recordSummary.getId(), this.step);
                    } else {
                        record = lastModifiedRecord;
                    }
                    setValuesInRecord(line, record, step);
                    lastModifiedRecordSummary = recordSummary;
                    lastModifiedRecord = record;
                } else {
                    status.addParsingError(new ParsingError(ErrorType.INVALID_VALUE, line.getLineNumber(), (String) null, RECORD_NOT_IN_SELECTED_STEP_MESSAGE_KEY));
                }
            }
        }
    }
    status.addProcessedRow(line.getLineNumber());
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) CollectRecord(org.openforis.collect.model.CollectRecord) NodeChangeSet(org.openforis.collect.model.NodeChangeSet) ParsingError(org.openforis.collect.io.metadata.parsing.ParsingError) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) Step(org.openforis.collect.model.CollectRecord.Step)

Aggregations

CollectRecordSummary (org.openforis.collect.model.CollectRecordSummary)55 RecordFilter (org.openforis.collect.model.RecordFilter)33 CollectRecord (org.openforis.collect.model.CollectRecord)25 Step (org.openforis.collect.model.CollectRecord.Step)21 CollectSurvey (org.openforis.collect.model.CollectSurvey)15 IOException (java.io.IOException)8 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)6 User (org.openforis.collect.model.User)5 Transactional (org.springframework.transaction.annotation.Transactional)5 MainStep (org.openforis.collect.io.data.DataImportState.MainStep)4 SubStep (org.openforis.collect.io.data.DataImportState.SubStep)4 StepSummary (org.openforis.collect.model.CollectRecordSummary.StepSummary)4 Schema (org.openforis.idm.metamodel.Schema)4 List (java.util.List)3 Test (org.junit.Test)3 CollectIntegrationTest (org.openforis.collect.CollectIntegrationTest)3 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2