Search in sources :

Example 46 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project briefcase by opendatakit.

the class ExportToCsv method processFormDefinition.

private boolean processFormDefinition() {
    TreeElement submission = briefcaseLfd.getSubmissionElement();
    String formName = baseFilename;
    File topLevelCsv = new File(outputDir, safeFilename(formName) + ".csv");
    log.info("Trying to create top level CSV file at {}", topLevelCsv);
    boolean exists = topLevelCsv.exists();
    FileOutputStream os;
    try {
        os = new FileOutputStream(topLevelCsv, !overwrite);
        OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
        fileMap.put(submission, osw);
        // only write headers if overwrite is set, or creating file for the first time
        if (overwrite || !exists) {
            emitString(osw, true, "SubmissionDate");
            emitCsvHeaders(osw, submission, submission, false);
            emitString(osw, false, "KEY");
            if (briefcaseLfd.isFileEncryptedForm()) {
                emitString(osw, false, "isValidated");
            }
            osw.append("\n");
        } else {
            populateRepeatGroupsIntoFileMap(submission, submission);
        }
    } catch (IOException e) {
        String msg = "Unable to create csv file";
        log.error(msg, e);
        EventBus.publish(new ExportProgressEvent(msg, briefcaseLfd));
        for (OutputStreamWriter w : fileMap.values()) {
            try {
                w.close();
            } catch (IOException e1) {
                log.warn("failed to close writer", e1);
            }
        }
        fileMap.clear();
        return false;
    }
    return true;
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) File(java.io.File) ExportProgressEvent(org.opendatakit.briefcase.model.ExportProgressEvent) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

Example 47 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project collect by opendatakit.

the class FormEntryActivity method showView.

/**
 * Displays the View specified by the parameter 'next', animating both the
 * current view and next appropriately given the AnimationType. Also updates
 * the progress bar.
 */
public void showView(View next, AnimationType from) {
    // disable notifications...
    if (inAnimation != null) {
        inAnimation.setAnimationListener(null);
    }
    if (outAnimation != null) {
        outAnimation.setAnimationListener(null);
    }
    // by createView()
    switch(from) {
        case RIGHT:
            inAnimation = AnimationUtils.loadAnimation(this, R.anim.push_left_in);
            outAnimation = AnimationUtils.loadAnimation(this, R.anim.push_left_out);
            // if animation is left or right then it was a swipe, and we want to re-save on
            // entry
            autoSaved = false;
            break;
        case LEFT:
            inAnimation = AnimationUtils.loadAnimation(this, R.anim.push_right_in);
            outAnimation = AnimationUtils.loadAnimation(this, R.anim.push_right_out);
            autoSaved = false;
            break;
        case FADE:
            inAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_in);
            outAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_out);
            break;
    }
    // complete setup for animations...
    inAnimation.setAnimationListener(this);
    outAnimation.setAnimationListener(this);
    if (shouldOverrideAnimations) {
        inAnimation.setDuration(0);
        outAnimation.setDuration(0);
    }
    // drop keyboard before transition...
    if (currentView != null) {
        InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(currentView.getWindowToken(), 0);
    }
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    // adjust which view is in the layout container...
    staleView = currentView;
    currentView = next;
    questionHolder.addView(currentView, lp);
    animationCompletionSet = 0;
    if (staleView != null) {
        // start OutAnimation for transition...
        staleView.startAnimation(outAnimation);
        // and remove the old view (MUST occur after start of animation!!!)
        questionHolder.removeView(staleView);
    } else {
        animationCompletionSet = 2;
    }
    // start InAnimation for transition...
    currentView.startAnimation(inAnimation);
    String logString = "";
    switch(from) {
        case RIGHT:
            logString = "next";
            break;
        case LEFT:
            logString = "previous";
            break;
        case FADE:
            logString = "refresh";
            break;
    }
    Collect.getInstance().getActivityLogger().logInstanceAction(this, "showView", logString);
    FormController formController = getFormController();
    if (formController.getEvent() == FormEntryController.EVENT_QUESTION || formController.getEvent() == FormEntryController.EVENT_GROUP || formController.getEvent() == FormEntryController.EVENT_REPEAT) {
        FormEntryPrompt[] prompts = getFormController().getQuestionPrompts();
        for (FormEntryPrompt p : prompts) {
            List<TreeElement> attrs = p.getBindAttributes();
            for (int i = 0; i < attrs.size(); i++) {
                if (!autoSaved && "saveIncomplete".equals(attrs.get(i).getName())) {
                    saveDataToDisk(false, false, null, false);
                    autoSaved = true;
                }
            }
        }
    }
}
Also used : FormController(org.odk.collect.android.logic.FormController) LayoutParams(android.view.ViewGroup.LayoutParams) FormEntryPrompt(org.javarosa.form.api.FormEntryPrompt) RelativeLayout(android.widget.RelativeLayout) InputMethodManager(android.view.inputmethod.InputMethodManager) FailedConstraint(org.odk.collect.android.logic.FormController.FailedConstraint) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 48 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project collect by opendatakit.

