use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.
the class MetadataLoader method loadDatatypesFromClasspathResource.
protected void loadDatatypesFromClasspathResource() {
SAXReader reader = new SAXReader();
URL resource = Datatypes.class.getResource(getGetDatatypesResourcePath());
if (resource != null) {
log.info("Loading datatypes from " + resource);
try {
Document document = reader.read(resource);
Element element = document.getRootElement();
List<Element> datatypeElements = Dom4j.elements(element, "datatype");
for (Element datatypeElement : datatypeElements) {
String datatypeClassName = datatypeElement.attributeValue("class");
try {
Datatype datatype;
Class<Datatype> datatypeClass = ReflectionHelper.getClass(datatypeClassName);
try {
final Constructor<Datatype> constructor = datatypeClass.getConstructor(Element.class);
datatype = constructor.newInstance(datatypeElement);
} catch (Throwable e) {
datatype = datatypeClass.newInstance();
}
String id = datatypeElement.attributeValue("id");
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);
}
}
} catch (DocumentException e) {
log.error("Fail to load datatype settings", e);
}
}
}
use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.
the class AppPropertiesLocator method setDataType.
private void setDataType(Method method, AppPropertyEntity entity) {
Class<?> returnType = method.getReturnType();
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");
EnumStore enumStoreAnn = method.getAnnotation(EnumStore.class);
if (enumStoreAnn != null && enumStoreAnn.value() == EnumStoreMode.ID) {
// noinspection unchecked
Class<EnumClass> enumeration = (Class<EnumClass>) returnType;
entity.setEnumValues(Arrays.asList(enumeration.getEnumConstants()).stream().map(ec -> String.valueOf(ec.getId())).collect(Collectors.joining(",")));
} else {
entity.setEnumValues(Arrays.asList(returnType.getEnumConstants()).stream().map(Object::toString).collect(Collectors.joining(",")));
}
} else {
Datatype<?> datatype = datatypes.get(returnType);
if (datatype != null)
entity.setDataTypeName(datatypes.getId(datatype));
else
entity.setDataTypeName(datatypes.getIdByJavaClass(String.class));
}
String dataTypeName = entity.getDataTypeName();
if (!dataTypeName.equals("enum")) {
Datatype datatype = Datatypes.get(dataTypeName);
String v = null;
try {
v = entity.getDefaultValue();
datatype.parse(v);
v = entity.getCurrentValue();
datatype.parse(v);
} catch (ParseException e) {
log.debug("Cannot parse '{}' with {} datatype, using StringDatatype for property {}", v, datatype, entity.getName());
entity.setDataTypeName(datatypes.getIdByJavaClass(String.class));
}
}
}
use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.
the class EntitySnapshot method getLabel.
@MetaProperty(related = { "snapshotDate,author" })
public String getLabel() {
String name = "";
if (author != null && StringUtils.isNotEmpty(this.author.getCaption())) {
name += this.author.getCaption() + " ";
}
Datatype datatype = Datatypes.getNN(Date.class);
UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.NAME);
if (userSessionSource != null && userSessionSource.checkCurrentUserSession()) {
name += datatype.format(snapshotDate, userSessionSource.getLocale());
}
return StringUtils.trim(name);
}
use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.
the class MetadataTools method format.
/**
* Formats a value according to the property type.
*
* @param value object to format
* @param property metadata
* @return formatted value as string
*/
public String format(@Nullable Object value, MetaProperty property) {
if (value == null)
return "";
Objects.requireNonNull(property, "property is null");
Range range = property.getRange();
if (DynamicAttributesUtils.isDynamicAttribute(property)) {
CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(property);
if (categoryAttribute.getDataType().equals(PropertyType.ENUMERATION)) {
return LocaleHelper.getEnumLocalizedValue((String) value, categoryAttribute.getEnumerationLocales());
}
if (categoryAttribute.getIsCollection() && value instanceof Collection) {
return DynamicAttributesUtils.getDynamicAttributeValueAsString(property, value);
}
}
if (range.isDatatype()) {
Datatype datatype = range.asDatatype();
if (value instanceof Date && datatype.getJavaClass().equals(Date.class)) {
Boolean ignoreUserTimeZone = getMetaAnnotationValue(property, IgnoreUserTimeZone.class);
if (!Boolean.TRUE.equals(ignoreUserTimeZone)) {
return datatypeFormatter.formatDateTime((Date) value);
}
}
return datatype.format(value, userSessionSource.getLocale());
} else if (range.isEnum()) {
return messages.getMessage((Enum) value);
} else if (value instanceof Instance) {
return ((Instance) value).getInstanceName();
} else {
return value.toString();
}
}
use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.
the class SessionAttributeEditor method commitAndClose.
@Override
public void commitAndClose() {
SessionAttribute item = datasource.getItem();
if (item.getStringValue() != null) {
Datatype dt = Datatypes.get(item.getDatatype());
try {
Object object = dt.parse(item.getStringValue());
item.setStringValue(object == null ? "" : object.toString());
} catch (IllegalArgumentException | ParseException e) {
showNotification(getMessage("unableToParseValue"), NotificationType.ERROR);
return;
}
}
super.commitAndClose();
}
Aggregations