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();
}
}
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();
}
}
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();
}
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;
}
}
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());
}
Aggregations