use of org.opendatakit.briefcase.model.ExportProgressPercentageEvent in project briefcase by opendatakit.
the class ExportToCsv method processInstance.
private boolean processInstance(File instanceDir) {
File submission = new File(instanceDir, "submission.xml");
if (!submission.exists() || !submission.isFile()) {
EventBus.publish(new ExportProgressEvent("Submission not found for instance directory: " + instanceDir.getPath(), briefcaseLfd));
return false;
}
processedInstances++;
EventBus.publish(new ExportProgressEvent("Processing instance: " + instanceDir.getName(), briefcaseLfd));
EventBus.publish(new ExportProgressPercentageEvent((processedInstances * 100.0) / totalInstances, briefcaseLfd));
// If we are encrypted, be sure the temporary directory
// that will hold the unencrypted files is created.
// If we aren't encrypted, the temporary directory
// is the same as the instance directory.
File unEncryptedDir;
if (briefcaseLfd.isFileEncryptedForm()) {
// create the temp directory that will hold the unencrypted
// files. Do this in the outputDir so that the briefcase storage location
// can be a read-only network mount. issue 676.
Path path;
try {
path = Files.createTempDirectory(Paths.get(outputDir.toURI()), ".temp");
} catch (IOException e) {
String msg = "Unable to create temp directory.";
log.error(msg, e);
EventBus.publish(new ExportProgressEvent(msg + " Cause : " + e.toString(), briefcaseLfd));
return false;
}
unEncryptedDir = path.toFile();
} else {
unEncryptedDir = instanceDir;
}
// parse the xml document (this is the manifest if encrypted)...
Document doc;
boolean isValidated = false;
try {
doc = XmlManipulationUtils.parseXml(submission);
} catch (ParsingException | FileSystemException e) {
log.error("Error parsing submission", e);
EventBus.publish(new ExportProgressEvent(("Error parsing submission " + instanceDir.getName()) + " Cause: " + e.toString(), briefcaseLfd));
return false;
}
String submissionDate = null;
// extract the submissionDate, if present, from the attributes
// of the root element of the submission or submission manifest (if encrypted).
submissionDate = doc.getRootElement().getAttributeValue(null, "submissionDate");
if (submissionDate == null || submissionDate.length() == 0) {
submissionDate = null;
} else {
Date theDate = WebUtils.parseDate(submissionDate);
DateFormat formatter = DateFormat.getDateTimeInstance();
submissionDate = formatter.format(theDate);
// just return true to skip records out of range
if (startDate != null && theDate.before(startDate)) {
log.info("Submission date is before specified, skipping: " + instanceDir.getName());
return true;
}
if (endDate != null && theDate.after(endDate)) {
log.info("Submission date is after specified, skipping: " + instanceDir.getName());
return true;
}
// don't export records without dates if either date is set
if ((startDate != null || endDate != null) && submissionDate == null) {
log.info("No submission date found, skipping: " + instanceDir.getName());
return true;
}
}
// failure.
try {
if (briefcaseLfd.isFileEncryptedForm()) {
// NOTE: this changes the value of 'doc'
try {
FileSystemUtils.DecryptOutcome outcome = FileSystemUtils.decryptAndValidateSubmission(doc, briefcaseLfd.getPrivateKey(), instanceDir, unEncryptedDir);
doc = outcome.submission;
isValidated = outcome.isValidated;
} catch (ParsingException | CryptoException | FileSystemException e) {
// Was unable to parse file or decrypt file or a file system error occurred
// Hence skip this instance
EventBus.publish(new ExportProgressEvent("Error decrypting submission " + instanceDir.getName() + " Cause: " + e.toString() + " skipping....", briefcaseLfd));
log.info("Error decrypting submission " + instanceDir.getName() + " Cause: " + e.toString());
// update total number of files skipped
totalFilesSkipped++;
return true;
}
}
String instanceId = null;
String base64EncryptedFieldKey = null;
// find an instanceId to use...
try {
FormInstanceMetadata sim = XmlManipulationUtils.getFormInstanceMetadata(doc.getRootElement());
instanceId = sim.instanceId;
base64EncryptedFieldKey = sim.base64EncryptedFieldKey;
} catch (ParsingException e) {
log.error("Could not extract metadata from submission", e);
EventBus.publish(new ExportProgressEvent("Could not extract metadata from submission " + submission.getAbsolutePath(), briefcaseLfd));
return false;
}
if (instanceId == null || instanceId.length() == 0) {
// if we have no instanceID, and there isn't any in the file,
// use the checksum as the id.
// NOTE: encrypted submissions always have instanceIDs.
// This is for legacy non-OpenRosa forms.
long checksum;
try {
checksum = FileUtils.checksumCRC32(submission);
} catch (IOException e1) {
String msg = "Failed during computing of crc";
log.error(msg, e1);
EventBus.publish(new ExportProgressEvent(msg + ": " + e1.getMessage(), briefcaseLfd));
return false;
}
instanceId = "crc32:" + Long.toString(checksum);
}
if (terminationFuture.isCancelled()) {
EventBus.publish(new ExportProgressEvent("Aborted", briefcaseLfd));
return false;
}
EncryptionInformation ei = null;
if (base64EncryptedFieldKey != null) {
try {
ei = new EncryptionInformation(base64EncryptedFieldKey, instanceId, briefcaseLfd.getPrivateKey());
} catch (CryptoException e) {
log.error("Error establishing field decryption", e);
EventBus.publish(new ExportProgressEvent("Error establishing field decryption for submission " + instanceDir.getName() + " Cause: " + e.toString(), briefcaseLfd));
return false;
}
}
// emit the csv record...
try {
OutputStreamWriter osw = fileMap.get(briefcaseLfd.getSubmissionElement());
emitString(osw, true, submissionDate);
emitSubmissionCsv(osw, ei, doc.getRootElement(), briefcaseLfd.getSubmissionElement(), briefcaseLfd.getSubmissionElement(), false, instanceId, unEncryptedDir);
emitString(osw, false, instanceId);
if (briefcaseLfd.isFileEncryptedForm()) {
emitString(osw, false, Boolean.toString(isValidated));
if (!isValidated) {
EventBus.publish(new ExportProgressEvent("Decrypted submission " + instanceDir.getName() + " may be missing attachments and could not be validated.", briefcaseLfd));
}
}
osw.append("\n");
return true;
} catch (IOException e) {
String msg = "Failed writing csv";
log.error(msg, e);
EventBus.publish(new ExportProgressEvent(msg + ": " + e.getMessage(), briefcaseLfd));
return false;
}
} finally {
if (briefcaseLfd.isFileEncryptedForm()) {
// destroy the temp directory and its contents...
try {
FileUtils.deleteDirectory(unEncryptedDir);
} catch (IOException e) {
String msg = "Unable to remove decrypted files";
log.error(msg, e);
EventBus.publish(new ExportProgressEvent(msg + ": " + e.getMessage(), briefcaseLfd));
return false;
}
}
}
}
Aggregations