public FieldValue get(ResourceId fieldId, FieldType fieldType) { FieldValue value = fieldMap.get(fieldId); if(value != null && value.getTypeClass() == fieldType.getTypeClass()) { return value; } else { return null; } }
/** * Returns the value of {@code fieldId} if the value is present and of * the specified {@code typeClass}, or {@code null} otherwise. */ public FieldValue get(ResourceId fieldId, FieldTypeClass typeClass) { FieldValue value = get(fieldId); if(value != null && value.getTypeClass() == typeClass) { return value; } else { return null; } }
@Override public FieldType resolveType(EvalContext context) { return value.getTypeClass().createType(); } }
public static String toString(FieldValue value) { if(value instanceof HasStringValue) { return ((HasStringValue) value).asString(); } throw new InvalidTypeException("Cannot cast field value of type " + value.getTypeClass() + " to string"); }
@Override public FieldValue apply(List<FieldValue> arguments) { checkArity(arguments, numArgs); String substring = substring(text(arguments), startPosition(arguments), numChars(arguments)); if (arguments.get(0).getTypeClass().equals(FieldTypeClass.NARRATIVE)) { return NarrativeValue.valueOf(substring); } else { return TextValue.valueOf(substring); } }
private static FieldValue validateType(FormField field, FieldValue updatedValue) { Preconditions.checkNotNull(field); if(updatedValue != null) { if ( !field.getType().isUpdatable()) { throw new InvalidUpdateException( format("Field %s ('%s') is a field of type '%s' and its value cannot be set. Found %s", field.getId(), field.getLabel(), field.getType().getTypeClass().getId(), updatedValue)); } if (!field.getType().getTypeClass().equals(updatedValue.getTypeClass())) { throw new InvalidUpdateException( format("Updated value for field %s ('%s') has invalid type. Expected %s, found %s.", field.getId(), field.getLabel(), field.getType().getTypeClass().getId(), updatedValue.getTypeClass().getId())); } } return updatedValue; }
public static ConstantNode valueOf(FieldValue value) { if(value instanceof TextValue) { return new ConstantNode(((TextValue) value).asString()); } else if(value instanceof BooleanFieldValue) { return new ConstantNode(value == BooleanFieldValue.TRUE); } else if(value instanceof Quantity) { return new ConstantNode(value, new QuantityType()); } else if (value instanceof EnumValue) { return new ConstantNode(value, new EnumType()); } else { throw new IllegalArgumentException(value.getTypeClass().getId()); } }
private String computeSerialNumberPrefix( FormClass formClass, SerialNumberType type, TypedFormRecord effectiveRecord) { if(!type.hasPrefix()) { return null; } try { FormEvalContext evalContext = new FormEvalContext(formClass); evalContext.setInstance(effectiveRecord); FormulaNode formula = FormulaParser.parse(type.getPrefixFormula()); FieldValue prefixValue = formula.evaluate(evalContext); if(prefixValue instanceof TextValue) { return ((TextValue) prefixValue).asString(); } else { throw new IllegalStateException("Prefix " + type.getPrefixFormula() + " resolves to type " + prefixValue.getTypeClass().getId()); } } catch (Exception e) { LOGGER.log(Level.SEVERE, "Failed to compute prefix for serial number", e); return null; } }
public Promise<Void> setValue(TypedFormRecord instance) { model.setWorkingRootInstance(instance); List<Promise<Void>> tasks = Lists.newArrayList(); for (FieldContainer container : widgetCreator.getContainers().values()) { FormField field = container.getField(); FieldValue value = model.getWorkingRootInstance().get(field.getId(), field.getType()); if (value != null && value.getTypeClass() == field.getType().getTypeClass()) { tasks.add(container.getFieldWidget().setValue(value)); } else { container.getFieldWidget().clearValue(); } container.setValid(); } return Promise.waitAll(tasks).then(new Function<Void, Void>() { @Override public Void apply(Void input) { relevanceHandler.onValueChange(); // invoke relevance handler once values are set return null; } }); }
private boolean validateField(FieldContainer container) { FormField field = container.getField(); FieldValue value = getCurrentValue(field); if (value != null && value.getTypeClass() != field.getType().getTypeClass()) { value = null; } if (!container.isInputValid()) { return false; } if (!container.getFieldWidget().isValid()) { return false; } Optional<Boolean> validatedBuiltInDates = validateBuiltinDates(container, field); if (validatedBuiltInDates.isPresent()) { return validatedBuiltInDates.get(); } if (field.isRequired() && isEmpty(value) && field.isVisible() && !container.getFieldWidget().isReadOnly()) { // if field is not visible user doesn't have chance to fix it container.setInvalid(I18N.CONSTANTS.requiredFieldMessage()); return false; } else { container.setValid(); return true; } }