private void transferAllStandardFields(
     Bulletin bulletinLoadedFromXForms, FieldSpecCollection standardFieldSpecs) {
   for (int index = 0; index < standardFieldSpecs.size(); ++index) {
     FieldSpec standardField = standardFieldSpecs.get(index);
     String tag = standardField.getTag();
     String originalValueToTransfer = bulletinToLoadFrom.get(tag);
     bulletinLoadedFromXForms.set(tag, originalValueToTransfer);
   }
 }
Beispiel #2
0
  public MartusField getField(String fieldTag) {
    PoolOfReusableChoicesLists noRelevantReusableChoices = PoolOfReusableChoicesLists.EMPTY_POOL;
    if (fieldTag.equals(PSEUDOFIELD_LOCAL_ID)) {
      MartusField localIdField =
          new MartusField(
              FieldSpec.createStandardField(fieldTag, new FieldTypeNormal()),
              noRelevantReusableChoices);
      localIdField.setData(getLocalId());
      return localIdField;
    }

    if (fieldTag.equals(PSEUDOFIELD_LAST_SAVED_DATE)) {
      MartusField lastSavedDateField =
          new MartusField(
              FieldSpec.createStandardField(fieldTag, new FieldTypeDate()),
              noRelevantReusableChoices);
      lastSavedDateField.setData(getLastSavedDate());
      return lastSavedDateField;
    }

    if (fieldTag.equals(Bulletin.TAGLASTSAVED)
        || fieldTag.equals(Bulletin.PSEUDOFIELD_LAST_SAVED_TIMESTAMP)) {
      MartusField lastSavedTimestampField =
          new MartusField(
              FieldSpec.createStandardField(fieldTag, new FieldTypeNormal()),
              noRelevantReusableChoices);
      lastSavedTimestampField.setData(Long.toString(getLastSavedTime()));
      return lastSavedTimestampField;
    }

    // FIXME: Rename TAGSTATUS to PSEUDOFIELD_STATUS (globally)
    if (fieldTag.equals(TAGSTATUS)) {
      MartusField statusField =
          new MartusField(
              FieldSpec.createStandardField(fieldTag, new FieldTypeNormal()),
              noRelevantReusableChoices);
      statusField.setData(getStatus());
      return statusField;
    }

    if (fieldTag.equals(PSEUDOFIELD_ALL_PRIVATE)) {
      MartusField allPrivateField =
          new MartusField(
              FieldSpec.createStandardField(fieldTag, new FieldTypeBoolean()),
              noRelevantReusableChoices);
      if (isAllPrivate()) allPrivateField.setData(FieldSpec.TRUESTRING);
      else allPrivateField.setData(FieldSpec.FALSESTRING);
      return allPrivateField;
    }

    if (fieldTag.equals(BulletinConstants.TAGWASSENT)
        || fieldTag.equals(Bulletin.PSEUDOFIELD_WAS_SENT))
      throw new RuntimeException("Bulletin doesn't know if it was sent or not");

    if (isFieldInPublicSection(fieldTag)) return fieldData.getField(fieldTag);
    return getPrivateFieldDataPacket().getField(fieldTag);
  }
  private void sortAllChoicesWithinDropdowns() {
    for (int i = 0; i < allChoices.size(); ++i) {
      SearchableFieldChoiceItem choiceItem = (SearchableFieldChoiceItem) allChoices.get(i);
      FieldSpec spec = choiceItem.getSpec();
      if (!spec.getType().isDropdown()) continue;

      DropDownFieldSpec dropdownSpec = (DropDownFieldSpec) spec;
      dropdownSpec.sortChoicesByLabel();
    }
  }
  private SearchFieldTreeNode[] getIndexesOfReusableDropdownChildren(SearchFieldTreeNode parent) {
    Vector reusableDropdownNodes = new Vector();
    for (int i = 0; i < parent.getChildCount(); ++i) {
      SearchFieldTreeNode child = (SearchFieldTreeNode) parent.getChildAt(i);
      if (!child.isSearchableFieldChoiceItemNode()) continue;
      SearchableFieldChoiceItem choice = child.getChoiceItem();
      FieldSpec spec = choice.getSpec();
      if (spec.getReusableChoicesCodes().length == 0) continue;
      reusableDropdownNodes.add(child);
    }

    return (SearchFieldTreeNode[]) reusableDropdownNodes.toArray(new SearchFieldTreeNode[0]);
  }
  private boolean shouldAddFieldSpec(FieldSpec spec) {
    String tag = spec.getTag();
    if (tag.equals(Bulletin.TAGAUTHOR)) return true;
    if (tag.equals(Bulletin.TAGLANGUAGE)) return true;
    if (tag.equals(Bulletin.TAGENTRYDATE)) return true;
    if (tag.equals(Bulletin.TAGTITLE)) return true;

    String data = bulletinToLoadFrom.get(spec.getTag());
    if (tag.equals(Bulletin.TAGEVENTDATE))
      return !data.equals(MartusFlexidate.toStoredDateFormat(MultiCalendar.UNKNOWN));

    return !data.isEmpty();
  }
 private void differentiateChildNodes(SearchFieldTreeNode parent) {
   SearchFieldTreeNode[] needDifferentiation = getIndexesOfReusableDropdownChildren(parent);
   for (int i = 0; i < needDifferentiation.length; ++i) {
     SearchFieldTreeNode child = needDifferentiation[i];
     SearchableFieldChoiceItem choice = child.getChoiceItem();
     FieldSpec spec = choice.getSpec();
     String reusableListLabels = "";
     for (int level = 0; level < spec.getReusableChoicesCodes().length; ++level) {
       if (level > 0) reusableListLabels += ", ";
       reusableListLabels += spec.getReusableChoicesCodes()[level];
     }
     child.setLabel(child.toString() + " (" + reusableListLabels + ")");
   }
 }
  private FieldSpecCollection recursivelyConvertXFormsFieldsToFieldSpecs(
      FormEntryController formEntryController, List<IFormElement> children, boolean inGroup)
      throws Exception {
    FieldSpecCollection fieldsFromXForms = new FieldSpecCollection();
    for (IFormElement child : children) {
      if (child instanceof GroupDef) {
        GroupDef groupDef = (GroupDef) child;
        List<IFormElement> groupChildren = groupDef.getChildren();
        FieldSpecCollection gridChildrenFieldSpecs =
            recursivelyConvertXFormsFieldsToFieldSpecs(formEntryController, groupChildren, true);
        if (isRepeatGroup(groupDef)) {
          GridFieldSpec gridSpec = new GridFieldSpec();
          TreeReference thisTreeReference = (TreeReference) groupDef.getBind().getReference();
          gridSpec.setTag(createGridTag(thisTreeReference));
          gridSpec.addColumns(gridChildrenFieldSpecs);
          gridSpec.setLabel(getNonNullLabel(groupDef));
          String sectionTag = createSectionTag(thisTreeReference.toString());
          fieldsFromXForms.add(
              FieldSpec.createCustomField(
                  sectionTag, getNonNullLabel(groupDef), new FieldTypeSectionStart()));
          fieldsFromXForms.add(gridSpec);
        } else {
          String sectionTag = createSectionTag(groupDef);
          fieldsFromXForms.add(
              FieldSpec.createCustomField(
                  sectionTag, getNonNullLabel(groupDef), new FieldTypeSectionStart()));
          fieldsFromXForms.addAll(gridChildrenFieldSpecs);
        }
        inGroup = false;
      }

      if (child instanceof QuestionDef) {
        if (!inGroup) {
          ++unGroupedUniqueId;
          String sectionTag = X_FORM_UN_GROUPED_BASE_TAG + unGroupedUniqueId;
          fieldsFromXForms.add(
              FieldSpec.createCustomField(
                  sectionTag, UN_GROUPED_SECTION_LABEL, new FieldTypeSectionStart()));
          inGroup = true;
        }
        QuestionDef questionDef = (QuestionDef) child;
        FormEntryPrompt questionPrompt =
            findQuestion(formEntryController, (TreeReference) questionDef.getBind().getReference());
        FieldSpec fieldSpec = convertToFieldSpec(questionPrompt);
        if (fieldSpec != null) fieldsFromXForms.add(fieldSpec);
      }
    }

    return fieldsFromXForms;
  }
  public static boolean areDropDownChoicesMergeable(
      SearchableFieldChoiceItem choice1, SearchableFieldChoiceItem choice2) {
    FieldSpec rawSpec1 = choice1.getSpec();
    FieldSpec rawSpec2 = choice2.getSpec();
    if (!rawSpec1.getType().isDropdown()) return false;
    if (!rawSpec2.getType().isDropdown()) return false;
    if (!rawSpec1.getTag().equals(rawSpec2.getTag())) return false;
    if (!rawSpec1.getLabel().equals(rawSpec2.getLabel())) return false;

    DropDownFieldSpec spec1 = (DropDownFieldSpec) rawSpec1;
    DropDownFieldSpec spec2 = (DropDownFieldSpec) rawSpec2;
    return Arrays.equals(spec1.getReusableChoicesCodes(), spec2.getReusableChoicesCodes());
  }
  private FieldSpec[] getSampleSpecs() {
    GridFieldSpec gridSpec = new GridFieldSpec();
    gridSpec.setTag("grid");

    return new FieldSpec[] {
      FieldSpec.createStandardField("date", new FieldTypeDate()),
      FieldSpec.createStandardField("text", new FieldTypeNormal()),
      FieldSpec.createStandardField("multi", new FieldTypeMultiline()),
      FieldSpec.createStandardField("range", new FieldTypeDateRange()),
      FieldSpec.createStandardField("bool", new FieldTypeBoolean()),
      FieldSpec.createStandardField("language", new FieldTypeLanguage()),
      gridSpec,
      LegacyCustomFields.createFromLegacy("custom,Custom <label>"),
    };
  }
  private FieldSpec convertToFieldSpec(FormEntryPrompt questionPrompt) {
    QuestionDef question = questionPrompt.getQuestion();
    final int dataType = questionPrompt.getDataType();
    TreeReference reference = (TreeReference) question.getBind().getReference();
    String tag = reference.getNameLast();
    String questionLabel = questionPrompt.getQuestion().getLabelInnerText();

    if (questionPrompt.isReadOnly())
      return FieldSpec.createCustomField(tag, questionLabel, new FieldTypeMessage());

    if (isNormalFieldType(dataType))
      return FieldSpec.createCustomField(tag, questionLabel, new FieldTypeNormal());

    if (dataType == Constants.DATATYPE_DATE)
      return FieldSpec.createCustomField(tag, questionLabel, new FieldTypeDate());

    if (shouldTreatSingleItemChoiceListAsBooleanField(dataType, question))
      return FieldSpec.createCustomField(tag, questionLabel, new FieldTypeBoolean());

    if (dataType == Constants.DATATYPE_CHOICE) {
      Vector<ChoiceItem> convertedChoices = new Vector<ChoiceItem>();
      List<SelectChoice> choicesToConvert = question.getChoices();
      for (SelectChoice choiceToConvert : choicesToConvert) {
        // String choiceItemCode = choiceToConvert.getValue();
        String choiceItemLabel = choiceToConvert.getLabelInnerText();
        // Martus doesn't keep Code's when exporting so use Label twice instead
        convertedChoices.add(new ChoiceItem(choiceItemLabel, choiceItemLabel));
      }

      FieldSpec fieldSpec = new CustomDropDownFieldSpec(convertedChoices);
      fieldSpec.setTag(tag);
      fieldSpec.setLabel(questionLabel);
      return fieldSpec;
    }
    MartusLogger.log(
        "Warning: BulletinFromXFormsLoader.convertToFieldSpec unknown Field Type:"
            + String.valueOf(dataType));
    return null;
  }
  public void testCustomField() throws Exception {
    FieldSpec[] specs =
        new FieldSpec[] {
          FieldSpec.createStandardField("date", new FieldTypeDate()),
          FieldSpec.createStandardField("text", new FieldTypeNormal()),
          FieldSpec.createStandardField("multi", new FieldTypeMultiline()),
          FieldSpec.createStandardField("range", new FieldTypeDateRange()),
          FieldSpec.createStandardField("bool", new FieldTypeBoolean()),
          FieldSpec.createStandardField("language", new FieldTypeLanguage()),
          LegacyCustomFields.createFromLegacy("custom,Custom <label>"),
        };

    MockMartusApp app = MockMartusApp.create();
    app.getLocalization().setCurrentLanguageCode(MiniLocalization.ENGLISH);
    Bulletin b =
        new Bulletin(app.getSecurity(), new FieldSpecCollection(specs), new FieldSpecCollection());
    String sampleCustomData = "Robert Plant";
    b.set("custom", sampleCustomData);
    b.setAllPrivate(false);
    app.saveBulletin(b, app.getFolderDraftOutbox());

    SortableBulletinList list =
        new SortableBulletinList(app.getLocalization(), new MiniFieldSpec[0]);
    list.add(b);
    ReportFormat rf = new ReportFormat();
    rf.setDetailSection("$bulletin.field('custom')");
    ReportOutput result = new ReportOutput();
    RunReportOptions options = new RunReportOptions();
    rr.runReport(
        rf,
        app.getStore().getDatabase(),
        list,
        result,
        options,
        PoolOfReusableChoicesLists.EMPTY_POOL);
    result.close();

    assertEquals(sampleCustomData, result.getPageText(0));
  }
  public void testPageReport() throws Exception {
    MockMartusApp app = MockMartusApp.create();
    FieldSpec[] topFields = {
      FieldSpec.createStandardField(Bulletin.TAGAUTHOR, new FieldTypeNormal()),
      FieldSpec.createCustomField("tag2", "Label 2", new FieldTypeDate()),
    };
    Bulletin b =
        new Bulletin(
            app.getSecurity(),
            new FieldSpecCollection(topFields),
            StandardFieldSpecs.getDefaultBottomSectionFieldSpecs());
    b.set(topFields[0].getTag(), "First");
    b.set(topFields[1].getTag(), "2005-04-07");
    b.set(Bulletin.TAGPRIVATEINFO, "Secret");
    app.saveBulletin(b, app.getFolderDraftOutbox());

    Bulletin b2 =
        new Bulletin(
            app.getSecurity(),
            new FieldSpecCollection(topFields),
            StandardFieldSpecs.getDefaultBottomSectionFieldSpecs());
    b2.set(topFields[0].getTag(), "Second");
    b2.set(topFields[1].getTag(), "2003-03-29");
    b2.set(Bulletin.TAGPRIVATEINFO, "Another secret");
    app.saveBulletin(b2, app.getFolderDraftOutbox());

    ReportFormat rf = new ReportFormat();
    rf.setBulletinPerPage(true);
    rf.setHeaderSection("Header\n");
    rf.setFooterSection("Footer\n");
    rf.setFakePageBreakSection("----\n");
    rf.setDetailSection(
        "TOP:\n"
            + "#foreach($field in $bulletin.getTopFields())\n"
            + "$field.getLocalizedLabel($localization) $field.html($localization)\n"
            + "#end\n"
            + "BOTTOM:\n"
            + "#foreach($field in $bulletin.getBottomFields())\n"
            + "$field.getLocalizedLabel($localization) $field.html($localization)\n"
            + "#end\n"
            + "");
    String expected0 =
        "Header\n"
            + "TOP:\n"
            + "<field:author> First\n"
            + "Label 2 04/07/2005\n"
            + "BOTTOM:\n"
            + "<field:privateinfo> Secret\n"
            + "Footer\n";
    String expected1 =
        "Header\n"
            + "TOP:\n"
            + "<field:author> Second\n"
            + "Label 2 03/29/2003\n"
            + "BOTTOM:\n"
            + "<field:privateinfo> Another secret\n"
            + "Footer\n";

    RunReportOptions options = new RunReportOptions();
    options.includePrivate = true;
    ReportOutput result = runReportOnAppData(rf, app, options);
    assertEquals("Wrong page report output?", expected0, result.getPageText(0));
    assertEquals("Wrong page report output?", expected1, result.getPageText(1));
    assertEquals("Didn't set fake page break?", "----\n", result.getFakePageBreak());
  }
 public void registerField(FieldSpec spec, UiField field) {
   fieldsByTag.put(spec.getTag(), field);
   if (spec.getType().isGrid()) addGrid(spec.getTag(), (UiGrid) field);
 }