use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.
the class MetaModelLoader method loadProperty.
protected MetadataObjectInfo<MetaProperty> loadProperty(MetaClassImpl metaClass, Method method, String name) {
Collection<RangeInitTask> tasks = new ArrayList<>();
MetaPropertyImpl property = new MetaPropertyImpl(metaClass, name);
Map<String, Object> map = new HashMap<>();
map.put("cardinality", Range.Cardinality.NONE);
map.put("mandatory", false);
Datatype datatype = getAdaptiveDatatype(method);
map.put("datatype", datatype);
Class<?> type;
Class typeOverride = getTypeOverride(method);
if (typeOverride != null)
type = typeOverride;
else
type = method.getReturnType();
property.setMandatory(false);
property.setReadOnly(!setterExists(method));
property.setAnnotatedElement(method);
property.setDeclaringClass(method.getDeclaringClass());
property.setJavaType(method.getReturnType());
MetadataObjectInfo<Range> info = loadRange(property, type, map);
Range range = info.getObject();
if (range != null) {
((AbstractRange) range).setCardinality(Range.Cardinality.NONE);
property.setRange(range);
assignPropertyType(method, property, range);
}
tasks.addAll(info.getTasks());
return new MetadataObjectInfo<>(property, tasks);
}
use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.
the class MetadataLoader method initDatatypes.
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);
}
}
}
use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.
the class UserSessionManager method compileSessionAttributes.
protected void compileSessionAttributes(UserSession session, Group group) {
List<SessionAttribute> list = new ArrayList<>(group.getSessionAttributes());
EntityManager em = persistence.getEntityManager();
TypedQuery<SessionAttribute> q = em.createQuery("select a from sec$GroupHierarchy h join h.parent.sessionAttributes a " + "where h.group.id = ?1 order by h.level desc", SessionAttribute.class);
q.setParameter(1, group);
List<SessionAttribute> attributes = q.getResultList();
list.addAll(attributes);
for (SessionAttribute attribute : list) {
Datatype datatype = Datatypes.get(attribute.getDatatype());
try {
if (session.getAttributeNames().contains(attribute.getName())) {
log.warn("Duplicate definition of '{}' session attribute in the group hierarchy", attribute.getName());
}
Serializable value = (Serializable) datatype.parse(attribute.getStringValue());
if (value != null)
session.setAttribute(attribute.getName(), value);
else
session.removeAttribute(attribute.getName());
} catch (ParseException e) {
throw new RuntimeException("Unable to set session attribute " + attribute.getName(), e);
}
}
}
use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.
the class PropertyWrapper method valueOf.
protected Object valueOf(Object newValue) throws Converter.ConversionException {
if (newValue == null) {
return null;
}
final Range range = propertyPath.getRange();
if (range == null) {
return newValue;
} else {
final Object obj;
if (range.isDatatype()) {
Datatype<Object> datatype = range.asDatatype();
if (newValue instanceof String) {
try {
newValue = Strings.emptyToNull((String) newValue);
UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
obj = datatype.parse((String) newValue, sessionSource.getLocale());
} catch (ParseException e) {
throw new Converter.ConversionException(e);
}
} else {
if (newValue.getClass().equals(datatype.getJavaClass())) {
return newValue;
} else {
Datatype newValueDatatype = Datatypes.getNN(newValue.getClass());
// noinspection unchecked
String str = newValueDatatype.format(newValue);
try {
obj = datatype.parse(str);
} catch (ParseException e) {
throw new Converter.ConversionException(e);
}
}
}
} else {
obj = newValue;
}
return obj;
}
}
use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.
the class DatatypesControllerManager method getDatatypesJson.
public String getDatatypesJson() {
JsonArray jsonArray = new JsonArray();
try {
for (String id : datatypes.getIds()) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("id", id);
// for backward compatibility
jsonObject.addProperty("name", id);
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);
}
return jsonArray.toString();
}
Aggregations