Search in sources :

Example 21 with Datatype

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);
}
Also used : AdaptiveNumberDatatype(com.haulmont.chile.core.datatypes.impl.AdaptiveNumberDatatype) Datatype(com.haulmont.chile.core.datatypes.Datatype)

Example 22 with Datatype

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);
        }
    }
}
Also used : Element(org.dom4j.Element) AnnotatedElement(java.lang.reflect.AnnotatedElement) Datatype(com.haulmont.chile.core.datatypes.Datatype)

Example 23 with Datatype

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);
        }
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) Datatype(com.haulmont.chile.core.datatypes.Datatype)

Example 24 with Datatype

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;
    }
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) Converter(com.vaadin.data.util.converter.Converter) ParseException(java.text.ParseException) Range(com.haulmont.chile.core.model.Range) Datatype(com.haulmont.chile.core.datatypes.Datatype)

Example 25 with Datatype

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();
}
Also used : JsonArray(com.google.gson.JsonArray) ParameterizedDatatype(com.haulmont.chile.core.datatypes.ParameterizedDatatype) JsonObject(com.google.gson.JsonObject) RestAPIException(com.haulmont.restapi.exception.RestAPIException) JsonObject(com.google.gson.JsonObject) Map(java.util.Map) RestAPIException(com.haulmont.restapi.exception.RestAPIException) Datatype(com.haulmont.chile.core.datatypes.Datatype) ParameterizedDatatype(com.haulmont.chile.core.datatypes.ParameterizedDatatype)

Aggregations

Datatype (com.haulmont.chile.core.datatypes.Datatype)27 ParseException (java.text.ParseException)9 Entity (com.haulmont.cuba.core.entity.Entity)6 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)6 Datatypes (com.haulmont.chile.core.datatypes.Datatypes)3 AdaptiveNumberDatatype (com.haulmont.chile.core.datatypes.impl.AdaptiveNumberDatatype)3 MetaClass (com.haulmont.chile.core.model.MetaClass)3 Element (org.dom4j.Element)3 Logger (org.slf4j.Logger)3 JsonObject (com.google.gson.JsonObject)2 EnumClass (com.haulmont.chile.core.datatypes.impl.EnumClass)2 MetaProperty (com.haulmont.chile.core.model.MetaProperty)2 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)2 AppBeans (com.haulmont.cuba.core.global.AppBeans)2 Metadata (com.haulmont.cuba.core.global.Metadata)2 ComponentsFactory (com.haulmont.cuba.gui.xml.layout.ComponentsFactory)2 RestAPIException (com.haulmont.restapi.exception.RestAPIException)2 Map (java.util.Map)2 Inject (javax.inject.Inject)2 Document (org.dom4j.Document)2