Search in sources :

Example 1 with DifferenceResult

use of org.opendatakit.aggregate.parser.BaseFormParserForJavaRosa.DifferenceResult in project briefcase by opendatakit.

the class BriefcaseFormDefinition method resolveAgainstBriefcaseDefn.

public static final BriefcaseFormDefinition resolveAgainstBriefcaseDefn(File tmpFormFile, boolean copyFile) throws BadFormDefinition {
    if (!tmpFormFile.exists()) {
        throw new BadFormDefinition("Form directory does not contain form");
    }
    // parse the temp file into a form definition...
    boolean badForm = false;
    JavaRosaParserWrapper newDefn;
    File briefcaseFormDirectory;
    File briefcaseFormFile;
    try {
        newDefn = new JavaRosaParserWrapper(tmpFormFile, readFile(tmpFormFile));
        briefcaseFormDirectory = FileSystemUtils.getFormDirectory(newDefn.getFormName());
        briefcaseFormFile = FileSystemUtils.getFormDefinitionFile(briefcaseFormDirectory);
    } catch (ODKIncompleteSubmissionData e) {
        log.warn("bad form definition", e);
        try {
            badForm = true;
            newDefn = null;
            briefcaseFormDirectory = FileSystemUtils.getFormDirectory("_badForm");
            briefcaseFormFile = FileSystemUtils.getFormDefinitionFile(briefcaseFormDirectory);
        } catch (FileSystemException ex) {
            log.error("failed to establish storage location for bad form", e);
            throw new BadFormDefinition(ex);
        }
    } catch (FileSystemException e) {
        log.error("failed to establish storage location for form", e);
        throw new BadFormDefinition(e);
    }
    boolean isIdentical = false;
    boolean needsMediaUpdate = false;
    File revised = new File(briefcaseFormFile.getParentFile(), briefcaseFormFile.getName() + ".revised");
    String revisedXml = null;
    JavaRosaParserWrapper revisedDefn = null;
    // determine the most up-to-date existing definition...
    JavaRosaParserWrapper existingDefn;
    try {
        if (revised.exists()) {
            revisedXml = readFile(revised);
            revisedDefn = new JavaRosaParserWrapper(revised, revisedXml);
        }
        if (!briefcaseFormFile.exists()) {
            // Rename it to formFile and parse it.
            if (copyFile) {
                try {
                    FileUtils.copyFile(tmpFormFile, briefcaseFormFile);
                } catch (IOException e) {
                    String msg = "Unable to copy form definition file into briefcase directory";
                    log.error(msg, e);
                    throw new BadFormDefinition(msg);
                }
            } else {
                if (!tmpFormFile.renameTo(briefcaseFormFile)) {
                    // if cannot rename, try to copy instead (and mark for deletion)
                    try {
                        FileUtils.copyFile(tmpFormFile, briefcaseFormFile);
                        tmpFormFile.deleteOnExit();
                    } catch (IOException e) {
                        String msg = "Form directory does not contain form (can neither rename nor copy into briefcase directory)";
                        log.error(msg);
                        throw new BadFormDefinition(msg);
                    }
                }
            }
            // weird if it does...
            needsMediaUpdate = !revised.exists();
            existingDefn = new JavaRosaParserWrapper(briefcaseFormFile, readFile(briefcaseFormFile));
        } else {
            // get the current existing definition...
            String existingXml = readFile(briefcaseFormFile);
            existingDefn = new JavaRosaParserWrapper(briefcaseFormFile, existingXml);
            String existingTitle = existingDefn.getFormName();
            // compare the two
            DifferenceResult result;
            if (badForm) {
                // newDefn is considered identical to what we have locally...
                result = DifferenceResult.XFORMS_IDENTICAL;
            } else {
                result = JavaRosaParserWrapper.compareXml(newDefn, existingXml, existingTitle, true);
            }
            if (result == DifferenceResult.XFORMS_DIFFERENT) {
                if (revised.exists()) {
                    result = JavaRosaParserWrapper.compareXml(newDefn, revisedXml, revisedDefn.getFormName(), true);
                    if (result == DifferenceResult.XFORMS_DIFFERENT) {
                        throw new BadFormDefinition("Form definitions are incompatible.");
                    } else if (result != DifferenceResult.XFORMS_EARLIER_VERSION && result != DifferenceResult.XFORMS_MISSING_VERSION && result != DifferenceResult.XFORMS_IDENTICAL) {
                        if (copyFile) {
                            try {
                                FileUtils.copyFile(tmpFormFile, revised);
                            } catch (IOException e) {
                                String msg = "Unable to overwrite the '.revised' form definition file in briefcase storage";
                                log.error(msg, e);
                                throw new BadFormDefinition(msg);
                            }
                        } else {
                            if (!tmpFormFile.renameTo(revised)) {
                                // if cannot rename, try to copy instead (and mark for deletion)
                                try {
                                    FileUtils.copyFile(tmpFormFile, revised);
                                    tmpFormFile.deleteOnExit();
                                } catch (IOException e) {
                                    String msg = "Form directory does not contain form (can neither rename nor copy into briefcase directory)";
                                    log.error(msg, e);
                                    throw new BadFormDefinition(msg);
                                }
                            }
                        }
                        needsMediaUpdate = true;
                        // and re-parse the new revised file (since we just updated it...)
                        revisedDefn = new JavaRosaParserWrapper(revised, readFile(revised));
                    } else if (result == DifferenceResult.XFORMS_IDENTICAL) {
                        // confirm that the media is up-to-date when the forms are
                        // identical
                        // allows briefcase to resume a form download when it failed
                        // during
                        // the early form-media-fetch phases.
                        isIdentical = true;
                        needsMediaUpdate = true;
                    }
                } else {
                    throw new BadFormDefinition("Form definitions are incompatible.");
                }
            } else if (result != DifferenceResult.XFORMS_EARLIER_VERSION && result != DifferenceResult.XFORMS_MISSING_VERSION && result != DifferenceResult.XFORMS_IDENTICAL) {
                if (!revised.exists()) {
                    // overwrite everything and re-parse the new file.
                    if (copyFile) {
                        try {
                            FileUtils.copyFile(tmpFormFile, briefcaseFormFile);
                        } catch (IOException e) {
                            String msg = "Unable to overwrite form definition file in briefcase storage";
                            log.error(msg, e);
                            throw new BadFormDefinition(msg);
                        }
                    } else {
                        if (!tmpFormFile.renameTo(briefcaseFormFile)) {
                            // if cannot rename, try to copy instead (and mark for deletion)
                            try {
                                FileUtils.copyFile(tmpFormFile, briefcaseFormFile);
                                tmpFormFile.deleteOnExit();
                            } catch (IOException e) {
                                String msg = "Form directory does not contain form (can neither rename nor copy into briefcase directory)";
                                log.error(msg, e);
                                throw new BadFormDefinition(msg);
                            }
                        }
                    }
                    needsMediaUpdate = true;
                    // and re-parse the new form file (since we just updated it...)
                    existingXml = readFile(briefcaseFormFile);
                    existingDefn = new JavaRosaParserWrapper(briefcaseFormFile, existingXml);
                }
            } else if (result == DifferenceResult.XFORMS_IDENTICAL) {
                // if a revised form exists, we assume the media is up-to-date in that
                // folder. Otherwise, confirm that the media is up-to-date when the
                // forms are identical. This allows briefcase to resume a form
                // download
                // when it failed during the early form-media-fetch phases.
                isIdentical = true;
                needsMediaUpdate = !revised.exists();
            }
        }
    } catch (ODKIncompleteSubmissionData e) {
        throw new BadFormDefinition(e, e.getReason());
    }
    BriefcaseFormDefinition defn;
    if (revised.exists()) {
        defn = new BriefcaseFormDefinition(briefcaseFormDirectory, revisedDefn, revised, needsMediaUpdate);
    } else {
        defn = new BriefcaseFormDefinition(briefcaseFormDirectory, existingDefn, null, needsMediaUpdate);
    }
    if (!isIdentical && needsMediaUpdate) {
        EventBus.publish(new UpdatedBriefcaseFormDefinitionEvent(defn));
    }
    return defn;
}
Also used : DifferenceResult(org.opendatakit.aggregate.parser.BaseFormParserForJavaRosa.DifferenceResult) ODKIncompleteSubmissionData(org.opendatakit.aggregate.exception.ODKIncompleteSubmissionData) IOException(java.io.IOException) BadFormDefinition(org.opendatakit.briefcase.util.BadFormDefinition) JavaRosaParserWrapper(org.opendatakit.briefcase.util.JavaRosaParserWrapper) File(java.io.File)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 ODKIncompleteSubmissionData (org.opendatakit.aggregate.exception.ODKIncompleteSubmissionData)1 DifferenceResult (org.opendatakit.aggregate.parser.BaseFormParserForJavaRosa.DifferenceResult)1 BadFormDefinition (org.opendatakit.briefcase.util.BadFormDefinition)1 JavaRosaParserWrapper (org.opendatakit.briefcase.util.JavaRosaParserWrapper)1