@Override protected boolean accept(FieldType type) { return type instanceof EnumType && ((EnumType) type).getCardinality() == Cardinality.SINGLE; }
public static List<SimpleOperator> forType(FieldType type) { if(type instanceof TextType) { return EQUALITY_OPERATORS; } else if(type instanceof QuantityType) { return QUANTITY_OPERATORS; } else if(type instanceof EnumType) { EnumType enumType = (EnumType) type; if(enumType.getCardinality() == Cardinality.SINGLE) { return EQUALITY_OPERATORS; } else { return INCLUDE_OPERATORS; } } return Collections.emptyList(); }
@Override public void validateInstance(SourceRow row, List<ValidationResult> results) { if (enumType.getCardinality() == Cardinality.SINGLE) { results.add(validateSingleValuedEnum(row)); } else { results.addAll(validateMultiValuedEnum(row)); } }
@Override public boolean updateInstance(SourceRow row, TypedFormRecord instance) { if (enumType.getCardinality() == Cardinality.SINGLE) { return persistSingleValuedEnum(row, instance); } else { return persistMultiValuedEnum(row, instance); } }
@Override public ColumnFormat visitEnum(EnumType enumType) { if(enumType.getCardinality() == Cardinality.SINGLE) { return new SingleEnumFormat(columnId, formula, enumType); } else { return new MultiEnumFormat(columnId, formula, enumType); } }
@Override public CursorObserver<FieldValue> newEnumBuilder(PendingSlot<ColumnView> result, EnumType enumType) { if (enumType.getCardinality() == Cardinality.SINGLE) { return new CompactingEnumColumnBuilder(result, enumType); } else { return new MultiEnumColumnBuilder(result, enumType); } }
@Override public BlockManager visitEnum(EnumType enumType) { if(enumType.getCardinality() == Cardinality.MULTIPLE) { return new MultiEnumBlock(fieldName); } else { return new SingleEnumBlock(fieldName, enumType); } }
public TreeMap<Integer, ImportTarget> getDistanceMap(String sourceLabel) { final TreeMap<Integer, ImportTarget> distanceMap = Maps.newTreeMap(); for (ImportTarget target : importTargets) { String targetLabel = target.getLabel(); if (target.getFormField().getType() instanceof EnumType) { EnumType enumType = (EnumType) target.getFormField().getType(); if (enumType.getCardinality() == Cardinality.MULTIPLE) { int index = targetLabel.indexOf("-"); if (index != -1) { targetLabel = targetLabel.substring(0, index - 1); } } } final int distance = Levenshtein.getLevenshteinDistance(sourceLabel, targetLabel); distanceMap.put(distance, target); } return distanceMap; } }
private static FieldValue parseEnumValue(EnumType type, JsonValue JsonValue) { Set<ResourceId> itemIds = new HashSet<>(); if(JsonValue.isJsonPrimitive()) { itemIds.add(parseEnumId(type, JsonValue.asString())); } else if(JsonValue.isJsonArray()) { for (JsonValue element : JsonValue.values()) { itemIds.add(parseEnumId(type, element.asString())); } } if(type.getCardinality() == Cardinality.SINGLE && itemIds.size() > 1) { throw new InvalidUpdateException("Field with SINGLE enum type has multiple values."); } return new EnumValue(itemIds); }
@Override public FieldImporter createImporter(FormTree.Node node, Map<TargetSiteId, ColumnAccessor> mappings, ImportModel model) { EnumType type = (EnumType) node.getType(); List<ColumnAccessor> sourceColumns = Lists.newArrayList(); if (type.getCardinality() == Cardinality.SINGLE) { sourceColumns.add(mappings.get(VALUE)); } else { for (EnumItem item : type.getValues()) { ColumnAccessor accessor = mappings.get(new TargetSiteId(item.getId().asString())); if (accessor == null) { accessor = new EmptyColumn(item.getLabel()); } sourceColumns.add(accessor); } } return new EnumFieldImporter(sourceColumns, getImportSites(node), type); }
private FormFieldWidget<EnumValue> createWidget(EnumType enumType, final ValueUpdater<EnumValue> valueUpdater, FieldWidgetMode fieldWidgetMode) { // Multiple selection should always use checkboxes. if(enumType.getCardinality() == Cardinality.MULTIPLE) { return new EnumCheckboxWidget(enumType, valueUpdater, fieldWidgetMode); } EnumType.Presentation presentation; if(fieldWidgetMode == FieldWidgetMode.NORMAL) { // Only apply presentation choices in data entry mode presentation = enumType.getEffectivePresentation(); } else { presentation = EnumType.Presentation.RADIO_BUTTON; } if(presentation == EnumType.Presentation.RADIO_BUTTON) { return new EnumCheckboxWidget(enumType, valueUpdater, fieldWidgetMode); } else { return new EnumDropDownWidget(enumType, valueUpdater); } }
public EffectiveMapping(FormTree formTree, int index, DimensionModel model, DimensionMapping mapping) { this.index = index; this.model = model; this.mapping = mapping; if(this.mapping != null) { this.formula = new ParsedFormula(formTree, mapping.getFormula()); if(this.formula.isValid()) { if(this.formula.getResultType() instanceof EnumType) { EnumType type = (EnumType) this.formula.getResultType(); if(type.getCardinality() == Cardinality.MULTIPLE) { multiValued = true; } } } } }
@Override public FieldWidget visitEnum(EnumType enumType) { if (enumType.getCardinality() == Cardinality.SINGLE) { // If the field is optional, then ALWAYS use a drop down widget whose value can be cleared if (!field.isRequired()) { return new DropDownEnumWidget(field, enumType, updater); } else if (enumType.getEffectivePresentation() == EnumType.Presentation.RADIO_BUTTON) { return new RadioGroupWidget(enumType, updater); } else { return new DropDownEnumWidget(field, enumType, updater); } } else { return new CheckBoxGroupWidget(enumType, updater); } }
public static Predicate<FormField> dimensionFilter() { return field -> { if (referenceFilter().test(field)) { return false; } if (measureFilter().test(field)) { return false; } if (field.getType() instanceof SubFormReferenceType) { return false; } if (field.getType() instanceof EnumType) { EnumType enumType = (EnumType) field.getType(); return enumType.getCardinality() == Cardinality.SINGLE; } if (field.getType() instanceof AttachmentType) { return false; } if (field.getType() instanceof NarrativeType) { return false; } if (field.getType() instanceof GeoPointType || field.getType() instanceof GeoAreaType) { return false; } return true; }; }
@Test public void deserializationEnumTypeNoChoices() { EnumType type = new EnumType(Cardinality.SINGLE, EnumType.Presentation.AUTOMATIC, Collections.<EnumItem>emptyList()); JsonValue jsonObject = type.getParametersAsJson(); EnumType reType = EnumType.TYPE_CLASS.deserializeType(jsonObject); assertThat(reType.getCardinality(), equalTo(Cardinality.SINGLE)); assertThat(reType.getPresentation(), equalTo(EnumType.Presentation.AUTOMATIC)); assertThat(reType.getValues(), hasSize(0)); } }
public EnumDropDownWidget(EnumType enumType, final ValueUpdater<EnumValue> valueUpdater) { this.enumType = enumType; this.valueUpdater = valueUpdater; dropBox = new ListBox(enumType.getCardinality() == Cardinality.MULTIPLE); dropBox.addStyleName("form-control"); // Empty value dropBox.addItem(""); for (EnumItem enumItem : enumType.getValues()) { dropBox.addItem(enumItem.getLabel()); } dropBox.addChangeHandler(new ChangeHandler() { @Override public void onChange(ChangeEvent event) { fireValueChanged(); } }); }
@Override public List<ImportTarget> getImportSites(FormTree.Node node) { EnumType type = (EnumType) node.getType(); List<ImportTarget> result = Lists.newArrayList(); if (type.getCardinality() == Cardinality.SINGLE) { result.add(new ImportTarget(node.getField(), VALUE, node.getField().getLabel(), node.getDefiningFormClass().getId())); } else { for (EnumItem item : type.getValues()) { result.add(new ImportTarget(node.getField(), new TargetSiteId(item.getId().asString()), label(item.getLabel(), node.getField().getLabel()), node.getDefiningFormClass().getId())); } } return result; }
private CheckBox createControl(EnumItem instance) { CheckBox checkBox; if (enumType.getCardinality() == Cardinality.SINGLE) { checkBox = new org.activityinfo.ui.client.widget.RadioButton(groupName, label(instance.getLabel())); } else { checkBox = new org.activityinfo.ui.client.widget.CheckBox(label(instance.getLabel())); } checkBox.setFormValue(instance.getId().asString()); checkBox.getElement().setAttribute("data-id", instance.getId().asString()); controls.add(checkBox); return checkBox; }
@Test public void deserializationEnumTypeNoChoices() { EnumType type = new EnumType(Cardinality.SINGLE, EnumType.Presentation.AUTOMATIC, Collections.<EnumItem>emptyList()); JsonValue jsonObject = type.getParametersAsJson(); EmbeddedEntity entity = FormConverter.toEmbeddedEntity(jsonObject); JsonValue fromEntity = FormConverter.fromEmbeddedEntity(entity); EnumType reType = EnumType.TYPE_CLASS.deserializeType(fromEntity); assertThat(reType.getCardinality(), equalTo(Cardinality.SINGLE)); assertThat(reType.getPresentation(), equalTo(EnumType.Presentation.AUTOMATIC)); assertThat(reType.getValues(), hasSize(0)); }
private Stream<Pair<String,DimensionMapping>> extractFormDimensions(FormClass form) { return form.getFields().stream() .filter(field -> { if (field.getType() instanceof QuantityType) { return false; } if (field.getType() instanceof EnumType) { EnumType enumType = (EnumType) field.getType(); return enumType.getCardinality() == Cardinality.SINGLE; } if (field.getType() instanceof AttachmentType) { return false; } return true; }) .map(dimensionField -> Pair.newPair( dimensionField.getLabel().toLowerCase().trim(), new DimensionMapping(form.getId(), dimensionField.getId()))); }