Search in sources :

Example 26 with Datatype

use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.

the class ServicesControllerManager method _invokeServiceMethod.

@Nullable
protected ServiceCallResult _invokeServiceMethod(String serviceName, String methodName, List<String> paramNames, List<String> paramValuesStr, String modelVersion) {
    Object service = AppBeans.get(serviceName);
    RestServicesConfiguration.RestMethodInfo restMethodInfo = restServicesConfiguration.getRestMethodInfo(serviceName, methodName, paramNames);
    if (restMethodInfo == null) {
        throw new RestAPIException("Service method not found", serviceName + "." + methodName + "(" + paramNames.stream().collect(Collectors.joining(",")) + ")", HttpStatus.NOT_FOUND);
    }
    Method serviceMethod = restMethodInfo.getMethod();
    List<Object> paramValues = new ArrayList<>();
    Type[] types = restMethodInfo.getMethod().getGenericParameterTypes();
    for (int i = 0; i < types.length; i++) {
        int idx = i;
        try {
            idx = paramNames.indexOf(restMethodInfo.getParams().get(i).getName());
            paramValues.add(restParseUtils.toObject(types[i], paramValuesStr.get(idx), modelVersion));
        } catch (Exception e) {
            log.error("Error on parsing service param value", e);
            throw new RestAPIException("Invalid parameter value", "Invalid parameter value for " + paramNames.get(idx), HttpStatus.BAD_REQUEST);
        }
    }
    Object methodResult;
    try {
        methodResult = serviceMethod.invoke(service, paramValues.toArray());
    } catch (InvocationTargetException | IllegalAccessException ex) {
        if (ex.getCause() instanceof ValidationException) {
            throw (ValidationException) ex.getCause();
        } else {
            log.error("Error on service method invoke", ex.getCause());
            throw new RestAPIException("Error on service method invoke", "", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    if (methodResult == null) {
        return null;
    }
    Class<?> methodReturnType = serviceMethod.getReturnType();
    if (Entity.class.isAssignableFrom(methodReturnType)) {
        Entity entity = (Entity) methodResult;
        restControllerUtils.applyAttributesSecurity(entity);
        String entityJson = entitySerializationAPI.toJson(entity, null, EntitySerializationOption.SERIALIZE_INSTANCE_NAME);
        entityJson = restControllerUtils.transformJsonIfRequired(entity.getMetaClass().getName(), modelVersion, JsonTransformationDirection.TO_VERSION, entityJson);
        return new ServiceCallResult(entityJson, true);
    } else if (Collection.class.isAssignableFrom(methodReturnType)) {
        Class returnTypeArgument = getMethodReturnTypeArgument(serviceMethod);
        if ((returnTypeArgument != null && Entity.class.isAssignableFrom(returnTypeArgument)) || isEntitiesCollection((Collection) methodResult)) {
            Collection<? extends Entity> entities = (Collection<? extends Entity>) methodResult;
            entities.forEach(entity -> restControllerUtils.applyAttributesSecurity(entity));
            String entitiesJson = entitySerializationAPI.toJson(entities, null, EntitySerializationOption.SERIALIZE_INSTANCE_NAME);
            if (returnTypeArgument != null) {
                MetaClass metaClass = metadata.getClass(returnTypeArgument);
                if (metaClass != null) {
                    entitiesJson = restControllerUtils.transformJsonIfRequired(metaClass.getName(), modelVersion, JsonTransformationDirection.TO_VERSION, entitiesJson);
                } else {
                    log.error("MetaClass for service collection parameter type {} not found", returnTypeArgument);
                }
            }
            return new ServiceCallResult(entitiesJson, true);
        } else {
            return new ServiceCallResult(restParseUtils.serialize(methodResult), true);
        }
    } else {
        Datatype<?> datatype = Datatypes.get(methodReturnType);
        if (datatype != null) {
            return new ServiceCallResult(datatype.format(methodResult), false);
        } else {
            return new ServiceCallResult(restParseUtils.serialize(methodResult), true);
        }
    }
}
Also used : JsonObject(com.google.gson.JsonObject) java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) JsonParser(com.google.gson.JsonParser) AppBeans(com.haulmont.cuba.core.global.AppBeans) MetaClass(com.haulmont.chile.core.model.MetaClass) Metadata(com.haulmont.cuba.core.global.Metadata) JsonTransformationDirection(com.haulmont.restapi.transform.JsonTransformationDirection) JsonElement(com.google.gson.JsonElement) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) EntitySerializationOption(com.haulmont.cuba.core.app.serialization.EntitySerializationOption) Datatype(com.haulmont.chile.core.datatypes.Datatype) RestControllerUtils(com.haulmont.restapi.common.RestControllerUtils) ParseException(java.text.ParseException) Method(java.lang.reflect.Method) Nullable(javax.annotation.Nullable) Logger(org.slf4j.Logger) RestAPIException(com.haulmont.restapi.exception.RestAPIException) Datatypes(com.haulmont.chile.core.datatypes.Datatypes) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) EntitySerializationAPI(com.haulmont.cuba.core.app.serialization.EntitySerializationAPI) HttpStatus(org.springframework.http.HttpStatus) Component(org.springframework.stereotype.Component) ParameterizedType(java.lang.reflect.ParameterizedType) RestParseUtils(com.haulmont.restapi.common.RestParseUtils) ValidationException(javax.validation.ValidationException) Type(java.lang.reflect.Type) RestServicesConfiguration(com.haulmont.restapi.config.RestServicesConfiguration) Entity(com.haulmont.cuba.core.entity.Entity) Entity(com.haulmont.cuba.core.entity.Entity) ValidationException(javax.validation.ValidationException) Method(java.lang.reflect.Method) ParseException(java.text.ParseException) RestAPIException(com.haulmont.restapi.exception.RestAPIException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ValidationException(javax.validation.ValidationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Datatype(com.haulmont.chile.core.datatypes.Datatype) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) MetaClass(com.haulmont.chile.core.model.MetaClass) RestAPIException(com.haulmont.restapi.exception.RestAPIException) JsonObject(com.google.gson.JsonObject) MetaClass(com.haulmont.chile.core.model.MetaClass) RestServicesConfiguration(com.haulmont.restapi.config.RestServicesConfiguration) Nullable(javax.annotation.Nullable)

Example 27 with Datatype

use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.

the class WebAbstractTextField method getValue.

@Override
public <V> V getValue() {
    String value = super.getValue();
    if (isTrimming()) {
        value = StringUtils.trim(value);
    }
    value = Strings.emptyToNull(value);
    Datatype datatype = getActualDatatype();
    if (value != null && datatype != null) {
        try {
            return (V) datatype.parse(value, locale);
        } catch (ParseException e) {
            Logger log = LoggerFactory.getLogger(WebAbstractTextField.class);
            log.debug("Unable to parse value of component {}\n{}", getId(), e.getMessage());
            return null;
        }
    } else {
        return (V) value;
    }
}
Also used : ParseException(java.text.ParseException) Logger(org.slf4j.Logger) Datatype(com.haulmont.chile.core.datatypes.Datatype)

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