the class FormLoaderTask method importData.

static void importData(File instanceFile, FormEntryController fec) {
    // convert files into a byte array
    byte[] fileBytes = FileUtils.getFileAsBytes(instanceFile);
    // get the root of the saved and template instances
    TreeElement savedRoot = XFormParser.restoreDataModel(fileBytes, null).getRoot();
    TreeElement templateRoot = fec.getModel().getForm().getInstance().getRoot().deepCopy(true);
    // weak check for matching forms
    if (!savedRoot.getName().equals(templateRoot.getName()) || savedRoot.getMult() != 0) {
        Timber.e("Saved form instance does not match template form definition");
        return;
    }
    // populate the data model
    TreeReference tr = TreeReference.rootRef();
    tr.add(templateRoot.getName(), TreeReference.INDEX_UNBOUND);
    // Here we set the Collect's implementation of the IAnswerResolver.
    // We set it back to the default after select choices have been populated.
    XFormParser.setAnswerResolver(new ExternalAnswerResolver());
    templateRoot.populate(savedRoot, fec.getModel().getForm());
    XFormParser.setAnswerResolver(new DefaultAnswerResolver());
    // populated model to current form
    fec.getModel().getForm().getInstance().setRoot(templateRoot);
    // http://bitbucket.org/javarosa/main/issue/5/itext-n-appearing-in-restored-instances
    if (fec.getModel().getLanguages() != null) {
        fec.getModel().getForm().localeChanged(fec.getModel().getLanguage(), fec.getModel().getForm().getLocalizer());
    }
    Timber.i("Done importing data");
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) ExternalAnswerResolver(org.odk.collect.android.external.ExternalAnswerResolver) DefaultAnswerResolver(org.javarosa.core.model.instance.utils.DefaultAnswerResolver) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 49 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project collect by opendatakit.

the class InstanceGoogleSheetsUploader method getAnswers.

private HashMap<String, String> getAnswers(TreeElement element, File instanceFile, String parentKey, String key) throws UploadException {
    HashMap<String, String> answers = new HashMap<>();
    for (TreeElement childElement : getChildElements(element)) {
        String elementTitle = getElementTitle(childElement);
        if (childElement.isRepeatable()) {
            answers.put(elementTitle, getHyperlink(getSheetUrl(getSheetId(elementTitle)), elementTitle));
        } else {
            String answer = childElement.getValue() != null ? childElement.getValue().getDisplayText() : "";
            if (new File(instanceFile.getParentFile() + "/" + answer).isFile()) {
                String mediaHyperlink = uploadMediaFile(instanceFile, answer);
                answers.put(elementTitle, mediaHyperlink);
            } else {
                answers.put(elementTitle, answer);
            }
        }
    }
    if (element.isRepeatable()) {
        answers.put(PARENT_KEY, parentKey);
        answers.put(KEY, key);
    } else if (hasRepeatableGroups(element)) {
        answers.put(KEY, key);
    }
    return answers;
}
Also used : HashMap(java.util.HashMap) File(java.io.File) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

Example 50 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project collect by opendatakit.

the class InstanceGoogleSheetsUploader method getSheetTitles.

private Set<String> getSheetTitles(TreeElement element) {
    Set<String> sheetTitles = new HashSet<>();
    for (int i = 0; i < element.getNumChildren(); i++) {
        TreeElement childElement = element.getChildAt(i);
        if (childElement.isRepeatable()) {
            sheetTitles.add(getElementTitle(childElement));
            sheetTitles.addAll(getSheetTitles(childElement));
        }
    }
    return sheetTitles;
}
Also used : HashSet(java.util.HashSet) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

Aggregations

TreeElement (org.javarosa.core.model.instance.TreeElement)86 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)33 TreeReference (org.javarosa.core.model.instance.TreeReference)28 FormInstance (org.javarosa.core.model.instance.FormInstance)16 ArrayList (java.util.ArrayList)15 Constraint (org.javarosa.core.model.condition.Constraint)11 Test (org.junit.Test)10 Element (org.kxml2.kdom.Element)9 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)8 FormDef (org.javarosa.core.model.FormDef)7 InstanceInitializationFactory (org.javarosa.core.model.instance.InstanceInitializationFactory)7 IOException (java.io.IOException)6 IFormElement (org.javarosa.core.model.IFormElement)6 StringData (org.javarosa.core.model.data.StringData)6 HashMap (java.util.HashMap)5 IAnswerData (org.javarosa.core.model.data.IAnswerData)4 File (java.io.File)3 GroupDef (org.javarosa.core.model.GroupDef)3 IDataReference (org.javarosa.core.model.IDataReference)3 QuestionDef (org.javarosa.core.model.QuestionDef)3