@Inject public void setDatatypeRegistry(DatatypeRegistry datatypeRegistry) { //noinspection unchecked this.defaultDatatype = (Datatype<V>) datatypeRegistry.get(BigDecimal.class); }
/** * Get Datatype instance by the corresponding Java class. This method tries to find matching supertype too. * @return Datatype instance * @throws IllegalArgumentException if no datatype suitable for the given type found */ @Nonnull public static <T> Datatype<T> getNN(Class<T> clazz) { return getDatatypeRegistry().getNN(clazz); }
public String getDatatypesJson() { JsonArray jsonArray = new JsonArray(); try { for (String id : datatypes.getIds()) { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("id", id); jsonObject.addProperty("name", id); // for backward compatibility Datatype datatype = datatypes.get(id); if (datatype instanceof ParameterizedDatatype) { Map<String, Object> parameters = ((ParameterizedDatatype) datatype).getParameters(); for (Map.Entry<String, Object> entry : parameters.entrySet()) { jsonObject.addProperty(entry.getKey(), entry.getValue().toString()); } } jsonArray.add(jsonObject); } } catch (Exception e) { log.error("Fail to get datatype settings", e); throw new RestAPIException("Fail to get datatype settings", e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR, e); } return jsonArray.toString(); } }
if (returnType.isPrimitive()) { if (returnType == boolean.class) entity.setDataTypeName(datatypes.getIdByJavaClass(Boolean.class)); if (returnType == int.class) entity.setDataTypeName(datatypes.getIdByJavaClass(Integer.class)); if (returnType == long.class) entity.setDataTypeName(datatypes.getIdByJavaClass(Long.class)); if (returnType == double.class || returnType == float.class) entity.setDataTypeName(datatypes.getIdByJavaClass(Double.class)); } else if (returnType.isEnum()) { entity.setDataTypeName("enum"); Datatype<?> datatype = datatypes.get(returnType); if (datatype != null) entity.setDataTypeName(datatypes.getId(datatype)); else entity.setDataTypeName(datatypes.getIdByJavaClass(String.class)); } catch (ParseException e) { log.debug("Cannot parse '{}' with {} datatype, using StringDatatype for property {}", v, datatype, entity.getName()); entity.setDataTypeName(datatypes.getIdByJavaClass(String.class));
/** * @return all registered Datatype ids. */ public static Set<String> getIds() { return getDatatypeRegistry().getIds(); }
public MetaPropertyInfo(MetaProperty metaProperty) { Messages messages = AppBeans.get(Messages.class); Metadata metadata = AppBeans.get(Metadata.class); this.name = metaProperty.getName(); this.attributeType = metaProperty.getType(); switch (attributeType) { case DATATYPE: Datatype<Object> datatype = metaProperty.getRange().asDatatype(); try { this.type = metadata.getDatatypes().getId(datatype); } catch (Exception e) { this.type = datatype.toString(); } break; case ASSOCIATION: case COMPOSITION: this.type = metaProperty.getRange().asClass().getName(); break; case ENUM: this.type = metaProperty.getRange().asEnumeration().getJavaClass().getName(); break; } this.cardinality = metaProperty.getRange().getCardinality(); this.readOnly = metaProperty.isReadOnly(); this.mandatory = metaProperty.isMandatory(); this.isPersistent = metadata.getTools().isPersistent(metaProperty); this.isTransient = metadata.getTools().isNotPersistent(metaProperty); this.description = messages.getTools().getPropertyCaption(metaProperty); }
protected void initDatatypes(List<Element> datatypeElements) { loadDatatypesFromClasspathResource(); for (Element datatypeEl : datatypeElements) { String id = datatypeEl.attributeValue("id"); String className = datatypeEl.attributeValue("class"); if (Strings.isNullOrEmpty(className)) throw new IllegalStateException("Missing required 'class' attribute for datatype " + id + ". Check your metadata.xml file."); if (Strings.isNullOrEmpty(id)) throw new IllegalStateException("Missing required 'id' attribute for datatype " + className + ". Check your metadata.xml file."); try { Datatype datatype; Class<Datatype> datatypeClass = ReflectionHelper.getClass(className); try { Constructor<Datatype> constructor = datatypeClass.getConstructor(Element.class); datatype = constructor.newInstance(datatypeEl); } catch (Throwable e) { datatype = datatypeClass.newInstance(); } datatypeRegistry.register(datatype, id, Boolean.valueOf(datatypeEl.attributeValue("default"))); } catch (Throwable e) { log.error("Fail to load datatype '{}'", className, e); } } }
if (Strings.isNullOrEmpty(id)) id = guessDatatypeId(datatype); datatypeRegistry.register(datatype, id, true); } catch (Throwable e) { log.error(String.format("Fail to load datatype '%s'", datatypeClassName), e);
/** * Get Datatype instance by the corresponding Java class. This method tries to find matching supertype too. * @return Datatype instance or null if not found */ @Nullable public static <T> Datatype<T> get(Class<T> clazz) { return getDatatypeRegistry().get(clazz); }
/** * Parse Date (time without date) using {@code timeFormat} string specified in the main message pack. * @return Date value or null if a blank string is provided */ @Nullable public Date parseTime(String str) throws ParseException { return datatypeRegistry.getNN(Time.class).parse(str, uss.getLocale()); }
/** * Get Datatype instance by its unique name * @return Datatype instance * @throws IllegalArgumentException if no datatype with the given name found */ @Nonnull public static Datatype get(String name) { return getDatatypeRegistry().get(name); }
/** * Parse BigDecimal using {@code decimalFormat} string specified in the main message pack. * @return BigDecimal value or null if a blank string is provided */ @Nullable public BigDecimal parseBigDecimal(String str) throws ParseException { return datatypeRegistry.getNN(BigDecimal.class).parse(str, uss.getLocale()); }
@Nullable protected Datatype getAdaptiveDatatype(AnnotatedElement annotatedElement) { com.haulmont.chile.core.annotations.MetaProperty annotation = annotatedElement.getAnnotation(com.haulmont.chile.core.annotations.MetaProperty.class); return annotation != null && !annotation.datatype().equals("") ? datatypes.get(annotation.datatype()) : null; }
/** * Parse Integer using {@code integerFormat} string specified in the main message pack. * @return Integer value or null if a blank string is provided */ @Nullable public Integer parseInteger(String str) throws ParseException { return datatypeRegistry.getNN(Integer.class).parse(str, uss.getLocale()); }
@SuppressWarnings("unchecked") @Override public <T extends Component> T create(TypeToken<T> type) { T t = (T) create((Class) type.getRawType()); if (t instanceof HasDatatype) { Type[] actualTypeArguments = ((ParameterizedType) type.getType()).getActualTypeArguments(); if (actualTypeArguments.length == 1 && actualTypeArguments[0] instanceof Class) { Class actualTypeArgument = (Class) actualTypeArguments[0]; ((HasDatatype) t).setDatatype(datatypeRegistry.get(actualTypeArgument)); } } return t; }
/** * Format BigDecimal using {@code decimalFormat} string specified in the main message pack. * @return string representation or empty string if the value is null */ public String formatBigDecimal(@Nullable BigDecimal value) { return datatypeRegistry.getNN(BigDecimal.class).format(value, uss.getLocale()); }
protected void initTextField(String dataType) { textField.setDatatype(dataType == null ? null : datatypes.get(dataType)); textField.setVisible(true); }
/** * Format Boolean using {@code trueString} and {@code falseString} strings specified in the main message pack. * @return string representation or empty string if the value is null */ public String formatBoolean(@Nullable Boolean value) { return datatypeRegistry.getNN(Boolean.class).format(value, uss.getLocale()); }
protected String getFormattedValue(Column<E> column, Object value) { String cellText; if (value == null) { cellText = ""; } else { if (value instanceof String) { cellText = (String) value; } else { Function<Object, String> formatter = column.getFormatter(); if (formatter != null) { cellText = formatter.apply(value); } else { Datatype datatype = datatypeRegistry.get(value.getClass()); if (datatype != null) { cellText = datatype.format(value, this.locale); } else { cellText = value.toString(); } } } } return cellText; }