use of org.openforis.collect.persistence.xml.DataUnmarshaller.ParseRecordResult in project collect by openforis.
the class DataMarshallerIntegrationTest method parseRecord.
private ParseRecordResult parseRecord(CollectSurvey survey, String xml) throws IOException, DataUnmarshallerException {
DataUnmarshaller dataUnmarshaller = new DataUnmarshaller(survey);
StringReader reader = new StringReader(xml);
ParseRecordResult result = dataUnmarshaller.parse(reader);
return result;
}
use of org.openforis.collect.persistence.xml.DataUnmarshaller.ParseRecordResult in project collect by openforis.
the class XMLDataImportProcess method importEntries.
private void importEntries(int recordId) throws IOException, DataImportExeption, RecordPersistenceException {
CollectRecord lastProcessedRecord = null;
Step originalRecordStep = null;
Step[] steps = Step.values();
for (Step step : steps) {
RecordEntry recordEntry = new RecordEntry(step, recordId);
String entryName = recordEntry.getName();
InputStream inputStream = backupFileExtractor.findEntryInputStream(entryName);
if (inputStream != null) {
InputStreamReader reader = OpenForisIOUtils.toReader(inputStream);
ParseRecordResult parseRecordResult = parseRecord(reader, validateRecords);
CollectRecord parsedRecord = parseRecordResult.getRecord();
if (parsedRecord == null) {
String message = parseRecordResult.getMessage();
state.addError(entryName, message);
} else {
parsedRecord.setStep(step);
List<CollectStoreQuery> queries;
if (lastProcessedRecord == null) {
CollectRecordSummary oldRecordSummary = findAlreadyExistingRecordSummary(parsedRecord);
if (oldRecordSummary != null) {
// overwrite existing record
originalRecordStep = oldRecordSummary.getStep();
parsedRecord.setId(oldRecordSummary.getId());
if (includesRecordFiles) {
recordFileManager.deleteAllFiles(parsedRecord);
}
int newWorkflowSequenceNumber = oldRecordSummary.getWorkflowSequenceNumber() + (step.getStepNumber() - originalRecordStep.getStepNumber());
parsedRecord.setDataWorkflowSequenceNumber(newWorkflowSequenceNumber);
parsedRecord.setWorkflowSequenceNumber(newWorkflowSequenceNumber);
if (step.after(originalRecordStep)) {
queries = Arrays.asList(recordManager.createDataInsertQuery(parsedRecord, oldRecordSummary.getId(), step, newWorkflowSequenceNumber));
} else {
queries = Arrays.asList(recordManager.createDataUpdateQuery(parsedRecord, oldRecordSummary.getId(), step, newWorkflowSequenceNumber));
}
} else {
parsedRecord.setId(nextRecordId++);
queries = recordManager.createNewRecordInsertQueries(parsedRecord);
}
lastProcessedRecord = parsedRecord;
} else {
replaceData(parsedRecord, lastProcessedRecord);
int sequenceNumber = parsedRecord.getWorkflowSequenceNumber() + 1;
parsedRecord.setDataWorkflowSequenceNumber(sequenceNumber);
if (step.after(originalRecordStep)) {
queries = Arrays.asList(recordManager.createDataInsertQuery(lastProcessedRecord, lastProcessedRecord.getId(), step, sequenceNumber));
} else {
queries = Arrays.asList(recordManager.createDataUpdateQuery(lastProcessedRecord, lastProcessedRecord.getId(), step, sequenceNumber));
}
}
appendQueries(queries);
// if ( parseRecordResult.hasWarnings() ) {
// state.addWarnings(entryName, parseRecordResult.getWarnings());
// }
}
}
}
if (lastProcessedRecord != null && originalRecordStep != null && originalRecordStep.compareTo(lastProcessedRecord.getStep()) > 0) {
// reset the step to the original one and revalidate the record
CollectSurvey survey = (CollectSurvey) lastProcessedRecord.getSurvey();
CollectRecord originalRecord = recordManager.load(survey, lastProcessedRecord.getId(), originalRecordStep, validateRecords);
originalRecord.setStep(originalRecordStep);
afterRecordUpdate(originalRecord);
appendQuery(recordManager.createSummaryUpdateQuery(originalRecord));
}
if (includesRecordFiles) {
importRecordFiles(lastProcessedRecord);
}
}
use of org.openforis.collect.persistence.xml.DataUnmarshaller.ParseRecordResult in project collect by openforis.
the class XMLDataImportProcess method createSummaryForEntry.
private void createSummaryForEntry(String entryName, Map<String, List<NodeUnmarshallingError>> packagedSkippedFileErrors, Map<Integer, CollectRecord> packagedRecords, Map<Integer, List<Step>> packagedStepsPerRecord, Map<Step, Integer> totalPerStep, Map<Integer, CollectRecordSummary> conflictingPackagedRecords, Map<Integer, Map<Step, List<NodeUnmarshallingError>>> warnings) throws IOException, DataParsingExeption {
RecordEntry recordEntry = RecordEntry.parse(entryName);
Step step = recordEntry.getStep();
InputStream is = backupFileExtractor.findEntryInputStream(entryName);
InputStreamReader reader = OpenForisIOUtils.toReader(is);
ParseRecordResult parseRecordResult = parseRecord(reader, false);
CollectRecord parsedRecord = parseRecordResult.getRecord();
if (!parseRecordResult.isSuccess()) {
List<NodeUnmarshallingError> failures = parseRecordResult.getFailures();
packagedSkippedFileErrors.put(entryName, failures);
} else if (includeRecordPredicate == null || includeRecordPredicate.evaluate(parsedRecord)) {
int entryId = recordEntry.getRecordId();
CollectRecord recordSummary = createRecordSummary(parsedRecord);
packagedRecords.put(entryId, recordSummary);
List<Step> stepsPerRecord = packagedStepsPerRecord.get(entryId);
if (stepsPerRecord == null) {
stepsPerRecord = new ArrayList<CollectRecord.Step>();
packagedStepsPerRecord.put(entryId, stepsPerRecord);
}
stepsPerRecord.add(step);
Integer totalPerStep1 = totalPerStep.get(step);
totalPerStep.put(step, totalPerStep1 + 1);
CollectRecordSummary oldRecord = findAlreadyExistingRecordSummary(parsedRecord);
if (oldRecord != null) {
conflictingPackagedRecords.put(entryId, oldRecord);
}
if (parseRecordResult.hasWarnings()) {
Map<Step, List<NodeUnmarshallingError>> warningsPerEntry = warnings.get(entryId);
if (warningsPerEntry == null) {
warningsPerEntry = new HashMap<CollectRecord.Step, List<NodeUnmarshallingError>>();
warnings.put(entryId, warningsPerEntry);
}
warningsPerEntry.put(step, parseRecordResult.getWarnings());
}
}
state.incrementCount();
}
use of org.openforis.collect.persistence.xml.DataUnmarshaller.ParseRecordResult in project collect by openforis.
the class BackupDataExtractor method findRecord.
public ParseRecordResult findRecord(BackupRecordEntry recordEntry) throws IOException {
String entryName = recordEntry.getName();
InputStream is = fileExtractor.findEntryInputStream(entryName);
if (is == null) {
return null;
} else {
ParseRecordResult result = parse(is);
return result;
}
}
use of org.openforis.collect.persistence.xml.DataUnmarshaller.ParseRecordResult in project collect by openforis.
the class BackupDataExtractor method parse.
private ParseRecordResult parse(InputStream inputStream) throws IOException {
InputStreamReader reader = OpenForisIOUtils.toReader(inputStream);
ParseRecordResult result = dataUnmarshaller.parse(reader);
return result;
}
Aggregations