private FormEntryController importXFormsData(
      String xFormsModelXmlAsString, String xFormsInstance) {
    InputStream xFormsModelInputStream =
        new ByteArrayInputStream(xFormsModelXmlAsString.getBytes(StandardCharsets.UTF_8));
    FormDef formDef = XFormUtils.getFormFromInputStream(xFormsModelInputStream);
    FormEntryModel formEntryModel = new FormEntryModel(formDef);
    FormEntryController formEntryController = new FormEntryController(formEntryModel);

    byte[] xFormsInstanceBytes = xFormsInstance.getBytes(StandardCharsets.UTF_8);
    TreeElement modelRootElement =
        formEntryController.getModel().getForm().getInstance().getRoot().deepCopy(true);
    TreeElement instanceRootElement =
        XFormParser.restoreDataModel(xFormsInstanceBytes, null).getRoot();
    String instanceRootName = instanceRootElement.getName();
    String modelRootName = modelRootElement.getName();
    if (!instanceRootName.equals(modelRootName)) return null;

    if (instanceRootElement.getMult() != TreeReference.DEFAULT_MUTLIPLICITY) return null;

    populateDataModel(modelRootElement);
    modelRootElement.populate(instanceRootElement, formEntryController.getModel().getForm());
    populateFormEntryControllerModel(formEntryController, modelRootElement);
    fixLanguageIusses(formEntryController);

    return formEntryController;
  }
 private void fixLanguageIusses(FormEntryController formEntryController) {
   // NOTE: this comment is from Collect's java rosa seference
   // fix any language issues
   // : http://bitbucket.org/javarosa/main/issue/5/itext-n-appearing-in-restored-instances
   if (formEntryController.getModel().getLanguages() != null)
     formEntryController
         .getModel()
         .getForm()
         .localeChanged(
             formEntryController.getModel().getLanguage(),
             formEntryController.getModel().getForm().getLocalizer());
 }
 protected FieldSpecCollection createFieldSpecsFromXForms(FormEntryController formEntryController)
     throws Exception {
   FormDef formDef = formEntryController.getModel().getForm();
   List<IFormElement> children = formDef.getChildren();
   unGroupedUniqueId = 0;
   return recursivelyConvertXFormsFieldsToFieldSpecs(formEntryController, children, false);
 }
  private FormEntryPrompt findQuestion(
      FormEntryController formEntryContorller, TreeReference treeReferenceToMatch) {
    formEntryContorller.jumpToIndex(FormIndex.createBeginningOfFormIndex());
    int event;
    while ((event = formEntryContorller.stepToNextEvent())
        != FormEntryController.EVENT_END_OF_FORM) {
      if (event == FormEntryController.EVENT_QUESTION) {
        FormEntryPrompt questionPrompt = formEntryContorller.getModel().getQuestionPrompt();
        QuestionDef thisQuestionDef = questionPrompt.getQuestion();
        TreeReference thisTreeReference = (TreeReference) thisQuestionDef.getBind().getReference();
        if (thisTreeReference.equals(treeReferenceToMatch)) return questionPrompt;
      }
    }

    return null;
  }
  private Bulletin createBulletin(
      MartusCrypto signatureGenerator,
      FormEntryController formEntryController,
      FieldSpecCollection fieldsFromXForms)
      throws Exception {
    FieldSpecCollection allFields = new FieldSpecCollection();
    FieldSpecCollection nonEmptytopSectionFieldSpecsFrom = getNonEmptyTopFieldSpecs();
    allFields.addAll(nonEmptytopSectionFieldSpecsFrom);
    allFields.addAll(fieldsFromXForms);

    Bulletin bulletinLoadedFromXForms =
        new Bulletin(signatureGenerator, allFields, new FieldSpecCollection());
    transferAllStandardFields(bulletinLoadedFromXForms, nonEmptytopSectionFieldSpecsFrom);

    resetFormEntryControllerIndex(formEntryController);
    int event;
    while ((event = formEntryController.stepToNextEvent())
        != FormEntryController.EVENT_END_OF_FORM) {
      if (event == FormEntryController.EVENT_REPEAT)
        convertXFormRepeatToGridData(
            formEntryController, fieldsFromXForms, bulletinLoadedFromXForms);

      if (event != FormEntryController.EVENT_QUESTION) continue;

      FormEntryPrompt questionPrompt = formEntryController.getModel().getQuestionPrompt();
      IAnswerData answer = questionPrompt.getAnswerValue();
      if (answer == null) continue;

      QuestionDef question = questionPrompt.getQuestion();
      final int dataType = questionPrompt.getDataType();
      TreeReference reference = (TreeReference) question.getBind().getReference();
      FieldDataPacket fieldDataPacket = bulletinLoadedFromXForms.getFieldDataPacket();
      String xFormsFieldTag = reference.getNameLast();
      String answerAsString = getMartusAnswerStringFromQuestion(answer, question, dataType);
      fieldDataPacket.set(xFormsFieldTag, answerAsString);
    }

    copyPrivateAttachmentProxies(bulletinLoadedFromXForms);
    copyPublicAttachmentProxies(bulletinLoadedFromXForms);

    return bulletinLoadedFromXForms;
  }
  private void fillGridRow(
      FormEntryController formEntryController, GridRow gridRow, int columnIndex) throws Exception {
    FormEntryPrompt currentQuestionPrompt = formEntryController.getModel().getQuestionPrompt();
    IAnswerData currentAnswer = currentQuestionPrompt.getAnswerValue();
    if (currentAnswer == null) return;

    final int dataType = currentQuestionPrompt.getDataType();
    QuestionDef questionDef = currentQuestionPrompt.getQuestion();
    String answerAsString = getMartusAnswerStringFromQuestion(currentAnswer, questionDef, dataType);
    gridRow.setCellText(columnIndex, answerAsString);
  }
  private void handleRepeat(
      FormEntryController formEntryController,
      FieldSpecCollection fieldsFromXForms,
      GridData gridData)
      throws Exception {
    int event = returnToPreivousEventToAvoidConsumingEvent(formEntryController);
    while ((event = formEntryController.stepToNextEvent())
        != FormEntryController.EVENT_END_OF_FORM) {
      if (event == FormEntryController.EVENT_REPEAT) {
        FormEntryModel formModel = formEntryController.getModel();
        IFormElement element = formModel.getForm().getChild(formModel.getFormIndex());
        if (element instanceof GroupDef) {
          GridRow gridRow =
              createGridRowWithData(
                  formEntryController,
                  gridData.getSpec(),
                  fieldsFromXForms.getAllReusableChoiceLists());
          gridData.addRow(gridRow);
        }
      }

      if (hasNoMoreUserFilledRepeats(event)) return;
    }
  }
  private void convertXFormRepeatToGridData(
      FormEntryController formEntryController,
      FieldSpecCollection fieldsFromXForms,
      Bulletin bulletinLoadedFromXForms)
      throws Exception {
    FormEntryModel formModel = formEntryController.getModel();
    IFormElement repeatElement = formModel.getForm().getChild(formModel.getFormIndex());
    GroupDef castedRepeatDef = (GroupDef) repeatElement;
    TreeReference repeatTreeReference = (TreeReference) castedRepeatDef.getBind().getReference();
    String gridTag = createGridTag(repeatTreeReference);
    GridFieldSpec foundGridFieldSpec = (GridFieldSpec) fieldsFromXForms.findBytag(gridTag);
    foundGridFieldSpec.setLabel(getNonNullLabel(castedRepeatDef));
    PoolOfReusableChoicesLists allReusableChoiceLists =
        fieldsFromXForms.getAllReusableChoiceLists();
    GridData gridData = new GridData(foundGridFieldSpec, allReusableChoiceLists);
    handleRepeat(formEntryController, fieldsFromXForms, gridData);

    bulletinLoadedFromXForms.set(gridTag, gridData.getXmlRepresentation());
  }
  private GridRow createGridRowWithData(
      FormEntryController formEntryController,
      GridFieldSpec gridFieldSpec,
      PoolOfReusableChoicesLists allReusableChoiceLists)
      throws Exception {
    GridRow gridRow = new GridRow(gridFieldSpec, allReusableChoiceLists);
    int columnIndex = 0;
    int event;
    while ((event = formEntryController.stepToNextEvent())
        != FormEntryController.EVENT_END_OF_FORM) {
      if (event == FormEntryController.EVENT_REPEAT || hasNoMoreUserFilledRepeats(event)) {
        returnToPreivousEventToAvoidConsumingEvent(formEntryController);
        return gridRow;
      }

      if (event == FormEntryController.EVENT_QUESTION) {
        fillGridRow(formEntryController, gridRow, columnIndex);
        ++columnIndex;
      }
    }

    return gridRow;
  }
예제 #10
0
    public boolean importData(File instanceFile, FormEntryController fec) {
        publishProgress(Collect.getInstance().getString(R.string.survey_loading_reading_data_message));

        // 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) {
            Log.e(t, "Saved form instance does not match template form definition");
            return false;
        } else {
            // 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);

            // fix any language issues
            // :
            // 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());
            }

            return true;

        }
    }
 private void populateFormEntryControllerModel(
     FormEntryController formEntryController, TreeElement modelRoot) {
   formEntryController.getModel().getForm().getInstance().setRoot(modelRoot);
 }
 private void resetFormEntryControllerIndex(FormEntryController formEntryController) {
   while (formEntryController.stepToPreviousEvent() != FormEntryController.EVENT_BEGINNING_OF_FORM)
     ;
 }
 private int returnToPreivousEventToAvoidConsumingEvent(FormEntryController formEntryController) {
   return formEntryController.stepToPreviousEvent();
 }