@Override public Result<TYPE> convertToModel(String value, ValueContext context) { try { return Result.ok(type .cast(type.getMethod(this.staticMethodName, String.class) .invoke(null, value))); } catch (InvocationTargetException e) { return Result.error(e.getCause().getMessage()); } catch (Exception e) { return Result.error(e.getMessage()); } }
@Override public Result<Date> convertToModel(String value, ValueContext context) { for (String pattern : new String[] { "yyyy-MM-dd HH:mm:ssZ", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH", "yyyy-MM-dd", "yyyy-MM", "yyyy" }) { try { return Result.ok(new SimpleDateFormat(pattern).parse(value)); } catch (ParseException e) { // not parseable, ignore and try another format } } return Result.error("Could not parse date value: " + value); }
@SuppressWarnings("unchecked") @Override public Result<T> convertToModel(String value, ValueContext context) { if (value == null || value.trim().isEmpty()) { return Result.ok(null); } try { T result = (T) Enum.valueOf(type, value.toUpperCase(Locale.ROOT)); return Result.ok(result); } catch (Exception e) { return Result.error(e.getMessage()); } }
/** * This method is called to handle a non-empty date string from the client * if the client could not parse it as a Date. * * By default, an error result is returned whose error message is * {@link #getParseErrorMessage()}. * * This can be overridden to handle conversions, to return a result with * {@code null} value (equivalent to empty input) or to return a custom * error. * * @param dateString * date string to handle * @return result that contains parsed Date as a value or an error */ protected Result<T> handleUnparsableDateString(String dateString) { return Result.error(getParseErrorMessage()); }
@Override public Result<LocalDate> convertToModel(String value, ValueContext context) { for (String pattern : new String[] { "yyyy-MM-dd", "yyyy-MM", "yyyy" }) { try { Locale effectiveLocale = context.getLocale() .orElse(Locale.ENGLISH); LocalDate date = DateTimeFormatter .ofPattern(pattern, effectiveLocale) .parse(value, LocalDate::from); return Result.ok(date); } catch (DateTimeParseException e) { // not parseable, ignore and try another format } } return Result.error("Could not parse date value: " + value); }
@Override public Result<Date> convertToModel(String value, ValueContext context) { if (value == null) { return Result.ok(null); } // Remove leading and trailing white space value = value.trim(); ParsePosition parsePosition = new ParsePosition(0); Date parsedValue = getFormat(context.getLocale().orElse(null)) .parse(value, parsePosition); if (parsePosition.getIndex() != value.length()) { return Result.error("Could not convert '" + value); } return Result.ok(parsedValue); }
@Override public Result<LocalDateTime> convertToModel(String value, ValueContext context) { for (String pattern : new String[] { "yyyy-MM-dd HH:mm:ssZ", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH", "yyyy-MM-dd", "yyyy-MM", "yyyy" }) { try { Locale effectiveLocale = context.getLocale() .orElse(Locale.ENGLISH); LocalDateTime date = DateTimeFormatter .ofPattern(pattern, effectiveLocale) .parse(value, LocalDateTime::from); return Result.ok(date); } catch (DateTimeParseException ignored) { // not parseable, ignore and try another format } } return Result.error("Could not parse date value: " + value); }
@Override public Result<Resource> convertToModel(String value, ValueContext context) { if (!value.contains("://")) { // assume it'is "file://" protocol, one that is used to access a // file on a given path on the server, this will later be striped // out anyway value = "file://" + value; } String protocol = value.split("://")[0]; try { ResourceConverterByProtocol converter = ResourceConverterByProtocol .valueOf(protocol.toUpperCase(Locale.ROOT)); return Result.ok(converter.parse(value)); } catch (IllegalArgumentException iae) { return Result.error("Unrecognized protocol: " + protocol); } }
/** * Returns a Result representing the result of invoking the given supplier. * If the supplier returns a value, returns a {@code Result.ok} of the * value; if an exception is thrown, returns the message in a * {@code Result.error}. * * @param <R> * the result value type * @param supplier * the supplier to run * @param onError * the function to provide the error message * @return the result of invoking the supplier */ public static <R> Result<R> of(SerializableSupplier<R> supplier, SerializableFunction<Exception, String> onError) { Objects.requireNonNull(supplier, "supplier cannot be null"); Objects.requireNonNull(onError, "onError cannot be null"); try { return ok(supplier.get()); } catch (Exception e) { return error(onError.apply(e)); } }
/** * Creates a new status change event. * <p> * The {@code message} must be {@code null} if the {@code status} is * {@link Status#OK}. * * @param source * field whose status has changed, not {@code null} * @param status * updated status value, not {@code null} * @param result * the related result, may be {@code null} */ @Deprecated public BindingValidationStatus(Binding<?, TARGET> source, Status status, ValidationResult result) { this(result.isError() ? Result.error(result.getErrorMessage()) : Result.ok(null), source); }
.parse(value, parsePosition); if (parsePosition.getIndex() != value.length()) { return Result.error(getErrorMessage(context));
data.length == 2 ? data[1] : null, keyCode, modifiers)); } catch (Exception e) { return Result.error("Invalid shortcut '" + value + "'");
@Override public Result<Boolean> convertToModel(String value, ValueContext context) { if (value == null) { return Result.ok(null); } // Remove leading and trailing white space value = value.trim(); Locale locale = context.getLocale().orElse(null); if (getTrueString(locale).equals(value)) { return Result.ok(true); } else if (getFalseString(locale).equals(value)) { return Result.ok(false); } else if (value.isEmpty()) { return Result.ok(null); } else { return Result.error(errorMessageProvider.apply(context)); } }
@Override public Result<Integer> convertToModel(String value, ValueContext context) { Result<Number> n = convertToNumber(value, context); return n.flatMap(number -> { if (number == null) { return Result.ok(null); } int intValue = number.intValue(); if (intValue == number.longValue()) { // If the value of n is outside the range of long, the // return value of longValue() is either Long.MIN_VALUE or // Long.MAX_VALUE. The/ above comparison promotes int to // long and thus does not need to consider wrap-around. return Result.ok(intValue); } return Result.error(getErrorMessage(context)); }); }
@Override public Result<T> convertToModel(String value, ValueContext context) { return Result.error("Conversion to model is not supported"); }
@Override public Result<PropertyBox> convertToModel(T value, ValueContext context) { if (value == null) { return Result.ok(null); } if (PropertyBox.class.isAssignableFrom(value.getClass())) { return Result.ok((PropertyBox) value); } return Result.error("Conversion to model is not supported for value type [" + value.getClass() + "]"); }
@Override public Result<T> convertToModel(String value, ValueContext context) { if (value != null && !value.trim().equals("")) { try { return Result.ok(ConversionUtils.convertNumberToTargetClass( getNumberFormat(context.getLocale().orElse(null)).parse(value.trim()), numberType)); } catch (@SuppressWarnings("unused") Exception e) { return Result.error("Could not convert '" + value + "' to " + numberType.getName()); } } return Result.ok(null); }
@Override public Result<PropertyBox> convertToModel(T value, ValueContext context) { try { return Result.ok(itemConverter.convert(value)); } catch (Exception e) { LOGGER.error("Conversion to model failed", e); return Result.error(e.getMessage()); } }
@Override protected Result<LocalDate> handleUnparsableDateString(String dateString) { if (Objects.equals(dateString, StringUtils.replaceChars(getState(false).dateMask, "#U", "__"))) { return Result.ok(null); } return Result.error(getParseErrorMessage()); }
@Override public Result<Account> convertToModel(String value, ValueContext valueContext) { if (value != null && !value.isEmpty()) { try { Account acct = (Account) DB.exec(db -> { GLSession session = new GLSession(db); Account res = session.getAccount("jcard",value); if (res == null && createNew) { if (createFinal) res = new FinalAccount(); else res = new CompositeAccount(); res.setCode(value); } else if (res == null) { throw new BLException("Invalid Account Code"); } return res; }); return Result.ok(acct); } catch (Exception e) { return Result.error(e.getMessage()); } } if (required) return Result.error(QI.getQI().getMessage("errorMessage.req", QI.getQI().getMessage("account"))); else return Result.ok(null); }